Commit bbe9181715463ad49628950ba57b4fa86296df2c

Authored by fengtao
1 parent 81607b65

fix:修改第三方登录获取openid通过后端方式

... ... @@ -8,7 +8,7 @@
8 8 <view class="u-flex u-p-l-30 u-p-r-20 u-p-t-75 u-p-b-30">
9 9 <block v-if="userInfo.isToken || userInfo.isThirdLogin">
10 10 <view @click="openPersonalInfo" class="u-m-r-20">
11   - <image class="avatar" mode="aspectFill" :src="!userInfo.avatar ? '../../static/logo.png' : userInfo.avatar"></image>
  11 + <image class="avatar" mode="aspectFill" :src="!userInfo.avatar ? '../../static/logo.png' : userInfo.avatar || thirdObj.avatarUrl"></image>
12 12 </view>
13 13 <view class="u-flex-1">
14 14 <view class="nickName u-flex">
... ... @@ -154,17 +154,19 @@ export default {
154 154 appUserKey: '',
155 155 appUserSecret: ''
156 156 },
157   - thirdObj: {}
  157 + thirdObj: {},
  158 + getOpenId: ''
158 159 };
159 160 },
160 161 onLoad(e) {
161 162 // 隐藏原生的tabbar
162 163 uni.hideTabBar();
163   - if (e.obj != null) {
164   - const params = JSON.parse(decodeURIComponent(e.obj));
165   - this.thirdObj = params;
166   - }
167   - console.log('UserInfo', this.userInfo);
  164 + // if (e.obj != null) {
  165 + // const params = JSON.parse(decodeURIComponent(e.obj));
  166 + // // uni.$u.toast('eee', params.avatarUrl);
  167 + // this.thirdObj = params;
  168 + // }
  169 + this.getOpenId = getApp().globalData.openId;
168 170 },
169 171 computed: {
170 172 ...mapState(['userInfo'])
... ... @@ -232,7 +234,7 @@ export default {
232 234 loginMethod: 'ACCOUNT',
233 235 ...this.bindAccountObj,
234 236 platformName: 'WECHAT',
235   - ...this.thirdObj
  237 + thirdUserId: this.getOpenId
236 238 };
237 239 uni.$u.http
238 240 .post('/yt/third/bind', postData)
... ... @@ -260,8 +262,8 @@ export default {
260 262 }
261 263 })
262 264 .catch(e => {
263   - uni.$u.toast(e.data?.msg || e.data?.message);
264   - this.show = false;
  265 + uni.$u.toast(e.data?.msg);
  266 + this.show = true;
265 267 });
266 268 } else {
267 269 const phoneRegular = /^1\d{10}$/;
... ... @@ -295,7 +297,7 @@ export default {
295 297 loginMethod: 'PHONE',
296 298 ...this.bindPhoneObj,
297 299 platformName: 'WECHAT',
298   - ...this.thirdObj
  300 + thirdUserId: this.getOpenId
299 301 };
300 302 uni.$u.http
301 303 .post('/yt/third/bind', postData)
... ... @@ -321,8 +323,8 @@ export default {
321 323 this.saveUserInfo();
322 324 })
323 325 .catch(e => {
324   - uni.$u.toast(e.data?.msg || e.data?.message);
325   - this.show = false;
  326 + uni.$u.toast(e.data?.msg);
  327 + this.show = true;
326 328 });
327 329 }
328 330 },
... ...
... ... @@ -6,7 +6,6 @@
6 6 <view class="content">
7 7 <view class="hello login-text-muted">您好,</view>
8 8 <view class="hello-welcome login-text-muted">欢迎来到ThingsKit!</view>
9   - <view class="circleStyle"></view>
10 9 </view>
11 10 </view>
12 11 <view class="f__login">
... ... @@ -17,7 +16,9 @@
17 16 </view>
18 17 <view class="form-row u-flex">
19 18 <view class="v-input"><input type="text" v-model="loginForm.password" maxlength="32" placeholder="请输入登录密码" :password="!showPassword" /></view>
20   - <view class="v-password" @click="showPasswordMode"><u-icon color="#9a9a9a" size="25" :name="showPassword ? 'eye-fill' : 'eye-off'"></u-icon></view>
  19 + <view class="v-password" @click="showPasswordMode">
  20 + <u-icon color="#9a9a9a" width="18" height="15" :name="showPassword ? '/static/eye.png' : '/static/eye-hide.png'"></u-icon>
  21 + </view>
21 22 <u-icon></u-icon>
22 23 </view>
23 24 <button class="submit" size="default" @click="onSubmitFunc"><text class="text">登录</text></button>
... ... @@ -28,8 +29,7 @@
28 29 <view class="u-flex link-login">
29 30 <view class="link-text login-text-gray">第三方账号登录</view>
30 31 <view style="height:20rpx"></view>
31   - <view @click="onAuthorization" class="link-image"><image class="image" src="../../static/weixin.png" mode="aspectFill"></image></view>
32   - <view class="circleStyleBottom"></view>
  32 + <button class="link-image" @tap="onWenxinAuthorization"><image class="image" src="../../static/weixin.png" mode="aspectFill"></image></button>
33 33 </view>
34 34 </view>
35 35 </view>
... ... @@ -52,79 +52,59 @@ export default {
52 52 },
53 53 showPassword: false,
54 54 code: '',
55   - openid: '',
56   - session_key: ''
  55 + openid: ''
