request.js 5.0 KB

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