xuruiqian
2025-06-04 17ce21955b4e3402d3d5868b52e50bfdd55bc572
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<template>
    <view class="content">
        <view class="logo">
            <!-- <up-text type="primary" size="30" text="LOGO"></up-text> -->
            <image class="image-logo" lazy-load src="../../static/image/loginlogo.png"></image>
        </view>
        <view class="card" v-if="loginShow">
            <view class="login-phone">
                <view class="phone">
                    <up-input placeholder="请输入手机号" border="bottom" v-model="phone" prefixIcon="phone"
                        prefixIconStyle="font-size: 22px;color: #909399" size="small"></up-input>
                </view>
                <view class="code">
                    <view class="code-input">
                        <up-input placeholder="请输入验证码" border="bottom" v-model="code" prefixIcon="bell"
                            prefixIconStyle="font-size: 22px;color: #909399" size="small"></up-input>
                    </view>
                    <view class="wrap">
                        <u-toast ref="uToastRef"></u-toast>
                        <u-code :seconds="seconds" @end="end()" @start="start()" ref="uCodeRef"
                            @change="codeChange"></u-code>
                        <u-button type="primary" @tap="getCode" size="small" :disabled="canSandMsg">{{ tips
                            }}</u-button>
                    </view>
                </view>
                <view class="submit">
                    <u-button size="large" type="primary" @click="submit">登录</u-button>
                </view>
            </view>
        </view>
        <view class="info" v-if="userProfileShow">
            <UserProfile @saveCB="userProfileCB" />
        </view>
    </view>
</template>
 
<script>
import { ref, reactive, toRefs, onMounted, getCurrentInstance } from "vue";
import { useBasicInfoStore } from "../../store/basicInfo"
import { GetSMSCode, GetWXCode2Session, RegisterUserByCellphone, LoginByWXOpenID } from "../../apis/authapi"
import { GetVIPOrganization } from "../../apis/api"
import UserProfile from "../../components/userProfile/userProfile.vue"
 
