浏览代码

加入环境切换功能

John-Hong 2 年之前
父节点
当前提交
25361392c3

+ 23 - 2
src/App.vue

@@ -27,6 +27,9 @@
 // import { KIP_API_CODE } from '@/common/js/config';
 // import qs from 'qs';
 export default {
+  created() {
+    this.initEnv()
+  },
   computed: {
     cachedViews() {
       return this.$store.state.cachedViews.cachedViews;
@@ -47,13 +50,31 @@ export default {
           meta: { keepAlive },
         } = route;
         if (name && keepAlive) {
-          console.log(5353535353);
-          console.log(name, keepAlive);
           this.$store.commit('cachedViews/ADD_CACHED_VIEW', route);
         }
       },
     },
   },
+  methods: {
+    initEnv() {
+      const href = window.location.href;
+      if (/dev-/.test(href)) {
+        window.env = 'dev'
+        window.profileApi = 'profileApi'
+        window.api = 'qaApi'
+        return
+      }
+      if (/qa-/.test(href)) {
+        window.env = 'qa'
+        window.api = 'qaApi'
+        window.profileApi = 'profileApi'
+        return
+      }
+      window.env = 'prod'
+      window.profileApi = 'profileApiProd'
+      window.api = 'api'
+    }
+  }
   // data() {
   //   return {
   //     ws: null,

+ 14 - 4
src/README.md

@@ -1,4 +1,5 @@
 启动前准备:
+
 - 确保微信开发工具中登录的账号是当前小程序的开发者
 - 因为引用了外部包所以需要指向`npm install`命令
 
@@ -7,12 +8,21 @@
 KIP相关: `app.globalData.auth`存储的是token相关数据,
 在`/auth/openid-login`之后会将`token`存储在storage中使用`uni.setStorageSync('kipAccessToken', access_token)`
 
-原有信息: `app.globalData.openId`,`app.globalData.userInfo`,`app.globalData.member`,`app.globalData.wxMember`; 
-同时这些信息在`storage`中也有一份 
+原有信息: `app.globalData.openId`,`app.globalData.userInfo`,`app.globalData.member`,`app.globalData.wxMember`;
+同时这些信息在`storage`中也有一份
 `userInfo` 是会员授权信息调用 `getUserProfile` 获取到的,其他信息是调用接口获取到的,`member`和`wxMember`是一样的数据
- 
+
 ## 切换环境准备
+
 1.manifest.json中`mp-weixin`的`appid`配置
 2.request-kip.js中`APPID`配置
 3.api-kip.js中`BASE_URL`配置
-4.plugins/install.js中`$baseURL`,`$baseURLH5`,`$wsBaseURL`,`$picUrl`,
+4.plugins/install.js中`$baseURL`,`$baseURLH5`,`$wsBaseURL`,`$picUrl`,
+
+## 环境变量说明
+
+本项目代码使用了 [CRM](https://git.kerryprops.com.cn/cornerstone/kpl_crm_uniapp) 的停车缴费代码,由于该项目中存在大量小程序方面的代码,需要单独配置一套环境数据,来确保本项目的环境指向是正确的。
+
+```js
+window.env = 'dev|qa|prod' 
+```

+ 20 - 2
src/components/Login/Login.vue

@@ -303,14 +303,32 @@ export default {
         })
       );
     },
