app.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. App({
  2. onLaunch: function () {
  3. let token=my.getStorageSync({'key':'token'}).data
  4. let data = Math.round(new Date().getTime()/1000).toString();;
  5. let tokenTime=my.getStorageSync({'key':'token_time'}).data
  6. let cha=data-tokenTime
  7. let data1=60*60*24
  8. if(!token){
  9. this.token()
  10. }else if(cha>data1){
  11. my.removeStorageSync('token')
  12. this.token()
  13. }
  14. // 登录
  15. var that = this;
  16. my.getSystemInfo({
  17. success: res => {
  18. console.log(res)
  19. var system = res.system.trim().split(/\s+/)[1];
  20. // var intNumber = system.substring(0, system.indexOf("."));
  21. // if (res.platform == 'ios' && intNumber < 13 || res.SDKVersion < '2.1.0') {
  22. // console.log('低版本')
  23. // that.globalData.compatible = true;
  24. // my.reLaunch({
  25. // url: '/pages/compatible/index/index',
  26. // })
  27. // }
  28. // that.globalData.platform = res.platform
  29. // that.globalData.version = res.version
  30. // console.log(res.safeArea.bottom)
  31. // console.log('手机信息res' + res.model)
  32. // if (res.model == 'iPhone X' || res.model == 'iPhone XR' || res.model == 'iPhone XS Max') {
  33. // that.globalData.screenHeight = 'xSeries'
  34. // } else if (res.safeArea.bottom >= 780) {
  35. // that.globalData.screenHeight = 'xSeries'
  36. // }
  37. }
  38. })
  39. if (my.getStorageSync({'key':'midTimes'}).data != '') {
  40. var time = my.getStorageSync({'key':'midTimes'}).data;
  41. var timestamp = Date.parse(new Date()) / 1000;
  42. if (time <= timestamp) {
  43. console.log('midtimes过期')
  44. my.setStorageSync({'key':'midTimes',data:''})
  45. }
  46. }
  47. var time = my.getStorageSync({'key':'token_time'}).data;
  48. var timestamp = Date.parse(new Date());
  49. if (time <= timestamp / 1000) {
  50. console.log('token过期')
  51. my.removeStorageSync('token_time')
  52. }
  53. if (my.canIUse('getUpdateManager')) {
  54. const updateManager = my.getUpdateManager()
  55. updateManager.onCheckForUpdate(function (res) {
  56. if (res.hasUpdate) {
  57. updateManager.onUpdateReady(function () {
  58. my.alert({
  59. title: '更新提示',
  60. content: '新版本已经准备好,是否重启应用?',
  61. success: function (res) {
  62. updateManager.applyUpdate()
  63. }
  64. })
  65. })
  66. updateManager.onUpdateFailed(function () {
  67. my.alert({
  68. title: '已经有新版本了哟~',
  69. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  70. })
  71. })
  72. }
  73. })
  74. } else {
  75. my.alert({
  76. title: '提示',
  77. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  78. })
  79. }
  80. //登录
  81. },
  82. globalData: {
  83. statusBarHeight: my.getSystemInfoSync()['statusBarHeight'],
  84. windowHeight: my.getSystemInfoSync()['windowHeight'],
  85. imgUrl: 'http://resource.bike.hanyiyun.com/',
  86. screenHeight: '',
  87. login: false,
  88. platform: '',
  89. version: '',
  90. req: true,
  91. id: 0,
  92. times: 60,
  93. invalid: false,
  94. compatible: '',
  95. title: '禹见你', //全局修改此小程序名称
  96. logoUrl: '/img/dingding.png', //logo图片路径 网络图片/本地图片 dejin.png shanxianLg.png yuzhouLg.png dingding.png
  97. appid: '2021002147603404', //小程序的appid wxc5f328a92356183f--遇见你appid
  98. merchant_id: '1',
  99. url: 'https://api.mtu.ximengnaikang.com/api', //线上域名
  100. // url: 'http://api.yutu.site.ximengnaikang.com/api', //本地域名
  101. mapKey: 'GTLBZ-F6BKO-5EDWH-SDHIX-K2ART-O2FRQ' //地图key值
  102. },
  103. request(api, params, method, req1) {
  104. var that = this;
  105. if (req1 == false) return;
  106. if (req1) {
  107. that.globalData.req = false;
  108. }
  109. if (my.getStorageSync({'key':'token'}).data != '' || my.getStorageSync({'key':'token'}).data != undefined) {
  110. if (req1 == true || req1 == undefined) {
  111. return new Promise((resolve, reject) => {
  112. my.request({
  113. url: this.globalData.url + api,
  114. data: params,
  115. headers: {
  116. 'content-type': 'application/json',
  117. 'Accept': 'application/json',
  118. 'Cache-Control': 'no-cache',
  119. 'Authorization': my.getStorageSync({'key':'token'}).data,
  120. 'merchant-id': this.globalData.merchant_id,
  121. 'source-type': 'alipay'
  122. },
  123. method: method,
  124. success: (res) => {
  125. resolve(res)
  126. // console.log(res)
  127. if (res.status == 450) {
  128. return;
  129. }
  130. if (res.status != 200) {
  131. if (res.data.message == '订单不存在或订单已结算') {
  132. if (this.globalData.compatible) {
  133. my.reLaunch({
  134. url: '/pages/compatible/index/index',
  135. })
  136. } else {
  137. my.reLaunch({
  138. url: '/pages/index/index',
  139. })
  140. }
  141. return false;
  142. } else if (res.data.message == '运动中不能关锁') {
  143. return false;
  144. }
  145. if (res.message != undefined) {
  146. my.showToast({
  147. content: res.message,
  148. icon: 'none',
  149. duration: 3000,
  150. mask: true
  151. })
  152. } else if (res.data.message != undefined) {
  153. my.showToast({
  154. content: res.data.message,
  155. icon: 'none',
  156. duration: 3000,
  157. mask: true
  158. })
  159. }
  160. }
  161. if (res.status == 401) {
  162. var timeout;
  163. my.removeStorageSync('token')
  164. my.removeStorageSync('token_time')
  165. // this.token()
  166. my.reLaunch({
  167. url: '/pages/index/index',
  168. })
  169. } else if (res.status == 404) {
  170. my.navigateTo({
  171. url: '/pages/undefind/undefind',
  172. })
  173. }
  174. },
  175. fail: (err) => {
  176. my.showToast({
  177. content: err,
  178. icon: 'none',
  179. mask: true,
  180. duration: 3000,
  181. });
  182. reject("请求失败")
  183. },
  184. complete: () => {
  185. that.globalData.req = true
  186. }
  187. })
  188. })
  189. }
  190. }
  191. },
  192. token:function(){
  193. let that=this
  194. my.getAuthCode({
  195. success: function (res) {
  196. my.getSystemInfo({
  197. success(res) {
  198. console.log(arr)
  199. var arr = [{
  200. label: '品牌',
  201. type: 'brand',
  202. value: res.brand
  203. },
  204. {
  205. label: '型号',
  206. type: 'model',
  207. value: res.model
  208. },
  209. {
  210. label: '系统平台',
  211. type: 'platform',
  212. value: res.platform
  213. },
  214. {
  215. label: '系统版本',
  216. type: 'system',
  217. value: res.system
  218. },
  219. {
  220. label: '微信版本',
  221. type: 'version',
  222. value: res.version
  223. },
  224. {
  225. label: '小程序版本',
  226. type: 'SDKVersion',
  227. value: res.SDKVersion
  228. },
  229. {
  230. label: '定位状态',
  231. type: 'locationEnabled',
  232. value: res.locationEnabled ? '开' : '关'
  233. },
  234. {
  235. label: '定位授权',
  236. type: 'locationAuthorized',
  237. value: res.locationAuthorized ? '已授权' : '未授权'
  238. }
  239. ]
  240. }
  241. })
  242. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  243. console.log(res.code)
  244. var data = {
  245. 'code': res.authCode,
  246. 'appid': that.globalData.appid,
  247. 'type': 1
  248. };
  249. my.request({
  250. url: that.globalData.url + '/auth/weapp-login',
  251. data: data,
  252. headers: {
  253. 'content-type': 'application/json',
  254. 'Accept': 'application/json',
  255. 'Cache-Control': 'no-cache',
  256. 'Authorization': my.getStorageSync({'key':'token'}).data,
  257. 'merchant-id': that.globalData.merchant_id
  258. },
  259. method:'POST',
  260. success:(res)=>{
  261. my.setStorageSync({'key':'token',data:res.data.token});
  262. my.setStorageSync({'key':'session_key',data:res.data.session_key});
  263. my.setStorageSync({'key':'token_time',data:res.data.exp});
  264. my.setStorageSync({'key':'user_ID',data:res.data.user.id});
  265. }
  266. })
  267. }
  268. })
  269. }
  270. })