get-univerify-manager.uvue 3.6 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4 5
<template>
  <view>
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap uni-common-mt">
      <view class="uni-btn-v uni-common-mt">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
6 7 8 9
        <button type="primary" @click="verify(false)">一键登录(半屏)</button>
      </view>
      <view class="uni-btn-v uni-common-mt">
        <button type="primary" @click="verify(true)">一键登录(全屏)</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        title: '一键登录',
        univerifyManager: null as UniverifyManager | null
      }
    },
    onLoad() {
      this.univerifyManager = uni.getUniverifyManager();
      // 预登录
      this.univerifyManager?.preLogin({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
27
        success: () => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28 29
          console.log("pre login success");
        },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
30
        fail: (err : PreLoginFail) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
31
          console.error("pre login fail => " + JSON.stringify(err));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
32 33
          uni.showToast({
            title: '预登录失败'
34
          });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
35 36 37 38
        }
      } as PreLoginOptions);
    },
    methods: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
39
      verify(fullScreen : boolean) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
40 41 42 43
        // 校验预登录是否有效
        const isPreLoginValid = this.univerifyManager?.isPreLoginValid() ?? false;
        if (isPreLoginValid) {
          // 预登录有效,执行登录
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44
          this.login(fullScreen);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
45 46 47
        } else {
          // 预登录无效,执行预登录
          this.univerifyManager?.preLogin({
雪洛's avatar
雪洛 已提交
48
            success: () => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
49
              console.log("pre login success");
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
50
              this.login(fullScreen);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
51
            },
雪洛's avatar
雪洛 已提交
52
            fail: (err : PreLoginFail) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
53
              console.error("pre login fail => " + JSON.stringify(err));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
54 55
              uni.showToast({
                title: '预登录失败'
56
              });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
57 58 59 60
            }
          } as PreLoginOptions);
        }
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
61
      login(fullScreen : boolean) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
62 63 64
        this.univerifyManager?.login({
          // 登录页样式
          univerifyStyle: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
65
            fullScreen: fullScreen,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
66 67 68 69
            backgroundColor: "#FFFFFF",
            loginBtnText: "一键登录",
            logoPath: "/static/logo.png"
          } as UniverifyStyle,
雪洛's avatar
雪洛 已提交
70
          success: (res : LoginSuccess) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
71 72 73 74 75 76 77 78 79
            console.log("login success => " + JSON.stringify(res));
            // 云函数取号
            uniCloud.callFunction({
              name: 'univerify',
              data: {
                access_token: res.accessToken, // 客户端一键登录接口返回的access_token
                openid: res.openId // 客户端一键登录接口返回的openid
              }
            }).then(res => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
80 81 82 83 84 85 86 87 88
              // 关闭登录页
              this.univerifyManager?.close();
              setTimeout(() => {
                uni.showModal({
                  title: '取号成功',
                  content: res.result.getJSON("res")?.getString("phoneNumber"),
                  showCancel: false
                });
              }, 100);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
89 90 91 92
            }).catch(err => {
              console.error(JSON.stringify(err));
              // 关闭登录页
              this.univerifyManager?.close();
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
93 94 95 96 97 98 99
              setTimeout(() => {
                uni.showModal({
                  title: '取号失败',
                  content: (err as Error).message,
                  showCancel: false
                });
              }, 100);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
100 101
            });
          },
雪洛's avatar
雪洛 已提交
102
          fail: (err : LoginFail) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
103
            console.error("login fail => " + err);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
104 105
            uni.showToast({
              title: '登录失败'
106
            });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
107 108 109 110 111 112 113 114 115 116
          }
        } as LoginOptions);
      }
    }
  }
</script>

<style>

</style>