get-univerify-manager.uvue 4.1 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
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
<template>
  <view>
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap uni-common-mt">
      <view class="uni-btn-v uni-common-mt">
        <button type="primary" @click="verify(false)">一键登录(半屏)</button>
      </view>
      <view class="uni-btn-v uni-common-mt">
        <button type="primary" @click="verify(true)">一键登录(全屏)</button>
      </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));
          uni.showModal({
            title: '预登录失败',
            content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
            showCancel: false
          });
        }
      } as PreLoginOptions);
    },
    methods: {
      verify(fullScreen : boolean) {
        // 校验预登录是否有效
        const isPreLoginValid = this.univerifyManager?.isPreLoginValid() ?? false;
        if (isPreLoginValid) {
          // 预登录有效,执行登录
          this.login(fullScreen);
        } else {
          // 预登录无效,执行预登录
          this.univerifyManager?.preLogin({
            success: () => {
              console.log("pre login success");
              this.login(fullScreen);
            },
            fail: (err : PreLoginFail) => {
              console.error("pre login fail => " + JSON.stringify(err));
              uni.showModal({
                title: '预登录失败',
                content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
                showCancel: false
              });
            }
          } as PreLoginOptions);
        }
      },
      login(fullScreen : boolean) {
        this.univerifyManager?.login({
          // 登录页样式
          univerifyStyle: {
            fullScreen: fullScreen,
            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 => {
              // 关闭登录页
              this.univerifyManager?.close();
              setTimeout(() => {
                uni.showModal({
                  title: '取号成功',
                  content: res.result.getJSON("res")?.getString("phoneNumber"),
                  showCancel: false
                });
              }, 100);
            }).catch(err => {
              console.error(JSON.stringify(err));
              // 关闭登录页
              this.univerifyManager?.close();
              setTimeout(() => {
                uni.showModal({
                  title: '取号失败',
                  content: (err as Error).message,
                  showCancel: false
                });
              }, 100);
            });
          },
          fail: (err : LoginFail) => {
            console.error("login fail => " + err);
            uni.showModal({
              title: '登录失败',
              content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
              showCancel: false
            });
          }
        } as LoginOptions);
      }
    }
  }
</script>

<style>

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
122
</style>