import { wxPhoneLogin, getPhoneNumber } from '../login' // 后端接口 // 服务端接口 - 获取openid function queryOpenIdFn(code) { return new Promise((resolve) => { wxPhoneLogin({ code, }) .then((res: any) => { if (res.code == 0) { console.log('获取openid失败1', res.msg) return } uni.setStorageSync('openId', res.data.openid) resolve(res.data) }) .catch((err) => { console.log('获取openid失败2', err) }) }) } // 服务端接口 - 获取手机号 function getPhoneNumberFn(phoneCode, openId) { return new Promise((resolve) => { getPhoneNumber({ code: phoneCode, openId, }) .then((res) => { if (res.code == 0) { console.log('获取手机号失败1:', res.msg) return } resolve(res) }) .catch((err) => { console.log('获取手机号失败2:', err) }) }) } // 微信api : 微信登录 function loginFn() { return new Promise((resolve, reject) => { uni.login({ success: async (res) => { queryOpenIdFn(res.code).then((res) => { resolve(res) }) }, fail: (err) => { console.log('login fail:', err) }, }) }) } export { loginFn, getPhoneNumberFn }
界面调用
loginFn().then((res: any) => { // 微信登录&服务端获取openid console.log(res, '接口换取的openid') console.log('获取手机号的动态令牌:', e.detail.code) // 动态令牌 getPhoneNumberFn(e.detail.code, res.openid).then((res2: any) => { // 服务端获取手机号 console.log(555, res2) if (res2.code == 1) { userStore.setUserInfs(res2.data?.userinfo) uni.setStorageSync('token', res2.data.userinfo.token) gonswi('/pages/index/index') } })
})
新建 login.ts 标签:openid,code,console,手机号,登录,微信,res,log From: https://www.cnblogs.com/7788mmhh/p/18552267