app.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. let data = {
  91. response: res,
  92. 'url': api,
  93. 'data': params,
  94. 'merchant-id': this.globalData.merchant_id,
  95. 'authorization':my.getStorageSync({ 'key': 'token' }).data,
  96. type: 'success',
  97. user: my.getStorageSync({ 'key': 'userInfo' }).data ? my.getStorageSync({ 'key': 'userInfo' }).data.user : '',
  98. version: '1.2.2'
  99. }
  100. my.request({
  101. url: this.globalData.url + '/logs', //仅为示例,并非真实的接口地址
  102. data: { data: data },
  103. method: 'POST',
  104. header: {
  105. 'content-type': 'application/json', // 默认值
  106. 'merchant-id': this.globalData.merchant_id
  107. },
  108. success(res) {
  109. console.log(res.data)
  110. }
  111. })
  112. }
  113. if (res.status != 200) {
  114. if (res.data.message == '订单不存在或订单已结算') {
  115. if (this.globalData.compatible) {
  116. my.reLaunch({
  117. url: '/pages/compatible/index/index',
  118. })
  119. } else {
  120. my.reLaunch({
  121. url: '/pages/index/index',
  122. })
  123. }
  124. return false;
  125. } else if (res.data.message == '运动中不能关锁') {
  126. return false;
  127. }
  128. if (res.message != undefined) {
  129. my.showToast({
  130. content: res.message,
  131. icon: 'none',
  132. duration: 3000,
  133. mask: true
  134. })
  135. } else if (res.data.message != undefined) {
  136. my.showToast({
  137. content: res.data.message,
  138. icon: 'none',
  139. duration: 3000,
  140. mask: true
  141. })
  142. }
  143. }
  144. console.log(res, 'token-------------------------------------------')
  145. resolve(res)
  146. },
  147. fail: (err) => {
  148. if (err.status == 401) {
  149. var timeout;
  150. console.log('token过期')
  151. my.removeStorageSync({ key: 'token' })
  152. my.removeStorageSync({ key: 'token_time' })
  153. // this.token()
  154. my.reLaunch({
  155. url: '/pages/index/index',
  156. })
  157. } else if (err.status == 404) {
  158. my.navigateTo({
  159. url: '/pages/undefind/undefind',
  160. })
  161. } else {
  162. my.showToast({
  163. content: err.data.message || err.data,
  164. icon: 'none',
  165. mask: true,
  166. duration: 3000,
  167. });
  168. }
  169. console.log(err, 'qingqiushibai')
  170. let data = {
  171. response: res,
  172. 'url': api,
  173. 'data': params,
  174. 'merchant-id': this.globalData.merchant_id,
  175. 'authorization': my.getStorageSync({'key':'token'}).data,
  176. type: 'success',
  177. user: my.getStorageSync({ 'key': 'userInfo' }).data ? my.getStorageSync({ 'key': 'userInfo' }).data.user : '',
  178. version: '1.2.2'
  179. }
  180. my.request({
  181. url: this.globalData.url + '/logs', //仅为示例,并非真实的接口地址
  182. data: { data: data },
  183. method: 'POST',
  184. header: {
  185. 'content-type': 'application/json', // 默认值
  186. 'merchant-id': this.globalData.merchant_id
  187. },
  188. success(res) {
  189. console.log(res.data)
  190. }
  191. })
  192. reject(err, "请求失败")
  193. },
  194. complete: () => {
  195. that.globalData.req = true
  196. }
  197. })
  198. })
  199. },
  200. login() {
  201. let that = this
  202. let token = my.getStorageSync({ 'key': 'token' }).data
  203. let data = Math.round(new Date().getTime() / 1000).toString();
  204. let tokenTime = my.getStorageSync({ 'key': 'token_time' }).data || 0;
  205. let cha = data - tokenTime
  206. let data1 = 60 * 60 * 4
  207. return new Promise((resolve, reject) => {
  208. if (token && cha < data1) {
  209. console.log(Math.round(new Date() / 1000), 'test--------')
  210. return resolve();
  211. } else {
  212. my.getAuthCode({
  213. success: (res) => {
  214. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  215. if (res.authCode) {
  216. console.log(res.authCode)
  217. var data = {
  218. 'code': res.authCode,
  219. 'appid': that.globalData.appid,
  220. 'type': 1
  221. };
  222. my.request({
  223. url: that.globalData.url + '/auth/weapp-login',
  224. data: data,
  225. headers: {
  226. 'content-type': 'application/json',
  227. 'Accept': 'application/json',
  228. 'Cache-Control': 'no-cache',
  229. // 'Authorization': my.getStorageSync({ 'key': 'token' }).data,
  230. 'merchant-id': that.globalData.merchant_id,
  231. 'source-type': 'alipay'
  232. },
  233. method: 'POST',
  234. success: (res) => {
  235. console.log(res, 'token')
  236. if (res.status == 401) {
  237. my.showToast({
  238. content: '登陆失败,请重新登录',
  239. icon: 'none'
  240. })
  241. setTimeout(function () {
  242. my.reLaunch({
  243. url: '/pages/authorization/authorization',
  244. })
  245. }, 1000)
  246. } else if (res.status != 200) {
  247. console.log(res, 'cesgu---------------')
  248. my.showToast({
  249. content: res.data.message,
  250. icon: 'none'
  251. })
  252. } else {
  253. my.setStorageSync({ 'key': 'token', data: res.data.token });
  254. my.setStorageSync({ 'key': 'session_key', data: res.data.session_key });
  255. my.setStorageSync({ 'key': 'token_time', data: res.data.exp });
  256. my.setStorageSync({ 'key': 'user_ID', data: res.data.user.id });
  257. }
  258. resolve()
  259. }, fail: (err) => {
  260. my.alert({
  261. title: '提示',
  262. content: '可能网络不太好,请重试!',
  263. success: () => {
  264. my.reLaunch({
  265. url: '/pages/error/error?error=0'
  266. });
  267. }
  268. })
  269. }
  270. })
  271. } else {
  272. my.showToast({
  273. content: res.errMsg,
  274. icon: none,
  275. })
  276. }
  277. }
  278. })
  279. }
  280. })
  281. },
  282. })