feat(用户): 添加动态码生成功能

- 新增 DynamicCodeResponse 类型定义
- 添加 generateDynamicCode API 方法
- 在首页登录流程中调用动态码生成接口
This commit is contained in:
dzq 2025-11-06 15:23:12 +08:00
parent 8e031231c1
commit a0df240aaf
3 changed files with 22 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { http } from "@/http/http"; import { http } from "@/http/http";
import type { CurrentUserResponseData, WxUserDTO } from './types'; import type { CurrentUserResponseData, WxUserDTO, DynamicCodeResponse } from './types';
/** 获取当前登录用户详情 */ /** 获取当前登录用户详情 */
export async function getCurrentUserApi() { export async function getCurrentUserApi() {
@ -9,3 +9,8 @@ export async function getCurrentUserApi() {
export async function mpCodeToOpenId(code: string) { export async function mpCodeToOpenId(code: string) {
return await http.get<WxUserDTO>("wx/mpCodeToOpenId", { code }); return await http.get<WxUserDTO>("wx/mpCodeToOpenId", { code });
} }
/** 根据openid获取动态码 */
export async function generateDynamicCode(openid: string) {
return await http.get<DynamicCodeResponse>("wx/generateDynamicCode", { openid });
}

View File

@ -27,3 +27,11 @@ export interface WxUserDTO {
/** 备注 */ /** 备注 */
remark?: string; remark?: string;
} }
/** 动态码响应数据 */
export interface DynamicCodeResponse {
/** 动态码 */
dynamicCode: string;
/** 有效时间(分钟) */
validityMinutes: string;
}

View File

@ -6,7 +6,7 @@ import { useCartStore } from '@/pinia/stores/cart'
import { getShopListApi } from '@/api/shop' import { getShopListApi } from '@/api/shop'
import type { ShopEntity } from '@/api/shop/types' import type { ShopEntity } from '@/api/shop/types'
import ProductContainer from './components/product-container.vue'; import ProductContainer from './components/product-container.vue';
import { mpCodeToOpenId } from '@/api/users' import { generateDynamicCode, mpCodeToOpenId } from '@/api/users'
import { toHttpsUrl } from '@/utils' import { toHttpsUrl } from '@/utils'
definePage({ definePage({
@ -32,7 +32,13 @@ onMounted(async () => {
provider: 'weixin', //使 provider: 'weixin', //使
success: function (loginRes) { success: function (loginRes) {
mpCodeToOpenId(loginRes.code).then((wxUser) => { mpCodeToOpenId(loginRes.code).then((wxUser) => {
console.log('wxUser:', wxUser) console.log('wxUser:', wxUser);
wxStore.setOpenid(wxUser.data.openid);
generateDynamicCode(wxUser.data.openid).then((dynamicCode) => {
console.log('dynamicCode:', dynamicCode);
}).catch((e) => {
console.error('generateDynamicCode error:', e)
})
}).catch((e) => { }).catch((e) => {
console.error('mpCodeToOpenId error:', e) console.error('mpCodeToOpenId error:', e)
}) })