index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // 服务器地址 开发环境 生产环境
  2. const REQUESTURL = {
  3. development: "",
  4. production: ""
  5. };
  6. // 请求数据拦截
  7. const REQUESTINTERCPTOR = function(request) {
  8. var token=uni.getStorageSync('token') || '';
  9. request['header'] = {
  10. 'Content-Type': 'application/x-www-form-urlencoded',
  11. 'Authorization': token
  12. }
  13. return request
  14. }
  15. // 接受数据拦截
  16. const RESPONSEINTERCPTOR = function(response) {
  17. return response;
  18. }
  19. // 报错拦截
  20. const ERRINTERCPTOR = function(err) {
  21. uni.showModal({
  22. title:"警告",
  23. content:`请求失败:${err.message || JSON.stringify(err)}`,
  24. confirmText:"关闭",
  25. showCancel:false,
  26. confirmColor:"#fbbd08"
  27. })
  28. return err
  29. }
  30. /**
  31. * request
  32. * @param {Object} options 请求参数
  33. */
  34. export default function(options) {
  35. // 开发环境 生产环境
  36. if (options.url) options.url = process.env.NODE_ENV == "development" ? REQUESTURL.development + options.url :
  37. REQUESTURL.production + options.url;
  38. return new Promise((resolve, reject) => {
  39. uni.request({
  40. ...REQUESTINTERCPTOR(options),
  41. success: res => resolve(RESPONSEINTERCPTOR(res)),
  42. fail: err => reject(ERRINTERCPTOR(err))
  43. })
  44. })
  45. }