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

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', {
invoke(args) {
// 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://127.0.0.1:80' + args.url;
args.url = 'http://127.0.0.1:80' + args.url;
// console.log("停止触发");
// return false;
},
@ -68,42 +68,47 @@ export default {
return true;
}
// TODO:调用小程序服务端确认是否是授权登录过的用户
let loginRes = {
logined: false,
userInfo: {}
};
// 未登录过的获取微信用户信息
if (!loginRes.logined) {
userInfo = await wx.getUserProfile({
desc: '用于小程序登录'
userInfo = await wx.getUserProfile({
desc: '用于小程序登录'
});
console.log("从微信获取基本用户信息:" + userInfo);
// 获取微信登录凭证
const wxLoginRes = await wx.login();
console.log(wxLoginRes)
// 获取openid
const wxAuthRes = await uni.request({
url: '/wx/auth',
header: {
code: wxLoginRes.code
}
})
console.log(wxAuthRes)
const openId = wxAuthRes[1].data.data.openid;
// 第一次从服务端获取用户信息
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
});
// 获取微信登录凭证
const wxLoginRes = await wx.login();
console.log(wxLoginRes)
// 再次请求小程序服务端存储用户,服务端添加附加用户信息后返回
loginRes = {
logined: true,
userInfo: {
...userInfo,
userId: 1,
wxLoginCode: wxLoginRes.code
}
};
const wxAuthRes = await uni.request({
url: '/wx/auth',
header: {
code: wxLoginRes.code
}
})
console.log(wxAuthRes)
loginRes.userInfo.openId = wxAuthRes[1].data.data.openid;
loginRes.userInfo.customerId = 2;
if (registerRes.code === 0) {
wxGetUserRes = await this.qryUserInfo(openId);
}
}
userInfo = loginRes.userInfo;
if (!userInfo) {
if (!wxGetUserRes.data) {
userInfo = null;
} else {
userInfo = {
...wxGetUserRes.data,
wxLoginCode: wxLoginRes.code
}
}
console.log("通过后台服务获取用户信息:" + userInfo);
if (!userInfo || userInfo.customerId == null || userInfo.customerId == undefined) {
uni.showToast({
icon: 'error',
title: '微信用户信息获取失败,请退出小程序重试'
@ -113,6 +118,24 @@ export default {
// 页面存储用户登录有效信息,以便其他页面调用
uni.setStorageSync('userProfile', userInfo);
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() {
return wx.login();

View File

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

View File

@ -4,9 +4,9 @@
<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="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="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 v-if="myInfo.vipInfo.isVip" class='cu-tag bg-yellow radius'>{{myInfo.vipInfo.level}}会员
</view>

View File

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