vue3 ts 请求封装后端接口

embedded/2025/3/31 1:39:43/

一 首页-广告区域-小程序

首页-广告区域-小程序

GET
/home/banner

在这里插入图片描述

1.1 请求封装 首页-广告区域

home.ts

export const getHomeBannerApi = (distributionSite = 1) => {return http<BannerItem[]>({method: 'GET',url: '/home/banner',data: {distributionSite,},})
}

函数定义:

  • getHomeBannerApi 是一个导出的常量函数。
  • 它接收一个可选参数 distributionSite,默认值为 1。

HTTP 请求配置:

  • method: ‘GET’:指定请求方法为 GET。

  • url: ‘/home/banner’:请求的 URL。

  • data: { distributionSite }:请求的参数。

返回值:

  • 函数返回 http<BannerItem[]>(…) 的结果。
{"msg": "string","result": [{"id": "string","imgUrl": "string","hrefUrl": "string","type": 0}]
}

2 定义返回值类型

/** 首页-广告区域数据类型 */
export type BannerItem = {/** 跳转链接 */hrefUrl : string/** id */id : string/** 图片链接 */imgUrl : string/** 跳转类型 */type : number
}

3 方法调用

const bannerList = ref<BannerItem[]>([])const getBannerData = () => {getHomeBannerApi().then((res) => {bannerList.value = res.result})}

二 猜你喜欢-小程序

在这里插入图片描述

1 封装请求

export const getGuessListApi=((data?:PageParams)=>{return http<PageResult<GuessItem[]>>({method:"GET",url:'/home/goods/guessLike',data})
})

函数定义:

  • getGuessListApi 是一个导出的常量函数。
  • 它接收一个可选参数 data,类型为 PageParams。

HTTP 请求配置:

  • method: ‘GET’:指定请求方法为 GET。

  • url: ‘/home/goods/guessLike’:请求的 URL。

  • data:请求的参数(可选)。

返回值:

  • 函数返回 http<PageResult<GuessItem[]>>(…) 的结果。
  • http 是一个泛型函数,<PageResult<GuessItem[]>> 表示期望的响应数据类型是一个分页结果,其中 data 字段是 GuessItem 数组。

2 返回值类型定义

/** 通用分页结果类型 */
export type PageResult<T> = {/** 列表数据 */items: T[]/** 总条数 */counts: number/** 当前页数 */page: number/** 总页数 */pages: number/** 每页条数 */pageSize: number
}
  • PageResult 是一个泛型类型,T 表示列表中每一项的数据类型。
  • 例如,如果 T 是 GuessItem,那么 items 的类型就是 GuessItem[]。

3 请求值类型定义

export type PageParams = {/** 页码:默认值为 1 */page?: number/** 页大小:默认值为 10 */pageSize?: number
}

4 方法调用

const guessList = ref<GuressItem[]>([]);// 分页参数const pageParams : Required<PageParams> = {page: 1,pageSize: 10}const flag = ref(false)const getGuessLike = (() => {if (flag.value) {return uni.showToast({icon: 'none',title: "没有更多数据了"})}getGuessListApi(pageParams).then(res => {guessList.value = guessList.value.concat(res.result.items)if (pageParams.page < res.result.pages) {pageParams.page++} else {flag.value = true}})})

三 商品详情

在这里插入图片描述

1 请求封装

/*** 商品详情* @param id 商品id*/
export const getGoodsByIdAPI = (id: string) => {return http<GoodsResult>({method: 'GET',url: '/goods',data: { id },})
}

2 返回值类型定义

