index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. appId:zlbConfig.appId,
  77. header: {
  78. "Content-Type":
  79. dataType === "form"
  80. ? "application/x-www-form-urlencoded"
  81. : "application/json",
  82. src: "wechat", // 小程序免登陆
  83. ...extraHeader,
  84. },
  85. onSuccess: (res) => {
  86. const code = res.ret[0].split("::")[0];
  87. if (code === "1000") {
  88. if (
  89. res.data.code === 0 ||
  90. res.data.code === "0" ||
  91. res.data.status === 0 ||
  92. res.data.status === "1" //高德地图
  93. ) {
  94. resolve(res.data);
  95. } else {
  96. //特殊处理下,获取门牌数据的接口code是1 ,是业务错误
  97. if (res.api === "mgop.alibaba.digitalDoorplate.getHomePageOneDoorplateFullInfo") {
  98. resolve(res.data);
  99. } else {
  100. reject(res);
  101. if (res.data.message || res.data.msg) {
  102. uni.showToast({
  103. title: res.data.message || res.data.msg,
  104. icon: "none",
  105. duration: 2000,
  106. });
  107. }
  108. }
  109. }
  110. } else {
  111. reject(res); // 接口不正常 reject
  112. uni.showToast({
  113. title: "服务异常",
  114. icon: "none",
  115. duration: 2000,
  116. });
  117. }
  118. },
  119. onFail: (err) => {
  120. reject(err);
  121. },
  122. });
  123. }
  124. function handleFetch(url, type, data, dataType, resolve, reject) {
  125. let header = {
  126. "Content-Type":
  127. dataType === "form"
  128. ? "application/x-www-form-urlencoded"
  129. : "application/json",
  130. Token: '1'
  131. };
  132. uni.request({
  133. url: httpApi + url,
  134. method: type,
  135. header,
  136. data: data,
  137. timeout: 60000,
  138. success: function (res) {
  139. if (res.statusCode === 200) {
  140. if (
  141. res.data.code === 0 ||
  142. res.data.code === "0" ||
  143. res.data.status === 0 ||
  144. res.data.status === "1" //高德地图
  145. ) {
  146. resolve(res.data);
  147. } else {
  148. reject();
  149. if (res.data.message || res.data.msg) {
  150. uni.showToast({
  151. title: res.data.message || res.data.msg,
  152. icon: "none",
  153. duration: 2000,
  154. });
  155. }
  156. }
  157. } else {
  158. reject(); // 接口不正常 reject
  159. uni.showToast({
  160. title: "服务异常",
  161. icon: "none",
  162. duration: 2000,
  163. });
  164. }
  165. },
  166. fail: function (err) {
  167. uni.showToast({
  168. title: "服务异常",
  169. icon: "none",
  170. duration: 2000,
  171. });
  172. reject(err);
  173. },
  174. complete: function () {
  175. },
  176. });
  177. }
  178. //export {index, download, commonUpload};
  179. /**
  180. * 封装请求
  181. * @param url 请求地址
  182. * @param type 请求方式
  183. * @param data 请求参数
  184. * @param dataType 请求数据类型
  185. */
  186. export const myFetch = async (url, type = "get", data = {}, dataType) => {
  187. const reqConfig = {
  188. url: httpApi + url,
  189. method: type,
  190. data: data,
  191. dataType: dataType,
  192. timeout: 60000,
  193. header: {
  194. 'content-type': 'application/json',
  195. Token: '1',
  196. appId:'2002407848',
  197. },
  198. }
  199. try {
  200. handleReqLoading(true);
  201. const response = await handleRequest(reqConfig);
  202. const responseData = handleNormalResponse(response);
  203. handleReqLoading(false);
  204. //返回一个promise对象
  205. return responseData;
  206. }catch (err) {
  207. handleReqLoading(false);
  208. return handleHttpError(err);
  209. }
  210. }