app.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // app.js
  2. var projectMode = 2;//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. const logs = wx.getStorageSync('logs') || []
  12. logs.unshift(Date.now())
  13. wx.setStorageSync('logs', logs)
  14. var that = this;
  15. this.getLocation();
  16. // 登录
  17. wx.checkSession({
  18. success: res => {
  19. console.log(res)
  20. },
  21. fail: (res) => {
  22. }
  23. })
  24. this.globalData.btnInfo = wx.getMenuButtonBoundingClientRect()
  25. console.log(this.globalData.btnInfo, '按钮信息');
  26. },
  27. globalData: {
  28. userInfo: null,
  29. btnInfo: null,
  30. baseAppUrl: projectMode == 2 ? 'https://wash.nanodreamtech.com/' : (projectMode == 1 ? 'https://wash.nanodreamtech.com/' : 'http://127.0.0.1:8000/'),
  31. location: {'latitude':123,'longitude':321}
  32. },
  33. api: api,
  34. getLocation(e) {
  35. var that = this;
  36. wx.startLocationUpdateBackground({
  37. success(res) {
  38. console.log('开启后台定位', res)
  39. wx.onLocationChange(function (ress) {
  40. wx.setStorageSync('latitude', ress.latitude)
  41. wx.setStorageSync('longitude', ress.longitude)
  42. that.globalData.location = ress;
  43. that.login(that.globalData.location.latitude, that.globalData.location.longitude).then(function (res) {
  44. if (res.code != 200) {
  45. }
  46. }, function (err) {
  47. })
  48. })
  49. },
  50. fail(res) {
  51. console.log('开启后台定位失败', res)
  52. }
  53. })
  54. },
  55. login(lat = "", lng = "") {
  56. var that = this;
  57. return new Promise((resolve, reject) => {
  58. wx.checkSession({
  59. success: (res) => {
  60. },
  61. fail: (res) => {
  62. wx.login({
  63. success: res => {
  64. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  65. that.api.useApi(that.globalData.baseAppUrl + "api/login", {
  66. code: res.code,
  67. lat: lat,
  68. lng: lng,
  69. appid: wx.getAppBaseInfo().host.appId
  70. }, "post").then(function (res) {
  71. wx.showLoading({
  72. title: '登录中',
  73. })
  74. console.log(res);
  75. if (res.code != 200) {
  76. setTimeout(function () {
  77. that.login(lat, lng);
  78. }, 1000);
  79. } else {
  80. wx.hideLoading();
  81. wx.setStorageSync('token', res.message.token);
  82. wx.setStorageSync('uid', res.message.user_id);
  83. wx.setStorageSync('nid', res.message.network_id);//当前网点选择id
  84. resolve(res);
  85. }
  86. }).catch(function (err) {
  87. console.log(222);
  88. })
  89. }
  90. })
  91. }
  92. })
  93. })
  94. }
  95. })