get-univerify-manager.uvue 4.8 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
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">
6
        <button type="default" @click="verify(false)">一键登录(半屏)</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
7 8
      </view>
      <view class="uni-btn-v uni-common-mt">
9
        <button type="default" @click="verify(true)">一键登录(全屏)</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
10
      </view>
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
11
      <view class="uni-btn-v uni-common-mt">
12
        <button type="default" @click="customLoginIn()">一键登录(自定义页面)</button>
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
13
      </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
        univerifyManager: null as UniverifyManager | null,
        phone: '',
        slogan: '',
        privacyName: '',
        privacyUrl: ''
DCloud-WZF's avatar
DCloud-WZF 已提交
28 29 30 31 32
      }
    },
    onLoad() {
      this.univerifyManager = uni.getUniverifyManager();
      // 预登录
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
33
      this.preLogin(() => { });
DCloud-WZF's avatar
DCloud-WZF 已提交
34 35
    },
    methods: {
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
36 37 38 39 40 41 42 43 44 45 46 47
      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;
48 49 50 51 52 53 54 55 56
        uni.openDialogPage({
          url: url,
          animationType: 'slide-in-bottom',
          success(res) {
            console.log("成功打开自定义登录页面");
          },
          fail(err) {
            console.log(err);
          }
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
57 58
        })
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
59 60 61 62 63 64 65 66
      verify(fullScreen : boolean) {
        // 校验预登录是否有效
        const isPreLoginValid = this.univerifyManager?.isPreLoginValid() ?? false;
        if (isPreLoginValid) {
          // 预登录有效,执行登录
          this.login(fullScreen);
        } else {
          // 预登录无效,执行预登录
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
67 68 69
          this.preLogin(() => {
            this.login(fullScreen);
          })
DCloud-WZF's avatar
DCloud-WZF 已提交
70 71
        }
      },
72
      preLogin(callback: (() => void)) {
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
73 74 75 76 77 78 79 80
        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 已提交
81
          },
82
          fail: (err) => {
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
83
            console.error("pre login fail => " + JSON.stringify(err));
DCloud-WZF's avatar
DCloud-WZF 已提交
84
            uni.showModal({
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
85
              title: '预登录失败',
DCloud-WZF's avatar
DCloud-WZF 已提交
86 87 88 89
              content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
              showCancel: false
            });
          }
90
        });
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
91 92 93 94 95 96 97 98 99
      },
      login(fullScreen : boolean) {
        this.univerifyManager?.login({
          univerifyStyle:{
            fullScreen: fullScreen,
            backgroundColor: "#FFFFFF",
            loginBtnText: "一键登录",
            logoPath: "/static/logo.png"
          },
100
          success: (res) => {
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
101 102 103
            console.log("login success => " + JSON.stringify(res));
            this.takePhoneNumber(res.accessToken,res.openId);
          },
104
          fail: (err) => {
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
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 143 144
            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 已提交
145 146 147 148 149 150 151
      }
    }
  }
</script>

<style>

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