request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { host, device_type } from "./constants";
  2. function showError(error) {
  3. const errorText = error.statusCode !== 200 ? error.errMsg : error.data.msg;
  4. uni.showToast({
  5. title: `发生错误: ${errorText}`,
  6. duration: 2000,
  7. icon: "none",
  8. });
  9. throw new Error(errorText);
  10. }
  11. function getType(data, option) {
  12. // 获取请求方式
  13. let token = uni.getStorageSync("token");
  14. var method = typeof option == "string" ? option : option.method;
  15. var type = {
  16. method,
  17. header: {
  18. // 'content-type': 'application/x-www-form-urlencoded'
  19. "Content-Type": "application/json; charset=utf-8",
  20. "JWT-TOKEN": token,
  21. "XX-Device-Type": device_type,
  22. ...option.header,
  23. },
  24. };
  25. // 判断是否为数组
  26. if (data instanceof Array) {
  27. // 数组不需要展开
  28. type.data = data;
  29. } else if (data instanceof Object) {
  30. type.data = {
  31. ...data,
  32. };
  33. } else {
  34. type.data = data;
  35. }
  36. return type;
  37. }
  38. /**
  39. * @param {Object} url 接口地址
  40. * @param {Object/String} option 接口配置
  41. * option类型为string时等同于{method: xxx}
  42. * @param {String} option.host 接口域名
  43. * @param {String} option.method 接口类型
  44. * @param {Object} data 接口传参
  45. */
  46. function request(url, option, data = {}, noLoading) {
  47. if (!noLoading) {
  48. uni.showLoading();
  49. }
  50. var fullUrl;
  51. if (typeof option == "object" && option.host) {
  52. fullUrl = option.host + url;
  53. } else {
  54. // fullUrl = host + "/api/" + url;
  55. fullUrl = "/api/" + url;
  56. }
  57. var option = {
  58. url: fullUrl,
  59. ...getType(data, option),
  60. };
  61. console.log({
  62. ...option,
  63. header: null,
  64. });
  65. return uni.request(option).then(
  66. (res) => {
  67. if (!noLoading) {
  68. uni.hideLoading();
  69. }
  70. const response = res[1];
  71. if (!response || !response.data) {
  72. uni.showToast({
  73. title: "网络异常,请稍后再试",
  74. icon: "none",
  75. });
  76. }
  77. const code = response.data.code;
  78. if (typeof option == "object" && option.host) {
  79. if (response.statusCode !== 200) {
  80. showError(response);
  81. }
  82. } else {
  83. if (code == 602 || code == 601 || code == 603) {
  84. uni.showModal({
  85. title: "提示",
  86. content: "登录已过期,请重新登录",
  87. showCancel: false,
  88. success: (res) => {
  89. uni.reLaunch({
  90. url: "../login/login",
  91. });
  92. },
  93. });
  94. return;
  95. }
  96. // 判断是否是jinke接口返回的内容
  97. if (response.data.status == 100) return response.data.data;
  98. if (code !== 200) {
  99. showError(response);
  100. // console.error(response)
  101. // uni.showToast({
  102. // title: `获取数据失败: ${response.data.msg}`,
  103. // icon: "none"
  104. // });
  105. }
  106. }
  107. return response.data;
  108. },
  109. (error) => {
  110. console.log(error);
  111. }
  112. );
  113. }
  114. export default request;