export default {
    components: { UserProfile },
    setup() {
        const userStore = useBasicInfoStore()
        const { proxy } = getCurrentInstance();
        const state = reactive({
            loginShow: false,
            phone: '',
            code: '',
            tips: '',
            seconds: 60,
            bindingPhone: '',
            canSandMsg: false,
            userProfileShow: false,
        })
 
        onMounted(() => {
            Init();
        });
 
        const Init = () => {
             getOpenID();
 
            // uni.navigateTo({
            //     url: '/pages/question/type'
            // });
            // uni.switchTab({
            //                     url: '/pages/question/type' // 目标页面路径
            //                 });
        }
 
        const getOpenID = () => {
            uni.login({
                provider: 'weixin',
                success: function (loginRes) {
                    // 登录成功
                    uni.getUserInfo({
                        provider: 'weixin',
                        success: function (info) {
                            userStore.$state.userinfo = info.userInfo;
                            // 获取用户信息成功, info.authResult保存用户信息
 
                            // uni.showLoading({
                            //     title: '身份获取中...',
                            // });
                            GetWXCode2Session(loginRes.code).then((res) => {
                                // uni.hideLoading();
                                if (res) {
                                    let session_key = res.data.session_key;
                                    let openid = res.data.openid;
 
                                    let params2 = {
                                        session_key: session_key,
                                        openid: openid,
                                    };
                                    userStore.$state.userinfo.openid = openid;
 
                                    uni.showLoading({
                                        title: '身份获取中...',
                                    });
                                    LoginByWXOpenID(params2).then((res2) => {
                                        uni.hideLoading();
 
                                        let token = '';
                                        if (res2) {
                                            if (res2.code == 200) {
                                                if (res2.data) {
                                                    token = res2.data.token;
                                                    uni.setStorageSync('token', token)
                                                    userStore.$state.hasLogin = true;
                                                    for (const key in res2.data) {
                                                        userStore.userinfo[key] = res2.data[key]
                                                    }
 
                                                    if (!res2.data.avatarurl || !res2.data.nickname) {
                                                        state.userProfileShow = true;
                                                        userStore.school = null
                                                        userStore.college = null
                                                        userStore.major = null
                                                    } else {
                                                        GetVIPOrganization(userStore.userinfo.id).then((res3) => {
                                                            if (!res3) {
                                                                uni.navigateTo({
                                                                    url: '/pages/index/index'
                                                                });
                                                            } else {
                                                                for (const key in res3) {
                                                                    userStore.masterOrganization[key] = res3[key]
                                                                }
                                                                userStore.schoolName = userStore.masterOrganization.organizationname
                                                                userStore.collegeName = userStore.masterOrganization.departmentname
                                                                userStore.majorName = userStore.masterOrganization.majorname
                                                                userStore.updatecount = userStore.masterOrganization.updatecount ? userStore.masterOrganization.updatecount : 0
                                                                uni.switchTab({
                                                                    url: '/pages/question/list'
                                                                });
                                                            }
 
 
                                                            // uni.navigateTo({
                                                            //     url: '/pages/question/list'
                                                            // });
                                                        })
                                                    }
                                                }
                                            } else if (res2.message == 'MSG_ERROR_USER_NOT_EXISTED') {
                                                state.loginShow = true
                                                console.log("未绑定手机号")
                                            }
                                        }
                                    });
                                }
                                //  else {
                                //     uni.hideLoading();
                                // }
                            })
                        }
                    })
 
                },
                fail: function (err) {
                    // 登录授权失败
                    // err.code是错误码
                }
            });
        }
 
 
        const codeChange = (text) => {
            state.tips = text;
        };
 
        const getCode = () => {
            if (state.canSandMsg == true) {
                return;
            }
            if (!state.phone) {
                uni.$u.toast('请输入手机号');
                return;
            }
            if (!/(^1[3|4|5|7|8|9][0-9]{9}$)/.test(state.phone)) {
                uni.$u.toast('请输入正确的手机号码');
                return;
            }
            uni.showLoading({
                title: '正在获取验证码',
            });
            GetSMSCode(state.phone).then(resp => {
                uni.hideLoading();
                if (resp.code == 200) {
                    setTimeout(() => {
                        // 这里此提示会被start()方法中的提示覆盖
                        uni.$u.toast('验证码已发送');
                        // 通知验证码组件内部开始倒计时
                        proxy.$refs.uCodeRef.start();
                    }, 2000);
                } else {
                    uni.$u.toast('验证码已发送失败');
                }
            }).catch(err => {
                uni.hideLoading();
                uni.$u.toast('验证码已发送失败');
            });
        };
 
        const end = () => {
            state.canSandMsg = false
            //uni.$u.toast('倒计时结束');
        };
 
        const start = () => {
            state.canSandMsg = true
            //uni.$u.toast('倒计时开始');
        };
 
        const submit = () => {
            if (!state.phone) {
                uni.$u.toast('请输入手机号');
                return;
            }
            if (!/(^1[3|4|5|7|8|9][0-9]{9}$)/.test(state.phone)) {
                uni.$u.toast('请输入正确的手机号码');
                return;
            }
            uni.showLoading({
                title: '登录中...',
            });
            let params2 = {
                openid: userStore.$state.userinfo.openid,
                cellphone: state.phone,
                randomcode: state.code
            };
            RegisterUserByCellphone(params2).then((res2) => {
                uni.hideLoading();
                let token = '';
                if (res2) {
                    if (res2.code == 200) {
                        if (res2.data) {
                            token = res2.data.token;
                            userStore.userinfo.id = res2.data.id
                            uni.setStorageSync('token', token)
                            userStore.$state.hasLogin = true;
                            state.loginShow = false
                            state.userProfileShow = true
                            // uni.navigateTo({
                            //     url: '/pages/question/type'
                            // });
                        }
                    } else {
                        uni.$u.toast('登录失败');
                    }
                }
            });
        }
 
        const userProfileCB = () => {
            uni.navigateTo({
                url: '/pages/index/index'
            });
        }
 
 
 
 
        return { ...toRefs(state), userStore, codeChange, getCode, end, start, submit, userProfileCB }
    }
}
 
 
 
</script>
 
<style scoped lang="scss">
.center {
    display: flex;
    flex-direction: column;
    align-items: center;
    // justify-content: center;
}
 
.content {
    @extend .center;
    height: 100vh;
    width: 100%;
    background-color: #f5f5f5;
}
 
.logo {
    @extend .center;
    height: 25vh;
    justify-content: center;
    .image-logo{
        height: 25vh;
        width: 50vw;
    }
}
 
.card {
    width: calc(90% - 80rpx);
    //height: 15vh;
    overflow: auto;
    border-radius: 10px;
    background-color: #fff;
    margin-top: 5vh;
    padding: 20rpx 40rpx 60rpx 40rpx;
 
    .login-phone {
        width: 100%;
        overflow: auto;
        margin-top: 20rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
 
        .phone {
            width: 95%;
            height: 60rpx;
            margin-top: 20rpx;
        }
 
        .code {
            width: 95%;
            height: 100rpx;
            margin-top: 50rpx;
            display: flex;
            justify-content: space-between;
 
            .code-input {
                padding: 24rpx 0rpx;
                width: 70%;
            }
 
            .wrap {
                padding: 24rpx 0rpx;
            }
        }
 
        .submit {
            width: 95%;
            overflow: auto;
            margin-top: 100rpx;
        }
    }
}
 
.info {
    width: 80%;
    //height: 15vh;
    overflow: auto;
    border-radius: 10px;
    background-color: #fff;
    margin-top: 5vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
</style>