App.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. silenceBindingSpread
  16. } from "@/utils";
  17. import {
  18. getCartCounts,
  19. } from '@/api/order.js';
  20. import {
  21. colorChange
  22. } from '@/api/api.js';
  23. import {
  24. mapGetters
  25. } from "vuex"
  26. import colors from '@/mixins/color.js';
  27. let green =
  28. '--view-theme: rgba(66,202,77,1);--view-theme-16: #42CA4D;--view-priceColor:#FF7600;--view-minorColor:rgba(108, 198, 94, 0.5);--view-minorColorT:rgba(66, 202, 77, 0.1);--view-bntColor:#FE960F;--view-op-ten: rgba(66,202,77, 0.1);--view-main-start:#70E038; --view-main-over:#42CA4D;--view-op-point-four: rgba(66,202,77, 0.04);'
  29. let red =
  30. '--view-theme: rgba(233,51,35,1);--view-theme-16: #e93323;--view-priceColor:#e93323;--view-minorColor:rgba(233, 51, 35, 0.5);--view-minorColorT:rgba(233, 51, 35, 0.1);--view-bntColor:#FE960F;--view-op-ten: rgba(233,51,35, 0.1);--view-main-start:#FF6151; --view-main-over:#e93323;--view-op-point-four: rgba(233,51,35, 0.04);'
  31. let blue =
  32. '--view-theme: rgba(29,176,252,1);--view-theme-16:#1db0fc;--view-priceColor:#FD502F;--view-minorColor:rgba(58, 139, 236, 0.5);--view-minorColorT:rgba(9, 139, 243, 0.1);--view-bntColor:#22CAFD;--view-op-ten: rgba(29,176,252, 0.1);--view-main-start:#40D1F4; --view-main-over:#1DB0FC;--view-op-point-four: rgba(29,176,252, 0.04);'
  33. let pink =
  34. '--view-theme: rgba(255,68,143,1);--view-theme-16:#ff448f;--view-priceColor:#FF448F;--view-minorColor:rgba(255, 68, 143, 0.5);--view-minorColorT:rgba(255, 68, 143, 0.1);--view-bntColor:#282828;--view-op-ten: rgba(255,68,143, 0.1);--view-main-start:#FF67AD; --view-main-over:#FF448F;--view-op-point-four: rgba(255,68,143, 0.04);'
  35. let orange =
  36. '--view-theme: rgba(254,92,45,1); --view-theme-16:#FE5C2D;--view-priceColor:#FE5C2D;--view-minorColor:rgba(254, 92, 45, 0.5);--view-minorColorT:rgba(254, 92, 45, 0.1);--view-bntColor:#FDB000;--view-op-ten: rgba(254,92,45, 0.1);--view-main-start:#FF9445; --view-main-over:#FE5C2D;--view-op-point-four: rgba(254,92,45, 0.04);'
  37. export default {
  38. globalData: {
  39. spid: 0,
  40. code: 0,
  41. isLogin: false,
  42. userInfo: {},
  43. MyMenus: [],
  44. globalData: false,
  45. isIframe: false,
  46. tabbarShow: true,
  47. windowHeight: 0
  48. },
  49. mixins: [colors],
  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: Number(uni.getStorageSync('FOOTER_ADDCART')) || 2,
  67. text: newCart + ''
  68. })
  69. } else {
  70. uni.hideTabBarRedDot({
  71. index: Number(uni.getStorageSync('FOOTER_ADDCART')) || 2
  72. })
  73. }
  74. }
  75. },
  76. async onLaunch(option) {
  77. let that = this;
  78. colorChange('color_change').then(res => {
  79. switch (res.data.status) {
  80. case 1:
  81. uni.setStorageSync('viewColor', blue)
  82. uni.$emit('ok', blue)
  83. break;
  84. case 2:
  85. uni.setStorageSync('viewColor', green)
  86. uni.$emit('ok', green)
  87. break;
  88. case 3:
  89. uni.setStorageSync('viewColor', red)
  90. uni.$emit('ok', red)
  91. break;
  92. case 4:
  93. uni.setStorageSync('viewColor', pink)
  94. uni.$emit('ok', pink)
  95. break;
  96. case 5:
  97. uni.setStorageSync('viewColor', orange)
  98. uni.$emit('ok', orange)
  99. break;
  100. default:
  101. uni.setStorageSync('viewColor', red)
  102. uni.$emit('ok', red)
  103. break
  104. }
  105. });
  106. if (option.query.spread) {
  107. that.$Cache.set('spread', option.query.spread);
  108. that.globalData.spid = option.query.spread;
  109. that.globalData.pid = option.query.spread;
  110. silenceBindingSpread()
  111. }
  112. // #ifdef APP-PLUS || H5
  113. uni.getSystemInfo({
  114. success: function(res) {
  115. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  116. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  117. let height = res.windowHeight - res.statusBarHeight - 44
  118. // #ifdef H5 || APP-PLUS
  119. that.globalData.windowHeight = res.windowHeight + 'px'
  120. // #endif
  121. // // #ifdef APP-PLUS
  122. // that.globalData.windowHeight = height + 'px'
  123. // // #endif
  124. }
  125. });
  126. // #endif
  127. // #ifdef MP
  128. if (HTTP_REQUEST_URL == '') {
  129. console.error(
  130. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  131. );
  132. return false;
  133. }
  134. if (option.query.hasOwnProperty('scene')) {
  135. switch (option.scene) {
  136. //扫描小程序码
  137. case 1047:
  138. let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
  139. that.globalData.code = val.pid === undefined ? val : val.pid;
  140. break;
  141. //长按图片识别小程序码
  142. case 1048:
  143. that.globalData.code = option.query.scene;
  144. break;
  145. //手机相册选取小程序码
  146. case 1049:
  147. that.globalData.code = option.query.scene;
  148. break;
  149. //直接进入小程序
  150. case 1001:
  151. that.globalData.spid = option.query.scene;
  152. break;
  153. }
  154. }
  155. const updateManager = wx.getUpdateManager();
  156. updateManager.onCheckForUpdate(function(res) {
  157. // 请求完新版本信息的回调
  158. });
  159. updateManager.onUpdateReady(function() {
  160. wx.showModal({
  161. title: '更新提示',
  162. content: '新版本已经准备好,是否重启应用?',
  163. success: function(res) {
  164. if (res.confirm) {
  165. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  166. updateManager.applyUpdate();
  167. }
  168. }
  169. });
  170. });
  171. updateManager.onUpdateFailed(function() {
  172. return that.Tips({
  173. title: '新版本下载失败'
  174. });
  175. });
  176. // #endif
  177. // getShopConfig().then(res => {
  178. // this.$store.commit('SETPHONESTATUS', res.data.status);
  179. // });
  180. // 获取导航高度;
  181. uni.getSystemInfo({
  182. success: function(res) {
  183. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  184. }
  185. });
  186. // #ifdef MP
  187. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  188. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  189. // #endif
  190. // #ifdef H5
  191. uni.getSystemInfo({
  192. success(e) {
  193. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  194. if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  195. window.location.pathname = '/static/html/pc.html';
  196. }
  197. }
  198. });
  199. if (option.query.hasOwnProperty('type') && option.query.type == "iframeWindow") {
  200. this.globalData.isIframe = true;
  201. } else {
  202. this.globalData.isIframe = false;
  203. }
  204. //公众号静默授权
  205. if (window.location.pathname !== '/' && option.query.scope === 'snsapi_base') {
  206. let snsapiBase = 'snsapi_base';
  207. let urlData = location.pathname + location.search;
  208. // if (!that.$store.getters.isLogin && uni.getStorageSync('authIng')) {
  209. // uni.setStorageSync('authIng', false)
  210. // }
  211. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  212. let code,
  213. state,
  214. scope = ''
  215. if (option.query.code instanceof Array) {
  216. code = option.query.code[option.query.code.length - 1]
  217. } else {
  218. code = option.query.code
  219. }
  220. if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
  221. '/pages/users/wechat_login/index') === -1) {
  222. // 存储静默授权code
  223. uni.setStorageSync('snsapiCode', code);
  224. let spread = that.globalData.spid ? that.globalData.spid : '';
  225. uni.setStorageSync('authIng', true)
  226. silenceAuth({
  227. code: code,
  228. spread: that.$Cache.get('spread'),
  229. spid: that.globalData.code
  230. })
  231. .then(res => {
  232. uni.setStorageSync('authIng', false)
  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.$Cache.set('WX_AUTH', code);
  244. this.$store.commit('SETUID', res.data.userInfo.uid);
  245. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  246. if (option.query.back_url) {
  247. location.replace(decodeURIComponent(decodeURIComponent(option.query
  248. .back_url)));
  249. }
  250. }
  251. })
  252. .catch(error => {
  253. uni.setStorageSync('authIng', false)
  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(snsapiBase, 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 H5
  293. // 添加crmeb chat 统计
  294. var __s = document.createElement('script');
  295. __s.src = `${HTTP_REQUEST_URL}/api/get_script`;
  296. document.head.appendChild(__s);
  297. // #endif
  298. },
  299. mounted() {},
  300. methods: {
  301. // 小程序静默授权
  302. silenceAuth(code) {
  303. let that = this;
  304. let spread = that.globalData.spid ? that.globalData.spid : '';
  305. silenceAuth({
  306. code: code,
  307. spread_spid: spread,
  308. spread_code: that.globalData.code
  309. })
  310. .then(res => {
  311. if (res.data.token !== undefined && res.data.token) {
  312. uni.hideLoading();
  313. let time = res.data.expires_time - this.$Cache.time();
  314. that.$store.commit('LOGIN', {
  315. token: res.data.token,
  316. time: time
  317. });
  318. that.$store.commit('SETUID', res.data.userInfo.uid);
  319. that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  320. }
  321. })
  322. .catch(res => {});
  323. },
  324. },
  325. onHide: function() {
  326. }
  327. };
  328. </script>
  329. <style>
  330. @import url('@/plugin/emoji-awesome/css/google.min.css');
  331. @import url('@/plugin/animate/animate.min.css');
  332. @import 'static/css/base.css';
  333. @import 'static/iconfont/iconfont.css';
  334. @import 'static/css/guildford.css';
  335. @import 'static/css/style.scss';
  336. view {
  337. box-sizing: border-box;
  338. }
  339. page {
  340. font-family: PingFang SC;
  341. }
  342. .bg-color-red {
  343. background-color: var(--view-theme) !important;
  344. }
  345. .syspadding {
  346. padding-top: var(--status-bar-height);
  347. }
  348. .flex {
  349. display: flex;
  350. }
  351. .uni-scroll-view::-webkit-scrollbar {
  352. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  353. display: none;
  354. }
  355. ::-webkit-scrollbar {
  356. width: 0;
  357. height: 0;
  358. color: transparent;
  359. }
  360. .uni-system-open-location .map-content.fix-position {
  361. height: 100vh;
  362. top: 0;
  363. bottom: 0;
  364. }
  365. </style>