app.js 3.8 KB

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