request.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import unirequest from '../plugins/uni_request.js'
  2. import store from "../store";
  3. // import { autoAuth } from '@/utils/common.js';
  4. import md5 from 'js-md5';
  5. import {
  6. uniShowToast,
  7. successToast,
  8. loadingToast,
  9. uniHideToast,
  10. uniHideLoading,
  11. uniShowModal
  12. } from './uni_api'
  13. import {
  14. VUE_APP_URL
  15. } from '../config.js'
  16. // 初始化自定义参数 login 是否需要登录权限 cancel 是否取消上次请求
  17. const defaultOptions = {
  18. login: true,
  19. auth: false,
  20. cancel: false,
  21. loading: true, //是否显示 请求加载中
  22. warn: true //是否异常弹窗提示
  23. };
  24. // 初始化请求
  25. let instance = unirequest.create({
  26. baseURL: VUE_APP_URL,
  27. timeout: 100000,
  28. debug: true
  29. });
  30. let setCancel;
  31. // 请求拦截器
  32. instance.interceptors.request.use(
  33. config => {
  34. var token = store.state.token||uni.getStorageSync('frontlixiangsid');//'7221b77a-899b-492b-8263-9507f5c470e9';// store.state.token
  35. //console.log("frontlixiangsid:"+token);
  36. config.header['frontlixiangsid']= token;//登录token 标志id
  37. config.header['platType']= 'slb';
  38. config.header['mpType'] = 'engineer';
  39. config.header['appType'] = 'ma';
  40. config.cancelToken = new unirequest.CancelToken(function executor(c) {
  41. setCancel = c; //记录当前请求
  42. });
  43. config.timeStamp= new Date().getTime();
  44. var jsToken ="";//防重复提交
  45. jsToken = md5(JSON.stringify(config))
  46. console.log('jsToken' + jsToken);
  47. console.log(config);
  48. config.data['tokenId'] = jsToken;
  49. return config
  50. })
  51. // 响应拦截器
  52. instance.interceptors.response.use(
  53. response => {
  54. // console.log('响应成功', response)
  55. return response
  56. })
  57. // 读取cookie
  58. function getCookie(cookie,c_name) {
  59. if (cookie && cookie.length > 0){
  60. let c_start = cookie.indexOf(c_name + "=")
  61. if (c_start != -1){
  62. c_start = c_start + c_name.length + 1
  63. let c_end = cookie.indexOf(";", c_start)
  64. if (c_end == -1)
  65. c_end = cookie.length
  66. return unescape(cookie.substring(c_start, c_end))
  67. }
  68. }
  69. return ""
  70. }
  71. // 请求主体
  72. function baseRequest(config, options) {
  73. const {
  74. login,
  75. auth,
  76. cancel,
  77. loading = true,
  78. warn
  79. } = options;
  80. if (options.cancel) {
  81. cancelRequest()
  82. }
  83. loadingStatus(loading, true)
  84. if (login && !store.state.token) {
  85. loadingStatus(loading, false);
  86. // autoAuth();
  87. }
  88. return instance.request(config).then(res => {
  89. console.error('res');
  90. console.error(config);
  91. if(config.url==='/slbMpAutoLogin'){
  92. try{
  93. var cookie = res.header["set-cookie"]||res.header["Set-Cookie"];
  94. let frontlixiangsid = getCookie(cookie,"frontlixiangsid")||res.resultMap.frontlixiangsid;
  95. console.log('cookie',frontlixiangsid);
  96. if(frontlixiangsid&&frontlixiangsid!='deleteMe'){
  97. store.state.token = frontlixiangsid;
  98. uni.setStorageSync('frontlixiangsid',frontlixiangsid)
  99. }
  100. }catch(err){
  101. console.log(err);
  102. }
  103. }
  104. //console.log(config);
  105. loadingStatus(loading, false)
  106. //console.log(res);
  107. if (res.statusCode) {
  108. let data = res.data;
  109. console.log('instance.request',res);
  110. console.log('instance.request data',data);
  111. if (res.statusCode === 200) {
  112. console.log('instance.request data 1',data);
  113. if (data.state == 1) {
  114. return Promise.resolve(data);
  115. }
  116. else if(data.msg&&(data.msg=='请勿频繁操作'||data.msg.indexOf('不能再次使用')>-1)){
  117. uniShowToast(data.msg);
  118. return;
  119. }
  120. else if(data._redirect||data.msg=='未登录'){
  121. // store.commit("LOGOUT");
  122. uni.reLaunch({
  123. url: '/pages/index/auth'
  124. });
  125. return Promise.resolve(data);
  126. }
  127. else if(data=='' || data=='success'|| data._redirect){
  128. console.log('instance.request data 2',data);
  129. // store.commit("LOGOUT");
  130. //未登录
  131. if (!store.state.isGOAuth) {
  132. store.commit("SET_GO_AUTH", true);
  133. //判断是否是第一次加载第一次加载未登录不用弹框提醒
  134. if(store.state.isFirstLoad){
  135. store.state.isFirstLoad=false;
  136. uni.redirectTo({
  137. url: '/pages/Login/Login'
  138. });
  139. return;
  140. }else{
  141. store.state.isFirstLoad=false;
  142. uniShowModal('', '账号过期重新登录认证', {
  143. showCancel: false,
  144. success: (res) => {
  145. //添加跳转 微信绑定公众号页面
  146. uni.redirectTo({
  147. url: '/pages/auth/auth'
  148. });
  149. }
  150. })
  151. return;
  152. }
  153. } else {
  154. console.log('路由已存在,不再重复跳转')
  155. return;
  156. }
  157. data.msg='未登录';
  158. return Promise.reject(data);
  159. } else {
  160. console.log('instance.request data 3',data);
  161. //data.state == -301||
  162. if(data.msg == undefined){
  163. if(uni.getStorageSync("loginFlag")){
  164. uni.redirectTo({
  165. url: "/pages/Login/Login"
  166. });
  167. }
  168. return;
  169. }
  170. if (warn) {
  171. uni.redirectTo({
  172. url: '/pages/error/error?code='+data.state+'&msg='+data.msg
  173. });
  174. return;
  175. }
  176. return Promise.resolve(data);
  177. }
  178. return Promise.resolve(res.data);
  179. } else {
  180. if (warn) {
  181. responseError({
  182. code: res.statusCode
  183. })
  184. }
  185. return Promise.reject(res.data);
  186. }
  187. } else {
  188. return Promise.reject(res.data);
  189. }
  190. loadingStatus(loading, false);
  191. }).catch(err => {
  192. loadingStatus(loading, false)
  193. // if (warn) {
  194. uniShowToast(err)
  195. // }
  196. return Promise.reject(err);
  197. })
  198. }
  199. function cancelRequest() {
  200. if (typeof setCancel === 'function') {
  201. setCancel('取消请求')
  202. }
  203. loadingStatus(loading, false);
  204. }
  205. // 定义请求提示
  206. const loadingStatus = (loading, type) => {
  207. if (loading) {
  208. if (type) {
  209. // loadingToast('正在请求中');
  210. setTimeout(function () {
  211. uniHideLoading();
  212. }, 5000);//防水异常加载不关闭影响操作
  213. } else {
  214. uniHideLoading();
  215. }
  216. } else {
  217. return
  218. }
  219. }
  220. // 请求返回statusCode
  221. const responseError = ({
  222. code
  223. }) => {
  224. let content = '';
  225. switch (code) {
  226. case 400:
  227. content = '请求参数错误';
  228. break
  229. case 401:
  230. content = '未授权,请重新登录-401';
  231. break
  232. case 403:
  233. content = '没有访问权限-403';
  234. break
  235. case 404:
  236. content = '请求错误,未找到该资源-404';
  237. break
  238. case 405:
  239. content = '请求方式错误-405';
  240. break
  241. case 408:
  242. content = '请求超时';
  243. break
  244. case 500:
  245. content = '服务器异常-500';
  246. break
  247. case 501:
  248. content = '服务未实现';
  249. break
  250. case 502:
  251. content = '网关错误';
  252. break
  253. case 503:
  254. content = '服务不可用';
  255. break
  256. case 504:
  257. content = '网关超时';
  258. break
  259. case 505:
  260. content = 'HTTP版本不受支持';
  261. break
  262. default:
  263. content = '请求异常-' + code;
  264. break
  265. }
  266. uniShowModal('', content, {
  267. showCancel: false,
  268. success: (res) => {
  269. console.log(res)
  270. }
  271. })
  272. }
  273. const request = ['get', 'post', 'put', 'delete', 'connect', 'head', 'options', 'trace'].reduce((request, method) => {
  274. request[method] = (url, data, options = {}) => baseRequest({
  275. url,
  276. method,
  277. data
  278. }, { ...defaultOptions,
  279. ...options
  280. });
  281. return request;
  282. }, {});
  283. export default request;