get-univerify-manager.uvue 4.0 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));
32
          uni.showModal({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
33
            title: '预登录失败',
34 35 36
            content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
            showCancel: false
          });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
37 38 39 40
        }
      } as PreLoginOptions);
    },
    methods: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
41
      verify(fullScreen : boolean) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42 43 44 45
        // 校验预登录是否有效
        const isPreLoginValid = this.univerifyManager?.isPreLoginValid() ?? false;
        if (isPreLoginValid) {
          // 预登录有效,执行登录
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
46
          this.login(fullScreen);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
47 48 49
        } else {
          // 预登录无效,执行预登录
          this.univerifyManager?.preLogin({
雪洛's avatar
雪洛 已提交
50
            success: () => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
51
              console.log("pre login success");
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
52
              this.login(fullScreen);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
53
            },
雪洛's avatar
雪洛 已提交
54
            fail: (err : PreLoginFail) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
55
              console.error("pre login fail => " + JSON.stringify(err));
56
              uni.showModal({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
57
                title: '预登录失败',
58 59 60
                content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
                showCancel: false
              });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
61 62 63 64
            }
          } as PreLoginOptions);
        }
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
65
      login(fullScreen : boolean) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
66 67 68
        this.univerifyManager?.login({
          // 登录页样式
          univerifyStyle: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
69
            fullScreen: fullScreen,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
70 71 72 73
            backgroundColor: "#FFFFFF",
            loginBtnText: "一键登录",
            logoPath: "/static/logo.png"
          } as UniverifyStyle,
雪洛's avatar
雪洛 已提交
74
          success: (res : LoginSuccess) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
75 76 77 78 79 80 81 82 83
            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 已提交
84 85 86 87 88 89 90 91 92
              // 关闭登录页
              this.univerifyManager?.close();
              setTimeout(() => {
                uni.showModal({
                  title: '取号成功',
                  content: res.result.getJSON("res")?.getString("phoneNumber"),
                  showCancel: false
                });
              }, 100);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
93 94 95 96
            }).catch(err => {
              console.error(JSON.stringify(err));
              // 关闭登录页
              this.univerifyManager?.close();
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
97 98 99 100 101 102 103
              setTimeout(() => {
                uni.showModal({
                  title: '取号失败',
                  content: (err as Error).message,
                  showCancel: false
                });
              }, 100);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
104 105
            });
          },
雪洛's avatar
雪洛 已提交
106
          fail: (err : LoginFail) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
107
            console.error("login fail => " + err);
108
            uni.showModal({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
109
              title: '登录失败',
110 111 112
              content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
              showCancel: false
            });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
113 114 115 116 117 118 119 120 121 122
          }
        } as LoginOptions);
      }
    }
  }
</script>

<style>

</style>