get-univerify-manager.uvue 4.9 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10
<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>
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
11 12 13
      <view class="uni-btn-v uni-common-mt">
        <button type="primary" @click="customLoginIn()">一键登录(自定义页面)</button>
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
14 15 16 17 18 19 20 21 22
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        title: '一键登录',
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
23 24 25 26 27 28 29 30 31
        univerifyManager: null as UniverifyManager | null,
        phone: '',
        slogan: '',
        privacyName: '',
        privacyUrl: ''
        // phone: '176****6657',
        // slogan: '认证服务由中国联通提供',
        // privacyName: '中国联通认证服务条款',
        // privacyUrl: 'https://opencloud.wostore.cn/authz/resource/html/disclaimer.html?fromsdk=true'
DCloud-WZF's avatar
DCloud-WZF 已提交
32 33 34 35 36
      }
    },
    onLoad() {
      this.univerifyManager = uni.getUniverifyManager();
      // 预登录
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
37
      this.preLogin(() => { });
DCloud-WZF's avatar
DCloud-WZF 已提交
38 39
    },
    methods: {
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
      customLoginIn() {
        const isPreLoginValid = this.univerifyManager?.isPreLoginValid() ?? false;
        if (isPreLoginValid) {
          this.pushCustomPage();
        } else {
          this.preLogin(() => {
            this.pushCustomPage();
          })
        }
      },
      pushCustomPage() {
        const url = '/pages/API/get-univerify-manager/univerify-custom-page?phone=' + this.phone + '&slogan=' + this.slogan + '&name=' + this.privacyName + '&link=' + this.privacyUrl;
        uni.navigateTo({
          url: url,
          animationType: "slide-in-bottom",
        })
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
57 58 59 60 61 62 63 64
      verify(fullScreen : boolean) {
        // 校验预登录是否有效
        const isPreLoginValid = this.univerifyManager?.isPreLoginValid() ?? false;
        if (isPreLoginValid) {
          // 预登录有效,执行登录
          this.login(fullScreen);
        } else {
          // 预登录无效,执行预登录
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
65 66 67
          this.preLogin(() => {
            this.login(fullScreen);
          })
DCloud-WZF's avatar
DCloud-WZF 已提交
68 69
        }
      },
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
70 71 72 73 74 75 76 77 78
      preLogin(callback) {
        this.univerifyManager?.preLogin({
          success: (res) => {
            this.phone = res.number;
            this.slogan = res.slogan;
            this.privacyName = res.privacyName;
            this.privacyUrl = res.privacyUrl;
            console.log("pre login success");
            callback();
DCloud-WZF's avatar
DCloud-WZF 已提交
79
          },
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
80 81
          fail: (err : PreLoginFail) => {
            console.error("pre login fail => " + JSON.stringify(err));
DCloud-WZF's avatar
DCloud-WZF 已提交
82
            uni.showModal({
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
83
              title: '预登录失败',
DCloud-WZF's avatar
DCloud-WZF 已提交
84 85 86 87
              content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
              showCancel: false
            });
          }
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        } as PreLoginOptions);
      },
      login(fullScreen : boolean) {
        this.univerifyManager?.login({
          univerifyStyle:{
            fullScreen: fullScreen,
            backgroundColor: "#FFFFFF",
            loginBtnText: "一键登录",
            logoPath: "/static/logo.png"
          },
          success: (res : LoginSuccess) => {
            console.log("login success => " + JSON.stringify(res));
            this.takePhoneNumber(res.accessToken,res.openId);
          },
          fail: (err : LoginFail) => {
            console.error("login fail => " + err);
            uni.showModal({
              title: '登录失败',
              content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
              showCancel: false
            });
          }
        });
      },
      takePhoneNumber(accessToken : string, openId : string) {
        // 云函数取号
        uniCloud.callFunction({
          name: 'univerify',
          data: {
            access_token: accessToken, // 客户端一键登录接口返回的access_token
            openid: 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);
        });

DCloud-WZF's avatar
DCloud-WZF 已提交
143 144 145 146 147 148 149
      }
    }
  }
</script>

<style>

DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
150
</style>/style>