+    // websocket 链接
+    getUrl() {
+      // 如果 kerry+ 这边的访问环境是 sl 或者 lt,需要把 wss 指向 qa 环境。
+      if (window?.injectConfig?.api) {
+        return `${window?.injectConfig?.api}/xcrm-api`;
+      }
+      const href = `${window.location.href}`;
+      if (/dev-/.test(href)) {
+        return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
+      }
+      if (/qa-/.test(href)) {
+        return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
+      }
+      // TODO 在 sl 环境验证生产数据时,先使用qa环境的websocket通信
+      return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
+      // return `https://crm.kerryplus.com/xcrm-api`;
+    },
     wss(callback) {
       try {
         console.log(
           107107,
-          this.host + '/hafengWebsocket' + '?token=' + window.token
+          this.getUrl() + '/hafengWebsocket' + '?token=' + window.token
         );
         const socket = new SockJS(
-          this.host + '/hafengWebsocket' + '?token=' + window.token
+          this.getUrl() + '/hafengWebsocket' + '?token=' + window.token
+          // this.host + '/hafengWebsocket' + '?token=' + window.token
         );
         window.stompClient = Stomp.over(socket);
         window.stompClient.debug = null;

+ 2 - 1
src/pages/parkingFee/parkingFeeDetail.vue

@@ -1752,7 +1752,8 @@ export default {
       };
       // console.log(1854, params);
       this.$md(params);
-      let path = `/profileApi/payment/v1/services/session/${session}/transactions`;
+      // let path = `/profileApi/payment/v1/services/session/${session}/transactions`;
+      let path = `/${window.profileApi}/payment/v1/services/session/${session}/transactions`;
       this.$request({
         url: path,
         data: params,

+ 1 - 1
src/utils/api-crm-member.js

@@ -97,7 +97,7 @@ export function crmQueryMemberInfo (params) {
 // 获取会员详细信息用于会员注册后完善信息操作,在obj.extend属性中,外层是基础会员信息
 export function kipGetUserDetail (params) {
   // https://apim.kerryplus.com/c/api/profile/v1/customer/crm/current
-  const url = + `/profileApi/profile/v1/customer/crm/current`;
+  const url = + `/${window.profileApi}/profile/v1/customer/crm/current`;
   return request.get(url, params, {...DEFAULT_CONFIG});
 }
 

+ 2 - 1
src/utils/cache-tool.js

@@ -46,7 +46,8 @@ export default {
       console.log(464646, window?.injectConfig);
       console.log(474747, key);
       if (key === 'KIP_API') {
-        return window?.injectConfig?.profileApi
+        // return window?.injectConfig?.profileApi
+        return window.profileApi
       }
       if (curEnvConst?.constants) {
         curEnvConst.constants = {

+ 1 - 1
src/utils/request-crm.js

@@ -49,7 +49,7 @@ function getHeaders(config = {}) {
 }
 
 function getUrl(url) {
-  if (url.indexOf('http') > -1 || url.indexOf('/profileApi') > -1) {
+  if (url.indexOf('http') > -1 || url.indexOf(`/${window.profileApi}`) > -1) {
     return url;
   } else {
     const baseUrl = CacheTool.getCurEnvConst('BASE_URL');

+ 4 - 4
src/utils/request-kip.js

@@ -55,9 +55,9 @@ function getUrl(path) {
   // console.log(555555555, path);
   // 如果是dev、qa、prod环境
   if (window.ininjectConfig) {
-    if (path.indexOf('/profileApi') > -1) {
+    if (path.indexOf(`/${window.profileApi}`) > -1) {
       // return `${window.ininjectConfig.profileApi}${path}`
-      return `/profileApi${path}`
+      return `/${window.profileApi}${path}`
     }
     if (/weixinApi/g.test(path)) {
       return path;
@@ -68,7 +68,7 @@ function getUrl(path) {
   }
   // 本地开发环境
   // path.indexOf('http') > -1 && 
-  if (path.indexOf('/profileApi') > -1 || path) {
+  if (path.indexOf(`/${window.profileApi}`) > -1 || path) {
     return path;
   } else if (/\/qaPayment/g.test(path)) {
     // 支付
@@ -129,7 +129,7 @@ export default {
       config
     }
     return new Promise((resolve, reject) => {
-      const url = getUrl(/\/sns|http/g.test(path) ? path : `/profileApi${path}`);
+      const url = getUrl(/\/sns|http/g.test(path) ? path : `/${window.profileApi}${path}`);
       // console.log(130, url);
       const { header } = handleConfig(config)
       log.info(`===>request-kip url: ${url}`)