app.js 2.8 KB

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