app.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 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:""
  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(res) {
  40. console.log('location change', res)
  41. that.globalData.location=res;
  42. that.login(res.latitude,res.longitude).then(function(res){
  43. if(res.code!=200){
  44. }
  45. },function(err){
  46. })
  47. })
  48. },
  49. fail(res) {
  50. console.log('开启后台定位失败', res)
  51. }
  52. })
  53. },
  54. login(lat="",lng=""){
  55. var that=this;
  56. return new Promise((resolve,reject)=>{
  57. wx.checkSession({
  58. success: (res) => {
  59. },
  60. fail:(res)=>{
  61. wx.login({
  62. success: res => {
  63. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  64. that.api.useApi(that.globalData.baseAppUrl+"api/login",{
  65. code:res.code,
  66. lat:lat,
  67. lng:lng,
  68. appid:wx.getAppBaseInfo().host.appId
  69. },"post").then(function(res){
  70. wx.showLoading({
  71. title: '登录中',
  72. })
  73. if(res.code!=200){
  74. setTimeout(function(){
  75. that.login(lat,lng);
  76. },1000);
  77. }else{
  78. wx.hideLoading();
  79. resolve(res);
  80. }
  81. }).catch(function(err){
  82. console.log(222);
  83. })
  84. }
  85. })
  86. }
  87. })
  88. })
  89. }
  90. })