app.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // app.js
  2. var projectMode = 1;//0代表开发模式(本地环境),1代表测试模式(线上测试环境),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. console.log(res)
  24. },
  25. fail: (res) => {
  26. this.login();
  27. }
  28. })
  29. this.globalData.btnInfo = wx.getMenuButtonBoundingClientRect()
  30. console.log(this.globalData.btnInfo, '按钮信息');
  31. },
  32. globalData: {
  33. userInfo: null,
  34. btnInfo: null,
  35. status: ['可下单', '服务中', '已满柜', '暂停服务'],
  36. baseAppUrl: projectMode == 2 ? 'https://wash.nanodreamtech.com/' : (projectMode == 1 ? 'https://wash.nanodreamtech.com/' : 'http://127.0.0.1:8000/'),
  37. location: "",
  38. token:"",
  39. nid:0,
  40. islocation: true
  41. },
  42. api: api,
  43. getLocation(e) {
  44. var that = this;
  45. wx.getLocation({
  46. success(res) {
  47. console.log('开启后台定位', res)
  48. console.log('location change', res)
  49. that.globalData.location = res;
  50. wx.setStorageSync('latitude', res.latitude)
  51. wx.setStorageSync('longitude', res.longitude)
  52. wx.checkSession({
  53. success: res => {
  54. },
  55. fail: (res) => {
  56. that.login(res.latitude, res.longitude).then(function (res) { })
  57. }
  58. })
  59. },fail(res) {
  60. console.log('开启后台定位失败', res)
  61. that.globalData.islocation = false;
  62. wx.setStorageSync('islocation', true)
  63. wx.showLoading({
  64. title: '请授权获取地址。',
  65. })
  66. setTimeout(function () {
  67. wx.hideLoading();
  68. }, 2000)
  69. }
  70. })
  71. },
  72. login(lat = "", lng = "") {
  73. var that = this;
  74. if(!lat || !lng){
  75. lat=wx.getStorageSync('latitude')
  76. lng=wx.getStorageSync('longitude')
  77. }
  78. return new Promise((resolve, reject) => {
  79. console.log(3333333)
  80. wx.checkSession({
  81. success: (res) => {
  82. console.log(444444)
  83. },
  84. fail: (res) => {
  85. wx.login({
  86. success: res => {
  87. console.log(res)
  88. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  89. that.api.useApi(that.globalData.baseAppUrl + "api/login", {
  90. code: res.code,
  91. lat: lat,
  92. lng: lng,
  93. appid: wx.getAppBaseInfo().host.appId
  94. }, "post").then(function (res) {
  95. wx.showLoading({
  96. title: '登录中',
  97. })
  98. if (res.code != 200) {
  99. setTimeout(function () {
  100. that.login(lat, lng);
  101. }, 1000);
  102. } else {
  103. wx.hideLoading();
  104. that.globalData.token= res.message.token;
  105. that.globalData.nid= res.message.network_id;
  106. wx.setStorageSync('token', res.message.token);
  107. wx.setStorageSync('uid', res.message.user_id);
  108. wx.setStorageSync('exp_time', res.message.exp_time);
  109. wx.setStorageSync('seisson_key', res.message.session_key);
  110. wx.setStorageSync('phone', res.message.phone);
  111. wx.setStorageSync('nid', res.message.network_id);//当前网点选择id
  112. resolve(res);
  113. }
  114. }).catch(function (err) {
  115. console.log(222);
  116. })
  117. }
  118. })
  119. }
  120. })
  121. })
  122. }
  123. })