newRequest.js 4.8 KB

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