newRequest.js 4.5 KB

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