App.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. uni.setStorageSync('is_diy', res.data.is_diy)
  80. uni.$emit('is_diy', res.data.is_diy)
  81. switch (res.data.status) {
  82. case 1:
  83. uni.setStorageSync('viewColor', blue)
  84. uni.$emit('ok', blue, res.data.status)
  85. break;
  86. case 2:
  87. uni.setStorageSync('viewColor', green)
  88. uni.$emit('ok', green, res.data.status)
  89. break;
  90. case 3:
  91. uni.setStorageSync('viewColor', red)
  92. uni.$emit('ok', red, res.data.status)
  93. break;
  94. case 4:
  95. uni.setStorageSync('viewColor', pink)
  96. uni.$emit('ok', pink, res.data.status)
  97. break;
  98. case 5:
  99. uni.setStorageSync('viewColor', orange)
  100. uni.$emit('ok', orange, res.data.status)
  101. break;
  102. default:
  103. uni.setStorageSync('viewColor', red)
  104. uni.$emit('ok', red, res.data.status)
  105. break
  106. }
  107. });
  108. if (option.query.spread) {
  109. that.$Cache.set('spread', option.query.spread);
  110. that.globalData.spid = option.query.spread;
  111. that.globalData.pid = option.query.spread;
  112. silenceBindingSpread()
  113. }
  114. if (option.query.spid) {
  115. that.$Cache.set('spread', option.query.spid);
  116. that.globalData.spid = option.query.spid;
  117. that.globalData.pid = option.query.spid;
  118. // silenceBindingSpread()
  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.pid === undefined ? val : val.pid;
  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. const updateManager = wx.getUpdateManager();
  164. updateManager.onCheckForUpdate(function(res) {
  165. // 请求完新版本信息的回调
  166. });
  167. updateManager.onUpdateReady(function() {
  168. wx.showModal({
  169. title: '更新提示',
  170. content: '新版本已经准备好,是否重启应用?',
  171. success: function(res) {
  172. if (res.confirm) {
  173. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  174. updateManager.applyUpdate();
  175. }
  176. }
  177. });
  178. });
  179. updateManager.onUpdateFailed(function() {
  180. return that.Tips({
  181. title: '新版本下载失败'
  182. });
  183. });
  184. // #endif
  185. // getShopConfig().then(res => {
  186. // this.$store.commit('SETPHONESTATUS', res.data.status);
  187. // });
  188. // 获取导航高度;
  189. uni.getSystemInfo({
  190. success: function(res) {
  191. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  192. }
  193. });
  194. // #ifdef MP
  195. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  196. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  197. // #endif
  198. // #ifdef H5
  199. uni.getSystemInfo({
  200. success(e) {
  201. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  202. if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  203. window.location.pathname = '/static/html/pc.html';
  204. }
  205. }
  206. });
  207. if (option.query.hasOwnProperty('type') && option.query.type == "iframeWindow") {
  208. this.globalData.isIframe = true;
  209. } else {
  210. this.globalData.isIframe = false;
  211. }
  212. //公众号静默授权
  213. if (window.location.pathname !== '/' && option.query.scope === 'snsapi_base') {
  214. let snsapiBase = 'snsapi_base';
  215. let urlData = location.pathname + location.search;
  216. // if (!that.$store.getters.isLogin && uni.getStorageSync('authIng')) {
  217. // uni.setStorageSync('authIng', false)
  218. // }
  219. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  220. let code,
  221. state,
  222. scope = ''
  223. if (option.query.code instanceof Array) {
  224. code = option.query.code[option.query.code.length - 1]
  225. } else {
  226. code = option.query.code
  227. }
  228. if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
  229. '/pages/users/wechat_login/index') === -1) {
  230. // 存储静默授权code
  231. uni.setStorageSync('snsapiCode', code);
  232. let spread = that.globalData.spid ? that.globalData.spid : '';
  233. uni.setStorageSync('authIng', true)
  234. silenceAuth({
  235. code: code,
  236. spread: that.$Cache.get('spread'),
  237. spid: that.globalData.code
  238. })
  239. .then(res => {
  240. uni.setStorageSync('authIng', false)
  241. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
  242. .back_url)));
  243. if (res.data.key !== undefined && res.data.key) {
  244. this.$Cache.set('snsapiKey', res.data.key);
  245. } else {
  246. let time = res.data.expires_time - this.$Cache.time();
  247. this.$store.commit('LOGIN', {
  248. token: res.data.token,
  249. time: time
  250. });
  251. this.$Cache.set('WX_AUTH', code);
  252. this.$store.commit('SETUID', res.data.userInfo.uid);
  253. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  254. if (option.query.back_url) {
  255. location.replace(decodeURIComponent(decodeURIComponent(option.query
  256. .back_url)));
  257. }
  258. }
  259. })
  260. .catch(error => {
  261. uni.setStorageSync('authIng', false)
  262. let url = ''
  263. if (option.query.back_url instanceof Array) {
  264. url = option.query.back_url[option.query.back_url.length - 1]
  265. } else {
  266. url = option.query.back_url
  267. }
  268. if (!that.$Cache.has('snsapiKey')) {
  269. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  270. Auth.oAuth(snsapiBase, url);
  271. }
  272. }
  273. });
  274. } else {
  275. if (!this.$Cache.has('snsapiKey')) {
  276. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  277. Auth.oAuth(snsapiBase, urlData);
  278. }
  279. }
  280. }
  281. } else {
  282. if (option.query.back_url) {
  283. location.replace(uni.getStorageSync('snRouter'));
  284. }
  285. }
  286. }
  287. // #endif
  288. // #ifdef MP
  289. // 小程序静默授权
  290. if (!this.$store.getters.isLogin) {
  291. Routine.getCode()
  292. .then(code => {
  293. this.silenceAuth(code);
  294. })
  295. .catch(res => {
  296. uni.hideLoading();
  297. });
  298. }
  299. // #endif
  300. // #ifdef H5
  301. // 添加crmeb chat 统计
  302. var __s = document.createElement('script');
  303. __s.src = `${HTTP_REQUEST_URL}/api/get_script`;
  304. document.head.appendChild(__s);
  305. // #endif
  306. },
  307. methods: {
  308. // 小程序静默授权
  309. silenceAuth(code) {
  310. let that = this;
  311. let spread = that.globalData.spid ? that.globalData.spid : '';
  312. silenceAuth({
  313. code: code,
  314. spread_spid: spread,
  315. spread_code: that.globalData.code
  316. })
  317. .then(res => {
  318. if (res.data.token !== undefined && res.data.token) {
  319. uni.hideLoading();
  320. let time = res.data.expires_time - this.$Cache.time();
  321. that.$store.commit('LOGIN', {
  322. token: res.data.token,
  323. time: time
  324. });
  325. that.$store.commit('SETUID', res.data.userInfo.uid);
  326. that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  327. }
  328. })
  329. .catch(res => {});
  330. },
  331. },
  332. onHide: function() {
  333. }
  334. };
  335. </script>
  336. <style>
  337. @import url('@/plugin/emoji-awesome/css/google.min.css');
  338. @import url('@/plugin/animate/animate.min.css');
  339. @import 'static/css/base.css';
  340. @import 'static/iconfont/iconfont.css';
  341. @import 'static/css/guildford.css';
  342. @import 'static/css/style.scss';
  343. view {
  344. box-sizing: border-box;
  345. }
  346. page {
  347. font-family: PingFang SC;
  348. }
  349. .bg-color-red {
  350. background-color: var(--view-theme) !important;
  351. }
  352. .syspadding {
  353. padding-top: var(--status-bar-height);
  354. }
  355. .flex {
  356. display: flex;
  357. }
  358. .uni-scroll-view::-webkit-scrollbar {
  359. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  360. display: none;
  361. }
  362. ::-webkit-scrollbar {
  363. width: 0;
  364. height: 0;
  365. color: transparent;
  366. }
  367. .uni-system-open-location .map-content.fix-position {
  368. height: 100vh;
  369. top: 0;
  370. bottom: 0;
  371. }
  372. .open-location {
  373. width: 100%;
  374. height: 100vh;
  375. }
  376. </style>