康巴易测肤/伤疤uniapp小程序类
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

170 lignes
4.4KB

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