import type { GoodsItem } from './global'/** 商品信息 */
export type GoodsResult = {/** id */id: string/** 商品名称 */name: string/** 商品描述 */desc: string/** 当前价格 */price: number/** 原价 */oldPrice: number/** 商品详情: 包含详情属性 + 详情图片 */details: Details/** 主图图片集合[ 主图图片链接 ] */mainPictures: string[]/** 同类商品[ 商品信息 ] */similarProducts: GoodsItem[]/** sku集合[ sku信息 ] */skus: SkuItem[]/** 可选规格集合备注[ 可选规格信息 ] */specs: SpecItem[]/** 用户地址列表[ 地址信息 ] */userAddresses: AddressItem[]
}/** 商品详情: 包含详情属性 + 详情图片 */
export type Details = {/** 商品属性集合[ 属性信息 ] */properties: DetailsPropertyItem[]/** 商品详情图片集合[ 图片链接 ] */pictures: string[]
}/** 属性信息 */
export type DetailsPropertyItem = {/** 属性名称 */name: string/** 属性值 */value: string
}/** sku信息 */
export type SkuItem = {/** id */id: string/** 库存 */inventory: number/** 原价 */oldPrice: number/** sku图片 */picture: string/** 当前价格 */price: number/** sku编码 */skuCode: string/** 规格集合[ 规格信息 ] */specs: SkuSpecItem[]
}/** 规格信息 */
export type SkuSpecItem = {/** 规格名称 */name: string/** 可选值名称 */valueName: string
}/** 可选规格信息 */
export type SpecItem = {/** 规格名称 */name: string/** 可选值集合[ 可选值信息 ] */values: SpecValueItem[]
}/** 可选值信息 */
export type SpecValueItem = {/** 是否可售 */available: boolean/** 可选值备注 */desc: string/** 可选值名称 */name: string/** 可选值图片链接 */picture: string
}/** 地址信息 */
export type AddressItem = {/** 收货人姓名 */receiver: string/** 联系方式 */contact: string/** 省份编码 */provinceCode: string/** 城市编码 */cityCode: string/** 区/县编码 */countyCode: string/** 详细地址 */address: string/** 默认地址,1为是,0为否 */isDefault: number/** 收货地址 id */id: string/** 省市区 */fullLocation: string
}

3 方法调用

  const goods = ref<GoodsResult>()const getGoodsInfo = (() => {getGoodsByIdAPI(query.id).then(res => {goods.value = res.result})})

四 小程序登录

在这里插入图片描述

1 请求封装

export const postLoginWxMinAPI = (data:LoginParams) => {return http<LoginResult>({method: 'POST',url: '/login/wxMin',data,})
}

2 定义请求值类型

/*** 小程序登录* @param data 请求参数*/
type LoginParams={code:string,encryptedData:string,iv:string
}

3 定义返回值类型


//通用的用户信息
type BaseProfile={/** 用户ID */id: number/** 头像  */avatar: string/** 账户名  */account: string/** 昵称 */nickname?: string
}/** 小程序登录 登录用户信息 */
export type LoginResult = BaseProfile & {/** 手机号 */mobile: string/** 登录凭证 */token: string
}
  • LoginResult 是基于 BaseProfile 的扩展类型,使用 &(相当于javaextend继承) 进行类型合并。
  • 它包含了 BaseProfile 的所有字段,并新增了 mobile 和 token 字段。

