App.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <script>
  2. import {
  3. checkLogin
  4. } from './libs/login';
  5. import {
  6. HTTP_REQUEST_URL
  7. } from './config/app';
  8. import {
  9. getShopConfig,
  10. silenceAuth
  11. } from '@/api/public';
  12. import Auth from '@/libs/wechat.js';
  13. import Routine from './libs/routine.js';
  14. import {
  15. colorChange,
  16. get_openid
  17. } from '@/api/api.js';
  18. import {
  19. mapGetters
  20. } from "vuex"
  21. // #ifdef MP
  22. // let livePlayer = requirePlugin('live-player-plugin')
  23. // #endif
  24. let green =
  25. '--view-theme: #42CA4D;--view-priceColor:#FF7600;--view-minorColor:rgba(108, 198, 94, 0.5);--view-minorColorT:rgba(66, 202, 77, 0.1);--view-bntColor:#FE960F;--view-assistColor:#FE960F;'
  26. let red =
  27. '--view-theme: #e93323;--view-priceColor:#e93323;--view-minorColor:rgba(233, 51, 35, 0.5);--view-minorColorT:rgba(233, 51, 35, 0.1);--view-bntColor:#FE960F;--view-assistColor:#FE960F;'
  28. let blue =
  29. '--view-theme: #1DB0FC;--view-priceColor:#FD502F;--view-minorColor:rgba(58, 139, 236, 0.5);--view-minorColorT:rgba(9, 139, 243, 0.1);--view-bntColor:#22CAFD;--view-assistColor:#C4D9EC;'
  30. let pink =
  31. '--view-theme: #FF448F;--view-priceColor:#FF448F;--view-minorColor:rgba(255, 68, 143, 0.5);--view-minorColorT:rgba(255, 68, 143, 0.1);--view-bntColor:#282828;--view-assistColor:#FEAC41;'
  32. let orange =
  33. '--view-theme: #FE5C2D;--view-priceColor:#FE5C2D;--view-minorColor:rgba(254, 92, 45, 0.5);--view-minorColorT:rgba(254, 92, 45, 0.1);--view-bntColor:#FDB000;--view-assistColor:#FDB000;'
  34. let gold =
  35. '--view-theme: #E0A558;--view-priceColor:#DA8C18;--view-minorColor:rgba(224, 165, 88, 0.5);--view-minorColorT:rgba(224, 165, 88, 0.1);--view-bntColor:#1A1A1A;'
  36. // #ifdef APP
  37. import {
  38. getUpApp
  39. } from './utils/upApp.js';
  40. // #endif
  41. export default {
  42. globalData: {
  43. spid: 0,
  44. code: 0,
  45. isLogin: false,
  46. userInfo: {},
  47. MyMenus: [],
  48. globalData: false,
  49. isIframe: false,
  50. tabbarShow: true,
  51. windowHeight: 0
  52. },
  53. computed: mapGetters(['isLogin', 'cartNum']),
  54. watch: {
  55. isLogin: {
  56. deep: true, //深度监听设置为 true
  57. handler: function(newV, oldV) {
  58. if (newV) {
  59. // this.getCartNum()
  60. } else {
  61. this.$store.commit('indexData/setCartNum', '')
  62. }
  63. }
  64. },
  65. cartNum(newCart, b) {
  66. this.$store.commit('indexData/setCartNum', newCart + '')
  67. if (newCart > 0) {
  68. uni.setTabBarBadge({
  69. index: 3,
  70. text: newCart > 99 ? '99+' : newCart + ''
  71. })
  72. } else {
  73. uni.hideTabBarRedDot({
  74. index: 3
  75. })
  76. }
  77. }
  78. },
  79. onLaunch: async function(option) {
  80. let that = this;
  81. // #ifdef H5
  82. let openid = uni.getStorageSync('openid')||false;
  83. if (this.$wechat.isWeixin()&&!openid) {
  84. if (!option.query.code) {
  85. that.openUrlWeixin();
  86. return;
  87. } else {
  88. let code = '';
  89. if (option.query.code instanceof Array) {
  90. code = option.query.code[option.query.code.length - 1]
  91. } else {
  92. code = option.query.code
  93. }
  94. let res = await get_openid({
  95. code: code,
  96. }).then((re) => {
  97. // 保存openid
  98. uni.setStorageSync('openid', re.data.openid);
  99. }).catch(error => {
  100. console.log(error,'error');
  101. // that.openUrlWeixin()
  102. uni.hideLoading();
  103. });
  104. }
  105. }
  106. // #endif
  107. //#ifdef APP
  108. plus.screen.lockOrientation("portrait-primary");
  109. //#endif
  110. colorChange('color_change').then(res => {
  111. let navigation = res.data.navigation; //判断悬浮导航是否显示
  112. let statusColor = res.data.status; //判断显示啥颜色
  113. uni.setStorageSync('navigation', navigation);
  114. uni.$emit('navOk', navigation);
  115. uni.setStorageSync('statusColor', statusColor);
  116. uni.$emit('colorOk', statusColor);
  117. switch (res.data.status) {
  118. case 1:
  119. uni.setStorageSync('viewColor', blue)
  120. uni.$emit('ok', blue)
  121. break;
  122. case 2:
  123. uni.setStorageSync('viewColor', green)
  124. uni.$emit('ok', green)
  125. break;
  126. case 3:
  127. uni.setStorageSync('viewColor', red)
  128. uni.$emit('ok', red)
  129. break;
  130. case 4:
  131. uni.setStorageSync('viewColor', pink)
  132. uni.$emit('ok', pink)
  133. break;
  134. case 5:
  135. uni.setStorageSync('viewColor', orange)
  136. uni.$emit('ok', orange)
  137. break;
  138. case 6:
  139. uni.setStorageSync('viewColor', gold)
  140. uni.$emit('ok', gold)
  141. break;
  142. default:
  143. uni.setStorageSync('viewColor', red)
  144. uni.$emit('ok', red)
  145. break
  146. }
  147. });
  148. if (option.query.spid) {
  149. that.$Cache.set('spid', option.query.spid);
  150. that.globalData.spid = option.query.spid;
  151. }
  152. // #ifdef APP-PLUS || H5
  153. uni.getSystemInfo({
  154. success: function(res) {
  155. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  156. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  157. let height = res.windowHeight - res.statusBarHeight - 44
  158. // #ifdef H5 || APP-PLUS
  159. that.globalData.windowHeight = res.windowHeight + 'px'
  160. // #endif
  161. // // #ifdef APP-PLUS
  162. // that.globalData.windowHeight = height + 'px'
  163. // // #endif
  164. }
  165. });
  166. // #endif
  167. // #ifdef MP
  168. if (HTTP_REQUEST_URL == '') {
  169. console.error(
  170. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  171. );
  172. return false;
  173. }
  174. if (option.query.hasOwnProperty('scene')) {
  175. switch (option.scene) {
  176. //扫描小程序码
  177. case 1047:
  178. let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
  179. that.globalData.code = val.spid === undefined ? val : val.spid;
  180. break;
  181. //长按图片识别小程序码
  182. case 1048:
  183. that.globalData.code = option.query.scene;
  184. break;
  185. //手机相册选取小程序码
  186. case 1049:
  187. that.globalData.code = option.query.scene;
  188. break;
  189. //直接进入小程序
  190. case 1001:
  191. that.globalData.spid = option.query.scene;
  192. break;
  193. }
  194. }
  195. this.checkUpdateVersion();
  196. // #endif
  197. // getShopConfig().then(res => {
  198. // this.$store.commit('SETPHONESTATUS', res.data.status);
  199. // });
  200. // 获取导航高度;
  201. uni.getSystemInfo({
  202. success: function(res) {
  203. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  204. }
  205. });
  206. // #ifdef MP
  207. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  208. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  209. const version = uni.getSystemInfoSync().SDKVersion
  210. if (Routine.compareVersion(version, '2.21.2') >= 0) {
  211. console.log(version)
  212. that.$Cache.set('MP_VERSION_ISNEW', true)
  213. } else {
  214. that.$Cache.set('MP_VERSION_ISNEW', false)
  215. }
  216. // #endif
  217. // #ifdef H5
  218. // 添加crmeb chat 统计
  219. var __s = document.createElement('script');
  220. __s.src = `${HTTP_REQUEST_URL}/api/get_script`;
  221. document.head.appendChild(__s);
  222. uni.getSystemInfo({
  223. success(e) {
  224. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  225. if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  226. window.location.pathname = '/static/html/pc.html';
  227. }
  228. }
  229. });
  230. if (option.query.hasOwnProperty('type')) {
  231. this.globalData.isIframe = true;
  232. } else {
  233. this.globalData.isIframe = false;
  234. }
  235. console.log(window.location.pathname, this.isWork(), '66666');
  236. if (window.location.pathname !== '/' && !this.isWork()) {
  237. console.log('静茹');
  238. let snsapiBase = 'snsapi_base';
  239. let urlData = location.pathname + location.search;
  240. if (!that.$store.getters.isLogin && uni.getStorageSync('authIng')) {
  241. uni.setStorageSync('authIng', false)
  242. }
  243. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  244. console.log('code');
  245. let code,
  246. state,
  247. scope = ''
  248. if (option.query.code instanceof Array) {
  249. code = option.query.code[option.query.code.length - 1]
  250. } else {
  251. code = option.query.code
  252. }
  253. if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
  254. '/pages/users/wechat_login/index') === -1) {
  255. // 存储静默授权code
  256. uni.setStorageSync('snsapiCode', code);
  257. try {
  258. let res = await silenceAuth({
  259. code: code,
  260. snsapi: 'snsapi_base',
  261. spread_spid: that.$Cache.get('spid')
  262. }).then((re) => {
  263. console.log(re, 're');
  264. }).catch(error => {
  265. uni.hideLoading()
  266. return this.$util.Tips({
  267. title: error
  268. })
  269. });
  270. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
  271. .back_url)));
  272. if (res.data.key !== undefined && res.data.key) {
  273. this.$Cache.set('snsapiKey', res.data.key);
  274. } else {
  275. let time = res.data.expires_time - this.$Cache.time();
  276. this.$store.commit('LOGIN', {
  277. token: res.data.token,
  278. time: time
  279. });
  280. this.$store.commit('SETUID', res.data.userInfo.uid);
  281. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  282. if (option.query.back_url) {
  283. location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url)));
  284. }
  285. }
  286. } catch (e) {
  287. let url = ''
  288. if (option.query.back_url instanceof Array) {
  289. url = option.query.back_url[option.query.back_url.length - 1]
  290. } else {
  291. url = option.query.back_url
  292. }
  293. if (!that.$Cache.has('snsapiKey')) {
  294. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  295. Auth.oAuth('snsapi_userinfo', url);
  296. }
  297. }
  298. }
  299. } else {
  300. if (!this.$Cache.has('snsapiKey')) {
  301. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  302. Auth.oAuth(snsapiBase, urlData);
  303. }
  304. }
  305. }
  306. } else {
  307. if (option.query.back_url) {
  308. location.replace(uni.getStorageSync('snRouter'));
  309. }
  310. }
  311. }
  312. // #endif
  313. // #ifdef MP
  314. // 小程序静默授权
  315. if (!this.$store.getters.isLogin) {
  316. Routine.getCode()
  317. .then(code => {
  318. this.silenceAuth(code);
  319. })
  320. .catch(res => {
  321. uni.hideLoading();
  322. });
  323. }
  324. // #endif
  325. },
  326. onShow(options) {
  327. let that = this;
  328. //直播间分享
  329. // #ifdef MP
  330. // const sceneList = [1007, 1008, 1014, 1044, 1045, 1046, 1047, 1048, 1049, 1073, 1154, 1155];
  331. // if (sceneList.includes(options.scene)) {
  332. // livePlayer.getShareParams()
  333. // .then(res => {
  334. // //记录推广人uid
  335. // if(res.custom_params.pid){
  336. // that.$Cache.set('spid', res.custom_params.pid);
  337. // that.globalData.spid = res.custom_params.pid;
  338. // }
  339. // }).catch(err => {
  340. // })
  341. // }
  342. // #endif
  343. // #ifdef APP
  344. // 升级App
  345. getUpApp();
  346. // #endif
  347. },
  348. mounted() {
  349. // setTimeout((e) => {
  350. // if (this.$store.getters.isLogin) {
  351. // this.getCartNum()
  352. // }
  353. // }, 100)
  354. },
  355. methods: {
  356. // 打开微信链接
  357. openUrlWeixin() {
  358. const appid = 'wxceb554336d5e17f3'
  359. // 判断是否真实路由模式
  360. let ul = encodeURIComponent(window.location.protocol + '//' + window.location.host + window.location
  361. .pathname);
  362. // 打开微信授权页面
  363. let url =
  364. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  365. appid +
  366. '&redirect_uri=' +
  367. ul +
  368. '&response_type=code&scope=snsapi_base&state=' +
  369. new Date().getTime() +
  370. '#wechat_redirect';
  371. window.location.href = url;
  372. },
  373. // 小程序静默授权
  374. silenceAuth(code) {
  375. let that = this;
  376. let spid = that.globalData.spid ? that.globalData.spid : '';
  377. silenceAuth({
  378. code: code,
  379. spread_spid: spid,
  380. spread_code: that.globalData.code
  381. })
  382. .then(res => {
  383. if (res.data.token !== undefined && res.data.token) {
  384. uni.hideLoading();
  385. let time = res.data.expires_time - this.$Cache.time();
  386. that.$store.commit('LOGIN', {
  387. token: res.data.token,
  388. time: time
  389. });
  390. that.$store.commit('SETUID', res.data.userInfo.uid);
  391. that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  392. }
  393. })
  394. .catch(err => {
  395. return that.$util.Tips({
  396. title: err
  397. })
  398. });
  399. },
  400. isWork() {
  401. return navigator.userAgent.toLowerCase().indexOf('wxwork') !== -1 && navigator.userAgent.toLowerCase()
  402. .indexOf("micromessenger") !== -1
  403. },
  404. /**
  405. * 检测当前的小程序
  406. * 是否是最新版本,是否需要下载、更新
  407. */
  408. checkUpdateVersion() {
  409. //判断微信版本是否 兼容小程序更新机制API的使用
  410. if (wx.canIUse('getUpdateManager')) {
  411. const updateManager = wx.getUpdateManager();
  412. //检测版本更新
  413. updateManager.onCheckForUpdate(function(res) {
  414. if (res.hasUpdate) {
  415. updateManager.onUpdateReady(function() {
  416. wx.showModal({
  417. title: '温馨提示',
  418. content: '检测到新版本,是否重启小程序?',
  419. showCancel: false,
  420. success: function(res) {
  421. if (res.confirm) {
  422. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  423. updateManager.applyUpdate()
  424. }
  425. }
  426. })
  427. })
  428. updateManager.onUpdateFailed(function() {
  429. // 新版本下载失败
  430. wx.showModal({
  431. title: '已有新版本',
  432. content: '请您删除小程序,重新搜索进入',
  433. })
  434. })
  435. }
  436. })
  437. } else {
  438. wx.showModal({
  439. title: '溫馨提示',
  440. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  441. })
  442. }
  443. }
  444. },
  445. onHide: function() {
  446. }
  447. };
  448. </script>
  449. <style lang="scss">
  450. /* #ifndef APP-PLUS-NVUE || APP-NVUE */
  451. @import url('@/plugin/emoji-awesome/css/tuoluojiang.css');
  452. @import url('@/plugin/animate/animate.min.css');
  453. @import 'static/css/base.css';
  454. @import 'static/iconfont/iconfont.css';
  455. @import 'static/css/guildford.css';
  456. @import 'static/css/style.scss';
  457. view {
  458. box-sizing: border-box;
  459. }
  460. page {
  461. font-family: PingFang SC;
  462. }
  463. .activityFrame {
  464. background-size: 100% 100%;
  465. background-repeat: no-repeat;
  466. position: absolute;
  467. top: 0;
  468. left: 0;
  469. width: 100%;
  470. height: 100%;
  471. z-index: 1;
  472. }
  473. .placeholder {
  474. color: #ccc;
  475. }
  476. .bg-color-red {
  477. background-color: var(--view-theme) !important;
  478. }
  479. .syspadding {
  480. padding-top: var(--status-bar-height);
  481. }
  482. .flex {
  483. display: flex;
  484. }
  485. .uni-scroll-view::-webkit-scrollbar {
  486. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  487. display: none;
  488. }
  489. ::-webkit-scrollbar {
  490. width: 0;
  491. height: 0;
  492. color: transparent;
  493. }
  494. .uni-system-open-location .map-content.fix-position {
  495. height: 100vh;
  496. top: 0;
  497. bottom: 0;
  498. }
  499. /* #endif */
  500. </style>