app.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // app.js
  2. var projectMode = 0;//0代表开发模式(本地环境),1代表测试模式(线上测r试环境),2代表线上模式(正式运行环境)
  3. var info = wx.getSystemInfoSync();
  4. if (info.platform != "devtools" && projectMode == 0) { projectMode = 0 }
  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. this.updateApp()
  11. //云开发
  12. wx.cloud.init({
  13. traceUser: true,
  14. })
  15. wx.setStorageSync('islocation', false)
  16. const logs = wx.getStorageSync('logs') || []
  17. logs.unshift(Date.now())
  18. wx.setStorageSync('logs', logs)
  19. var that = this;
  20. this.getLocation();
  21. // 登录
  22. wx.checkSession({
  23. success: res => {
  24. if(!wx.getStorageSync('token')){
  25. // this.login();
  26. }
  27. },
  28. fail: (res) => {
  29. // this.login();
  30. }
  31. })
  32. this.globalData.btnInfo = wx.getMenuButtonBoundingClientRect()
  33. console.log(this.globalData.btnInfo, '按钮信息');
  34. },
  35. globalData: {
  36. userInfo: null,
  37. btnInfo: null,
  38. status: ['可下单', '服务中', '已满柜', '暂停服务'],
  39. baseAppUrl: projectMode == 2 ? 'https://hx.xishuashua.art/' : (projectMode == 1 ? 'https://hx.xishuashua.art/' : 'https://wash.nanodreamtech.com/'),
  40. location: "",
  41. token:"",
  42. nid:0,
  43. islocation: true,
  44. logisticsStatus:['用户未存衣','用户已存衣', '送洗途中', '工厂已签收','管家送回中', '管家已存衣', '用户已取衣'],
  45. systeminfo:""
  46. },
  47. api: api,
  48. getLocation(e) {
  49. var that = this;
  50. this.globalData.headerBtnPosi = wx.getMenuButtonBoundingClientRect()
  51. wx.getSystemInfo({ // iphonex底部适配
  52. success: res => {
  53. that.globalData.systeminfo = res
  54. }
  55. })
  56. wx.getLocation({
  57. success(res) {
  58. console.log('开启后台定位', res)
  59. console.log('location change', res)
  60. that.globalData.location = res;
  61. wx.setStorageSync('latitude', res.latitude)
  62. wx.setStorageSync('longitude', res.longitude)
  63. wx.checkSession({
  64. success: res => {
  65. },
  66. fail: (res) => {
  67. that.login(res.latitude, res.longitude).then(function (res) { })
  68. }
  69. })
  70. },fail(res) {
  71. console.log('开启后台定位失败', res)
  72. that.globalData.islocation = false;
  73. wx.setStorageSync('islocation', true)
  74. wx.showLoading({
  75. title: '请授权获取地址。',
  76. })
  77. setTimeout(function () {
  78. wx.hideLoading();
  79. }, 2000)
  80. }
  81. })
  82. },
  83. login(lat = "", lng = "") {
  84. var that = this;
  85. if(!lat || !lng){
  86. lat=wx.getStorageSync('latitude')
  87. lng=wx.getStorageSync('longitude')
  88. }
  89. return new Promise((resolve, reject) => {
  90. // console.log(3333333)
  91. // wx.checkSession({
  92. // success: (res) => {
  93. // console.log(444444)
  94. // },
  95. // fail: (res) => {
  96. wx.login({
  97. success: res => {
  98. console.log(res)
  99. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  100. that.api.useApi(that.globalData.baseAppUrl + "api/login", {
  101. code: res.code,
  102. lat: lat,
  103. lng: lng,
  104. appid: wx.getAccountInfoSync().miniProgram.appId
  105. }, "post").then(function (res) {
  106. wx.showLoading({
  107. title: '登录中',
  108. })
  109. if (res.code != 200) {
  110. setTimeout(function () {
  111. that.login(lat, lng);
  112. }, 1000);
  113. } else {
  114. wx.hideLoading();
  115. that.globalData.token= res.message.token;
  116. that.globalData.nid= res.message.network_id;
  117. wx.setStorageSync('token', res.message.token);
  118. wx.setStorageSync('uid', res.message.user_id);
  119. wx.setStorageSync('exp_time', res.message.exp_time);
  120. wx.setStorageSync('seisson_key', res.message.session_key);
  121. wx.setStorageSync('phone', res.message.phone);
  122. wx.setStorageSync('is_online', res.is_online);
  123. wx.setStorageSync('nid', res.message.network_id);//当前网点选择id
  124. resolve(res);
  125. }
  126. }).catch(function (err) {
  127. console.log(222);
  128. })
  129. }
  130. })
  131. // }
  132. //})
  133. })
  134. },
  135. getQueryVariable(url,name){
  136. var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i');
  137. var r = url.substr(1).match(reg);
  138. if (r != null) {
  139. return r[2];
  140. }
  141. return null;
  142. },
  143. updateApp:function(){
  144. const updateManager = wx.getUpdateManager()
  145. updateManager.onCheckForUpdate(function (res) {
  146. // 请求完新版本信息的回调
  147. if (res.hasUpdate) {
  148. wx.showLoading({
  149. title:'更新下载中...',
  150. })
  151. }
  152. })
  153. updateManager.onUpdateReady(function () {
  154. wx.hideLoading();
  155. wx.showModal({
  156. title:'更新提示',
  157. content:'新版本已经准备好,是否重启应用?',
  158. success:function (res) {
  159. if (res.confirm) {
  160. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  161. updateManager.applyUpdate()
  162. }
  163. }
  164. })
  165. })
  166. updateManager.onUpdateFailed(function () {
  167. // 新的版本下载失败
  168. wx.hideLoading();
  169. wx.showToast({ title:'下载失败...', icon:"none" });
  170. })
  171. },
  172. })