工行这里学车报名流程h5
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.7 KiB

  1. import {
  2. APP_API,
  3. ADD_API,
  4. APP_HOST
  5. } from '@/site.config.js';
  6. import {
  7. isDef,
  8. storage,
  9. showToast,
  10. calcHeader,
  11. formatReturn
  12. } from '@/utils/utils.js';
  13. let whiteListLogin = ['/account/manage/getOwnerAccountBase.do','/sysInfoSend/getStudentAllInfoCount'] //401也不跳转到登录页面 s
  14. let whiteListCodeNegative = ['/apply/manage/queryTrainingApplyByOwner.do','/account/manage/getOwnerAccountBase.do','/order/manage/getPrepaidResult.do','/order/manage/getKwzxPrepaidResult.do'] //code==-也不弹出提示
  15. function notLogin() {
  16. storage.remove('userInfo');
  17. storage.remove('Authorization');
  18. storage.remove();
  19. try{
  20. let currentRoute = getCurrentPages().pop();
  21. const {options,route} = currentRoute
  22. const optionsKeys = Object.keys(options)
  23. let params = ''
  24. if(optionsKeys.length !=0) {
  25. params = optionsKeys.reduce((pre,current)=>{
  26. return pre + current + '=' + options[current] + '&'
  27. }, '?').slice(0,-1)
  28. }
  29. uni.setStorageSync('currentRoute',route + params)
  30. console.log(uni.getStorageSync("currentRoute"))
  31. console.log('uni.getStorageSync("currentRoute")')
  32. }catch(e){
  33. console.log(e)
  34. }
  35. uni.showToast({
  36. title:"请先登入",
  37. icon:"error",
  38. mask:true
  39. })
  40. let timer = setTimeout(()=>{
  41. // 跳转登录页
  42. uni.navigateTo({
  43. url: '/pages/user/login/login'
  44. })
  45. clearTimeout(timer)
  46. },1500)
  47. }
  48. function invalidLogin(url, datas = {}, method = 'GET') {
  49. return new Promise((reslove, reject) => {
  50. setTimeout(async () => {
  51. const [err, data] = await http(url, (datas = {}), (method = 'GET'));
  52. if (err) return formatReturn('网络异常,请稍后再试~');
  53. if (data.code === 401) {
  54. showToast('登录失效');
  55. storage.remove('userInfo');
  56. storage.remove('Authorization');
  57. uni.redirectTo({
  58. url: '/pages/user/login/login'
  59. });
  60. return reject(formatReturn('登录失效~'));
  61. }
  62. return reslove(formatReturn(null, data));
  63. }, 5000);
  64. });
  65. }
  66. /**
  67. * 对内接口请求
  68. * @param {*} api - api
  69. * @param {*} data - 参数/数据
  70. * @param {*} method - 请求的方法默认GET
  71. * @param {*} headers - 请求的header
  72. * @param {*} contentType - 传输类型 默认为0
  73. */
  74. async function http(api, datas = {}, method = 'GET', headers = {}, contentType = 0, timeout = 6000) {
  75. const _url = APP_HOST + APP_API + api;
  76. const _header = calcHeader(headers, contentType);
  77. const [err, res] = await uni.request({
  78. url: _url,
  79. data: datas,
  80. method: method.toUpperCase(),
  81. header: _header,
  82. sslVerify: false,
  83. timeout: timeout,
  84. });
  85. // console.log('_url')
  86. // console.log(_url)
  87. // console.log('请求结果')
  88. // console.log(res)
  89. // console.log(err)
  90. if (err) return formatReturn('网络异常,请稍后再试~');
  91. const {
  92. data,
  93. header,
  94. statusCode
  95. } = res;
  96. const Authorization = header.token || header.token || '';
  97. if (isDef(Authorization)) storage.set('Authorization', Authorization);
  98. if (statusCode >= 200 && statusCode < 300) {
  99. const code = data.code;
  100. if (code === 401&&whiteListLogin.indexOf(api) == -1) return notLogin();
  101. if (code === 500) return await invalidLogin(api, datas, method);
  102. if (code == -1 && whiteListCodeNegative.indexOf(api) == -1) return showToast(data.msg || data.message);
  103. return formatReturn(null, data);
  104. }
  105. }
  106. async function Ajax1(api, datas = {}, method = 'GET', headers = {}, contentType = 1, timeout = 60000) {
  107. const datasSrc = getPostUrl(datas)
  108. let _url = APP_HOST + APP_API + api
  109. if (datasSrc) _url = _url + '?' + datasSrc;
  110. const _header = calcHeader(headers, contentType);
  111. const [err, res] = await uni.request({
  112. url: _url,
  113. params: datas,
  114. method: method.toUpperCase(),
  115. header: _header,
  116. sslVerify: false,
  117. timeout: timeout,
  118. enableCookie: true
  119. });
  120. // console.log('_url')
  121. // console.log(_url)
  122. // console.log(res)
  123. if (err) return formatReturn('网络异常,请稍后再试~');
  124. const {
  125. data,
  126. header,
  127. statusCode
  128. } = res;
  129. const Authorization = header.token || header.token || '';
  130. if (isDef(Authorization)) storage.set('Authorization', Authorization);
  131. if (statusCode >= 200 && statusCode < 300) {
  132. const code = data.code;
  133. if (code === 401) return notLogin();
  134. if (code === 500) return await invalidLogin(api, datas, method);
  135. if (code == -1 && whiteListCodeNegative.indexOf(api) == -1) return showToast(data.msg || data.message);
  136. return formatReturn(null, data);
  137. }
  138. }
  139. /**
  140. * 对外接口请求
  141. * @param {*} url - 请求路径/参数
  142. * @param {*} method - 请求的方法默认GET
  143. */
  144. async function Ajax1Add(api, datas = {}, method = 'GET', headers = {}, contentType = 1, timeout = 60000) {
  145. const _url = ADD_API + api;
  146. const _header = calcHeader(headers, contentType);
  147. const [err, res] = await uni.request({
  148. url: _url,
  149. data: datas,
  150. method: method.toUpperCase(),
  151. header: {
  152. 'Authorization': _header.token
  153. },
  154. sslVerify: false,
  155. timeout: timeout,
  156. });
  157. console.log('_url')
  158. console.log(_url)
  159. console.log('请求广告列表结果')
  160. console.log(res)
  161. console.log(_header)
  162. if (err) return formatReturn('网络异常,请稍后再试~');
  163. const {
  164. data,
  165. header,
  166. statusCode
  167. } = res;
  168. const Authorization = header.token || header.token || '';
  169. if (isDef(Authorization)) storage.set('Authorization', Authorization);
  170. if (statusCode >= 200 && statusCode < 300) {
  171. const code = data.code;
  172. if (code === 401&&whiteListLogin.indexOf(api) == -1) return notLogin();
  173. if (code === 500) return await invalidLogin(api, datas, method);
  174. if (code == -1 && whiteListCodeNegative.indexOf(api) == -1) return showToast(data.msg || data.message);
  175. return formatReturn(null, data);
  176. }
  177. }
  178. function getPostUrl(data) {
  179. let str = Object.keys(data).map(key => `${key}=${data[key]}`).join('&')
  180. str.split(0, -1)
  181. return str
  182. }
  183. // 导出请求
  184. export {
  185. Ajax1Add,
  186. Ajax1
  187. }; // 外部接口请求
  188. export default http; // 内部接口请求