57 56 };
58 57 },
59 58 onLoad() {
60   - //#ifdef MP-WEIXIN
61 59 wx.login({
62 60 success: res => {
63 61 if (res.code) {
64 62 this.code = res.code;
65   - wx.request({
66   - url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${appSecrect}&js_code=${this.code}&grant_type=authorization_code`,
67   - method: 'GET',
68   - success: res => {
69   - if (res.statusCode == 200) {
70   - this.openid = res.data.openid;
71   - this.session_key = res.data.session_key;
72   - //设置全局变量openId
73   - getApp().globalData.openId = res.data.openid;
74   - }
75   - },
76   - complete: e => {
77   - if (e.data.errcode != null) {
78   - return;
79   - // return uni.$u.toast('获取用户唯一id失败');
80   - }
81   - }
82   - });
  63 + //这里获取openid
83 64 } else {
  65 + return;
84 66 }
85 67 }
86 68 });
87   - //#endif
88 69 },
89 70 methods: {
90 71 ...mapMutations(['setUserInfo']),
91 72 ...mapActions(['updateBadgeTotal']),
92 73 //微信授权登录
93 74 //#ifdef MP-WEIXIN
94   - onAuthorization() {
95   - /**
96   - * 注意:通过wx.getUserProfile并不能获取用户的openid,openid是唯一识别用户的标识,
97   - * 所以最好在用户授权登录时就获取。调用wx.login()获取
98   - */
  75 + onWenxinAuthorization() {
99 76 wx.getUserProfile({
100   - desc: '获取用户授权信息',
101   - success: res => {
102   - if (res) {
103   - //微信官方自带解密(node.js实现)
104   - let pc = new WXBizDataCrypt(appId, this.session_key);
105   - let data = pc.decryptData(res.encryptedData, res.iv);
  77 + desc: '微信第三方授权',
  78 + success: reswenxin => {
  79 + console.log('res=======>', reswenxin);
  80 + if (reswenxin.errMsg === 'getUserProfile:ok' && reswenxin.encryptedData) {
  81 + console.log('获取code', this.code);
  82 + //获取用户信息
106 83 let obj = {
107   - avatarUrl: data.avatarUrl,
108   - nickName: data.nickName,
  84 + avatarUrl: reswenxin.userInfo.avatarUrl,
109 85 thirdUserId: this.openid
110 86 };
111 87 //判断是否需要绑定
112 88 uni.$u.http
113   - .get(`/yt/third/login/${this.openid}`)
  89 + .get(`/yt/third/login/${this.code}`)
114 90 .then(res => {
115   - if (res.token == '' || res.token == null) {
116   - //需要绑定,跳转我的页面进行绑定,显示绑定按钮
117   - uni.reLaunch({
118   - url: '../../pages/personal/personal'
119   - });
  91 + console.log('Res', res);
  92 + //设置全局变量openId
  93 + // getApp().globalData.openId = res.data.openid;
  94 + if (res.token == '' || (res.token == null && res.refreshToken)) {
  95 + //需要绑定
120 96 let userInfo = {
121   - isThirdLogin: true, //token用于判断是否登录
122   - avatar: data.avatarUrl
  97 + isThirdLogin: true, //用于判断是否是第三方登录并且需要绑定账号
  98 + avatar: obj.avatarUrl
123 99 };
124 100 this.setUserInfo(userInfo);
  101 + //设置全局变量openId
  102 + getApp().globalData.openId = res.refreshToken;
  103 + uni.reLaunch({
  104 + url: '../../pages/personal/personal'
  105 + });
125 106 } else {
126   - // 不需要绑定
127   - // 储存登录信息
  107 + // 不需要绑定,直接第三方登录使用
128 108 let resObj = {
129 109 refreshToken: res.refreshToken,
130 110 isToken: res.token
... ... @@ -132,7 +112,7 @@ export default {
132 112 let userInfo = {
133 113 ...resObj,
134 114 token: true, //token用于判断是否登录
135   - isThirdLogin: false
  115 + isThirdLoginAndNoDind: true //用于判断是否是第三方登录并且不需要绑定账号
136 116 };
137 117 if (userInfo.token) {
138 118 this.setUserInfo(userInfo);
... ... @@ -151,18 +131,11 @@ export default {
151 131 .catch(e => {
152 132 uni.$u.toast(e.data?.message);
153 133 });
154   - /**
155   - * 有些时候uni.navigatorBack({})没有返回到上级页面
156   - * 才使用uni.reLaunch()进行跳转
157   - */
158   - // #ifdef MP
159   - setTimeout(() => {
160   - uni.reLaunch({
161   - url: '../../pages/personal/personal?obj=' + encodeURIComponent(JSON.stringify(obj))
162   - });
163   - }, 500);
164   - // #endif
165 134 }
  135 + },
  136 + fail: res => {
  137 + //拒绝授权
  138 + return;
166 139 }
167 140 });
168 141 },
... ... @@ -243,14 +216,6 @@ export default {
243 216 uni.$u.toast(e.data?.message);
244 217 });
245 218 },
246   - saveUserInfo() {
247   - //储存个人信息
248   - uni.$u.http.get('/yt/user/me/info').then(res => {
249   - if (res) {
250   - this.setUserInfo(res);
251   - }
252   - });
253   - },
254 219 openCodeFunc() {
255 220 uni.navigateTo({
256 221 url: '../other/code'
... ... @@ -270,4 +235,7 @@ export default {
270 235
271 236 <style lang="scss" scoped>
272 237 @import './static/login.scss';
  238 +/deep/ button {
  239 + background: rgba(0, 0, 0, 0);
  240 +}
273 241 </style>
... ...
... ... @@ -14,6 +14,7 @@ export const mutations = {
14 14 // #endif
15 15 // #ifndef H5
16 16 uni.setStorageSync('userInfo', state.userInfo);
  17 + wx.setStorageSync('userInfo', state.userInfo);
17 18 // #endif
18 19 }
19 20 },
... ...