App.vue 13 KB

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