request.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import uni from '@/utils/uniHooks';
  2. import { getUTMSource } from '@/utils/utils';
  3. import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
  4. // import { Message } from 'element-ui';
  5. // import { jumpLogin } from '@/utils';
  6. // import { Loading } from 'element-ui';
  7. // import { ElLoadingComponent } from 'element-ui/types/loading';
  8. // import vm from "@/main";
  9. import { Toast } from 'vant';
  10. import vue from 'vue';
  11. import store from '@/store';
  12. import { v4 as uuidv4 } from 'uuid';
  13. let loadingInstance = null;
  14. let requestNum = 0;
  15. const CONTENT_TYPE_ARRAY = {
  16. json: 'application/json',
  17. form: 'application/x-www-form-urlencoded',
  18. };
  19. function getHeaders(config = {}) {
  20. const { contentType = 'json' } = config;
  21. const ct = CONTENT_TYPE_ARRAY[contentType];
  22. let header = {
  23. // appId: uni.getStorageSync('appid'),
  24. appId: 'wx92c3e55fbef6b2af',
  25. 'Content-Type': ct,
  26. };
  27. const token = getToken();
  28. if (token) {
  29. header['Authorization'] = `Bearer ${token}`;
  30. }
  31. const groupId = uni.getStorageSync('groupId');
  32. const mallId = uni.getStorageSync('mallid');
  33. if (groupId) {
  34. header['brandId'] = groupId;
  35. }
  36. if (mallId) {
  37. header['lbsId'] = mallId;
  38. }
  39. const sourceObj = getUTMSource();
  40. return Object.assign(header, sourceObj);
  41. }
  42. function handleConfig(config = {}) {
  43. const header = getHeaders(config);
  44. const noToken = config.noToken;
  45. if (noToken) {
  46. delete header.Authorization;
  47. }
  48. console.log(525252, header);
  49. return { header, ...config };
  50. }
  51. function getToken() {
  52. const token = uni.getStorageSync('kipAccessToken');
  53. // if (!token || token?.trim() == '' || token == 'null' || token == 'undefined') {
  54. if (!token || token == 'null' || token == 'undefined') {
  55. return false;
  56. }
  57. return token;
  58. }
  59. const addLoading = () => {
  60. // 增加loading 如果pending请求数量等于1,弹出loading, 防止重复弹出
  61. requestNum++;
  62. if (requestNum === 1) {
  63. // 展示加载中的状态
  64. loadingInstance = Toast.loading({
  65. message: '正在努力加载中....',
  66. });
  67. }
  68. };
  69. const cancelLoading = () => {
  70. // 取消loading 如果pending请求数量等于0,关闭loading
  71. requestNum--;
  72. if (requestNum === 0) loadingInstance?.clear();
  73. };
  74. function XUser(config) {
  75. /*'X-User': JSON.stringify({"userId": "5106534569910475","sourceType": "WECHAT","nickName": "yedUsFwdkelQbxeTeQOvaScfqIOOmaa","mobile": "18521563898","projectId": "paroject1","buildingId": "shenzhen01","cid": "8aaa809d835ba76d018378bc57180006","vipCode":"KERRY100213505"})*/
  76. /*'X-User': JSON.stringify({
  77. // userId: store.state.member.id, // K+用户ID
  78. userId: '36cec68f3a9a4f4b9e04a59268ba20f2', // K+用户ID
  79. sourceType: 'WECHAT', // 登录来源: WECHAT/ALIPAY/H5/APP
  80. cId: store.state.openid, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  81. // projectId: store.state.member.groupId, // 楼盘id
  82. projectId: store.state.projectId || '4028e3817c2b3f79017c2b48c54c0000', // 楼盘id
  83. // buildingId: store.state.member.mallid, // 楼栋id
  84. // buildingId: '8aaa80b47c784020017c78b00d060022', // 楼栋id
  85. // buildingId: store.state.buildingId || 'shenzhen01', // 楼栋id
  86. buildingId: store.state.buildingId || 'QHKC-P1', // 楼栋id
  87. phoneNumber: store.state.mobile, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  88. // vipCode: store.state.member.vipcode, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  89. // vipCode: 'KERRY100213815', // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  90. vipCode: 'KERRY100213505', // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  91. lbsId: '8aaa87bc7ce98224017ce995fd8a0002', // 楼盘ID
  92. })*/
  93. // console.log('101101101101', store.state.member);
  94. const params = {
  95. // userId: '8aaa828583bbbc030183cf34b536000a',
  96. // userId: store.state.kipUserId, // K+用户ID
  97. userId: '2c9d85868652dee50186532bdbbb0001', // K+用户ID
  98. sourceType: 'WECHAT',
  99. // phoneNumber: '18521563898',
  100. phoneNumber: store.state.mobile, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  101. // projectId: 'paroject1',
  102. projectId: store.state.projectId || '4028e3817c2b3f79017c2b48c54c0000', // 楼盘id
  103. buildingId: 'QHKC-P1',
  104. // buildingId: window.localStorage.getItem('buildingId'),
  105. brandId: store.state.groupId,
  106. // brandId: '8aaa81947c6e1ca0017c73c13cc30006',
  107. // brandId: store.state.brandId,
  108. // cid: '8aaa809d835ba76d018378bc57180006',
  109. cid: store.state.openid, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  110. // vipCode: 'KERRY100213505',
  111. // vipCode: store.state.member.vipcode, // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  112. vipCode: 'KERRY100213853', // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  113. // vipCode: 'KERRY100213851', // 终端用户ID, 微信端传openId, 支付宝小程序传阿里userId, APP传KIP的userId
  114. lbsId: '8aaa82ea804d07cd0180516ff03b0008',
  115. // lbsId: store.state.lbsId,
  116. };
  117. /*const params = {
  118. userId: '5106534569910475',
  119. sourceType: 'WECHAT',
  120. nickName: 'yedUsFwdkelQbxeTeQOvaScfqIOOmaa',
  121. mobile: '18521563898',
  122. projectId: 'paroject1',
  123. buildingId: 'shenyang01',
  124. cid: '8aaa809d835ba76d018378bc57180006',
  125. vipCode: 'KERRY100213853',
  126. lbsId: '8aaa82ea804d07cd0180516ff03b0008',
  127. };*/
  128. if (/orders-and-prepay|calculate-discount|unlicensed-car-check-in/.test(config.url)) {
  129. params.buildingId = window.localStorage.getItem('buildingId');
  130. }
  131. console.log(140140140140, params);
  132. return JSON.stringify(params);
  133. }
  134. export const createAxiosByinterceptors = (config) => {
  135. const instance = axios.create({
  136. timeout: 100000, //超时配置
  137. baseURL: `${window.profileApi}/temporary-parking-service`,
  138. // baseURL: `https://dev-kip-service-internal.kerryonvip.com/temporary-parking-service`,
  139. withCredentials: true, //跨域携带cookie
  140. ...config, // 自定义配置覆盖基本配置
  141. });
  142. // 添加请求拦截器
  143. instance.interceptors.request.use(
  144. function (config) {
  145. // 在发送请求之前做些什么
  146. const { loading = true } = config;
  147. console.log('configconfig:', config);
  148. if (loading) addLoading();
  149. // 设置 headers
  150. config.headers = {
  151. ...config.headers,
  152. ...handleConfig().header,
  153. 'XConversationId': uuidv4(),
  154. 'X-User': XUser(config),
  155. };
  156. // console.log(111111, config.headers, handleConfig().header)
  157. // console.log(111111, handleConfig().header)
  158. // console.log(123, config);
  159. return config;
  160. },
  161. function (error) {
  162. // 对请求错误做些什么
  163. return Promise.reject(error);
  164. }
  165. );
  166. // 添加响应拦截器
  167. instance.interceptors.response.use(
  168. function (response) {
  169. // 对响应数据做点什么
  170. console.log('response:', response);
  171. const { loading = true } = response.config;
  172. if (loading) cancelLoading();
  173. const { code, data, message } = response.data;
  174. console.log('62response', response);
  175. // TODO: 临时注释
  176. // Promise.reject(response.data)
  177. return response.data;
  178. /*if (code === 200) return data;
  179. else if (code === 401) {
  180. // 跳转到登陆
  181. // jumpLogin();
  182. } else {
  183. // 错误信息提示
  184. // Message.error(message);
  185. Toast({
  186. message: message,
  187. });
  188. return Promise.reject(response.data);
  189. }*/
  190. },
  191. function (error) {
  192. // console.log(158, error.response.data)
  193. // 对响应错误做点什么
  194. console.log('error-response:', error.response);
  195. console.log('error-config:', error.config);
  196. console.log('error-request:', error.request);
  197. const { loading = true } = error.config || {};
  198. if (loading) cancelLoading();
  199. console.log('79error', error);
  200. if (error.response) {
  201. if (error.response.status === 401) {
  202. // 跳转到登陆
  203. // jumpLogin();
  204. }
  205. }
  206. // 错误信息提示
  207. const { code, langMessage } = error.response.data;
  208. const codeList = ['INTERNAL_SERVER_ERROR', 'VALIDATION_FAILED', 'CAR_NOT_FOUND', 'CAR_HAS_PLATE']; // 默认处理的错误code
  209. if (codeList.indexOf(code) > -1) {
  210. uni.showToast({ title: langMessage, duration: 3000, icon: 'fail' });
  211. }
  212. // Message.error(error?.response?.data?.message || '服务端异常');
  213. return Promise.reject(error.response.data);
  214. }
  215. );
  216. return instance;
  217. };