request.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. export const baseURL = 'https://cloud.long-chi.com/api'
  2. export const request = (options) => {
  3. return new Promise((resolve, reject) => {
  4. uni.request({
  5. url: baseURL + options.url, //接口地址:前缀+方法中传入的地址
  6. method: options.method || 'GET', //请求方法:传入的方法或者默认是“GET”
  7. data: options.data || {}, //传递参数:传入的参数或者默认传递空集合
  8. header: {
  9. 'Authorization': uni.getStorageSync("token") || "" //自定义请求头信息
  10. },
  11. success: (res) => {
  12. //返回的数据(不固定,看后端接口,这里是做了一个判断,如果不为true,用uni.showToast方法提示获取数据失败)
  13. if (res.data.code == 200) {
  14. resolve(res.data)
  15. } else {
  16. if(res.data.code== 401){
  17. uni.navigateTo({
  18. url:'/pages/login/login'
  19. })
  20. }
  21. reject(res.data.msg)
  22. uni.showModal({
  23. title:'提示',
  24. content:res.data.msg,
  25. showCancel:false
  26. })
  27. }
  28. // 如果不满足上述判断就输出数据
  29. },
  30. // 这里的接口请求,如果出现问题就输出接口请求失败
  31. fail: (err) => {
  32. console.log(err)
  33. reject(err)
  34. }
  35. })
  36. })
  37. }
  38. // import {getUserInfo} from '@/api/mine'
  39. // methods: {
  40. // async getUserInfo(){
  41. // try {
  42. // const res = await getUserInfo()
  43. // this.userInfo = res
  44. // console.log('getUserInfo', res)
  45. // // 保存数据
  46. // } catch (err) {
  47. // uni.showToast({ title:err,icon:'none' })
  48. // console.log('getUserInfo', err)
  49. // }
  50. // },
  51. // }