对接注册,获取用户信息接口

This commit is contained in:
donqi 2022-05-19 22:15:06 +08:00
parent a0e1ab5614
commit 767b05e9fa
4 changed files with 64 additions and 49 deletions

View File

@ -6,9 +6,9 @@ export default {
uni.addInterceptor('request', { uni.addInterceptor('request', {
invoke(args) { invoke(args) {
// request 触发前拼接 url // request 触发前拼接 url
args.url = 'https://www.opsoul.com' + args.url; // args.url = 'https://www.opsoul.com' + args.url;
// args.url = 'http://192.168.2.3:80' + args.url; // args.url = 'http://192.168.2.3:80' + args.url;
// args.url = 'http://127.0.0.1:80' + args.url; args.url = 'http://127.0.0.1:80' + args.url;
// console.log("停止触发"); // console.log("停止触发");
// return false; // return false;
}, },
@ -68,29 +68,14 @@ export default {
return true; return true;
} }
// TODO:调用小程序服务端确认是否是授权登录过的用户
let loginRes = {
logined: false,
userInfo: {}
};
// 未登录过的获取微信用户信息
if (!loginRes.logined) {
userInfo = await wx.getUserProfile({ userInfo = await wx.getUserProfile({
desc: '用于小程序登录' desc: '用于小程序登录'
}); });
console.log("从微信获取基本用户信息:" + userInfo);
// 获取微信登录凭证 // 获取微信登录凭证
const wxLoginRes = await wx.login(); const wxLoginRes = await wx.login();
console.log(wxLoginRes) console.log(wxLoginRes)
// 再次请求小程序服务端存储用户,服务端添加附加用户信息后返回 // 获取openid
loginRes = {
logined: true,
userInfo: {
...userInfo,
userId: 1,
wxLoginCode: wxLoginRes.code
}
};
const wxAuthRes = await uni.request({ const wxAuthRes = await uni.request({
url: '/wx/auth', url: '/wx/auth',
header: { header: {
@ -98,12 +83,32 @@ export default {
} }
}) })
console.log(wxAuthRes) console.log(wxAuthRes)
loginRes.userInfo.openId = wxAuthRes[1].data.data.openid; const openId = wxAuthRes[1].data.data.openid;
loginRes.userInfo.customerId = 2; // 第一次从服务端获取用户信息
let wxGetUserRes = await this.qryUserInfo(openId);
// 获取失败则获取微信信息再调用注册接口
if (!wxGetUserRes.data) {
// 注册完成后再次从服务端获取用户信息
let registerRes = await this.registerUser({
openId: openId,
name: userInfo.userInfo.nickName,
customerLogoUrl: userInfo.userInfo.avatarUrl,
status: 0
});
if (registerRes.code === 0) {
wxGetUserRes = await this.qryUserInfo(openId);
} }
userInfo = loginRes.userInfo; }
if (!wxGetUserRes.data) {
if (!userInfo) { userInfo = null;
} else {
userInfo = {
...wxGetUserRes.data,
wxLoginCode: wxLoginRes.code
}
}
console.log("通过后台服务获取用户信息:" + userInfo);
if (!userInfo || userInfo.customerId == null || userInfo.customerId == undefined) {
uni.showToast({ uni.showToast({
icon: 'error', icon: 'error',
title: '微信用户信息获取失败,请退出小程序重试' title: '微信用户信息获取失败,请退出小程序重试'
@ -114,6 +119,24 @@ export default {
uni.setStorageSync('userProfile', userInfo); uni.setStorageSync('userProfile', userInfo);
return true; return true;
}, },
async registerUser(params = {}) {
let res = await uni.request({
url: '/wx/addUser',
method: 'POST',
data: params
})
return res[1].data;
},
async qryUserInfo(openId) {
let res = await uni.request({
url: '/wx/getUserInfo',
method: 'POST',
data: {
openId: openId
}
})
return res[1].data;
},
wxLogin() { wxLogin() {
return wx.login(); return wx.login();
}, },

View File

@ -116,7 +116,7 @@
}, },
methods: { methods: {
async loadData() { async loadData() {
let curUserInfo = uni.getStorageSync('userProfile'); let curUserInfo = this.$request.getCurUserInfo();
this.isAuthWxLoginModal = curUserInfo ? false : true; this.isAuthWxLoginModal = curUserInfo ? false : true;
if (this.isAuthWxLoginModal) { if (this.isAuthWxLoginModal) {
return; return;

View File

@ -4,9 +4,9 @@
<view class="padding bg-main-color" :style="'padding-top: ' + pageContentTop + 'px; padding-bottom: 100rpx;'"> <view class="padding bg-main-color" :style="'padding-top: ' + pageContentTop + 'px; padding-bottom: 100rpx;'">
<view class="flex justify-start padding-bottom-xl solid-bottom"> <view class="flex justify-start padding-bottom-xl solid-bottom">
<view class="cu-avatar round" <view class="cu-avatar round"
:style="'width: 120rpx; height: 120rpx; background-image:url(' + curUserInfo.userInfo.avatarUrl + ');'"></view> :style="'width: 120rpx; height: 120rpx; background-image:url(' + curUserInfo.customerLogoUrl + ');'"></view>
<view class="margin-lr-sm"> <view class="margin-lr-sm">
<view class="text-xl margin-bottom-xs">{{curUserInfo.userInfo.nickName}}</view> <view class="text-xl margin-bottom-xs">{{curUserInfo.name}}</view>
<view class="padding-xs text-sm"> <view class="padding-xs text-sm">
<view v-if="myInfo.vipInfo.isVip" class='cu-tag bg-yellow radius'>{{myInfo.vipInfo.level}}会员 <view v-if="myInfo.vipInfo.isVip" class='cu-tag bg-yellow radius'>{{myInfo.vipInfo.level}}会员
</view> </view>

View File

@ -240,21 +240,13 @@
} }
let curUserInfo = this.$request.getCurUserInfo(); let curUserInfo = this.$request.getCurUserInfo();
let params = { let params = {
// customerId: curUserInfo.userId, customerId: curUserInfo.customerId,
customerId: 2,
serverTime: this.formInfo.doorTime, serverTime: this.formInfo.doorTime,
addressId: this.formInfo.defaultAddress.customerAddressId, addressId: this.formInfo.defaultAddress.customerAddressId,
payType: this.formInfo.payWay, payType: this.formInfo.payWay,
remark: this.formInfo.comments, remark: this.formInfo.comments,
isNeedBill: this.formInfo.isNeedBill, isNeedBill: this.formInfo.isNeedBill,
// goodsList: this.parseGoodsList() goodsList: this.parseGoodsList()
goodsList: [{
goodsId: 2,
num: 1
}, {
goodsId: 3,
num: 1
}]
} }
// //
let res = await this.$request.placeOrder(params); let res = await this.$request.placeOrder(params);