request.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. console.log({data,url});
  27. return request(url, { method: obj.method, data }, additional);
  28. } else {
  29. uni.showModal({
  30. title: '错误提示',
  31. content: '请求的接口未定义!',
  32. showCancel: false
  33. });
  34. }
  35. }
  36. /**
  37. * url: 请求的url
  38. * options: uni.request的一系列参数,常用的method和data
  39. * additional: { successMessage、needLoading }
  40. */
  41. function request(url, options, additional) {
  42. if (additional.needLoading === undefined) additional.needLoading = true;
  43. // 加载中提示打开
  44. openCloseLoading(additional, true);
  45. // 模拟token,做登录的同学将这行代码移到登录成功函数中.
  46. // uni.setStorageSync('token', 'gfkZlq36bBPefzvIoukl5exPJ5/jkil2gi9fNPTABSk=');
  47. const token = uni.getStorageSync('token');
  48. return uni.request({
  49. url: `${BASE_URL}${url}`,
  50. ...options,
  51. header: {
  52. token: token ? token : null,
  53. 'Content-Type': 'application/json',
  54. }
  55. }).then((res) => notifyException(res, additional))
  56. .catch((error) => {
  57. if ('stack' in error && 'message' in error) {
  58. uni.showModal({
  59. title: '错误提示',
  60. content: '服务器异常,请稍后重试!',
  61. showCancel: false
  62. });
  63. console.error(`请求错误: ${url} ${error.message}`);
  64. }
  65. return error;
  66. });
  67. }
  68. // 加载中提示打开/关闭
  69. function openCloseLoading(additional, isOpen) {
  70. if (additional.needLoading) {
  71. if (isOpen) { // 打开(如果已经打开不必重复打开)
  72. if (AllRequestNum === 0)
  73. uni.showLoading();
  74. AllRequestNum++;
  75. } else { // 关闭
  76. if (AllRequestNum > 0) { // 防止数值错乱
  77. AllRequestNum--;
  78. if (AllRequestNum === 0)
  79. uni.hideLoading();
  80. } else {
  81. AllRequestNum = 0;
  82. }
  83. }
  84. }
  85. }
  86. // 登录过期,重置请求计数,强制关闭loading
  87. function LoginExpired(msg) {
  88. AllRequestNum = 0;
  89. uni.showModal({
  90. title: '提示',
  91. content:msg,
  92. showCancel:false,
  93. success: function (_res) {
  94. if (_res.confirm) {
  95. uni.navigateTo({
  96. url: '/pages/login/login'
  97. });
  98. }
  99. }
  100. });
  101. }
  102. function notifyException(resArr, additional) {
  103. openCloseLoading(additional, false);
  104. const [error, res] = resArr;
  105. if (error) {
  106. uni.showModal({
  107. title: '错误提示',
  108. content: JSON.stringify(error),
  109. showCancel: false
  110. });
  111. console.error(`请求错误: ${error.errMsg}`);
  112. return false;
  113. } else if (res.data.code !== 'SUCCESS') {
  114. const { code, msg } = res.data;
  115. console.log('res.data',res.data);
  116. if (code === '410' || code === '401') {
  117. LoginExpired(msg);
  118. } else if (code === '403') {
  119. uni.showModal({
  120. title: '提示',
  121. content: '登录过期,请重新登录!',
  122. success: function (_res) {
  123. if (_res.confirm) {
  124. LoginExpired();
  125. } else if (_res.cancel) {
  126. console.log('用户点击取消');
  127. }
  128. }
  129. });
  130. } else {
  131. uni.showModal({
  132. title: '错误提示',
  133. content: msg,
  134. showCancel: false
  135. });
  136. }
  137. console.error(`错误信息: ${msg}`);
  138. return false;
  139. } else if (additional.successMessage) {
  140. let msg = additional.successMessage;
  141. uni.showToast({
  142. title: msg === 'msg' ? res.data.msg : msg,
  143. icon:'none',
  144. duration:1000
  145. });
  146. }
  147. return res.data.data ? res.data.data : true;
  148. }
  149. export {
  150. request,
  151. creatRequest
  152. }