feat(微信登录): 添加根据cid获取corpid功能并完善微信登录流程
添加getCorpidById接口用于根据cid查询corpid 修改wxMpCallback方法接收corpid参数 在页面加载时获取corpid并用于微信登录流程
This commit is contained in:
parent
918bc53059
commit
022d307271
|
|
@ -78,4 +78,8 @@ export async function getShopListApi(corpid: string, mode?: number) {
|
||||||
params.mode = mode;
|
params.mode = mode;
|
||||||
}
|
}
|
||||||
return await http.get<ShopEntity[]>("shop/list", params);
|
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 });
|
||||||
}
|
}
|
||||||
|
|
@ -3,7 +3,7 @@ import { ref, onMounted } from 'vue'
|
||||||
import { useWxStore } from '@/pinia/stores/wx'
|
import { useWxStore } from '@/pinia/stores/wx'
|
||||||
import { useProductStore } from '@/pinia/stores/product'
|
import { useProductStore } from '@/pinia/stores/product'
|
||||||
import { useCartStore } from '@/pinia/stores/cart'
|
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 type { ShopEntity } from '@/api/shop/types'
|
||||||
import ProductContainer from './components/product-container.vue';
|
import ProductContainer from './components/product-container.vue';
|
||||||
import { generateDynamicCode, getWxUserByOpenid, mpCodeToOpenId } from '@/api/users'
|
import { generateDynamicCode, getWxUserByOpenid, mpCodeToOpenId } from '@/api/users'
|
||||||
|
|
@ -31,51 +31,6 @@ const shopId = ref<number>(0)
|
||||||
|
|
||||||
// 页面加载
|
// 页面加载
|
||||||
onMounted(async () => {
|
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);
|
console.log('page index onLoad query: ', query);
|
||||||
// scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
|
// scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
|
||||||
if (query && query.scene) {
|
if (query && query.scene) {
|
||||||
const scene = decodeURIComponent(query.scene);
|
const scene = decodeURIComponent(query.scene);
|
||||||
const wxParamsStore = useWxParamsStore();
|
|
||||||
wxParamsStore.parseScene(scene);
|
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>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -185,8 +185,8 @@ export const useWxStore = defineStore("wx", () => {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const wxMpCallback = (userDTO: WxUserDTO) => {
|
const wxMpCallback = (userDTO: WxUserDTO, corpid_: string) => {
|
||||||
corpid.value = "wpZ1ZrEgAA2QTxIRcB4cMtY7hQbTcPAw";
|
corpid.value = corpid_;
|
||||||
openid.value = userDTO.openid || openid.value;
|
openid.value = userDTO.openid || openid.value;
|
||||||
wxUserDTO.value = userDTO;
|
wxUserDTO.value = userDTO;
|
||||||
ab98UserId.value = userDTO.ab98UserId || ab98UserId.value;
|
ab98UserId.value = userDTO.ab98UserId || ab98UserId.value;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue