newRequest.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. //console.log(data);
  48. // 请求前数据处理
  49. requestData = service.request.success(data);
  50. url=service.baseURL + requestData.url;
  51. console.log(service.baseURL,'service.baseURL')
  52. uni.request({
  53. url:url,
  54. method: requestData.method || 'GET',
  55. data: requestData.data,
  56. header: data.header || service.header,
  57. success: res => {
  58. if (res.statusCode == 200) {
  59. try {
  60. resolve(service.response.success(res));
  61. } catch (e) {
  62. service.response.error(e)
  63. }
  64. }
  65. if (res.statusCode == 401) {
  66. try {
  67. resolve(service.response.success(res));
  68. } catch (e) {
  69. service.response.error(e.msg)
  70. }
  71. }
  72. if (res.statusCode == 402) {
  73. resolve(res.data);
  74. }
  75. },
  76. // 请求失败数据处理
  77. fail: (err) => {
  78. service.response.error(err)
  79. }
  80. });
  81. })
  82. },
  83. // 上传文件
  84. upFilse: async function(data) {
  85. return await new Promise(function(resolve, reject) {
  86. let requestData = ''; //保存处理完成后请求数据
  87. let url = '' //保存请求地址
  88. try {
  89. // 请求前数据处理
  90. requestData = service.request.success(data);
  91. } catch (e) {
  92. // 请求报错拦截
  93. service.request.error(e);
  94. }
  95. // #ifdef H5
  96. if(process.env.NODE_ENV === 'development'){
  97. url= requestData.url;
  98. console.log('开发环境')
  99. }else{
  100. url=service.baseURL + requestData.url;
  101. console.log('生产环境')
  102. }
  103. // #endif
  104. // #ifndef H5
  105. url=service.baseURL + requestData.url;
  106. // #endif
  107. let config = {
  108. // 上传图片的API
  109. upload_img_url:url
  110. }
  111. let Uploader = {
  112. // 选择图片
  113. choose(num) {
  114. return new Promise((resolve, reject) => {
  115. uni.chooseImage({
  116. count: num,
  117. success(res) {
  118. // console.log(res);
  119. // 缓存文件路径
  120. resolve(res.tempFilePaths)
  121. },
  122. fail(err) {
  123. console.log(err)
  124. reject(err)
  125. }
  126. })
  127. })
  128. },
  129. // 上传图片
  130. upload_one(path) {
  131. return new Promise((resolve, reject) => {
  132. uni.showLoading({
  133. title: '上传中'
  134. })
  135. uni.uploadFile({
  136. url: config.upload_img_url, //仅为示例,非真实的接口地址
  137. filePath: path,
  138. name: 'file',
  139. header: {
  140. "Authori-zation": 'Bearer ' + uni.getStorageSync('token')
  141. },
  142. success: (uploadFileRes) => {
  143. if ("string" === typeof uploadFileRes.data) {
  144. resolve(JSON.parse(uploadFileRes.data).data)
  145. } else {
  146. resolve(uploadFileRes.data.data)
  147. }
  148. },
  149. complete() {
  150. uni.hideLoading()
  151. }
  152. });
  153. })
  154. },
  155. // 上传多个图片
  156. upload(path_arr) {
  157. let num = path_arr.length;
  158. return new Promise(async (resolve, reject) => {
  159. let img_urls = []
  160. for (let i = 0; i < num; i++) {
  161. let img_url = await this.upload_one(path_arr[i]);
  162. img_urls.push(img_url)
  163. };
  164. resolve(img_urls)
  165. })
  166. },
  167. // 触发上传图片事件
  168. choose_and_upload(num) {
  169. return new Promise(async (resolve, reject) => {
  170. // 选择图片
  171. let path_arr = await this.choose(num);
  172. // 上传图片
  173. await this.upload(path_arr).then((e) => {
  174. resolve(e);
  175. }).catch((e) => {
  176. reject(e)
  177. });
  178. })
  179. }
  180. };
  181. Uploader.choose_and_upload(1).then((e) => {
  182. resolve(e);
  183. }).catch((e) => {
  184. console.log(e);
  185. })
  186. })
  187. },
  188. };
  189. export default service