Pārlūkot izejas kodu

feat: 微信公众号h5授权-v4

John-Hong 2 gadi atpakaļ
vecāks
revīzija
032e6627d8
1 mainītis faili ar 53 papildinājumiem un 53 dzēšanām
  1. 53 53
      src/utils/request-kip.js

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

@@ -1,4 +1,4 @@
-import { APPID,SOURCE_KEY } from '@/constants.js'
+import { APPID, SOURCE_KEY } from '@/constants.js'
 import CacheTool from '@/utils/cache-tool.js'
 import log from './log.js'
 import { getUTMSource } from '@/utils/utils.js'
@@ -9,59 +9,59 @@ const CONTENT_TYPE_ARRAY = {
   'form': 'application/x-www-form-urlencoded'
 }
 
-function getHeaders( config = {} ) {
-  const {contentType = 'json'} = config;
+function getHeaders(config = {}) {
+  const { contentType = 'json' } = config;
   const ct = CONTENT_TYPE_ARRAY[contentType];
   let header = {
     'appId': APPID,
     'Content-Type': ct
   };
   const token = getToken()
-  if ( token ) {
-    header['Authorization'] = `Bearer ${ token }`;
+  if (token) {
+    header['Authorization'] = `Bearer ${token}`;
   }
 
   const groupId = uni.getStorageSync("groupId");
   const mallId = uni.getStorageSync("mallid");
-  if ( groupId ) {
+  if (groupId) {
     header['brandId'] = groupId
   }
-  if ( mallId ) {
+  if (mallId) {
     header['lbsId'] = mallId
   }
   const sourceObj = getUTMSource();
-  return Object.assign(header,sourceObj);
+  return Object.assign(header, sourceObj);
 }
 
-function handleConfig( config = {} ) {
+function handleConfig(config = {}) {
   const header = getHeaders(config)
   const noToken = config.noToken
-  if ( noToken ) {
+  if (noToken) {
     delete header.Authorization;
   }
-  return {header,...config};
+  return { header, ...config };
 }
 
 function getToken() {
   const token = uni.getStorageSync('kipAccessToken')
   // if (!token || token?.trim() == '' || token == 'null' || token == 'undefined') {
-  if ( !token || token == 'null' || token == 'undefined' ) {
+  if (!token || token == 'null' || token == 'undefined') {
     return false;
   }
   return token
 }
 
-function getUrl( path ) {
+function getUrl(path) {
   console.log(555555555, path);
   // 如果是dev、qa、prod环境
   if (window.ininjectConfig) {
-    if(path.indexOf('/profileApi') > -1) {
+    if (path.indexOf('/profileApi') > -1) {
       return `${window.ininjectConfig.profileApi}${path}`
     }
     if (/weixinApi/g.test(path)) {
       return path;
     }
-    if(/\/qaPayment/g.test(path)) {
+    if (/\/qaPayment/g.test(path)) {
       return `${window.ininjectConfig.qaPayment}${path}`
     }
   }
@@ -92,25 +92,25 @@ function cleanCacheAndGoLogin() {
   })
 }
 
-function handleKipResponse( resp,request ) {
-  if ( resp && resp.data ) {
-    const {status} = resp;
-    console.log(`kip request resp => errMsg ${ status }`)
-    const {path} = request.detail;
+function handleKipResponse(resp, request) {
+  if (resp && resp.data) {
+    const { status } = resp;
+    console.log(`kip request resp => errMsg ${status}`)
+    const { path } = request.detail;
     let isLogout = false;
-    if ( path.indexOf("/logout") > -1 ) {
+    if (path.indexOf("/logout") > -1) {
       isLogout = true;
     }
-    if ( !resp || !resp.data ) {
+    if (!resp || !resp.data) {
       return resp;
     }
     const result = resp.data;
-    if ( result ) {
-      const {message,code} = result || {}
-      console.log(`kip api resul => message: ${ message } code: ${ code }`)
-      if ( result.code == '300000' && !isLogout ) {
+    if (result) {
+      const { message, code } = result || {}
+      console.log(`kip api resul => message: ${message} code: ${code}`)
+      if (result.code == '300000' && !isLogout) {
         console.warn('===> kip的access_token已过期')
-        log.info(`kip的access_token已过期`,result)
+        log.info(`kip的access_token已过期`, result)
       }
     }
   }
@@ -119,7 +119,7 @@ function handleKipResponse( resp,request ) {
 
 export default {
   detail: null,
-  base( method,path,param = {},config ) {
+  base(method, path, param = {}, config) {
     const _this = this;
     _this.detail = {
       method,
@@ -127,44 +127,44 @@ export default {
       param,
       config
     }
-    return new Promise(( resolve,reject ) => {
-      const url = getUrl(`/profileApi${path}`);
+    return new Promise((resolve, reject) => {
+      const url = getUrl(/weixinApi/.test(path) ? path : `/profileApi${path}`);
       console.log(130, url);
-      const {header} = handleConfig(config)
-      log.info(`===>request-kip url: ${ url }`)
-      log.info(`===>request-kip header: ${ JSON.stringify(header) }`)
+      const { header } = handleConfig(config)
+      log.info(`===>request-kip url: ${url}`)
+      log.info(`===>request-kip header: ${JSON.stringify(header)}`)
       uni.request({
         header: header,
         method: method,
         url: url,
         data: param,
       }).then(res => {
-        log.info(`===>request-kip response: ${ JSON.stringify(res.data) }`)
+        log.info(`===>request-kip response: ${JSON.stringify(res.data)}`)
         resolve(handleKipResponse(res, _this))
-        if ( res.status == 581 ) {
+        if (res.status == 581) {
           uni.navigateTo({
             url: '/pages/errorPage/errorLimit?msg=当前访问太火爆了,稍后试试吧~'
           })
         }
-      }).catch(( response ) => {
-        log.error(`===>request-kip error: ${ JSON.stringify(response) }`)
+      }).catch((response) => {
+        log.error(`===>request-kip error: ${JSON.stringify(response)}`)
         reject(response)
       })
     })
   },
-  get( path,param = {},config ) {
-    return this.base('GET',path,param,config)
+  get(path, param = {}, config) {
+    return this.base('GET', path, param, config)
   },
-  post( path,param = {},config ) {
-    return this.base('POST',path,param,config)
+  post(path, param = {}, config) {
+    return this.base('POST', path, param, config)
   },
-  put( path,param = {},config ) {
-    return this.base('PUT',path,param,config)
+  put(path, param = {}, config) {
+    return this.base('PUT', path, param, config)
   },
-  delete( path,param = {},config ) {
-    return this.base('DELETE',path,param,config)
+  delete(path, param = {}, config) {
+    return this.base('DELETE', path, param, config)
   },
-  uploadFile( path,param = {},config ) {
+  uploadFile(path, param = {}, config) {
     const _this = this;
     _this.detail = {
       method: 'POST',
@@ -172,9 +172,9 @@ export default {
       param,
       config
     }
-    return new Promise(( resolve,reject ) => {
+    return new Promise((resolve, reject) => {
       const url = getUrl(path);
-      const {fileType,file,formData} = param
+      const { fileType, file, formData } = param
       let header = getHeaders(config)
       header["Content-Type"] = "multipart/form-data"
       uni.uploadFile({
@@ -187,14 +187,14 @@ export default {
         file: file,
         formData: formData
       }).then(res => {
-        resolve(handleKipResponse(res[1],_this))
-      }).catch(( response ) => {
+        resolve(handleKipResponse(res[1], _this))
+      }).catch((response) => {
         reject(response)
       })
     })
   },
-  refreshToken( path ) {
-    return new Promise(( resolve,reject ) => {
+  refreshToken(path) {
+    return new Promise((resolve, reject) => {
       const url = getUrl(path);
       const header = getHeaders()
       delete header.Authorization;
@@ -204,7 +204,7 @@ export default {
         url: url,
       }).then(res => {
         resolve(res[1])
-      }).catch(( response ) => {
+      }).catch((response) => {
         reject(response)
       })
     })