request.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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;//'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. if(config.url==='/slbMpAutoLogin'||config.url==='/slbSmLogin'){
  90. try{
  91. var cookie = res.header["set-cookie"]||res.header["Set-Cookie"];
  92. let frontId = res.data.resultMap?(res.data.resultMap.frontlixiangsid||''):'';
  93. let frontlixiangsid = getCookie(cookie,"frontlixiangsid")||frontId;
  94. if(frontlixiangsid=='deleteMe'&&frontId){
  95. frontlixiangsid = frontId;
  96. }
  97. if(frontlixiangsid&&frontlixiangsid!='deleteMe'){
  98. // uni.setStorageSync('frontlixiangsid',frontlixiangsid)
  99. store.state.token = frontlixiangsid;
  100. }
  101. }catch(err){
  102. console.log(err);
  103. }
  104. }
  105. //console.log(config);
  106. loadingStatus(loading, false)
  107. //console.log(res);
  108. if (res.statusCode) {
  109. let data = res.data;
  110. console.log('instance.request',res);
  111. console.log('instance.request data',data);
  112. if (res.statusCode === 200) {
  113. console.log('instance.request data 1',data);
  114. if (data.state == 1) {
  115. return Promise.resolve(data);
  116. }
  117. else if(data.msg&&(data.msg=='请勿频繁操作'||data.msg.indexOf('不能再次使用')>-1)){
  118. uniShowToast(data.msg);
  119. return;
  120. }
  121. else if(data._redirect||data.msg=='未登录'){
  122. uni.removeStorageSync('userNo');
  123. // if(uni.getStorageSync('userNo')){
  124. // uniShowModal('', '账号过期,重新登录?', {
  125. // showCancel: true,
  126. // success: (res) => {
  127. // if(res.confirm){
  128. // //添加跳转 微信绑定公众号页面
  129. // uni.redirectTo({
  130. // url: '/pages/login/login'
  131. // });
  132. // }else{
  133. // return Promise.resolve(data);
  134. // }
  135. // }
  136. // })
  137. // }
  138. // store.commit("LOGOUT");
  139. // uni.reLaunch({
  140. // url: '/pages/index/auth'
  141. // });
  142. return Promise.resolve(data);
  143. }else if (data.state == -1) {
  144. return Promise.resolve(data);
  145. }
  146. else if(data=='' || data=='success'|| data._redirect){
  147. console.log('instance.request data 2',data);
  148. // store.commit("LOGOUT");
  149. //未登录
  150. if (!store.state.isGOAuth) {
  151. store.commit("SET_GO_AUTH", true);
  152. //判断是否是第一次加载第一次加载未登录不用弹框提醒
  153. if(store.state.isFirstLoad){
  154. store.state.isFirstLoad=false;
  155. uni.redirectTo({
  156. url: '/pages/Login/Login'
  157. });
  158. return;
  159. }else{
  160. store.state.isFirstLoad=false;
  161. uniShowModal('', '账号过期重新登录认证', {
  162. showCancel: false,
  163. success: (res) => {
  164. //添加跳转 微信绑定公众号页面
  165. uni.redirectTo({
  166. url: '/pages/auth/auth'
  167. });
  168. }
  169. })
  170. return;
  171. }
  172. } else {
  173. console.log('路由已存在,不再重复跳转')
  174. return;
  175. }
  176. data.msg='未登录';
  177. return Promise.reject(data);
  178. } else {
  179. console.log('instance.request data 3',data);
  180. //data.state == -301||
  181. if(data.msg == undefined){
  182. if(uni.getStorageSync("loginFlag")){
  183. uni.redirectTo({
  184. url: "/pages/Login/Login"
  185. });
  186. }
  187. return;
  188. }
  189. if (warn) {
  190. uni.redirectTo({
  191. url: '/pages/error/error?code='+data.state+'&msg='+data.msg
  192. });
  193. return;
  194. }
  195. return Promise.resolve(data);
  196. }
  197. return Promise.resolve(res.data);
  198. } else {
  199. if (warn) {
  200. responseError({
  201. code: res.statusCode
  202. })
  203. }
  204. return Promise.reject(res.data);
  205. }
  206. } else {
  207. return Promise.reject(res.data);
  208. }
  209. loadingStatus(loading, false);
  210. }).catch(err => {
  211. loadingStatus(loading, false)
  212. // if (warn) {
  213. uniShowToast(err)
  214. // }
  215. return Promise.reject(err);
  216. })
  217. }
  218. function cancelRequest() {
  219. if (typeof setCancel === 'function') {
  220. setCancel('取消请求')
  221. }
  222. loadingStatus(loading, false);
  223. }
  224. // 定义请求提示
  225. const loadingStatus = (loading, type) => {
  226. if (loading) {
  227. if (type) {
  228. // loadingToast('正在请求中');
  229. // setTimeout(function () {
  230. // uniHideLoading();
  231. // }, 5000);//防水异常加载不关闭影响操作
  232. } else {
  233. // uniHideLoading();
  234. }
  235. } else {
  236. return
  237. }
  238. }
  239. // 请求返回statusCode
  240. const responseError = ({
  241. code
  242. }) => {
  243. let content = '';
  244. switch (code) {
  245. case 400:
  246. content = '请求参数错误';
  247. break
  248. case 401:
  249. content = '未授权,请重新登录-401';
  250. break
  251. case 403:
  252. content = '没有访问权限-403';
  253. break
  254. case 404:
  255. content = '请求错误,未找到该资源-404';
  256. break
  257. case 405:
  258. content = '请求方式错误-405';
  259. break
  260. case 408:
  261. content = '请求超时';
  262. break
  263. case 500:
  264. content = '服务器异常-500';
  265. break
  266. case 501:
  267. content = '服务未实现';
  268. break
  269. case 502:
  270. content = '网关错误';
  271. break
  272. case 503:
  273. content = '服务不可用';
  274. break
  275. case 504:
  276. content = '网关超时';
  277. break
  278. case 505:
  279. content = 'HTTP版本不受支持';
  280. break
  281. default:
  282. content = '请求异常-' + code;
  283. break
  284. }
  285. uniShowModal('', content, {
  286. showCancel: false,
  287. success: (res) => {
  288. console.log(res)
  289. }
  290. })
  291. }
  292. const request = ['get', 'post', 'put', 'delete', 'connect', 'head', 'options', 'trace'].reduce((request, method) => {
  293. request[method] = (url, data, options = {}) => baseRequest({
  294. url,
  295. method,
  296. data
  297. }, { ...defaultOptions,
  298. ...options
  299. });
  300. return request;
  301. }, {});
  302. export default request;