App.vue 13 KB

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