From 767b05e9faf00ef8469bca1a8bbd30dab05a3d80 Mon Sep 17 00:00:00 2001 From: donqi Date: Thu, 19 May 2022 22:15:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E6=B3=A8=E5=86=8C=EF=BC=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/js/request.js | 95 ++++++++++++++++++++++-------------- pages/index/home.vue | 2 +- pages/index/my-home.vue | 4 +- pages/order/order-detail.vue | 12 +---- 4 files changed, 64 insertions(+), 49 deletions(-) diff --git a/common/js/request.js b/common/js/request.js index f3199ff..0556ff0 100644 --- a/common/js/request.js +++ b/common/js/request.js @@ -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(); diff --git a/pages/index/home.vue b/pages/index/home.vue index a34d0c4..2a59d14 100644 --- a/pages/index/home.vue +++ b/pages/index/home.vue @@ -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; diff --git a/pages/index/my-home.vue b/pages/index/my-home.vue index d2a5b57..71cb123 100644 --- a/pages/index/my-home.vue +++ b/pages/index/my-home.vue @@ -4,9 +4,9 @@ + :style="'width: 120rpx; height: 120rpx; background-image:url(' + curUserInfo.customerLogoUrl + ');'"> - {{curUserInfo.userInfo.nickName}} + {{curUserInfo.name}} {{myInfo.vipInfo.level}}会员 diff --git a/pages/order/order-detail.vue b/pages/order/order-detail.vue index ca5c653..08363a7 100644 --- a/pages/order/order-detail.vue +++ b/pages/order/order-detail.vue @@ -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);