App.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <script>
  2. import { checkLogin } from './libs/login';
  3. import { HTTP_REQUEST_URL, SYSTEM_VERSION } from './config/app';
  4. import { getShopConfig, silenceAuth, getSystemVersion, basicConfig, remoteRegister } from '@/api/public';
  5. import Auth from '@/libs/wechat.js';
  6. import Routine from './libs/routine.js';
  7. import { silenceBindingSpread } from '@/utils';
  8. import { colorChange, getCrmebCopyRight } from '@/api/api.js';
  9. import { getLangJson, getLangVersion } from '@/api/user.js';
  10. import { mapGetters } from 'vuex';
  11. import colors from '@/mixins/color.js';
  12. import Cache from '@/utils/cache';
  13. import themeList from '@/utils/theme';
  14. import { debug } from 'util';
  15. export default {
  16. globalData: {
  17. spid: 0,
  18. code: 0,
  19. isLogin: false,
  20. userInfo: {},
  21. MyMenus: [],
  22. globalData: false,
  23. isIframe: false,
  24. tabbarShow: true,
  25. windowHeight: 0,
  26. locale: ''
  27. },
  28. mixins: [colors],
  29. computed: mapGetters(['isLogin', 'cartNum']),
  30. watch: {
  31. isLogin: {
  32. deep: true, //深度监听设置为 true
  33. handler: function (newV, oldV) {
  34. if (newV) {
  35. // this.getCartNum()
  36. } else {
  37. this.$store.commit('indexData/setCartNum', '');
  38. }
  39. }
  40. },
  41. cartNum(newCart, b) {
  42. this.$store.commit('indexData/setCartNum', newCart + '');
  43. if (newCart > 0) {
  44. uni.setTabBarBadge({
  45. index: Number(uni.getStorageSync('FOOTER_ADDCART')) || 2,
  46. text: newCart + ''
  47. });
  48. } else {
  49. uni.hideTabBarRedDot({
  50. index: Number(uni.getStorageSync('FOOTER_ADDCART')) || 2
  51. });
  52. }
  53. }
  54. },
  55. onShow() {
  56. const queryData = uni.getEnterOptionsSync(); // uni-app版本 3.5.1+ 支持
  57. if (queryData.query.spread) {
  58. this.$Cache.set('spread', queryData.query.spread);
  59. this.globalData.spid = queryData.query.spread;
  60. this.globalData.pid = queryData.query.spread;
  61. silenceBindingSpread(this.globalData);
  62. }
  63. if (queryData.query.spid) {
  64. this.$Cache.set('spread', queryData.query.spid);
  65. this.globalData.spid = queryData.query.spid;
  66. this.globalData.pid = queryData.query.spid;
  67. silenceBindingSpread(this.globalData);
  68. }
  69. // #ifdef MP
  70. if (queryData.query.scene) {
  71. let param = this.$util.getUrlParams(decodeURIComponent(queryData.query.scene));
  72. if (param.pid) {
  73. this.$Cache.set('spread', param.pid);
  74. this.globalData.spid = param.pid;
  75. this.globalData.pid = param.pid;
  76. } else {
  77. switch (queryData.scene) {
  78. //扫描小程序码
  79. case 1047:
  80. this.globalData.code = queryData.query.scene;
  81. break;
  82. //长按图片识别小程序码
  83. case 1048:
  84. this.globalData.code = queryData.query.scene;
  85. break;
  86. //手机相册选取小程序码
  87. case 1049:
  88. this.globalData.code = queryData.query.scene;
  89. break;
  90. //直接进入小程序
  91. case 1001:
  92. this.globalData.spid = queryData.query.scene;
  93. break;
  94. }
  95. }
  96. silenceBindingSpread(this.globalData);
  97. }
  98. // #endif
  99. },
  100. async onLaunch(option) {
  101. uni.hideTabBar();
  102. let that = this;
  103. basicConfig().then((res) => {
  104. uni.setStorageSync('BASIC_CONFIG', res.data);
  105. });
  106. // #ifdef H5
  107. if (option.query.hasOwnProperty('mdType') && option.query.mdType == 'iframeWindow') {
  108. this.globalData.isIframe = true;
  109. } else {
  110. this.globalData.isIframe = false;
  111. }
  112. if (!this.isLogin && option.query.hasOwnProperty('remote_token')) {
  113. this.remoteRegister(option.query.remote_token);
  114. }
  115. // #endif
  116. colorChange('color_change').then((res) => {
  117. uni.setStorageSync('is_diy', res.data.is_diy);
  118. uni.$emit('is_diy', res.data.is_diy);
  119. uni.setStorageSync('color_status', res.data.status);
  120. switch (res.data.status) {
  121. case 1:
  122. uni.setStorageSync('viewColor', themeList.blue);
  123. uni.$emit('ok', themeList.blue, res.data.status);
  124. break;
  125. case 2:
  126. uni.setStorageSync('viewColor', themeList.green);
  127. uni.$emit('ok', themeList.green, res.data.status);
  128. break;
  129. case 3:
  130. uni.setStorageSync('viewColor', themeList.red);
  131. uni.$emit('ok', themeList.red, res.data.status);
  132. break;
  133. case 4:
  134. uni.setStorageSync('viewColor', themeList.pink);
  135. uni.$emit('ok', themeList.pink, res.data.status);
  136. break;
  137. case 5:
  138. uni.setStorageSync('viewColor', themeList.orange);
  139. uni.$emit('ok', themeList.orange, res.data.status);
  140. break;
  141. default:
  142. uni.setStorageSync('viewColor', themeList.red);
  143. uni.$emit('ok', themeList.red, res.data.status);
  144. break;
  145. }
  146. });
  147. getLangVersion().then((res) => {
  148. let version = res.data.version;
  149. if (version != uni.getStorageSync('LANG_VERSION')) {
  150. getLangJson().then((res) => {
  151. let value = Object.keys(res.data)[0];
  152. Cache.set('locale', Object.keys(res.data)[0]);
  153. this.$i18n.setLocaleMessage(value, res.data[value]);
  154. uni.setStorageSync('localeJson', res.data);
  155. });
  156. }
  157. uni.setStorageSync('LANG_VERSION', version);
  158. });
  159. // #ifdef APP-PLUS || H5
  160. uni.getSystemInfo({
  161. success: function (res) {
  162. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  163. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  164. let height = res.windowHeight - res.statusBarHeight - 44;
  165. // #ifdef H5 || APP-PLUS
  166. that.globalData.windowHeight = res.windowHeight + 'px';
  167. // #endif
  168. // // #ifdef APP-PLUS
  169. // that.globalData.windowHeight = height + 'px'
  170. // // #endif
  171. }
  172. });
  173. // #endif
  174. // #ifdef MP
  175. if (HTTP_REQUEST_URL == '') {
  176. console.error(
  177. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  178. );
  179. return false;
  180. }
  181. const updateManager = wx.getUpdateManager();
  182. const startParamObj = wx.getEnterOptionsSync();
  183. if (wx.canIUse('getUpdateManager') && startParamObj.scene != 1154) {
  184. const updateManager = wx.getUpdateManager();
  185. updateManager.onCheckForUpdate(function (res) {
  186. // 请求完新版本信息的回调
  187. // console.log(res.hasUpdate)
  188. if (res.hasUpdate) {
  189. updateManager.onUpdateFailed(function () {
  190. return that.Tips({
  191. title: '新版本下载失败'
  192. });
  193. });
  194. updateManager.onUpdateReady(function () {
  195. wx.showModal({
  196. title: '更新提示',
  197. content: '新版本已经下载好,是否重启当前应用?',
  198. success(res) {
  199. if (res.confirm) {
  200. updateManager.applyUpdate();
  201. }
  202. }
  203. });
  204. });
  205. updateManager.onUpdateFailed(function () {
  206. wx.showModal({
  207. title: '发现新版本',
  208. content: '请删除当前小程序,重启搜索打开...'
  209. });
  210. });
  211. }
  212. });
  213. }
  214. // #endif
  215. // getShopConfig().then(res => {
  216. // this.$store.commit('SETPHONESTATUS', res.data.status);
  217. // });
  218. // 获取导航高度;
  219. uni.getSystemInfo({
  220. success: function (res) {
  221. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  222. }
  223. });
  224. // #ifdef MP
  225. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  226. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  227. const version = uni.getSystemInfoSync().SDKVersion;
  228. if (Routine.compareVersion(version, '2.21.3') >= 0) {
  229. that.$Cache.set('MP_VERSION_ISNEW', true);
  230. } else {
  231. that.$Cache.set('MP_VERSION_ISNEW', false);
  232. }
  233. // #endif
  234. // #ifdef MP
  235. // 小程序静默授权
  236. // if (!this.$store.getters.isLogin) {
  237. // Routine.getCode()
  238. // .then(code => {
  239. // this.silenceAuth(code);
  240. // })
  241. // .catch(res => {
  242. // uni.hideLoading();
  243. // });
  244. // }
  245. // #endif
  246. // #ifdef H5
  247. // 添加crmeb chat 统计
  248. var __s = document.createElement('script');
  249. __s.src = `${HTTP_REQUEST_URL}/api/get_script`;
  250. document.head.appendChild(__s);
  251. // #endif
  252. getCrmebCopyRight().then((res) => {
  253. uni.setStorageSync('copyRight', res.data);
  254. });
  255. // #ifdef MP
  256. getSystemVersion().then((res) => {
  257. if (res.data.version_code != SYSTEM_VERSION) {
  258. uni.showModal({
  259. title: '警告',
  260. content: '前后端版本不一致!',
  261. success: function (res) {
  262. if (res.confirm) {
  263. }
  264. }
  265. });
  266. }
  267. });
  268. // #endif
  269. },
  270. // #ifdef H5
  271. onHide() {
  272. this.$Cache.clear('snsapiKey');
  273. },
  274. // #endif
  275. methods: {
  276. remoteRegister(remote_token) {
  277. remoteRegister({ remote_token }).then((res) => {
  278. let data = res.data;
  279. if (data.get_remote_login_url) {
  280. location.href = data.get_remote_login_url
  281. } else {
  282. this.$store.commit('LOGIN', {
  283. token: data.token,
  284. time: data.expires_time - this.$Cache.time()
  285. });
  286. this.$store.commit('SETUID', data.userInfo.uid);
  287. location.reload();
  288. }
  289. });
  290. }
  291. // 小程序静默授权
  292. // silenceAuth(code) {
  293. // let that = this;
  294. // let spread = that.globalData.spid ? that.globalData.spid : '';
  295. // silenceAuth({
  296. // code: code,
  297. // spread_spid: spread,
  298. // spread_code: that.globalData.code
  299. // })
  300. // .then(res => {
  301. // if (res.data.token !== undefined && res.data.token) {
  302. // uni.hideLoading();
  303. // let time = res.data.expires_time - this.$Cache.time();
  304. // that.$store.commit('LOGIN', {
  305. // token: res.data.token,
  306. // time: time
  307. // });
  308. // that.$store.commit('SETUID', res.data.userInfo.uid);
  309. // that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  310. // }
  311. // })
  312. // .catch(res => {});
  313. // },
  314. }
  315. };
  316. </script>
  317. <style>
  318. @import url('@/plugin/emoji-awesome/css/tuoluojiang.css');
  319. @import url('@/plugin/animate/animate.min.css');
  320. @import 'static/css/base.css';
  321. @import 'static/iconfont/iconfont.css';
  322. @import 'static/css/guildford.css';
  323. @import 'static/css/style.scss';
  324. view {
  325. box-sizing: border-box;
  326. }
  327. page {
  328. font-family: PingFang SC;
  329. }
  330. .bg-color-red {
  331. background-color: var(--view-theme) !important;
  332. }
  333. .syspadding {
  334. padding-top: var(--status-bar-height);
  335. }
  336. .flex {
  337. display: flex;
  338. }
  339. .uni-scroll-view::-webkit-scrollbar {
  340. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  341. display: none;
  342. }
  343. ::-webkit-scrollbar {
  344. width: 0;
  345. height: 0;
  346. color: transparent;
  347. }
  348. .uni-system-open-location .map-content.fix-position {
  349. height: 100vh;
  350. top: 0;
  351. bottom: 0;
  352. }
  353. .open-location {
  354. width: 100%;
  355. height: 100vh;
  356. }
  357. </style>