newRequest.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import store from '../store'
  2. let service = {
  3. baseURL: store.state.baseURL, // 请求头
  4. header: {
  5. 'Content-Type': "application/x-www-form-urlencoded"
  6. },
  7. // 请求拦截器
  8. interceptors: {
  9. // 安装请求前拦截
  10. request: function(success, iserror) {
  11. service.request.success = success;
  12. service.request.error = iserror;
  13. },
  14. // 安装请求完成后拦截
  15. response: function(success, iserror) {
  16. service.response.success = success;
  17. service.response.error = iserror;
  18. }
  19. },
  20. // 请求前拦截
  21. request: {
  22. // 回调成功执行方法
  23. success: function(e) {
  24. return e
  25. },
  26. // 回调失败
  27. error: function(e) {
  28. return e
  29. }
  30. },
  31. // 请求完成后拦截
  32. response: {
  33. // 回调成功执行方法
  34. success: function(e) {
  35. return e
  36. },
  37. // 回调失败
  38. error: function(e) {
  39. return e
  40. }
  41. },
  42. // 开始请求
  43. open: async function(data) {
  44. return await new Promise(function(resolve, reject) {
  45. let requestData = ''; //保存处理完成后请求数据
  46. let url =''//保存链接地址
  47. // 请求前数据处理
  48. requestData = service.request.success(data);
  49. // #ifdef H5
  50. if(process.env.NODE_ENV === 'development'){
  51. url= requestData.url;
  52. }else{
  53. url=service.baseURL + requestData.url;
  54. }
  55. // #endif
  56. // #ifndef H5
  57. url=service.baseURL + requestData.url;
  58. // #endif
  59. // 数据复制用于请求
  60. uni.request({
  61. url:url,
  62. method: requestData.method || 'GET',
  63. data: requestData.data,
  64. header: data.header || service.header,
  65. success: res => {
  66. if (res.statusCode == 200) {
  67. try {
  68. resolve(service.response.success(res));
  69. } catch (e) {
  70. service.response.error(e)
  71. }
  72. }
  73. },
  74. // 请求失败数据处理
  75. fail: (err) => {
  76. service.response.error(err)
  77. }
  78. });
  79. })
  80. },
  81. // 上传文件
  82. upFilse: async function(data) {
  83. return await new Promise(function(resolve, reject) {
  84. let requestData = ''; //保存处理完成后请求数据
  85. let url = '' //保存请求地址
  86. try {
  87. // 请求前数据处理
  88. requestData = service.request.success(data);
  89. } catch (e) {
  90. // 请求报错拦截
  91. service.request.error(e);
  92. }
  93. // #ifdef H5
  94. if(process.env.NODE_ENV === 'development'){
  95. url= requestData.url;
  96. }else{
  97. url=service.baseURL + requestData.url;
  98. }
  99. // #endif
  100. // #ifndef H5
  101. url=service.baseURL + requestData.url;
  102. // #endif
  103. let config = {
  104. // 上传图片的API
  105. upload_img_url:url
  106. }
  107. let Uploader = {
  108. // 选择图片
  109. choose(num) {
  110. return new Promise((resolve, reject) => {
  111. // #ifndef MP
  112. uni.chooseImage({
  113. count: num,
  114. success(res) {
  115. // 缓存文件路径
  116. resolve(res.tempFilePaths)
  117. },
  118. fail(err) {
  119. uni.showModal({
  120. title: '文件打开错误',
  121. content: '请设置授权文件存储权限',
  122. showCancel: false,
  123. });
  124. reject(err)
  125. }
  126. })
  127. // #endif
  128. // #ifdef MP-WEIXIN
  129. uni.chooseMedia({
  130. count: num,
  131. success(res) {
  132. // 缓存文件路径
  133. resolve(res.tempFiles)
  134. },
  135. fail(err) {
  136. console.log(err,'’1111')
  137. uni.showModal({
  138. title: '文件打开错误',
  139. content: '请设置授权文件存储权限',
  140. showCancel: false,
  141. });
  142. reject(err)
  143. }
  144. })
  145. // #endif
  146. })
  147. },
  148. // 上传图片
  149. upload_one(path) {
  150. return new Promise((resolve, reject) => {
  151. uni.showLoading({
  152. title: '上传中',
  153. mask:true
  154. })
  155. uni.uploadFile({
  156. url: config.upload_img_url, //仅为示例,非真实的接口地址
  157. filePath: path,
  158. name: 'file',
  159. header: {
  160. "token": uni.getStorageSync('token')
  161. },
  162. success: (uploadFileRes) => {
  163. if ("string" === typeof uploadFileRes.data) {
  164. resolve(JSON.parse(uploadFileRes.data).data)
  165. } else {
  166. resolve(uploadFileRes.data.data)
  167. }
  168. },
  169. fail(err) {
  170. console.log(err,"错误")
  171. },
  172. complete() {
  173. uni.hideLoading()
  174. }
  175. });
  176. })
  177. },
  178. // 上传多个图片
  179. upload(path_arr) {
  180. let num = path_arr.length;
  181. return new Promise(async (resolve, reject) => {
  182. let img_urls = []
  183. for (let i = 0; i < num; i++) {
  184. // #ifdef MP
  185. let url=path_arr[i].tempFilePath
  186. // #endif
  187. // #ifndef MP
  188. let url = path_arr[i]
  189. // #endif
  190. let img_url = await this.upload_one(url);
  191. img_urls.push(img_url)
  192. };
  193. resolve(img_urls)
  194. })
  195. },
  196. // 触发上传图片事件
  197. choose_and_upload(num) {
  198. return new Promise(async (resolve, reject) => {
  199. // 选择图片
  200. let path_arr = await this.choose(num);
  201. // 上传图片
  202. await this.upload(path_arr).then((e) => {
  203. resolve(e);
  204. }).catch((e) => {
  205. reject(e)
  206. });
  207. })
  208. }
  209. };
  210. Uploader.choose_and_upload(1).then((e) => {
  211. resolve(e);
  212. }).catch((e) => {
  213. console.log(e);
  214. })
  215. })
  216. },
  217. };
  218. export default service