newRequest.js 4.0 KB

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