feat(微信登录): 添加根据cid获取corpid功能并完善微信登录流程

添加getCorpidById接口用于根据cid查询corpid
修改wxMpCallback方法接收corpid参数
在页面加载时获取corpid并用于微信登录流程
This commit is contained in:
dzq 2025-12-03 15:43:53 +08:00
parent 918bc53059
commit 022d307271
3 changed files with 60 additions and 50 deletions

View File

@ -78,4 +78,8 @@ export async function getShopListApi(corpid: string, mode?: number) {
params.mode = mode;
}
return await http.get<ShopEntity[]>("shop/list", params);
}
export async function getCorpidById(cid?: number) {
return await http.get<string>("qy/getCorpidById", { id: cid ? cid : 0 });
}

View File

@ -3,7 +3,7 @@ import { ref, onMounted } from 'vue'
import { useWxStore } from '@/pinia/stores/wx'
import { useProductStore } from '@/pinia/stores/product'
import { useCartStore } from '@/pinia/stores/cart'
import { getShopListApi } from '@/api/shop'
import { getCorpidById, getShopListApi } from '@/api/shop'
import type { ShopEntity } from '@/api/shop/types'
import ProductContainer from './components/product-container.vue';
import { generateDynamicCode, getWxUserByOpenid, mpCodeToOpenId } from '@/api/users'
@ -31,51 +31,6 @@ const shopId = ref<number>(0)
//
onMounted(async () => {
let openid = uni.getStorageSync('local_openid');
if (!openid) {
uni.login({
provider: 'weixin', //使
success: function (loginRes) {
mpCodeToOpenId(loginRes.code).then((wxUser) => {
console.log('wxUser:', wxUser);
wxStore.wxMpCallback(wxUser.data);
uni.setStorageSync('local_openid', wxUser.data.openid || '');
if (wxUser.data.ab98UserId) {
ab98UserStore.setAb98UserName(wxUser.data.ab98Name || wxUser.data.nickName || '')
ab98UserStore.setAb98UserFaceImg(wxUser.data.ab98FaceImg || '')
}
}).catch((e) => {
console.error('mpCodeToOpenId error:', e)
})
},
});
} else {
//
const wxUser = await getWxUserByOpenid(openid);
console.log('wxUser:', wxUser);
wxStore.wxMpCallback(wxUser.data);
uni.setStorageSync('local_openid', wxUser.data.openid || '');
if (wxUser.data.ab98UserId) {
ab98UserStore.setAb98UserName(wxUser.data.ab98Name || wxUser.data.nickName || '')
ab98UserStore.setAb98UserFaceImg(wxUser.data.ab98FaceImg || '')
}
}
if (showShopList.value) {
try {
// handleWxCallback
await wxStore.waitForHandleWxCallbackComplete();
const res = await getShopListApi(wxStore.corpid || '');
console.log('获取店铺列表:', res)
if (res?.code === 0 && res?.data?.length > 0) {
shopList.value = res.data;
// shopList.value = [...shopList.value, ...res.data, ...res.data, ...res.data];
}
} catch (error) {
console.error('获取店铺列表失败:', error)
}
}
})
//
@ -110,13 +65,64 @@ function handleCheckout() {
})
}
onLoad((query) => {
onLoad(async (query) => {
const wxParamsStore = useWxParamsStore();
console.log('page index onLoad query: ', query);
// scene 使 decodeURIComponent scene
if (query && query.scene) {
const scene = decodeURIComponent(query.scene);
const wxParamsStore = useWxParamsStore();
wxParamsStore.parseScene(scene);
}
const cid = wxParamsStore.getNumberParam('cid');
const corpid = (await getCorpidById(cid)).data;
let openid = uni.getStorageSync('local_openid');
if (!openid) {
uni.login({
provider: 'weixin', //使
success: function (loginRes) {
mpCodeToOpenId(loginRes.code).then((wxUser) => {
console.log('wxUser:', wxUser);
wxStore.wxMpCallback(wxUser.data, corpid);
uni.setStorageSync('local_openid', wxUser.data.openid || '');
if (wxUser.data.ab98UserId) {
ab98UserStore.setAb98UserName(wxUser.data.ab98Name || wxUser.data.nickName || '')
ab98UserStore.setAb98UserFaceImg(wxUser.data.ab98FaceImg || '')
}
}).catch((e) => {
console.error('mpCodeToOpenId error:', e)
})
},
});
} else {
//
const wxUser = await getWxUserByOpenid(openid);
console.log('wxUser:', wxUser);
wxStore.wxMpCallback(wxUser.data, corpid);
uni.setStorageSync('local_openid', wxUser.data.openid || '');
if (wxUser.data.ab98UserId) {
ab98UserStore.setAb98UserName(wxUser.data.ab98Name || wxUser.data.nickName || '')
ab98UserStore.setAb98UserFaceImg(wxUser.data.ab98FaceImg || '')
}
}
if (showShopList.value) {
try {
// handleWxCallback
await wxStore.waitForHandleWxCallbackComplete();
const res = await getShopListApi(wxStore.corpid || '');
console.log('获取店铺列表:', res)
if (res?.code === 0 && res?.data?.length > 0) {
shopList.value = res.data;
// shopList.value = [...shopList.value, ...res.data, ...res.data, ...res.data];
}
} catch (error) {
console.error('获取店铺列表失败:', error)
}
}
})
</script>

View File

@ -185,8 +185,8 @@ export const useWxStore = defineStore("wx", () => {
return true;
}
const wxMpCallback = (userDTO: WxUserDTO) => {
corpid.value = "wpZ1ZrEgAA2QTxIRcB4cMtY7hQbTcPAw";
const wxMpCallback = (userDTO: WxUserDTO, corpid_: string) => {
corpid.value = corpid_;
openid.value = userDTO.openid || openid.value;
wxUserDTO.value = userDTO;
ab98UserId.value = userDTO.ab98UserId || ab98UserId.value;