app.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. App({
  2. onLaunch: function (options) {
  3. if (options.query) {
  4. console.log('扫码加载')
  5. var code1 = decodeURIComponent(options.query.qrCode)
  6. var index = code1.lastIndexOf("\=");
  7. let code = code1.substring(index + 1, code1.length);
  8. this.globalData.scanCode=code
  9. console.log(code,'ceode-------------------------------------')
  10. }
  11. if (my.canIUse('getUpdateManager')) {
  12. const updateManager = my.getUpdateManager()
  13. updateManager.onCheckForUpdate(function (res) {
  14. if (res.hasUpdate) {
  15. updateManager.onUpdateReady(function () {
  16. my.alert({
  17. title: '更新提示',
  18. content: '新版本已经准备好,是否重启应用?',
  19. success: function (res) {
  20. updateManager.applyUpdate()
  21. }
  22. })
  23. })
  24. updateManager.onUpdateFailed(function () {
  25. my.alert({
  26. title: '已经有新版本了哟~',
  27. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  28. })
  29. })
  30. }
  31. })
  32. } else {
  33. my.alert({
  34. title: '提示',
  35. content: '当前支付宝版本过低,无法使用该功能,请升级到最新支付宝版本后重试。'
  36. })
  37. }
  38. console.log(my.getStorageSync({ 'key': 'token' }).data, 'XXXXXXXXXXXXXXXXXXXXX')
  39. },
  40. globalData: {
  41. statusBarHeight: my.getSystemInfoSync()['statusBarHeight'],
  42. windowHeight: my.getSystemInfoSync()['windowHeight'],
  43. imgUrl: 'http://resource.bike.hanyiyun.com/',
  44. screenHeight: '',
  45. login: false,
  46. scanCode:'',//扫描普通二维码跳转小程序解析出的车牌号码
  47. platform: '',
  48. version: '',
  49. req: true,
  50. id: 0,
  51. times: 60,
  52. invalid: false,
  53. scanLength: 9,//车辆编码数
  54. compatible: '',
  55. title: '禹见你', //全局修改此小程序名称
  56. logoUrl: '../../img/logo.png', //logo图片路径 网络图片/本地图片 dejin.png shanxianLg.png yuzhouLg.png dingding.png
  57. appid: '2021002147603404', //小程序的appid wxc5f328a92356183f--遇见你appid
  58. merchant_id: '1',
  59. url: 'https://api.mtu.ximengnaikang.com/api', //线上域名
  60. // url: 'http://api.yutu.site.ximengnaikang.com/api', //本地域名
  61. mapKey: 'GTLBZ-F6BKO-5EDWH-SDHIX-K2ART-O2FRQ' //地图key值
  62. },
  63. request(api, params, method, req1) {
  64. var that = this;
  65. if (req1 == false) return;
  66. if (req1) {
  67. that.globalData.req = false;
  68. }
  69. if (my.getStorageSync({ 'key': 'token' }).data != '' || my.getStorageSync({ 'key': 'token' }).data != undefined) {
  70. if (req1 == true || req1 == undefined) {
  71. return new Promise((resolve, reject) => {
  72. my.request({
  73. url: this.globalData.url + api,
  74. data: params,
  75. headers: {
  76. 'content-type': 'application/json',
  77. 'Accept': 'application/json',
  78. 'Cache-Control': 'no-cache',
  79. 'Authorization': my.getStorageSync({ 'key': 'token' }).data,
  80. 'merchant-id': this.globalData.merchant_id,
  81. 'source-type': 'alipay'
  82. },
  83. method: method,
  84. success: (res) => {
  85. // console.log(res)
  86. if (res.status == 450) {
  87. return;
  88. }
  89. if (res.status != 200) {
  90. if (res.data.message == '订单不存在或订单已结算') {
  91. if (this.globalData.compatible) {
  92. my.reLaunch({
  93. url: '/pages/compatible/index/index',
  94. })
  95. } else {
  96. my.reLaunch({
  97. url: '/pages/index/index',
  98. })
  99. }
  100. return false;
  101. } else if (res.data.message == '运动中不能关锁') {
  102. return false;
  103. }
  104. if (res.message != undefined) {
  105. my.showToast({
  106. content: res.message,
  107. icon: 'none',
  108. duration: 3000,
  109. mask: true
  110. })
  111. } else if (res.data.message != undefined) {
  112. my.showToast({
  113. content: res.data.message,
  114. icon: 'none',
  115. duration: 3000,
  116. mask: true
  117. })
  118. }
  119. }
  120. console.log(res, 'token-------------------------------------------')
  121. resolve(res)
  122. },
  123. fail: (err) => {
  124. if (err.status == 401) {
  125. var timeout;
  126. console.log('token过期')
  127. my.removeStorageSync({key:'token'})
  128. my.removeStorageSync({key:'token_time'})
  129. // this.token()
  130. my.reLaunch({
  131. url: '/pages/index/index',
  132. })
  133. } else if (err.status == 404) {
  134. my.navigateTo({
  135. url: '/pages/undefind/undefind',
  136. })
  137. } else {
  138. my.showToast({
  139. content: err.data.message || err.data,
  140. icon: 'none',
  141. mask: true,
  142. duration: 3000,
  143. });
  144. }
  145. console.log(err, 'qingqiushibai')
  146. reject(err, "请求失败")
  147. },
  148. complete: () => {
  149. that.globalData.req = true
  150. }
  151. })
  152. })
  153. }
  154. }
  155. },
  156. login() {
  157. let that = this
  158. let token = my.getStorageSync({ 'key': 'token' }).data
  159. let data = Math.round(new Date() / 1000);
  160. let tokenTime = my.getStorageSync({ 'key': 'token_time' }).data;
  161. let cha = data - tokenTime
  162. let data1 = 60 * 60 * 12
  163. return new Promise((resolve, reject) => {
  164. if (token && cha < data1) {
  165. console.log(Math.round(new Date() / 1000), 'test--------')
  166. return resolve();
  167. } else {
  168. my.getAuthCode({
  169. success: (res) => {
  170. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  171. if (res.authCode) {
  172. console.log(res.authCode)
  173. // var data = {
  174. // 'code': res.authCode,
  175. // 'appid': that.globalData.appid,
  176. // // 'phone_detail': JSON.stringify(arr),
  177. // 'phone_detail': '',
  178. // 'invite_user_id': that.globalData.id,
  179. // 'language': 'zh'
  180. // };
  181. var data = {
  182. 'code': res.authCode,
  183. 'appid': that.globalData.appid,
  184. 'type': 1
  185. };
  186. my.request({
  187. url: that.globalData.url + '/auth/weapp-login',
  188. data: data,
  189. headers: {
  190. 'content-type': 'application/json',
  191. 'Accept': 'application/json',
  192. 'Cache-Control': 'no-cache',
  193. // 'Authorization': my.getStorageSync({ 'key': 'token' }).data,
  194. 'merchant-id': that.globalData.merchant_id,
  195. 'source-type': 'alipay'
  196. },
  197. method: 'POST',
  198. success: (res) => {
  199. console.log(res, 'token')
  200. if (res.status == 401) {
  201. my.showToast({
  202. content: '登陆失败,请重新登录',
  203. icon: 'none'
  204. })
  205. setTimeout(function () {
  206. my.reLaunch({
  207. url: '/pages/authorization/authorization',
  208. })
  209. }, 1000)
  210. } else if (res.status != 200) {
  211. console.log(res, 'cesgu---------------')
  212. my.showToast({
  213. content: '登录失败,请刷新',
  214. icon: 'none'
  215. })
  216. } else {
  217. my.setStorageSync({ 'key': 'token', data: res.data.token });
  218. my.setStorageSync({ 'key': 'session_key', data: res.data.session_key });
  219. my.setStorageSync({ 'key': 'token_time', data: res.data.exp });
  220. my.setStorageSync({ 'key': 'user_ID', data: res.data.user.id });
  221. }
  222. resolve()
  223. }, fail: (err) => {
  224. my.showToast({
  225. // content: err.message,
  226. content: '121212',
  227. icon: 'none'
  228. })
  229. }
  230. })
  231. } else {
  232. my.showToast({
  233. content: res.errMsg,
  234. icon: none,
  235. })
  236. }
  237. }
  238. })
  239. }
  240. })
  241. },
  242. })
  243. // App({
  244. // onLaunch: function () {
  245. // // 登录
  246. // var that = this;
  247. // my.getSystemInfo({
  248. // success: res => {
  249. // console.log(res)
  250. // var system = res.system.trim().split(/\s+/)[1];
  251. // }
  252. // })
  253. // if (my.getStorageSync({'key':'midTimes'}).data != '') {
  254. // var time = my.getStorageSync({'key':'midTimes'}).data;
  255. // var timestamp = Date.parse(new Date()) / 1000;
  256. // if (time <= timestamp) {
  257. // console.log('midtimes过期')
  258. // my.setStorageSync({'key':'midTimes',data:''})
  259. // }
  260. // }
  261. // var time = my.getStorageSync({'key':'token_time'}).data;
  262. // var timestamp = Date.parse(new Date());
  263. // if (time <= timestamp / 1000) {
  264. // console.log('token过期')
  265. // my.removeStorageSync('token_time')
  266. // }
  267. // if (my.canIUse('getUpdateManager')) {
  268. // const updateManager = my.getUpdateManager()
  269. // updateManager.onCheckForUpdate(function (res) {
  270. // if (res.hasUpdate) {
  271. // updateManager.onUpdateReady(function () {
  272. // my.alert({
  273. // title: '更新提示',
  274. // content: '新版本已经准备好,是否重启应用?',
  275. // success: function (res) {
  276. // updateManager.applyUpdate()
  277. // }
  278. // })
  279. // })
  280. // updateManager.onUpdateFailed(function () {
  281. // my.alert({
  282. // title: '已经有新版本了哟~',
  283. // content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  284. // })
  285. // })
  286. // }
  287. // })
  288. // } else {
  289. // my.alert({
  290. // title: '提示',
  291. // content: '当前支付宝版本过低,无法使用该功能,请升级到最新支付宝版本后重试。'
  292. // })
  293. // }
  294. // //登录
  295. // },
  296. // globalData: {
  297. // statusBarHeight: my.getSystemInfoSync()['statusBarHeight'],
  298. // windowHeight: my.getSystemInfoSync()['windowHeight'],
  299. // imgUrl: 'http://resource.bike.hanyiyun.com/',
  300. // screenHeight: '',
  301. // login: false,
  302. // platform: '',
  303. // version: '',
  304. // req: true,
  305. // id: 0,
  306. // times: 60,
  307. // invalid: false,
  308. // compatible: '',
  309. // title: '禹见你', //全局修改此小程序名称
  310. // logoUrl: '../../img/logo.png', //logo图片路径 网络图片/本地图片 dejin.png shanxianLg.png yuzhouLg.png dingding.png
  311. // appid: '2021002147603404', //小程序的appid wxc5f328a92356183f--遇见你appid
  312. // merchant_id: '1',
  313. // url: 'https://api.mtu.ximengnaikang.com/api', //线上域名
  314. // // url: 'http://api.yutu.site.ximengnaikang.com/api', //本地域名
  315. // mapKey: 'GTLBZ-F6BKO-5EDWH-SDHIX-K2ART-O2FRQ' //地图key值
  316. // },
  317. // request(api, params, method, req1) {
  318. // var that = this;
  319. // if (req1 == false) return;
  320. // if (req1) {
  321. // that.globalData.req = false;
  322. // }
  323. // if (my.getStorageSync({'key':'token'}).data != '' || my.getStorageSync({'key':'token'}).data != undefined) {
  324. // if (req1 == true || req1 == undefined) {
  325. // return new Promise((resolve, reject) => {
  326. // my.request({
  327. // url: this.globalData.url + api,
  328. // data: params,
  329. // headers: {
  330. // 'content-type': 'application/json',
  331. // 'Accept': 'application/json',
  332. // 'Cache-Control': 'no-cache',
  333. // 'Authorization': my.getStorageSync({'key':'token'}).data,
  334. // 'merchant-id': this.globalData.merchant_id,
  335. // 'source-type': 'alipay'
  336. // },
  337. // method: method,
  338. // success: (res) => {
  339. // resolve(res)
  340. // // console.log(res)
  341. // if (res.status == 450) {
  342. // return;
  343. // }
  344. // if (res.status != 200) {
  345. // if (res.data.message == '订单不存在或订单已结算') {
  346. // if (this.globalData.compatible) {
  347. // my.reLaunch({
  348. // url: '/pages/compatible/index/index',
  349. // })
  350. // } else {
  351. // my.reLaunch({
  352. // url: '/pages/index/index',
  353. // })
  354. // }
  355. // return false;
  356. // } else if (res.data.message == '运动中不能关锁') {
  357. // return false;
  358. // }
  359. // if (res.message != undefined) {
  360. // my.showToast({
  361. // content: res.message,
  362. // icon: 'none',
  363. // duration: 3000,
  364. // mask: true
  365. // })
  366. // } else if (res.data.message != undefined) {
  367. // my.showToast({
  368. // content: res.data.message,
  369. // icon: 'none',
  370. // duration: 3000,
  371. // mask: true
  372. // })
  373. // }
  374. // }
  375. // if (res.status == 401) {
  376. // var timeout;
  377. // my.removeStorageSync('token')
  378. // my.removeStorageSync('token_time')
  379. // // this.token()
  380. // my.reLaunch({
  381. // url: '/pages/index/index',
  382. // })
  383. // } else if (res.status == 404) {
  384. // my.navigateTo({
  385. // url: '/pages/undefind/undefind',
  386. // })
  387. // }
  388. // },
  389. // fail: (err) => {
  390. // my.showToast({
  391. // content: err.data.message || err.data,
  392. // icon: 'none',
  393. // mask: true,
  394. // duration: 3000,
  395. // });
  396. // console.log(err,'qingqiushibai')
  397. // reject(err,"请求失败")
  398. // },
  399. // complete: () => {
  400. // that.globalData.req = true
  401. // }
  402. // })
  403. // })
  404. // }
  405. // }
  406. // },
  407. // token:function(){
  408. // let that=this
  409. // my.getAuthCode({
  410. // success: function (res) {
  411. // my.getSystemInfo({
  412. // success(res) {
  413. // console.log(arr)
  414. // var arr = [{
  415. // label: '品牌',
  416. // type: 'brand',
  417. // value: res.brand
  418. // },
  419. // {
  420. // label: '型号',
  421. // type: 'model',
  422. // value: res.model
  423. // },
  424. // {
  425. // label: '系统平台',
  426. // type: 'platform',
  427. // value: res.platform
  428. // },
  429. // {
  430. // label: '系统版本',
  431. // type: 'system',
  432. // value: res.system
  433. // },
  434. // {
  435. // label: '支付宝版本',
  436. // type: 'version',
  437. // value: res.version
  438. // },
  439. // {
  440. // label: '小程序版本',
  441. // type: 'SDKVersion',
  442. // value: res.SDKVersion
  443. // },
  444. // {
  445. // label: '定位状态',
  446. // type: 'locationEnabled',
  447. // value: res.locationEnabled ? '开' : '关'
  448. // },
  449. // {
  450. // label: '定位授权',
  451. // type: 'locationAuthorized',
  452. // value: res.locationAuthorized ? '已授权' : '未授权'
  453. // }
  454. // ]
  455. // }
  456. // })
  457. // // 发送 res.code 到后台换取 openId, sessionKey, unionId
  458. // console.log(res.code)
  459. // var data = {
  460. // 'code': res.authCode,
  461. // 'appid': that.globalData.appid,
  462. // 'type': 1
  463. // };
  464. // my.request({
  465. // url: that.globalData.url + '/auth/weapp-login',
  466. // data: data,
  467. // headers: {
  468. // 'content-type': 'application/json',
  469. // 'Accept': 'application/json',
  470. // 'Cache-Control': 'no-cache',
  471. // 'Authorization': my.getStorageSync({'key':'token'}).data,
  472. // 'merchant-id': that.globalData.merchant_id
  473. // },
  474. // method:'POST',
  475. // success:(res)=>{
  476. // my.setStorageSync({'key':'token',data:res.data.token});
  477. // my.setStorageSync({'key':'session_key',data:res.data.session_key});
  478. // my.setStorageSync({'key':'token_time',data:res.data.exp});
  479. // my.setStorageSync({'key':'user_ID',data:res.data.user.id});
  480. // },err:(err=>{
  481. // console.log(err,'登录错误---------------------------------')
  482. // })
  483. // })
  484. // }
  485. // })
  486. // }
  487. // })