http.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // const baseURL = 'https://console.sxtvs.com.cn'
  2. const baseURL = 'https://baoliao.sxtvs.net'
  3. import { config } from '../utils/config.js'
  4. export const myRequest = options => {
  5. uni.showLoading({
  6. title: '加载中...'
  7. })
  8. return new Promise((resolve, reject) => {
  9. uni.request({
  10. url: `${baseURL}${options.url}`, //接口地址:前缀+方法中传入的地址
  11. method: options.method || 'GET', //请求方法:传入的方法或者默认是“GET”
  12. data: options.data || {}, //传递参数:传入的参数或者默认传递空集合
  13. header: {
  14. 'tenantId': config.tenantId
  15. },
  16. success: (res) => {
  17. //返回的数据(不固定,看后端接口,这里是做了一个判断,如果不为true,用uni.showToast方法提示获取数据失败)
  18. // if (res.data.success != true) {
  19. // return uni.showToast({
  20. // title: '获取数据失败',
  21. // icon: 'none'
  22. // })
  23. // }
  24. // 如果不满足上述判断就输出数据
  25. uni.hideLoading()
  26. resolve(res)
  27. },
  28. // 这里的接口请求,如果出现问题就输出接口请求失败
  29. fail: (err) => {
  30. uni.hideLoading()
  31. console.log(err)
  32. reject(err)
  33. }
  34. })
  35. })
  36. }
  37. //上传文件
  38. export const uploadFile = options => {
  39. uni.showLoading({
  40. title: '加载中...'
  41. })
  42. return new Promise((resolve, reject) => {
  43. console.log(options);
  44. uni.uploadFile({
  45. url: `${baseURL}${options.url}`,
  46. name: 'file',
  47. fileType: options.fileType,
  48. filePath: options.filePath, // 文件
  49. formData: options.formData, // 除文件外其他所有数据,传对象,会默认转换为 FormData
  50. header: {
  51. 'tenantId': config.tenantId
  52. },
  53. success: res => {
  54. uni.hideLoading()
  55. resolve(JSON.parse(res.data))
  56. },
  57. // 这里的接口请求,如果出现问题就输出接口请求失败
  58. fail: (err) => {
  59. uni.hideLoading()
  60. console.log(err)
  61. reject(err)
  62. }
  63. })
  64. })
  65. }