http.js 1.8 KB

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