newRequest.js 4.1 KB

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