app.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // app.js
  2. var projectMode = 2;//0代表开发模式(本地环境),1代表测试模式(线上测r试环境),2代表线上模式(正式运行环境)
  3. var info = wx.getSystemInfoSync();
  4. if (info.platform != "devtools" && projectMode == 0) { projectMode = 1 }
  5. var envVersion = `${__wxConfig.envVersion}`;//develop 开发版,trial 体验版,release 正式版
  6. if (envVersion == 'release') { projectMode = 2 }
  7. import api from './utils/api'
  8. App({
  9. onLaunch() {
  10. //云开发
  11. wx.cloud.init({
  12. traceUser: true,
  13. })
  14. wx.setStorageSync('islocation', false)
  15. const logs = wx.getStorageSync('logs') || []
  16. logs.unshift(Date.now())
  17. wx.setStorageSync('logs', logs)
  18. var that = this;
  19. this.getLocation();
  20. // 登录
  21. wx.checkSession({
  22. success: res => {
  23. if(!wx.getStorageSync('token')){
  24. this.login();
  25. }
  26. },
  27. fail: (res) => {
  28. this.login();
  29. }
  30. })
  31. this.globalData.btnInfo = wx.getMenuButtonBoundingClientRect()
  32. console.log(this.globalData.btnInfo, '按钮信息');
  33. },
  34. globalData: {
  35. userInfo: null,
  36. btnInfo: null,
  37. status: ['可下单', '服务中', '已满柜', '暂停服务'],
  38. baseAppUrl: projectMode == 2 ? 'https://hx.xishuashua.art/' : (projectMode == 1 ? 'https://hx.xishuashua.art/' : 'http://127.0.0.1:8000/'),
  39. location: "",
  40. token:"",
  41. nid:0,
  42. islocation: true,
  43. logisticsStatus:['用户未存衣','用户已存衣', '送洗途中', '工厂已签收','管家送回中', '管家已存衣', '用户已取衣'],
  44. systeminfo:""
  45. },
  46. api: api,
  47. getLocation(e) {
  48. var that = this;
  49. this.globalData.headerBtnPosi = wx.getMenuButtonBoundingClientRect()
  50. wx.getSystemInfo({ // iphonex底部适配
  51. success: res => {
  52. that.globalData.systeminfo = res
  53. }
  54. })
  55. wx.getLocation({
  56. success(res) {
  57. console.log('开启后台定位', res)
  58. console.log('location change', res)
  59. that.globalData.location = res;
  60. wx.setStorageSync('latitude', res.latitude)
  61. wx.setStorageSync('longitude', res.longitude)
  62. wx.checkSession({
  63. success: res => {
  64. },
  65. fail: (res) => {
  66. that.login(res.latitude, res.longitude).then(function (res) { })
  67. }
  68. })
  69. },fail(res) {
  70. console.log('开启后台定位失败', res)
  71. that.globalData.islocation = false;
  72. wx.setStorageSync('islocation', true)
  73. wx.showLoading({
  74. title: '请授权获取地址。',
  75. })
  76. setTimeout(function () {
  77. wx.hideLoading();
  78. }, 2000)
  79. }
  80. })
  81. },
  82. login(lat = "", lng = "") {
  83. var that = this;
  84. if(!lat || !lng){
  85. lat=wx.getStorageSync('latitude')
  86. lng=wx.getStorageSync('longitude')
  87. }
  88. return new Promise((resolve, reject) => {
  89. // console.log(3333333)
  90. // wx.checkSession({
  91. // success: (res) => {
  92. // console.log(444444)
  93. // },
  94. // fail: (res) => {
  95. wx.login({
  96. success: res => {
  97. console.log(res)
  98. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  99. that.api.useApi(that.globalData.baseAppUrl + "api/login", {
  100. code: res.code,
  101. lat: lat,
  102. lng: lng,
  103. appid: wx.getAccountInfoSync().miniProgram.appId
  104. }, "post").then(function (res) {
  105. wx.showLoading({
  106. title: '登录中',
  107. })
  108. if (res.code != 200) {
  109. setTimeout(function () {
  110. that.login(lat, lng);
  111. }, 1000);
  112. } else {
  113. wx.hideLoading();
  114. that.globalData.token= res.message.token;
  115. that.globalData.nid= res.message.network_id;
  116. wx.setStorageSync('token', res.message.token);
  117. wx.setStorageSync('uid', res.message.user_id);
  118. wx.setStorageSync('exp_time', res.message.exp_time);
  119. wx.setStorageSync('seisson_key', res.message.session_key);
  120. wx.setStorageSync('phone', res.message.phone);
  121. wx.setStorageSync('is_online', res.is_online);
  122. wx.setStorageSync('nid', res.message.network_id);//当前网点选择id
  123. resolve(res);
  124. }
  125. }).catch(function (err) {
  126. console.log(222);
  127. })
  128. }
  129. })
  130. // }
  131. //})
  132. })
  133. },
  134. getQueryVariable(url,name){
  135. var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i');
  136. var r = url.substr(1).match(reg);
  137. if (r != null) {
  138. return r[2];
  139. }
  140. return null;
  141. }
  142. })