当前位置:   article > 正文

UniApp+Vue3.2+ts实现请求API配置_uni+vue3+ts调用uni-api的api

uni+vue3+ts调用uni-api的api

1.下载luch-request请求库,官网

npm i luch-request -S

2.在utils文件夹下创建request.ts

  1. /**
  2. * @name: luch-request uni-app跨平台请求库
  3. * @description: https://www.quanzhan.co/luch-request/
  4. * @author: tjp
  5. * @time: 2022-10-09 15:57:24
  6. */
  7. import Request, {HttpResponse, HttpRequestConfig, HttpError} from 'luch-request'
  8. // 本地环境地址
  9. const BASE_API = 'http://localhost:8080'
  10. // 局域网环境地址
  11. // const BASE_API = ''
  12. // 线上地址
  13. // const BASE_API = ''
  14. const http = new Request({
  15. //设置请求的base url
  16. baseURL: BASE_API,
  17. //超时时长5分钟
  18. timeout: 300000,
  19. header: {
  20. 'Content-Type': 'application/json',
  21. 'x-token': uni.getStorageSync('x-token') ? uni.getStorageSync('x-token') : ''
  22. }
  23. })
  24. //请求拦截器
  25. http.interceptors.request.use(
  26. (config: HttpRequestConfig) => {
  27. // 拦截添加token
  28. config.header = {
  29. 'x-token': uni.getStorageSync('x-token') ? uni.getStorageSync('x-token') : ''
  30. }
  31. return config
  32. },
  33. (error: any) => {
  34. return Promise.resolve(error)
  35. }
  36. )
  37. // 响应拦截器
  38. http.interceptors.response.use(
  39. (response: HttpResponse) => {
  40. switch (response.config.method) {
  41. case 'GET':
  42. return formatData(response.data)
  43. case 'POST':
  44. // 登录接口保存token
  45. // ————————————————待修改—————————————————————
  46. if (response.config.url === '/user/login') {
  47. uni.setStorageSync('x-token', response.data.data['x-token'])
  48. return formatData(response.data)
  49. } else if (response.config.url === '/api/user_withdraw/apply') {
  50. return formatData(response)
  51. }
  52. return formatData(response.data)
  53. default:
  54. return formatData(response.data)
  55. }
  56. },
  57. (error: HttpError) => {
  58. console.log('响应拦截器错误捕获', error)
  59. return Promise.resolve(error)
  60. }
  61. )
  62. function formatData(data: any) {
  63. switch (data.code) {
  64. case 200:
  65. return data.data
  66. case 400:
  67. uni.showToast({
  68. title: data.msg,
  69. icon: 'none'
  70. })
  71. return Promise.reject(data)
  72. case 401:
  73. uni.showToast({
  74. title: data.msg,
  75. icon: 'none'
  76. })
  77. return Promise.reject(data)
  78. case 405:
  79. // 一般是交易查询不到
  80. return Promise.reject(data)
  81. default:
  82. return data
  83. }
  84. }
  85. export default http

3.创建TestApi.ts文件

创建api文件夹,将所有的请求放在此文件夹中,在该文件夹中创建test.ts文件

  1. /**
  2. @name: 示例
  3. @description: 示例请求api
  4. @author: tjp
  5. @time: 2022-10-09 15:53:56
  6. */
  7. import http from '@/utils/request'
  8. // GET
  9. /*export function getInformation() {
  10. return http.request({
  11. url: '/api/common/config',
  12. method: 'GET'
  13. })
  14. }*/
  15. // POST
  16. export function login(data) {
  17. return http.request({
  18. url: '/user/login',
  19. method: 'POST',
  20. data
  21. })
  22. }
  23. // 指定类型
  24. /*export function switchLanguage(data: types.languageTypes) {
  25. return http.request({
  26. url: '/api/common/language',
  27. method: 'POST',
  28. data
  29. })
  30. }*/
  31. // 类型
  32. /*interface userLoginTypes {
  33. address: string
  34. sign: string
  35. code?: string | boolean
  36. }
  37. interface languageTypes {
  38. language: string
  39. }
  40. export { userLoginTypes, languageTypes }*/

4.在页面中请求测试

  1. <!--
  2. @name: 首页
  3. @description: 首页主界面
  4. @author: tjp
  5. @time: 2022-10-09 14:36:26
  6. -->
  7. <template>
  8. <view>
  9. <text>首页</text>
  10. <div>{{ token }}</div>
  11. </view>
  12. </template>
  13. <script setup lang="ts">
  14. import {ref} from 'vue'
  15. import {login} from '@/api/TestApi'
  16. /*请求测试*/
  17. const token = ref('')
  18. const loginTest = async () => {
  19. const res = await login({loginName: 'test1', loginPass: '123456'})
  20. token.value=String(res)
  21. console.log(res)
  22. console.log(token)
  23. }
  24. onLoad(() => {
  25. loginTest()
  26. console.log("homestorage",uni.getStorageSync("USER_INFORMATION"))
  27. })
  28. onMounted(() => {
  29. })
  30. </script>
  31. <style scoped lang="scss">
  32. </style>


Master-Tang资源文档服务器http://www.tangjp.fun:8080/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/578115
推荐阅读
相关标签
  

闽ICP备14008679号