index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import {mgop} from "@aligov/jssdk-mgop";
  2. import zlbConfig from "@/common/js/zlbConfig";
  3. import {httpApi} from "@/common/js/baseUrl";
  4. import {handleHttpError, handleNormalResponse} from "@/utils/fetch/responseHandler";
  5. import handleReqLoading from "@/utils/fetch/handleReqLoading";
  6. import {handleRequest} from "@/utils/fetch/requestHandler";
  7. function download(url, type = "get", data = {}) {
  8. return new Promise((resolve, reject) => {
  9. uni.request({
  10. url: url,
  11. method: type,
  12. responseType: "arraybuffer",
  13. header: {
  14. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  15. src: "wechat",
  16. },
  17. data: data,
  18. timeout: 10000,
  19. success: function (res) {
  20. console.log("返回结果:", res);
  21. if (res.statusCode === 200) {
  22. resolve(res);
  23. } else {
  24. reject(); // 接口不正常 reject
  25. uni.showToast({title: "服务异常", icon: "none"});
  26. }
  27. },
  28. fail: function (err) {
  29. uni.showToast({title: "服务异常", icon: "none"});
  30. reject(err);
  31. },
  32. });
  33. });
  34. }
  35. // 通用版upload
  36. function commonUpload(url, data, config = {}) {
  37. return new Promise((resolve, reject) => {
  38. uni.uploadFile({
  39. url: httpApi + url,
  40. ...data,
  41. ...config,
  42. header: {
  43. "content-type": "multipart/form-data",
  44. Token: '1'
  45. },
  46. // 设置请求的 header
  47. success: function (res) {
  48. },
  49. fail: function (res) {
  50. uni.showToast({
  51. title: "服务异常",
  52. icon: "none",
  53. duration: 2000,
  54. });
  55. reject();
  56. },
  57. });
  58. });
  59. }
  60. function index(url, type = "get", data = {}, dataType) {
  61. return new Promise((resolve, reject) => {
  62. (url && url.startsWith("mgop.alibaba")) ? handleMgop(url, type, data, dataType, resolve, reject) : handleFetch(url, type, data, dataType, resolve, reject);
  63. });
  64. }
  65. function handleMgop(url, type, data, dataType, resolve, reject) {
  66. let extraHeader = {};
  67. /* 当请求头 isTestUrl 为 "1" 时,使用联调环境 */
  68. extraHeader.isTestUrl = "1";
  69. mgop({
  70. api: url, // 必填
  71. // host: 'https://mapi.zjzwfw.gov.cn/',
  72. dataType: "JSON",
  73. type: type,
  74. data: data,
  75. appKey: zlbConfig.appKey, // 必填
  76. header: {
  77. "Content-Type":
  78. dataType === "form"
  79. ? "application/x-www-form-urlencoded"
  80. : "application/json",
  81. src: "wechat", // 小程序免登陆
  82. ...extraHeader,
  83. },
  84. onSuccess: (res) => {
  85. const code = res.ret[0].split("::")[0];
  86. if (code === "1000") {
  87. if (
  88. res.data.code === 0 ||
  89. res.data.code === "0" ||
  90. res.data.status === 0 ||
  91. res.data.status === "1" //高德地图
  92. ) {
  93. resolve(res.data);
  94. } else {
  95. //特殊处理下,获取门牌数据的接口code是1 ,是业务错误
  96. if (res.api === "mgop.alibaba.digitalDoorplate.getHomePageOneDoorplateFullInfo") {
  97. resolve(res.data);
  98. } else {
  99. reject(res);
  100. if (res.data.message || res.data.msg) {
  101. uni.showToast({
  102. title: res.data.message || res.data.msg,
  103. icon: "none",
  104. duration: 2000,
  105. });
  106. }
  107. }
  108. }
  109. } else {
  110. reject(res); // 接口不正常 reject
  111. uni.showToast({
  112. title: "服务异常",
  113. icon: "none",
  114. duration: 2000,
  115. });
  116. }
  117. },
  118. onFail: (err) => {
  119. reject(err);
  120. },
  121. });
  122. }
  123. function handleFetch(url, type, data, dataType, resolve, reject) {
  124. let header = {
  125. "Content-Type":
  126. dataType === "form"
  127. ? "application/x-www-form-urlencoded"
  128. : "application/json",
  129. Token: '1'
  130. };
  131. uni.request({
  132. url: httpApi + url,
  133. method: type,
  134. header,
  135. data: data,
  136. timeout: 60000,
  137. success: function (res) {
  138. if (res.statusCode === 200) {
  139. if (
  140. res.data.code === 0 ||
  141. res.data.code === "0" ||
  142. res.data.status === 0 ||
  143. res.data.status === "1" //高德地图
  144. ) {
  145. resolve(res.data);
  146. } else {
  147. reject();
  148. if (res.data.message || res.data.msg) {
  149. uni.showToast({
  150. title: res.data.message || res.data.msg,
  151. icon: "none",
  152. duration: 2000,
  153. });
  154. }
  155. }
  156. } else {
  157. reject(); // 接口不正常 reject
  158. uni.showToast({
  159. title: "服务异常",
  160. icon: "none",
  161. duration: 2000,
  162. });
  163. }
  164. },
  165. fail: function (err) {
  166. uni.showToast({
  167. title: "服务异常",
  168. icon: "none",
  169. duration: 2000,
  170. });
  171. reject(err);
  172. },
  173. complete: function () {
  174. },
  175. });
  176. }
  177. //export {index, download, commonUpload};
  178. /**
  179. * 封装请求
  180. * @param url 请求地址
  181. * @param type 请求方式
  182. * @param data 请求参数
  183. * @param dataType 请求数据类型
  184. */
  185. export const myFetch = async (url, type = "get", data = {}, dataType) => {
  186. const reqConfig = {
  187. url: httpApi + url,
  188. method: type,
  189. data: data,
  190. dataType: dataType,
  191. timeout: 60000,
  192. header: {
  193. 'content-type': 'application/json',
  194. Token: '1'
  195. },
  196. }
  197. try {
  198. handleReqLoading(true);
  199. const response = await handleRequest(reqConfig);
  200. const responseData = handleNormalResponse(response);
  201. handleReqLoading(false);
  202. //返回一个promise对象
  203. return responseData;
  204. }catch (err) {
  205. handleReqLoading(false);
  206. return handleHttpError(err);
  207. }
  208. }