App.vue 13 KB

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