zhengyiming
2025-02-19 c8724a3d88607516fe95ffb91e4b52c99405d063
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
<template>
  <LoginPageLayout class="authorization-page-wrapper">
    <div class="authorization-page-wechat-btn-wrapper">
      <nut-button
        v-if="isAccount"
        type="primary"
        class="authorization-page-wechat-wrapper"
        @click="handleLoginByHasAccount"
      >
        <div class="authorization-page-wechat">手机号快速登录</div></nut-button
      >
      <AccessOpenTypeButton
        v-else
        type="primary"
        class="authorization-page-wechat-wrapper"
        open-type="getPhoneNumber"
        :access="state.policyChecked"
        @noAccess="noAccess"
        @getphonenumber="login"
      >
        <div class="authorization-page-wechat">手机号快速登录</div>
      </AccessOpenTypeButton>
    </div>
    <Policy
      v-model="state.policyChecked"
      policyBtnText="若手机号未注册,将为您直接注册账号,注册即为同意"
    />
    <div class="other-login-channel-wrapper" v-if="!isLoginByWeb">
      <nut-divider>其他登录方式</nut-divider>
      <div class="other-login-channel-list">
        <div class="other-login-channel-list-item" @click="goLoginByForm()">
          <div class="other-login-channel-list-item-icon">
            <img :src="IconCaptcha" />
          </div>
          <div class="other-login-channel-list-item-text">验证码登录</div>
        </div>
        <div
          class="other-login-channel-list-item"
          @click="goLoginByForm(LoginFormTabs.AccountLogin)"
        >
          <div class="other-login-channel-list-item-icon">
            <img :src="IconPassword" />
          </div>
          <div class="other-login-channel-list-item-text">账户登录</div>
        </div>
      </div>
    </div>
  </LoginPageLayout>
</template>
 
<script setup lang="ts">
import { Policy } from '@/components';
import { AccessOpenTypeButton } from '@12333/components';
import LoginPageLayout from '../components/LoginPageLayout/LoginPageLayout.vue';
import { useUserStore } from '@/stores/modules/user';
import { Message } from '@12333/utils';
import { CommonEventFunction, ButtonProps } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { useLoginedJump } from '@/hooks';
import * as accountServices from '@12333/services/api/Account';
import IconCaptcha from '@/assets/login/icon-btn-captcha.png';
import IconPassword from '@/assets/login/icon-btn-password.png';
import { LoginFormTabs } from '../constants';
import { WxMiniAppEnum } from '@12333/constants';
 
defineOptions({
  name: 'authorization',
});
 
const state = reactive({
  policyChecked: false,
});
 
const userStore = useUserStore();
 
function noAccess() {
  Message.warning('请先阅读并勾选协议');
}
 
const { jump, redirectParams } = useLoginedJump();
 
const launchOptions = Taro.getEnterOptionsSync();
console.log('launchOptions: ', launchOptions);
const router = Taro.useRouter();
 
const uuid = router.params?.scene ?? '';
 
const isLoginByWeb = computed(
  () =>
    RouterPath.authorization.includes(launchOptions.path) && launchOptions.scene === 1047 && uuid
);
 
const wxIndentityRes = ref<API.WxMiniAppIndentityInfo>();
const wxMiniAppUserLoginRes = ref<API.IdentityModelTokenCacheItem>();
 
const isAccount = ref(false);
 
onMounted(async () => {
  try {
    let loginRes = await Taro.login();
    if (isLoginByWeb.value) {
      const wxIndentity = await accountServices.getWxIndentity({
        code: loginRes.code,
        wxMiniApp: WxMiniAppEnum.C端小程序,
      });
      wxIndentityRes.value = wxIndentity;
      if (wxIndentityRes.value.userName) {
        isAccount.value = true;
      }
    } else {
      const params: API.APIwxMiniAppUserLoginParams = {
        code: loginRes.code,
        wxMiniApp: WxMiniAppEnum.C端小程序,
      };
      wxMiniAppUserLoginRes.value = await accountServices.wxMiniAppUserLogin(params);
      if (wxMiniAppUserLoginRes.value.accessToken) {
        isAccount.value = true;
      }
    }
  } catch (error) {}
});
 