4 方法调用

  const onGetphonenumber : UniHelper.ButtonOnGetphonenumber = ((e) => {const encryptedData = e.detail!.encryptedData!const iv = e.detail!.iv!postLoginWxMinAPI({code,encryptedData,iv}).then(res => {console.log(res);loginSuccess(res.result)})})const loginSuccess = (profile : LoginResult) => {// 保存会员信息const memberStore = useMemberStore()memberStore.setProfile(profile)// 成功提示uni.showToast({ icon: 'success', title: '登录成功' })setTimeout(() => {// 页面跳转uni.navigateBack()}, 500)}

五 新建地址

在这里插入图片描述

1 请求封装

/*** 添加收货地址* @param data 请求参数*/
export const postMemberAddressAPI = (data: AddressParams) => {return http({method: 'POST',url: '/member/address',data,})
}

2 参数定义

/** 添加收货地址: 请求参数 */
export type AddressParams = {/** 收货人姓名 */receiver: string/** 联系方式 */contact: string/** 省份编码 */provinceCode: string/** 城市编码 */cityCode: string/** 区/县编码 */countyCode: string/** 详细地址 */address: string/** 默认地址,1为是,0为否 */isDefault: number
}

3 方法调用

// 表单数据

  const form = ref({receiver: '', // 收货人contact: '', // 联系方式fullLocation: '', // 省市区(前端展示)provinceCode: '', // 省份编码(后端参数)cityCode: '', // 城市编码(后端参数)countyCode: '', // 区/县编码(后端参数)address: '', // 详细地址isDefault: 0, // 默认地址,1为是,0为否})
postMemberAddressAPI(form.value)

六 修改地址

1 请求封装

export const putMemberAddressByIdApi=((id:string,data:AddressItem)=>{return http({method:"PUT",url:`/member/address/${id}`,data})
})

2 参数定义

/** 添加收货地址: 请求参数 */
export type AddressParams = {/** 收货人姓名 */receiver: string/** 联系方式 */contact: string/** 省份编码 */provinceCode: string/** 城市编码 */cityCode: string/** 区/县编码 */countyCode: string/** 详细地址 */address: string/** 默认地址,1为是,0为否 */isDefault: number
}export type AddressItem= AddressParams &{id:string,fullLocation:string,
}

3 方法调用

// 表单数据const form = ref({receiver: '', // 收货人contact: '', // 联系方式fullLocation: '', // 省市区(前端展示)provinceCode: '', // 省份编码(后端参数)cityCode: '', // 城市编码(后端参数)countyCode: '', // 区/县编码(后端参数)address: '', // 详细地址isDefault: 0, // 默认地址,1为是,0为否})
putMemberAddressByIdApi(query.id, form.value)

http://www.ppmy.cn/embedded/177305.html

相关文章

【MySQL | 六、索引特性(进一步理解)】

目录 索引的理解索引的作用MySQL与磁盘的IOPage单个Page的分类多个Page的组织B树的特点 B树和B树的区别聚簇索引 VS 非聚簇索引聚簇索引的优缺点非聚簇索引的优缺点 创建索引常见索引分为&#xff1a;主键索引InnoDB主键索引的生成过程&#xff08;1&#xff09;初始化&#xf…

【区块链 + 文化版权】基于区块链的艺术作品版权登记与交易平台 | FISCO BCOS 应用案例

北京奕江科技与信阳艺术职业学院合作&#xff0c; 实施产教融合&#xff0c; 利用区块链技术为艺术作品版权保护与交易打造新平台。平台通过区块链的分布式、透明性和不可篡改性&#xff0c; 为艺术作品的创作、注册、流通及交易等各个环节提供解决方案&#xff0c; 确保作品确…

Redis学习二

Redis和数据库数据一致性问题 Redis作为缓存分两种情形 只读缓存, 只读缓存无需考虑数据更新问题, Redis中有则返回Redis中的数据, Redis无则查询数据库读写缓存 同步直写策略异步缓写策略 数据读取流程: 正常回写Redis代码流程: public Object getDataById(String id) {…

reactor网络模型

一、介绍 1.为什么需要reactor网络模型 1.1 高并发支持 非阻塞I/O&#xff1a;Reactor模型通过非阻塞I/O操作&#xff0c;允许单线程处理多个连接&#xff0c;减少线程切换开销&#xff0c;提升并发能力。 事件驱动&#xff1a;基于事件驱动机制&#xff0c;系统只在有事件发…

【2025】基于springboot+vue的医院在线问诊系统设计与实现(源码、万字文档、图文修改、调试答疑)

基于Spring Boot Vue的医院在线问诊系统设计与实现功能结构图如下&#xff1a; 课题背景 随着互联网技术的飞速发展和人们生活水平的不断提高&#xff0c;传统医疗模式面临着诸多挑战&#xff0c;如患者就医排队时间长、医疗资源分配不均、医生工作压力大等。同时&#xff0c;…

【2025】基于ssm+jsp的二手商城系统设计与实现(源码、万字文档、图文修改、调试答疑)

基于SSMJSP的二手商城系统设计与实现系统功能结构图&#xff1a; 课题背景 随着经济的发展和人们生活水平的提高&#xff0c;二手交易市场日益活跃。人们对于闲置物品的处理方式逐渐从传统的废品回收转变为通过二手交易平台进行再利用。这种交易模式不仅能够帮助用户节省开支&a…

rabbitmq + minio +python 上传文件

功能实现 RabbitMq接收hello里面传来的消息根据消息在 MobileFile里面新建文件新建文件上传到minio python 新建文件 import os path ./MobileFile file_path os.path.join(path,"new_file.txt") with open(file_path, "w") as file: pass转换成…

RabbitMQ 快速入门

目录 为什么有 RabbitMQ&#xff1f;QueueExchange&#xff08;消息分发策略&#xff09;DirectTopicFanoutHeaders 常见的队列类型死信队列 &#xff08;Dead Letter Queue&#xff0c;DLQ&#xff09;应用场景定时任务监控与告警消费者拒绝&#xff08;NACK/Reject&#xff0…