xusd
7 days ago 998218675eb243d43912c203174a6b72b299c0f8
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
// pages/login/index.js
const app = getApp();
const $$ = require('../../utils/util');
 
// 登录
function loginApi(submitData) {
    return $$.request({ url: 'paAccount/empower', type: 'post', submitData, service: 'cust', noToken: true });
}
 
Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: $$.url.img, // icon图片地址
    },
 
    // 登录,获取用户信息
    async handleGetUserInfo() {
        $$.showLoading();
        wx.getUserProfile({
            desc: '完善用户信息',
            complete(res) {
                if (res.errMsg === 'getUserProfile:ok') {
                    wx.login({
                        async success(res2) {
                            if (res2.code) {
                                const accountInfo = wx.getAccountInfoSync();
                                const submitData = {
                                    appid: accountInfo.miniProgram.appId,
                                    code: res2.code,
                                    avatar: res?.userInfo.avatarUrl,
                                    encryptedData: res.encryptedData,
                                    ivStr: res.iv,
                                };
                                const res3 = await loginApi(submitData);
                                $$.hideLoading();
                                if (res3.type) {
                                    wx.setStorage({ key: 'userInfo', data: res3.data });
                                    app.globalData.token = res3.data.token;
                                    $$.showToast({ title: '登录成功', icon: 'success' });
                                    await $$.sleep();
                                    wx.reLaunch({
                                        url: '../../pages/homePage/index',
                                    });
                                }
                            } else {
                                $$.hideLoading();
                                $$.showToast('登录失败,请稍后重试');
                            }
                        },
                    });
                } else {
                    $$.hideLoading();
                    $$.showToast({ title: '抱歉!授权失败' });
                }
            },
        });
    },
 
    // 返回首页
    handleGoToHomePage() {
        wx.reLaunch({
            url: '../../pages/homePage/index',
        });
    },
});