康巴易测肤/伤疤uniapp小程序类
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

163 行
4.1KB

  1. import Vue from "vue";
  2. import store from '@/store'
  3. // 基础
  4. // 根据体验版本和线上版本区分接口地址
  5. const baseUrl = __wxConfig.envVersion !== "release" ? "" : "";
  6. const apiArray = {
  7. // 登录
  8. Login: {
  9. url: baseUrl + "/api/User/Login",
  10. config: {},
  11. },
  12. };
  13. let api = {};
  14. Object.keys(apiArray).forEach((item) => {
  15. api[item] = async function (config = {}, deep = false) {
  16. return new Promise(async (res, rej) => {
  17. let temp = apiArray[item];
  18. let apiconfig = temp.config;
  19. let header = config.header
  20. ? config.header
  21. : apiconfig.header
  22. ? apiconfig.header
  23. : {
  24. "content-type": "application/json;charset:utf-8",
  25. };
  26. // 鉴权-登录检测
  27. if (!(temp.auth === false)) {
  28. // 如果没有用户信息则跳转至登录页
  29. // Replace
  30. // if (!store.state.agentId) {
  31. // setTimeout(() => {
  32. // uni.showToast({
  33. // title: '请先登录',
  34. // icon: 'none',
  35. // duration: 4000,
  36. // })
  37. // }, 500)
  38. // uni.reLaunch({
  39. // url: '/pages/login/login',
  40. // })
  41. // return
  42. // }
  43. // 统一注入token
  44. }
  45. let url = config.url ? config.url : temp.url;
  46. if (config.query) {
  47. url += Vue.prototype.$u.queryParams(config.query);
  48. }
  49. uni.request({
  50. url: url,
  51. data: config.data ? config.data : {},
  52. method: config.method
  53. ? config.method
  54. : apiconfig.method
  55. ? apiconfig.method
  56. : "POST",
  57. timeout: config.timeout
  58. ? config.timeout
  59. : apiconfig.timeout
  60. ? apiconfig.timeout
  61. : 60000,
  62. header: header,
  63. dataType: config.dataType
  64. ? config.dataType
  65. : apiconfig.dataType
  66. ? apiconfig.dataType
  67. : "json",
  68. responseType: config.responseType
  69. ? config.responseType
  70. : apiconfig.responseType
  71. ? apiconfig.responseType
  72. : "text",
  73. sslVerify:
  74. config.sslVerify === false
  75. ? false
  76. : apiconfig.sslVerify === false
  77. ? false
  78. : true,
  79. withCredentials:
  80. config.withCredentials === false
  81. ? false
  82. : apiconfig.withCredentials === true
  83. ? true
  84. : false,
  85. firstIpv4:
  86. config.firstIpv4 === false
  87. ? false
  88. : apiconfig.firstIpv4 === false
  89. ? false
  90. : true,
  91. async success(data) {
  92. // console.log(config.url?config.url:temp.url);
  93. if (data.statusCode === 200) {
  94. console.log("请求数据", {
  95. ...config,
  96. });
  97. console.log("响应数据", {
  98. ...data,
  99. });
  100. let re = data.data;
  101. if (
  102. re.code === 200 ||
  103. re.stateCode === 0 ||
  104. re.code === 106 ||
  105. apiconfig.err === false
  106. ) {
  107. res(re);
  108. } else {
  109. setTimeout(() => {
  110. uni.showToast({
  111. icon: "none",
  112. title: re.msg || re.message,
  113. duration: 3000,
  114. });
  115. }, 500);
  116. res(false);
  117. }
  118. } else {
  119. let str = `${data.statusCode}:${data.data.msg}`;
  120. setTimeout(() => {
  121. uni.showToast({
  122. icon: "none",
  123. title: str,
  124. duration: 3000,
  125. });
  126. }, 500);
  127. res(false);
  128. }
  129. },
  130. fail(err) {
  131. console.log(err);
  132. let str =
  133. "网络错误:" + (err.statusCode ? err.statusCode : err.errMsg);
  134. if (err.errMsg == "request:fail timeout") {
  135. str = "网络异常,请检查网络";
  136. }
  137. setTimeout(() => {
  138. uni.showToast({
  139. icon: "none",
  140. title: str,
  141. duration: 3000,
  142. });
  143. }, 500);
  144. res(false);
  145. },
  146. });
  147. });
  148. };
  149. });
  150. export default {
  151. api,
  152. baseUrl,
  153. };