app.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. var http = require("/utils/config.js");
  2. App({
  3. onLaunch: function () {
  4. // 登录
  5. console.log(http)
  6. var that = this;
  7. wx.getSystemInfo({
  8. success: res => {
  9. console.log(res)
  10. var system = res.system.trim().split(/\s+/)[1];
  11. var intNumber = system.substring(0, system.indexOf("."));
  12. if (res.platform == 'ios' && intNumber < 13 || res.SDKVersion<'2.1.0') {
  13. console.log('低版本')
  14. that.globalData.compatible = true;
  15. wx.reLaunch({
  16. url: '/pages/compatible/index/index',
  17. })
  18. } else {
  19. console.log('高版本')
  20. that.globalData.compatible = false;
  21. wx.reLaunch({
  22. url: '/pages/index/index',
  23. })
  24. }
  25. that.globalData.platform = res.platform
  26. that.globalData.version = res.version
  27. console.log(res.safeArea.bottom)
  28. console.log('手机信息res' + res.model)
  29. if (res.model == 'iPhone X' || res.model == 'iPhone XR' || res.model == 'iPhone XS Max') {
  30. that.globalData.screenHeight = 'xSeries'
  31. } else if (res.safeArea.bottom >= 780) {
  32. that.globalData.screenHeight = 'xSeries'
  33. }
  34. }
  35. })
  36. if (wx.getStorageSync('midTimes') != '') {
  37. var time = wx.getStorageSync('midTimes');
  38. var timestamp = Date.parse(new Date()) / 1000;
  39. if (time <= timestamp) {
  40. console.log('midtimes过期')
  41. wx.setStorageSync('midTimes', '')
  42. }
  43. }
  44. var imgArr = wx.getStorageSync('imgUrl');
  45. if (imgArr == undefined || imgArr == '') {
  46. var imgUrl = ['weilaibike.com/xiaobanma/mineBg.png', 'weilaibike.com/xiaobanma/walletBg.png', 'weilaibike.com/xiaobanma/moneyBg.png'];
  47. wx.setStorageSync('imgUrl', imgUrl)
  48. }
  49. var time = wx.getStorageSync('token_time');
  50. var timestamp = Date.parse(new Date());
  51. if (time <= timestamp / 1000) {
  52. console.log('token过期')
  53. wx.removeStorageSync('token_time')
  54. }
  55. if (wx.canIUse('getUpdateManager')) {
  56. const updateManager = wx.getUpdateManager()
  57. updateManager.onCheckForUpdate(function (res) {
  58. if (res.hasUpdate) {
  59. updateManager.onUpdateReady(function () {
  60. wx.showModal({
  61. title: '更新提示',
  62. content: '新版本已经准备好,是否重启应用?',
  63. success: function (res) {
  64. if (res.confirm) {
  65. updateManager.applyUpdate()
  66. }
  67. }
  68. })
  69. })
  70. updateManager.onUpdateFailed(function () {
  71. wx.showModal({
  72. title: '已经有新版本了哟~',
  73. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  74. })
  75. })
  76. }
  77. })
  78. } else {
  79. wx.showModal({
  80. title: '提示',
  81. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  82. })
  83. }
  84. },
  85. globalData: {
  86. statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
  87. windowHeight: wx.getSystemInfoSync()['windowHeight'],
  88. localUrl: http.url,
  89. screenHeight: '',
  90. login: false,
  91. platform: '',
  92. version: '',
  93. req: true,
  94. id: 0,
  95. times: 60,
  96. invalid: false,
  97. compatible: ''
  98. },
  99. request(api, params, method, req1) {
  100. var that = this;
  101. // console.log(req1)
  102. if (req1 == false) return;
  103. if (req1) {
  104. that.globalData.req = false;
  105. }
  106. if (wx.getStorageSync('token') != '' || wx.getStorageSync('token') != undefined) {
  107. if (req1 == true || req1 == undefined) {
  108. return new Promise((resolve, reject) => {
  109. wx.request({
  110. url: this.globalData.localUrl + api,
  111. data: params,
  112. header: {
  113. 'content-type': 'application/x-www-form-urlencoded',
  114. 'Authorization': wx.getStorageSync('token'),
  115. },
  116. method: method,
  117. success: (res) => {
  118. resolve(res)
  119. if (res.statusCode == 450) {
  120. return;
  121. }
  122. if (res.data.statusCode != 200) {
  123. if (res.data.message == '订单不存在或订单已结算') {
  124. if (this.globalData.compatible) {
  125. wx.reLaunch({
  126. url: '/pages/compatible/index/index',
  127. })
  128. } else {
  129. wx.reLaunch({
  130. url: '/pages/index/index',
  131. })
  132. }
  133. return false;
  134. } else if (res.data.message == '运动中不能关锁') {
  135. return false;
  136. }
  137. if (res.message != undefined) {
  138. wx.showToast({
  139. title: res.message,
  140. icon: 'none',
  141. duration: 3000,
  142. mask: true
  143. })
  144. } else if (res.data.message != undefined) {
  145. wx.showToast({
  146. title: res.data.message,
  147. icon: 'none',
  148. duration: 3000,
  149. mask: true
  150. })
  151. }
  152. } else if (res.statusCode != 200) {
  153. if (res.data.message == '订单不存在或订单已结算') {
  154. if (this.globalData.compatible) {
  155. wx.reLaunch({
  156. url: '/pages/compatible/index/index',
  157. })
  158. } else {
  159. wx.reLaunch({
  160. url: '/pages/index/index',
  161. })
  162. }
  163. return false;
  164. } else if (res.data.message == '运动中不能关锁') {
  165. return false;
  166. }
  167. if (res.message != undefined) {
  168. wx.showToast({
  169. title: res.message,
  170. icon: 'none',
  171. duration: 3000,
  172. mask: true
  173. })
  174. } else if (res.data.message != undefined) {
  175. wx.showToast({
  176. title: res.data.message,
  177. icon: 'none',
  178. duration: 3000,
  179. mask: true
  180. })
  181. }
  182. }
  183. if (res.statusCode == 401) {
  184. var timeout;
  185. wx.removeStorageSync('token')
  186. wx.removeStorageSync('token_time')
  187. that.globalData.invalid = true
  188. console.log(that.globalData.login)
  189. if (that.globalData.login) {
  190. return false;
  191. }
  192. if (that.globalData.login == false) {
  193. that.globalData.login = true
  194. wx.showToast({
  195. title: '登陆过期请重新登陆~',
  196. icon: 'none',
  197. duration: 1000,
  198. mask: true,
  199. success: function () {
  200. wx.reLaunch({
  201. url: '/pages/authentication/authentication',
  202. })
  203. }
  204. })
  205. timeout = setTimeout(function () {
  206. that.globalData.login = false
  207. if (that.globalData.login == false) {
  208. clearTimeout(timeout)
  209. }
  210. }, 3000)
  211. }
  212. } else if (res.statusCode == 404) {
  213. wx.navigateTo({
  214. url: '/pages/undefind/undefind',
  215. })
  216. }
  217. },
  218. fail: (err) => {
  219. wx.showToast({
  220. title: err,
  221. icon: 'none',
  222. mask: true
  223. });
  224. reject("请求失败")
  225. },
  226. complete: () => {
  227. that.globalData.req = true
  228. }
  229. })
  230. })
  231. }
  232. }
  233. },
  234. })