index.vue
6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-14 15:37:49
* @FilePath: /jgdl/src/pages/auth/index.vue
* @Description: 文件描述
-->
<template>
<div>
<!-- <button wx:if="{{canIUse}}" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">授权登录</button>
<view @tap="auth">授权登陆</view> -->
</div>
</template>
<script setup>
import Taro from '@tarojs/taro'
import request from '@/utils/request';
</script>
<script>
import "./index.less";
import { getCurrentPageParam } from "@/utils/weapp";
export default {
name: "authPage",
/**
* 页面准备完成后执行登录
*/
onReady() {
// 添加短暂延迟确保页面完全准备好
setTimeout(() => {
this.performLogin();
}, 100);
},
data () {
return {
canIUse: wx.canIUse('button.open-type.getUserInfo')
}
},
onLoad: function() {
// 查看是否授权
// wx.getSetting({
// success (res){
// if (res.authSetting['scope.userInfo']) {
// // 已经授权,可以直接调用 getUserInfo 获取头像昵称
// wx.getUserInfo({
// success: function(res) {
// console.warn(res.userInfo)
// }
// })
// }
// }
// })
},
methods: {
/**
* 执行登录授权流程
*/
performLogin() {
Taro.login({
success: (res) => {
if (res.code) {
this.handleLoginSuccess(res.code);
} else {
console.error('登录失败!' + res.errMsg);
this.handleLoginError('登录失败,请重试');
}
},
fail: (err) => {
console.error('登录调用失败:', err);
this.handleLoginError('登录失败,请重试');
}
});
},
/**
* 处理登录成功后的网络请求
* @param {string} code - 微信登录code
*/
handleLoginSuccess(code) {
Taro.showLoading({
title: '授权中',
});
request.post('/srv/?a=openid', {
code: code,
openid: 'h-008'
// openid: 'o5NFZ5cFQtLRy3aVHaZMLkjHFusI'
// openid: 'o5NFZ5TpgG4FwYursGCLjcUJH2ak'
// openid: 'o5NFZ5cqroPYwawCp8FEOxewtgnw'
})
.then(res => {
if (res.data.code) {
const cookie = res.cookies[0];
if (cookie != null) {
// 保存sessionid到本地存储
wx.setStorageSync("sessionid", res.cookies[0]);
// 修改请求头
request.defaults.headers.cookie = res.cookies[0];
// 添加延迟确保存储操作完成
setTimeout(() => {
this.handleNavigateAfterAuth();
}, 200);
} else {
// cookie为空时跳转到首页
this.navigateToHome();
}
} else {
console.warn(res.data.msg);
this.handleLoginError(res.data.msg || '授权失败');
}
})
.catch(err => {
console.error('网络请求失败:', err);
this.handleLoginError('网络请求失败,请重试');
})
.finally(() => {
Taro.hideLoading();
});
},
/**
* 处理授权成功后的页面跳转
*/
handleNavigateAfterAuth() {
try {
const params = getCurrentPageParam();
if (params.url === 'pages/productDetail/index') {
// 详情页的分享跳转处理 - 使用绝对路径
this.navigateToPage(`/pages/productDetail/index?id=${params.id}`);
} else {
// 其他页面跳转到首页
this.navigateToHome();
}
} catch (error) {
console.error('页面跳转参数解析失败:', error);
// 降级处理:跳转到首页
this.navigateToHome();
}
},
/**
* 跳转到指定页面
* @param {string} url - 页面路径
*/
navigateToPage(url) {
Taro.reLaunch({
url: url
}).catch(err => {
console.error('页面跳转失败:', err);
// 降级处理:跳转到首页
this.navigateToHome();
});
},
/**
* 跳转到首页
*/
navigateToHome() {
Taro.reLaunch({
url: `/pages/index/index`
}).catch(err => {
console.error('跳转首页失败:', err);
// 最后的降级处理
Taro.showToast({
title: '页面跳转失败',
icon: 'error'
});
});
},
/**
* 处理登录错误
* @param {string} message - 错误信息
*/
handleLoginError(message) {
Taro.hideLoading();
Taro.showToast({
title: message,
icon: 'error',
duration: 2000
});
// 错误情况下延迟跳转到首页
setTimeout(() => {
this.navigateToHome();
}, 2000);
},
bindGetUserInfo (e) {
console.warn(e.detail.userInfo)
},
// auth () {
// Taro.getSetting({
// success: function (res) {
// if (!res.authSetting['scope.userInfo']) {
// console.warn(0);
// Taro.authorize({
// scope: 'scope.userInfo',
// success: function () {
// Taro.getUserInfo({
// success: function(res) {
// var userInfo = res.userInfo
// console.warn(userInfo);
// }
// })
// },
// fail: function (error) {
// console.error(error)
// }
// })
// }
// }
// })
// }
auth () {
// wx.getSetting({
// success (res){
// if (res.authSetting['scope.userInfo']) {
// // 已经授权,可以直接调用 getUserInfo 获取头像昵称
// wx.getUserInfo({
// success: function(res) {
// console.warn(res.userInfo)
// }
// })
// }
// }
// })
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userInfo']) {
wx.authorize({
scope: 'scope.userInfo',
success () {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: function(res) {
console.warn(res.userInfo)
}
})
}
})
}
}
})
}
}
};
</script>