app.js 9.4 KB

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