App.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <script>
  2. export default {
  3. onLaunch: function () {
  4. // setInterval((d) => {
  5. // //定时器,定时去调取聊天未读消息
  6. // let userId = uni.getStorageSync('userId')
  7. // if (userId) {
  8. // this.$Request.getMsg('/app/ordersChat/selectUserAllUnreadCount').then((res) => {
  9. // if (res.code === 0) {
  10. // let messageCount = res.data
  11. // uni.setStorageSync('messageCount', messageCount)
  12. // let num = messageCount
  13. // // console.log(num, '未读消息数')
  14. // if (num == 0) {
  15. // uni.removeTabBarBadge({
  16. // index: 2
  17. // })
  18. // return
  19. // }
  20. // uni.setTabBarBadge({
  21. // index: 2,
  22. // text: num + ''
  23. // })
  24. // }
  25. // })
  26. // }
  27. // }, 3000)
  28. // 是否开启总送跑腿新用户红包 256
  29. this.$Request.get('/app/common/type/256').then((res) => {
  30. if (res.code == 0) {
  31. this.$queue.setData('paotuiHB', res.data.value)
  32. }
  33. })
  34. // 是否开启赠送外卖新用户红包 324
  35. this.$Request.get('/app/common/type/324').then((res) => {
  36. if (res.code == 0) {
  37. this.$queue.setData('waimaiHB', res.data.value)
  38. }
  39. })
  40. //小程序上线开关
  41. console.log('App Launch')
  42. // this.$Request.get('/app/common/type/261').then(res => {
  43. // if (res.code == 0) {
  44. // // #ifdef MP-WEIXIN
  45. // this.$queue.setData('XCXIsSelect', res.data.value);
  46. // this.$queue.setData('shangxianSelect', res.data.value);
  47. // // #endif
  48. // // #ifndef MP-WEIXIN
  49. // this.$queue.setData('XCXIsSelect', '是');
  50. // this.$queue.setData('shangxianSelect', '是');
  51. // // #endif
  52. // }
  53. // });
  54. this.$Request.get('/app/common/type/264').then((res) => {
  55. if (res.code == 0) {
  56. // #ifdef MP-WEIXIN
  57. this.$queue.setData('XCXIsSelect', res.data.value)
  58. this.$queue.setData('shangxianSelect', res.data.value)
  59. // #endif
  60. // #ifndef MP-WEIXIN
  61. this.$queue.setData('XCXIsSelect', '是')
  62. this.$queue.setData('shangxianSelect', '是')
  63. // #endif
  64. }
  65. })
  66. //#ifdef APP-PLUS
  67. // APP检测更新 具体打包流程可以参考:https://ask.dcloud.net.cn/article/35667
  68. plus.screen.lockOrientation('portrait-primary') //竖屏正方向锁定
  69. //获取是否热更新过
  70. const updated = uni.getStorageSync('updated') // 尝试读取storage
  71. let that = this
  72. if (updated.completed === true) {
  73. // 如果上次刚更新过
  74. // 删除安装包及安装记录
  75. console.log('安装记录被删除,更新成功')
  76. uni.removeSavedFile({
  77. filePath: updated.packgePath,
  78. success: (res) => {
  79. uni.removeStorageSync('updated')
  80. }
  81. })
  82. } else if (updated.completed === false) {
  83. uni.removeStorageSync('updated')
  84. plus.runtime.install(updated.packgePath, {
  85. force: true
  86. })
  87. uni.setStorage({
  88. key: 'updated',
  89. data: {
  90. completed: true,
  91. packgePath: updated.packgePath
  92. },
  93. success: (res) => {
  94. console.log('成功安装上次的更新,应用需要重启才能继续完成')
  95. }
  96. })
  97. uni.showModal({
  98. title: '温馨提示',
  99. content: '应用将重启以完成更新',
  100. showCancel: false,
  101. complete: () => {
  102. plus.runtime.restart()
  103. }
  104. })
  105. } else {
  106. //获取当前系统版本信息
  107. plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
  108. //请求后台接口 解析数据 对比版本
  109. that.$Request.getT('/app/user/selectNewApp').then((res) => {
  110. res = res.data[0]
  111. if (res.wgtUrl && widgetInfo.version < res.version) {
  112. let downloadLink = ''
  113. let androidLink = res.androidWgtUrl
  114. let iosLink = res.iosWgtUrl
  115. let ready = false
  116. //校验是是不是热更新
  117. if (res.wgtUrl.match(RegExp(/.wgt/))) {
  118. // 判断系统类型
  119. if (plus.os.name.toLowerCase() === 'android') {
  120. console.log('安卓系统')
  121. if (androidLink && androidLink !== '#') {
  122. // 我这里默认#也是没有地址,请根据业务自行修改
  123. console.log('发现下载地址')
  124. // 安卓:创建下载任务
  125. if (androidLink.match(RegExp(/.wgt/))) {
  126. console.log('确认wgt热更新包')
  127. downloadLink = androidLink
  128. ready = true
  129. } else {
  130. console.log('安卓推荐.wgt强制更新,.apk的强制更新请您自行修改程序')
  131. }
  132. } else {
  133. console.log('下载地址是空的,无法继续')
  134. }
  135. } else {
  136. console.log('苹果系统')
  137. if (iosLink && iosLink !== '#') {
  138. // 我这里默认#也是没有地址,请根据业务自行修改
  139. console.log('发现下载地址')
  140. // 苹果(A):进行热更新(如果iosLink是wgt更新包的下载地址)判断文件名中是否含有.wgt
  141. if (iosLink.match(RegExp(/.wgt/))) {
  142. console.log('确认wgt热更新包')
  143. downloadLink = iosLink
  144. ready = true
  145. } else {
  146. console.log('苹果只支持.wgt强制更新')
  147. }
  148. } else {
  149. console.log('下载地址是空的,无法继续')
  150. }
  151. }
  152. if (ready) {
  153. console.log('任务开始')
  154. let downloadTask = uni.downloadFile({
  155. url: downloadLink,
  156. success: (res) => {
  157. if (res.statusCode === 200) {
  158. // 保存下载的安装包
  159. console.log('保存安装包')
  160. uni.saveFile({
  161. tempFilePath: res.tempFilePath,
  162. success: (res) => {
  163. const packgePath = res.savedFilePath
  164. // 保存更新记录到stroage,下次启动app时安装更新
  165. uni.setStorage({
  166. key: 'updated',
  167. data: {
  168. completed: false,
  169. packgePath: packgePath
  170. },
  171. success: () => {
  172. console.log('成功保存记录')
  173. }
  174. })
  175. // 任务完成,关闭下载任务
  176. console.log('任务完成,关闭下载任务,下一次启动应用时将安装更新')
  177. downloadTask.abort()
  178. downloadTask = null
  179. }
  180. })
  181. }
  182. }
  183. })
  184. } else {
  185. console.log('下载地址未准备,无法开启下载任务')
  186. }
  187. } else {
  188. //不是热更新是在线更新 校验是否强制升级
  189. if (res.method == 'true') {
  190. uni.showModal({
  191. showCancel: false,
  192. confirmText: '立即更新',
  193. title: '发现新版本',
  194. content: res.des,
  195. success: (res) => {
  196. if (res.confirm) {
  197. that.$queue.showLoading('下载中...')
  198. if (uni.getSystemInfoSync().platform == 'android') {
  199. uni.downloadFile({
  200. url: androidLink,
  201. success: (downloadResult) => {
  202. if (downloadResult.statusCode === 200) {
  203. plus.runtime.install(
  204. downloadResult.tempFilePath,
  205. {
  206. force: false
  207. },
  208. (d) => {
  209. console.log('install success...')
  210. plus.runtime.restart()
  211. },
  212. (e) => {
  213. console.error('install fail...')
  214. }
  215. )
  216. }
  217. }
  218. })
  219. }
  220. if (uni.getSystemInfoSync().platform == 'ios') {
  221. plus.runtime.openURL(iosLink, function (res) {})
  222. }
  223. } else if (res.cancel) {
  224. console.log('取消')
  225. }
  226. }
  227. })
  228. } else {
  229. uni.showModal({
  230. title: '发现新版本',
  231. confirmText: '立即更新',
  232. cancelText: '下次更新',
  233. content: res.des,
  234. success: (res) => {
  235. if (res.confirm) {
  236. that.$queue.showLoading('下载中...')
  237. if (uni.getSystemInfoSync().platform == 'android') {
  238. uni.downloadFile({
  239. url: androidLink,
  240. success: (downloadResult) => {
  241. if (downloadResult.statusCode === 200) {
  242. plus.runtime.install(
  243. downloadResult.tempFilePath,
  244. {
  245. force: false
  246. },
  247. (d) => {
  248. console.log('install success...')
  249. plus.runtime.restart()
  250. },
  251. (e) => {
  252. console.error('install fail...')
  253. }
  254. )
  255. }
  256. }
  257. })
  258. }
  259. if (uni.getSystemInfoSync().platform == 'ios') {
  260. plus.runtime.openURL(iosLink, function (res) {})
  261. }
  262. } else if (res.cancel) {
  263. console.log('取消')
  264. }
  265. }
  266. })
  267. }
  268. }
  269. }
  270. })
  271. })
  272. }
  273. //#endif
  274. //#ifdef H5
  275. let ua = navigator.userAgent.toLowerCase()
  276. if (ua.indexOf('micromessenger') !== -1) {
  277. let openid = uni.getStorageSync('openid')
  278. let userId = uni.getStorageSync('userId')
  279. let that = this
  280. console.log(openid)
  281. if (!openid) {
  282. if (window.location.href.indexOf('?code=') !== -1 || window.location.href.indexOf('&code=') !== -1) {
  283. let code
  284. if (window.location.href.indexOf('?code=') !== -1) {
  285. code = window.location.href.split('?code=')[1].split('&')[0]
  286. } else {
  287. code = window.location.href.split('&code=')[1].split('&')[0]
  288. }
  289. let data = {
  290. code: code
  291. }
  292. this.$Request.get('/app/Login/getOpenId', data).then((ret) => {
  293. this.$queue.setData('openid', ret.data)
  294. let data2 = {
  295. openId: ret.data,
  296. userType: 1
  297. }
  298. this.$Request.get('/app/Login/wxOpenIdLogin', data2).then((res) => {
  299. console.error(res)
  300. if (res.code == 0) {
  301. this.$queue.setData('token', res.token)
  302. this.$queue.setData('userId', res.user.userId)
  303. this.$queue.setData('phone', res.user.phone)
  304. uni.setStorageSync('userName', res.user.userName)
  305. uni.setStorageSync('avatar', res.user.avatar)
  306. uni.setStorageSync('phone', res.user.phone)
  307. uni.setStorageSync('sex', res.user.sex)
  308. uni.setStorageSync('userId', res.user.userId)
  309. uni.setStorageSync('wxCode', res.user.wxCode)
  310. uni.setStorageSync('wxQrCode', res.user.wxQrCode)
  311. uni.setStorageSync('zhiFuBao', res.user.zhiFuBao)
  312. uni.setStorageSync('zhiFuBaoName', res.user.zhiFuBaoName)
  313. }
  314. })
  315. })
  316. } else {
  317. window.location.href =
  318. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  319. that.$queue.getWxAppid() +
  320. '&redirect_uri=' +
  321. window.location.href.split('#')[0] +
  322. '&response_type=code&scope=snsapi_base#wechat_redirect'
  323. }
  324. } else {
  325. this.$Request.get('/app/Login/wxOpenIdLogin?openId=' + openid + '&userType=1').then((res) => {
  326. if (res.code == 0) {
  327. this.$queue.setData('token', res.token)
  328. this.$queue.setData('userId', res.user.userId)
  329. this.$queue.setData('phone', res.user.phone)
  330. uni.setStorageSync('userName', res.user.userName)
  331. uni.setStorageSync('avatar', res.user.avatar)
  332. uni.setStorageSync('phone', res.user.phone)
  333. uni.setStorageSync('sex', res.user.sex)
  334. uni.setStorageSync('userId', res.user.userId)
  335. uni.setStorageSync('wxCode', res.user.wxCode)
  336. uni.setStorageSync('wxQrCode', res.user.wxQrCode)
  337. uni.setStorageSync('zhiFuBao', res.user.zhiFuBao)
  338. uni.setStorageSync('zhiFuBaoName', res.user.zhiFuBaoName)
  339. }
  340. })
  341. }
  342. }
  343. //#endif
  344. },
  345. onShow: function () {
  346. console.log('App Show')
  347. // #ifdef APP-PLUS
  348. if (uni.getSystemInfoSync().platform == 'android') {
  349. let clientid = plus.push.getClientInfo().clientid
  350. let userId = this.$queue.getData('userId')
  351. if (userId) {
  352. console.log(clientid, '----', userId)
  353. this.$Request.postT('/app/user/updateClientId?clientId=' + clientid).then((res) => {})
  354. }
  355. }
  356. //#endif
  357. },
  358. onHide: function () {
  359. console.log('App Hide')
  360. }
  361. }
  362. </script>
  363. <style lang="scss">
  364. /*每个页面公共css */
  365. @import 'uview-ui/index.scss';
  366. @import 'components/colorui/main.css';
  367. @import 'components/colorui/icon.css';
  368. </style>