You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
9.4 KiB

3 months ago
  1. <script>
  2. import {
  3. checkLogin
  4. } from "./libs/login";
  5. import {
  6. HTTP_REQUEST_URL
  7. } from './config/app';
  8. import Auth from './libs/wechat.js';
  9. import Routine from './libs/routine.js';
  10. import Apps from './libs/apps.js';
  11. import Cache from './utils/cache.js';
  12. import {
  13. getTheme,
  14. loginConfigApi,
  15. tokenIsExistApi
  16. } from './api/api.js';
  17. import {
  18. mapActions
  19. } from 'vuex'
  20. import {
  21. spread
  22. } from "@/api/user";
  23. var statusBarHeight = uni.getSystemInfoSync().statusBarHeight; //手机端头部手机时间位置高度
  24. // const app = getApp();
  25. export default {
  26. globalData: {
  27. statusBarHeight: statusBarHeight, //手机端头部手机时间位置高度
  28. spread: 0, //推广人id
  29. code: 0,
  30. isLogin: false,
  31. userInfo: {},
  32. MyMenus: [],
  33. windowHeight: 0,
  34. navHeight: 0,
  35. navH: 0,
  36. id: 0, //商品id
  37. isIframe: false,
  38. theme: 'theme1', //主题色
  39. publicLoginType: Cache.get('publicLoginType') ? Cache.get('publicLoginType') : '', //公众号登录方式(单选),1微信授权,2手机号登录
  40. routinePhoneVerification: '', //小程序手机号校验类型(多选)1微信小程序验证 2短信验证
  41. companyName: uni.getStorageSync('companyName') ? uni.getStorageSync('companyName') : '欢迎你', //公司名称
  42. tokenIsExist: false, //登录是否失效 false 失效,true没失效
  43. mobileLoginLogo: uni.getStorageSync('mobileLoginLogo') || `${Cache.get("imgHost")}crmebimage/perset/staticImg/logo2.png` //登录页logo
  44. },
  45. onLaunch: function(option) {
  46. //获取登录配置
  47. this.getLoginConfig();
  48. //校验token是否有效,true为有效,false为无效
  49. tokenIsExistApi().then(res => {
  50. this.globalData.tokenIsExist = res.data;
  51. })
  52. let that = this;
  53. // #ifdef APP-PLUS || H5
  54. uni.getSystemInfo({
  55. success: function(res) {
  56. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  57. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  58. let height = res.windowHeight - res.statusBarHeight - 44
  59. // #ifdef H5 || APP-PLUS
  60. that.globalData.windowHeight = res.windowHeight + 'px'
  61. // #endif
  62. }
  63. });
  64. // #endif
  65. // #ifdef MP
  66. const updateManager = uni.getUpdateManager();
  67. updateManager.onCheckForUpdate(function(res) {
  68. // 请求完新版本信息的回调
  69. if (res.hasUpdate) {
  70. updateManager.onUpdateReady(function(res2) {
  71. uni.showModal({
  72. title: '更新提示',
  73. content: '发现新版本,是否重启应用?',
  74. cancelColor: '#eeeeee',
  75. confirmColor: '#FF0000',
  76. success(res2) {
  77. if (res2.confirm) {
  78. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  79. updateManager.applyUpdate();
  80. }
  81. }
  82. });
  83. });
  84. }
  85. });
  86. updateManager.onUpdateFailed(function(res) {
  87. // 新的版本下载失败
  88. uni.showModal({
  89. title: '提示',
  90. content: '检查到有新版本,但下载失败,请检查网络设置',
  91. success(res) {
  92. if (res.confirm) {
  93. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  94. updateManager.applyUpdate();
  95. }
  96. }
  97. });
  98. });
  99. if (HTTP_REQUEST_URL == '') {
  100. console.error(
  101. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  102. );
  103. return false;
  104. }
  105. //小程序扫码进入场景
  106. if (option.query.hasOwnProperty('scene')) {
  107. switch (option.scene) {
  108. case 1047: //扫描小程序码
  109. case 1048: //长按图片识别小程序码
  110. case 1049: //手机相册选取小程序码
  111. case 1001: //直接进入小程序
  112. let value = this.$util.getUrlParams(decodeURIComponent(option.query.scene));
  113. let mapeMpQrCodeValue = this.$util.formatMpQrCodeData(value);
  114. // that.globalData = mapeMpQrCodeValue;
  115. that.globalData = Object.assign(that.globalData,mapeMpQrCodeValue);
  116. that.globalData.spread = mapeMpQrCodeValue.spread ? mapeMpQrCodeValue.spread : '';
  117. break;
  118. }
  119. }
  120. if (option.spread) that.globalData.spread = option.spread;
  121. // #endif
  122. // 获取导航高度;
  123. uni.getSystemInfo({
  124. success: function(res) {
  125. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  126. }
  127. });
  128. // #ifdef MP
  129. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  130. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  131. // #endif
  132. // #ifdef H5
  133. if (option.query.hasOwnProperty('type') && option.query.type == "iframeVisualizing") {
  134. this.globalData.isIframe = true;
  135. } else {
  136. this.globalData.isIframe = false;
  137. }
  138. // wx公众号自动授权登录
  139. let snsapiBase = 'snsapi_base';
  140. let urlData = location.pathname + location.search;
  141. //publicLoginType,公众号登录方式(单选),1微信授权,2手机号登录
  142. if (!that.$store.getters.isLogin && Auth.isWeixin() && this.globalData.publicLoginType == 1 && !that
  143. .globalData.tokenIsExist) {
  144. const {
  145. code,
  146. state,
  147. scope
  148. } = option.query;
  149. if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
  150. '/pages/users/wechat_login/index') === -1) {
  151. // 存储静默授权code
  152. uni.setStorageSync('snsapiCode', code);
  153. let spread = that.globalData.spread ? that.globalData.spread : 0;
  154. Auth.auth(code, that.$Cache.get('SPREAD'))
  155. .then(res => {
  156. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
  157. .back_url)));
  158. if (res.type === 'register') {
  159. this.$Cache.set('snsapiKey', res.key);
  160. }
  161. if (res.type === 'login') {
  162. this.$store.commit('LOGIN', {
  163. token: res.token
  164. });
  165. this.$store.commit("SETUID", res.uid);
  166. location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url)));
  167. }
  168. })
  169. .catch(error => {
  170. if (!this.$Cache.has('snsapiKey')) {
  171. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  172. Auth.oAuth(snsapiBase, option.query.back_url);
  173. }
  174. }
  175. });
  176. } else {
  177. if (!this.$Cache.has('snsapiKey')) {
  178. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  179. Auth.oAuth(snsapiBase, urlData);
  180. }
  181. }
  182. }
  183. } else {
  184. if (option.query.back_url) {
  185. location.replace(uni.getStorageSync('snRouter'));
  186. }
  187. }
  188. // #endif
  189. // #ifdef MP
  190. // 小程序静默授权
  191. if (!this.$store.getters.isLogin && !this.globalData.tokenIsExist) {
  192. Routine.getCode().then(code => {
  193. Routine.authUserInfo(code)
  194. })
  195. .catch(res => {
  196. uni.hideLoading();
  197. });
  198. }
  199. // #endif
  200. // 主题变色
  201. getTheme().then(resP => {
  202. that.globalData.theme = `theme${Number(resP.data.value)}`
  203. that.$Cache.set('theme', that.globalData.theme);
  204. // #ifdef H5
  205. window.document.documentElement.setAttribute('data-theme', that.globalData.theme);
  206. // #endif
  207. })
  208. },
  209. async mounted() {
  210. if (this.$store.getters.isLogin && !this.$Cache.get('USER_INFO')) await this.$store.dispatch('USERINFO');
  211. },
  212. methods: {
  213. //获取登录配置
  214. getLoginConfig() {
  215. loginConfigApi().then(res => {
  216. let data = res.data;
  217. //公众号登录方式(单选),1微信授权,2手机号登录
  218. this.globalData.publicLoginType = data.publicLoginType;
  219. this.$Cache.set('publicLoginType', data.publicLoginType);
  220. //小程序手机号校验类型(多选)1微信小程序验证 2短信验证
  221. this.globalData.routinePhoneVerification = data.routinePhoneVerification;
  222. //公司名称
  223. this.globalData.companyName = data.siteName || '欢迎你';
  224. uni.setStorageSync('companyName', data.siteName);
  225. //登录页logo
  226. this.globalData.mobileLoginLogo = data.mobileLoginLogo;
  227. uni.setStorageSync('mobileLoginLogo', data.mobileLoginLogo);
  228. //版权title
  229. uni.setNavigationBarTitle({
  230. title:data.siteName
  231. });
  232. })
  233. }
  234. },
  235. onShow: function() {
  236. // #ifdef H5
  237. uni.getSystemInfo({
  238. success(e) {
  239. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  240. if (e.windowWidth > 430 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  241. // window.location.pathname = 'https://java.crmeb.net/';
  242. /* 若你的项目未设置根目录(默认为 / 时),则使用下方代码 */
  243. window.location.pathname = '/static/html/pc.html';
  244. }
  245. }
  246. })
  247. // #endif
  248. },
  249. onHide: function() {}
  250. }
  251. </script>
  252. <style lang="scss">
  253. @import url("@/plugin/animate/animate.min.css");
  254. @import 'static/css/base.css';
  255. @import 'static/iconfont/iconfont.css';
  256. @import 'static/css/guildford.css';
  257. @import 'static/css/style.scss';
  258. @import 'static/css/unocss.css';
  259. @import 'static/fonts/font.css';
  260. /* 条件编译,仅在H5平台生效 */
  261. // #ifdef H5
  262. body::-webkit-scrollbar,
  263. html::-webkit-scrollbar {
  264. display: none;
  265. }
  266. // #endif
  267. view {
  268. box-sizing: border-box;
  269. }
  270. .bg-color-red {
  271. background-color: #E93323;
  272. }
  273. .syspadding {
  274. padding-top: var(--status-bar-height);
  275. }
  276. .flex {
  277. display: flex;
  278. }
  279. .uni-scroll-view::-webkit-scrollbar {
  280. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  281. display: none
  282. }
  283. ::-webkit-scrollbar {
  284. width: 0;
  285. height: 0;
  286. color: transparent;
  287. }
  288. </style>