53 lines
2.2 KiB
TypeScript
53 lines
2.2 KiB
TypeScript
import {defineStore} from "pinia";
|
|||
|
|
import {ref} from "vue";
|
||
|
|
import type {ApiAxiosResponse, UserType} from "@/stores/requestFile";
|
||
|
|
import {instance, 用户_登陆} from "@/stores/请求基础文件";
|
||
|
|
import type {typeGoodsItem, 商品列表类型} from "@/stores/types/烟花商品类型";
|
||
|
|
|
||
|
|
export const useMainXiaoStore = defineStore("MainXiaoStore", () => {
|
||
|
|
let mini_openid = ref<null | string>(null); //用户的小程序openid
|
||
|
|
let mini_unionid = ref<null | string>(null); // 用户的小程序unioid
|
||
|
|
let superior_unionid = ref<null | string>(null); // 用户的小程序unioid
|
||
|
|
let self_user_img = ref<null | string>(null);
|
||
|
|
let self_user_nick = ref<null | string>(null);
|
||
|
|
let goods_list_data = ref<商品列表类型>();
|
||
|
|
let location_goods_list_data = ref<商品列表类型>();
|
||
|
|
let goodsCurrentData = ref<typeGoodsItem>();
|
||
|
|
const wechatLogin = async (scene: number | undefined, superior_unionid: string, shareTicket: string | undefined): Promise<UserType | undefined> => {
|
||
|
|
try {
|
||
|
|
// 1. 获取 code
|
||
|
|
let loginSuccessCallbackResult = await wx.login();
|
||
|
|
|
||
|
|
let code = loginSuccessCallbackResult.code;
|
||
|
|
// 2. 构造请求参数
|
||
|
|
const params: any = {code, shareTicket, scene, superior_unionid};
|
||
|
|
// 3. 如果是群场景,获取分享信息
|
||
|
|
if (scene === 1044 || shareTicket) {
|
||
|
|
const share = await new Promise<UniNamespace.GetGroupEnterInfoSuccessCallbackResult>((resolve) => {
|
||
|
|
uni.getGroupEnterInfo({
|
||
|
|
needGroupOpenID: true, success: (result) => {
|
||
|
|
resolve(result);
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
params.iv = share.iv;
|
||
|
|
params.encryptedData = share.encryptedData;
|
||
|
|
}
|
||
|
|
let data = await instance.post<ApiAxiosResponse<Array<string>, UserType>>(用户_登陆, params);
|
||
|
|
mini_openid.value = data.data!.resData.dictData.self_mini_openid;
|
||
|
|
mini_unionid.value = data.data!.resData.dictData.self_user_unionid;
|
||
|
|
return data.data!.resData.dictData; // 返回用户数据
|
||
|
|
} catch (err) {
|
||
|
|
console.error("微信登录失败:", err);
|
||
|
|
throw err; // 或返回默认值
|
||
|
|
}
|
||
|
|
};
|
||
|
|
return {goods_list_data, goodsCurrentData, location_goods_list_data};
|
||
|
|
},
|
||
|
|
{
|
||
|
|
persist: {
|
||
|
|
pick: ["goods_list_data", "location_goods_list_data"],
|
||
|
|
},
|
||
|
|
},
|
||
|
|
);
|