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

<style>

</style>