wangrong
11 小时以前 db685b04e52efd75213ef7540fdc0bed7dccfd9e
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
<template>
    <!-- #ifdef MP-WEIXIN -->
    <view class="phone-auth" v-if="visible">
        <view class="phone-auth__mask" @tap="later"></view>
        <view class="phone-auth__panel">
            <text class="phone-auth__title">授权手机号</text>
            <text class="phone-auth__desc">
                回收、预约等功能需要使用您的手机号。请授权后再继续使用。
            </text>
            <button
                class="phone-auth__btn"
                open-type="getPhoneNumber"
                @getphonenumber="onGetPhone"
            >
                登录并授权手机号
            </button>
            <view class="phone-auth__later" @tap="later">暂不授权</view>
        </view>
    </view>
    <!-- #endif -->
</template>
 
<script lang="ts" setup>
import { computed, ref } from "vue";
import { onShow } from "@dcloudio/uni-app";
import { useCool, useStore, useWx } from "/@/cool";
import { phoneAuthState } from "/@/cool/mini-login";
import { useUi } from "/$/cool-ui";
 
const authState = phoneAuthState();
 
const { service, router } = useCool();
const { user } = useStore();
const wx = useWx();
const ui = useUi();
const tick = ref(0);
const submitting = ref(false);
 
const visible = computed(() => {
    tick.value;
    return authState.ready && !authState.dismissed && (!user.token || !user.info?.phone);
});
 
function later() {
    authState.dismissed = true;
    tick.value++;
}
 
function phonePayload(detail: { code?: string; encryptedData?: string; iv?: string }) {
    // 新版手机号按钮只返回动态令牌,必须原样提交,不能换成 wx.login 的 code
    if (detail.code && !detail.encryptedData) {
        return { code: detail.code };
    }
    return {
        encryptedData: detail.encryptedData,
        iv: detail.iv,
        code: wx.code.value,
    };
}
 
function enterHome(phone: string) {
    user.set({
        ...(user.info || {}),
        phone,
    });
    authState.dismissed = true;
    tick.value++;
    router.home();
}
 
function onGetPhone(e: {
    detail: { errMsg?: string; code?: string; encryptedData?: string; iv?: string };
}) {
    const detail = e.detail || {};
    const errMsg = detail.errMsg || "";
    if (errMsg.indexOf("ok") < 0) {
        ui.showToast("未授权手机号");
        return;
    }
    if (!detail.code && !detail.encryptedData) {
        ui.showToast("未获取到手机号");
        return;
    }
    if (submitting.value) {
        return;
    }
 
    submitting.value = true;
    ui.showLoading();
 
    const task = new Promise<string>((resolve) => {
        uni.login({
            provider: "weixin",
            success: (res) => resolve(res.code || ""),
            fail: () => resolve(""),
        });
    })
        .then((loginCode) => {
            return service.user.login.miniPhone({
                ...phonePayload(detail),
                loginCode,
            });
        })
        .then(async (res) => {
        user.setToken(res);
        let info = user.info;
        try {
            info = (await service.user.info.person()) || info;
        } catch (err) {
            console.error(err);
        }
        const phone = info?.phone || res.phone;
        user.set({
            ...(info || {}),
            phone,
        });
        return phone as string | undefined;
    });
 
    task
        .then((phone) => {
            ui.hideLoading();
            submitting.value = false;
            if (!phone) {
                ui.showToast("未获取到手机号");
                return;
            }
            enterHome(phone);
        })
        .catch((err) => {
            ui.hideLoading();
            submitting.value = false;
            ui.showToast(err?.message || "手机号授权失败");
        });
}
 
onShow(() => {
    tick.value++;
});
</script>
 
<style lang="scss" scoped>
.phone-auth {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 10000;
 
    &__mask {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background: rgba(0, 0, 0, 0.45);
    }
 
    &__panel {
        position: absolute;
        left: 48rpx;
        right: 48rpx;
        top: 30%;
        background: #fff;
        border-radius: 24rpx;
        padding: 48rpx 40rpx 36rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
 
    &__title {
        font-size: 34rpx;
        font-weight: 600;
        color: #222;
    }
 
    &__desc {
        margin-top: 20rpx;
        font-size: 26rpx;
        line-height: 1.6;
        color: #666;
        text-align: center;
    }
 
    &__btn {
        margin-top: 36rpx;
        width: 100%;
        height: 84rpx;
        line-height: 84rpx;
        border-radius: 42rpx;
        background: #2ecc71;
        color: #fff;
        font-size: 30rpx;
        border: 0;
    }
 
    &__later {
        margin-top: 24rpx;
        font-size: 26rpx;
        color: #999;
    }
}
</style>