App.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <script>
  2. import { HTTP_REQUEST_URL } from './config/app';
  3. import Auth from './libs/wechat.js';
  4. import Routine from './libs/routine.js';
  5. export default {
  6. globalData: {
  7. spid: 0,
  8. code: 0,
  9. isLogin: false,
  10. userInfo: {},
  11. MyMenus: [],
  12. windowHeight: 0,
  13. id: 0
  14. },
  15. onLaunch: function(option) {
  16. let that = this;
  17. // 获得全局的 window 高度
  18. // #ifdef H5
  19. uni.getSystemInfo({
  20. success: function(res) {
  21. // 首页没有title获取的整个页面的高度,里面的页面有原生标题要减掉就是视口的高度
  22. // 状态栏是动态的可以拿到 标题栏是固定写死的是44px
  23. let height = res.windowHeight - res.statusBarHeight - 44
  24. // #ifdef H5
  25. that.globalData.windowHeight = res.windowHeight + 'px'
  26. // #endif
  27. }
  28. });
  29. // #endif
  30. // 获取导航高度;
  31. uni.getSystemInfo({
  32. success: function(res) {
  33. that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
  34. }
  35. });
  36. // #ifdef MP
  37. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  38. that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
  39. // #endif
  40. // #ifdef MP
  41. if (HTTP_REQUEST_URL === '') {
  42. console.error(
  43. "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
  44. );
  45. return false;
  46. }
  47. // TODO 芋艿: 分销
  48. if (option.query.hasOwnProperty('scene')) {
  49. switch(option.scene){
  50. case 1047: // 扫描小程序码
  51. case 1048: // 长按图片识别小程序码
  52. case 1049: // 手机相册选取小程序码
  53. case 1001: // 直接进入小程序
  54. let value = this.$util.getUrlParams(decodeURIComponent(option.query.scene));
  55. let values = value.split(',');
  56. if(values.length === 2){
  57. let v1 = values[0].split(":");
  58. if (v1[0] === 'pid') {
  59. that.globalData.spid = v1[1];
  60. } else{
  61. that.globalData.id = v1[1];
  62. }
  63. let v2 = values[1].split(":");
  64. if (v2[0] === 'pid') {
  65. that.globalData.spid = v2[1];
  66. }else{
  67. that.globalData.id = v2[1];
  68. }
  69. }else{
  70. that.globalData.spid = values[0].split(":")[1];
  71. }
  72. break;
  73. }
  74. }
  75. // #endif
  76. // #ifdef H5
  77. // 静默登录的情况一:微信公众号
  78. let snsapiBase = 'snsapi_base';
  79. let urlData = location.pathname + location.search;
  80. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  81. const { code, state } = option.query;
  82. if (code && code !== uni.getStorageSync('snsapiCode')
  83. && location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  84. // 存储静默授权code
  85. uni.setStorageSync('snsapiCode', code);
  86. Auth.auth(code, state, that.$Cache.get('spread')).then(res => {
  87. // TODO 芋艿:snRouter 的作用是啥
  88. uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query.back_url)));
  89. // 跳转回去
  90. location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url)));
  91. }).catch(error => {
  92. this.$Cache.set('snsapiKey', code);
  93. // TODO 芋艿:为什么没有 snsapiKey 就反复认证???没看懂
  94. // if (!this.$Cache.has('snsapiKey')) {
  95. // if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  96. // Auth.oAuth(snsapiBase, option.query.back_url);
  97. // }
  98. // }
  99. });
  100. } else {
  101. if (!this.$Cache.has('snsapiKey')) {
  102. if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
  103. Auth.oAuth(snsapiBase, urlData);
  104. }
  105. }
  106. }
  107. } else {
  108. if (option.query.back_url) {
  109. location.replace(uni.getStorageSync('snRouter'));
  110. }
  111. }
  112. // #endif
  113. // #ifdef MP
  114. // 静默登录的情况二:微信小程序
  115. if (!this.$store.getters.isLogin) {
  116. let spread = that.globalData.spid ? that.globalData.spid : 0;
  117. Routine.getCode().then(code => {
  118. Routine.authUserInfo(code, spread)
  119. .then(res => {})
  120. }).catch(res => {
  121. uni.hideLoading();
  122. });
  123. }
  124. // #endif
  125. },
  126. async mounted() {
  127. // 读取用户的基本信息
  128. if (this.$store.getters.isLogin && !this.$Cache.get('USER_INFO')) {
  129. await this.$store.dispatch('USERINFO');
  130. }
  131. },
  132. onShow: function() {
  133. // #ifdef H5
  134. uni.getSystemInfo({
  135. success(e) {
  136. // TODO 芋艿:这样是否合理???
  137. /* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
  138. if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
  139. // window.location.pathname = 'https://java.crmeb.net/';
  140. /* 若你的项目未设置根目录(默认为 / 时),则使用下方代码 */
  141. window.location.pathname = '/static/html/pc.html';
  142. }
  143. }
  144. })
  145. // #endif
  146. }
  147. }
  148. </script>
  149. <style>
  150. @import url("@/plugin/animate/animate.min.css");
  151. @import 'static/css/base.css';
  152. @import 'static/iconfont/iconfont.css';
  153. @import 'static/css/guildford.css';
  154. @import 'static/css/style.scss';
  155. /* 条件编译,仅在H5平台生效 */
  156. // #ifdef H5
  157. body::-webkit-scrollbar,
  158. html::-webkit-scrollbar {
  159. display: none;
  160. }
  161. // #endif
  162. view {
  163. box-sizing: border-box;
  164. }
  165. .bg-color-red {
  166. background-color: #E93323 !important;
  167. }
  168. .syspadding {
  169. padding-top: var(--status-bar-height);
  170. }
  171. .flex {
  172. display: flex;
  173. }
  174. .uni-scroll-view::-webkit-scrollbar {
  175. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  176. display: none
  177. }
  178. ::-webkit-scrollbar {
  179. width: 0;
  180. height: 0;
  181. color: transparent;
  182. }
  183. </style>