request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }
  56. var option = {
  57. url: fullUrl,
  58. ...getType(data, option),
  59. };
  60. console.log({
  61. ...option,
  62. header: null,
  63. });
  64. return uni.request(option).then(
  65. (res) => {
  66. if (!noLoading) {
  67. uni.hideLoading();
  68. }
  69. const response = res[1];
  70. if (!response || !response.data) {
  71. uni.showToast({
  72. title: "网络异常,请稍后再试",
  73. icon: "none",
  74. });
  75. }
  76. const code = response.data.code;
  77. if (typeof option == "object" && option.host) {
  78. if (response.statusCode !== 200) {
  79. showError(response);
  80. }
  81. } else {
  82. if (code == 602 || code == 601 || code == 603) {
  83. uni.showModal({
  84. title: "提示",
  85. content: "登录已过期,请重新登录",
  86. showCancel: false,
  87. success: (res) => {
  88. uni.reLaunch({
  89. url: "../login/login",
  90. });
  91. },
  92. });
  93. return;
  94. }
  95. // 判断是否是jinke接口返回的内容
  96. if (response.data.status == 100) return response.data.data;
  97. if (code !== 200) {
  98. showError(response);
  99. // console.error(response)
  100. // uni.showToast({
  101. // title: `获取数据失败: ${response.data.msg}`,
  102. // icon: "none"
  103. // });
  104. }
  105. }
  106. return response.data;
  107. },
  108. (error) => {
  109. console.log(error);
  110. }
  111. );
  112. }
  113. export default request;