request.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * 接口请求封装
  3. *
  4. * 1、需要成功提示的,带 additional.successMessage ,如果值为'msg',提示内容为返回的msg
  5. * 2、默认loading,不需要的传 additional.needLoading = false。
  6. */
  7. import { URL } from "./requestUrl";
  8. import { stringify } from 'qs';
  9. const BASE_URL = `http://${URL}`;
  10. let AllRequestNum = 0; // 存放请求数,以保证有请求未返回就持续loading
  11. /**
  12. *
  13. * @param obj 接口配置数据{url,method,successMessage,needLoading}
  14. * @param data 接口请求数据
  15. */
  16. function creatRequest(obj, data) {
  17. if (obj) {
  18. let additional = {}, url = obj.url;
  19. if (obj.successMessage !== undefined) additional.successMessage = obj.successMessage;
  20. if (obj.needLoading !== undefined) additional.needLoading = obj.needLoading;
  21. // DELETE 参数以问号形式拼接到url
  22. if (obj.method === 'DELETE') {
  23. url = data ? `${url}?${stringify(data)}` : url;
  24. data = null;
  25. }
  26. return request(url, { method: obj.method, data }, additional);
  27. } else {
  28. uni.showModal({
  29. title: '错误提示',
  30. content: '请求的接口未定义!',
  31. showCancel: false
  32. });
  33. }
  34. }
  35. /**
  36. * url: 请求的url
  37. * options: uni.request的一系列参数,常用的method和data
  38. * additional: { successMessage、needLoading }
  39. */
  40. function request(url, options, additional) {
  41. if (additional.needLoading === undefined) additional.needLoading = true;
  42. // 加载中提示打开
  43. // console.log({url, options, additional});
  44. openCloseLoading(additional, true);
  45. // 模拟token,做登录的同学将这行代码移到登录成功函数中.
  46. const token = uni.getStorageSync('token');
  47. return uni.request({
  48. url: `${BASE_URL}${url}`,
  49. ...options,
  50. header: {
  51. token: token ? token : null,
  52. 'Content-Type': 'application/json',
  53. }
  54. }).then((res) => notifyException(res, additional))
  55. .catch((error) => {
  56. if ('stack' in error && 'message' in error) {
  57. uni.showModal({
  58. title: '错误提示',
  59. content: '服务器异常,请稍后重试!',
  60. showCancel: false
  61. });
  62. console.error(`请求错误: ${url} ${error.message}`);
  63. }
  64. return error;
  65. });
  66. }
  67. // 加载中提示打开/关闭
  68. function openCloseLoading(additional, isOpen) {
  69. if (additional.needLoading) {
  70. if (isOpen) { // 打开(如果已经打开不必重复打开)
  71. if (AllRequestNum === 0)
  72. uni.showLoading();
  73. AllRequestNum++;
  74. } else { // 关闭
  75. if (AllRequestNum > 0) { // 防止数值错乱
  76. AllRequestNum--;
  77. if (AllRequestNum === 0)
  78. uni.hideLoading();
  79. } else {
  80. AllRequestNum = 0;
  81. }
  82. }
  83. }
  84. }
  85. // 登录过期,重置请求计数,强制关闭loading
  86. function LoginExpired(msg) {
  87. AllRequestNum = 0;
  88. uni.showModal({
  89. title: '提示',
  90. content: '登录过期,请重新登录!',
  91. success: function (_res) {
  92. if (_res.confirm) {
  93. uni.navigateTo({
  94. url: '/pages/login/login'
  95. });
  96. } else if (_res.cancel) {
  97. console.log('用户点击取消');
  98. }
  99. }
  100. });
  101. }
  102. //广播弹窗关闭
  103. function dispatchEventGloal(type){
  104. // console.log({type,uni});
  105. uni.$emit(type,{});
  106. }
  107. function notifyException(resArr, additional) {
  108. // console.log({resArr, additional});
  109. openCloseLoading(additional, false);
  110. const [error, res] = resArr;
  111. if (error) {
  112. const errorStr = JSON.stringify(error);
  113. if(errorStr.indexOf('timed out') != -1){
  114. //超时
  115. uni.showModal({
  116. title: '错误提示',
  117. content: '网络连接超时!',
  118. showCancel: false,
  119. success: function (res) {
  120. if (res.confirm) {
  121. dispatchEventGloal(JSON.stringify(error));
  122. }
  123. }
  124. });
  125. }else{
  126. uni.showModal({
  127. title: '错误提示',
  128. content: JSON.stringify(error),
  129. showCancel: false,
  130. success: function (res) {
  131. if (res.confirm) {
  132. dispatchEventGloal(JSON.stringify(error));
  133. }
  134. }
  135. });
  136. }
  137. console.error(`请求错误: ${error.errMsg}`);
  138. return false;
  139. } else if (res.data.code !== 'SUCCESS') {
  140. const { code, msg } = res.data;
  141. console.log('res.data',res.data)
  142. if (code === '410' || code === '401'||code === '403') {
  143. // 当页面被嵌套告诉主页面,token过期
  144. if(window.parent){
  145. console.log('window.parent',window.parent);
  146. window.parent.postMessage('tokenExpired','*');
  147. }
  148. LoginExpired(msg);
  149. } else {
  150. uni.showModal({
  151. title: '错误提示',
  152. content: msg,
  153. showCancel: false,
  154. success: function (res) {
  155. if (res.confirm) {
  156. dispatchEventGloal(msg);
  157. }
  158. },
  159. });
  160. }
  161. console.error(`错误信息: ${msg}`);
  162. return false;
  163. } else if (additional.successMessage) {
  164. let msg = additional.successMessage;
  165. uni.showToast({
  166. title: msg === 'msg' ? res.data.msg : msg,
  167. icon:'none',
  168. duration:1000
  169. });
  170. }
  171. return res.data.data ? res.data.data : true;
  172. }
  173. export {
  174. request,
  175. creatRequest
  176. }