const login: CommonEventFunction<ButtonProps.onGetPhoneNumberEventDetail> = async function (ev) {
  try {
    const { errMsg, code } = ev.detail;
    console.log('errMsg: ', errMsg, isLoginByWeb.value);
    if (errMsg === 'getPhoneNumber:ok') {
      if (isLoginByWeb.value) {
        let res = await userStore.wxMiniAppPhoneAuthLoginFromScan(
          ev.detail,
          wxIndentityRes.value,
          uuid
        );
        if (res) {
          handleLoginSuccess();
        }
      } else {
        let res = await userStore.getTokenByPhone(ev.detail, wxMiniAppUserLoginRes.value);
        if (res) {
          handleLoginSuccess();
        }
      }
    } else {
      Message.warning('无法获取手机号!');
    }
  } catch (error) {
    console.log('error: ', error);
  }
};
 
async function handleLoginByHasAccount() {
  try {
    if (state.policyChecked) {
      if (isLoginByWeb.value) {
        console.log('uuid: ', uuid);
        let res = await userStore.wxMiniAppUserLoginFromScan(wxIndentityRes.value, uuid);
        if (res) {
          handleLoginSuccess();
        }
      } else {
        userStore.loginSuccess(wxMiniAppUserLoginRes.value);
        handleLoginSuccess();
      }
    } else {
      noAccess();
    }
  } catch (error) {}
}
 
function handleLoginSuccess() {
  Message.success('授权登录成功', {
    onClosed: () => {
      jump();
    },
  });
}
 
function goLoginByForm(tab = LoginFormTabs.VerificationCodeLogin) {
  Taro.navigateTo({
    url: `${RouterPath.loginByForm}?redirect=${redirectParams.value}&tab=${tab}`,
  });
}
</script>
 
<style lang="scss">
@import '@/styles/common.scss';
 
.authorization-page-wrapper {
  .authorization-page-top {
    width: 750px;
    height: 225px;
    margin-bottom: 78px;
  }
 
  .authorization-page-title {
    margin-bottom: 160px;
  }
 
  .authorization-page-wechat-btn-wrapper {
    padding: 0 64px;
    margin-bottom: 40px;
  }
 
  .authorization-page-wechat-wrapper {
    width: 100%;
    height: 88px;
  }
 
  .authorization-page-wechat {
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 400;
    font-size: 32px;
    color: #ffffff;
    line-height: 44px;
 
    .authorization-page-wechat-icon {
      width: 44px;
      height: 36px;
      margin-right: 10px;
    }
  }
 
  .page-layout-scroll-view-wrapper {
    position: relative;
 
    .other-login-channel-wrapper {
      width: 520px;
      position: absolute;
      bottom: 40px;
      left: 50%;
      transform: translateX(-50%);
 
      .nut-divider {
        margin: 0;
        margin-bottom: 40rpx;
        color: boleGetCssVar('text-color', 'secondary');
 
        &::before,
        &::after {
          border-color: #f0f0f0;
        }
      }
 
      .other-login-channel-list {
        display: flex;
        justify-content: space-around;
 
        .other-login-channel-list-item {
          .other-login-channel-list-item-icon {
            width: 80px;
            height: 80px;
            background: #f5f5f5;
            border-radius: 50%;
            margin: 0 auto 8px;
            display: flex;
            justify-content: center;
            align-items: center;
 
            img {
              width: 38px;
              height: 48px;
            }
          }
 
          .other-login-channel-list-item-text {
            font-weight: 400;
            font-size: 24px;
            color: boleGetCssVar('text-color', 'regular');
            line-height: 34px;
            text-align: center;
          }
        }
      }
    }
  }
}
 
.authorization-page-auth {
  position: fixed;
  z-index: -1;
  top: 0;
  left: 0;
  width: 750px;
  height: 1624px;
  object-fit: cover;
}
</style>