newRequest.js 5.1 KB

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