get-univerify-manager.uvue 3.2 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 27 28 29 30 31 32 33 34 35
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        title: '一键登录',
        univerifyManager: null as UniverifyManager | null
      }
    },
    onLoad() {
      this.univerifyManager = uni.getUniverifyManager();
      // 预登录
      this.univerifyManager?.preLogin({
        success() {
          console.log("pre login success");
        },
        fail(err : PreLoginFail) {
          console.error("pre login fail => " + JSON.stringify(err));
        }
      } as PreLoginOptions);
    },
    methods: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
36
      verify(fullScreen : boolean) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
37 38 39 40
        // 校验预登录是否有效
        const isPreLoginValid = this.univerifyManager?.isPreLoginValid() ?? false;
        if (isPreLoginValid) {
          // 预登录有效,执行登录
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
41
          this.login(fullScreen);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42 43 44 45 46
        } else {
          // 预登录无效,执行预登录
          this.univerifyManager?.preLogin({
            success() {
              console.log("pre login success");
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
47
              this.login(fullScreen);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
48 49 50 51 52 53 54
            },
            fail(err : PreLoginFail) {
              console.error("pre login fail => " + JSON.stringify(err));
            }
          } as PreLoginOptions);
        }
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
55
      login(fullScreen : boolean) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
56 57 58
        this.univerifyManager?.login({
          // 登录页样式
          univerifyStyle: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
59
            fullScreen: fullScreen,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
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
            backgroundColor: "#FFFFFF",
            loginBtnText: "一键登录",
            logoPath: "/static/logo.png"
          } as UniverifyStyle,
          success(res : LoginSuccess) {
            console.log("login success => " + JSON.stringify(res));
            // 云函数取号
            uniCloud.callFunction({
              name: 'univerify',
              data: {
                access_token: res.accessToken, // 客户端一键登录接口返回的access_token
                openid: res.openId // 客户端一键登录接口返回的openid
              }
            }).then(res => {
              uni.showModal({
                title: '取号成功',
                content: res.result.getJSON("res")?.getString("phoneNumber"),
                showCancel: false,
                success(_) {
                  // 关闭登录页
                  this.univerifyManager?.close();
                }
              });
            }).catch(err => {
              console.error(JSON.stringify(err));
              // 关闭登录页
              this.univerifyManager?.close();
            });
          },
          fail(err : LoginFail) {
            console.error("login fail => " + err);
            uni.showToast({
              title: "登录失败",
              icon: "error"
            });
          }
        } as LoginOptions);
      }
    }
  }
</script>

<style>

</style>