123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <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="default" @click="verify(false)">一键登录(半屏)</button>
- </view>
- <view class="uni-btn-v uni-common-mt">
- <button type="default" @click="verify(true)">一键登录(全屏)</button>
- </view>
- <view class="uni-btn-v uni-common-mt">
- <button type="default" @click="customLoginIn()">一键登录(自定义页面)</button>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- title: '一键登录',
- uniVerifyManager: null as UniVerifyManager | null,
- phone: '',
- slogan: '',
- privacyName: '',
- privacyUrl: ''
- }
- },
- onLoad() {
- this.uniVerifyManager = uni.getUniVerifyManager();
- // 预登录
- this.preLogin(() => { });
- },
- methods: {
- customLoginIn() {
- if('production' === process.env.NODE_ENV && '__UNI__HelloUniAppX'===uni.getAppBaseInfo().appId){
- uni.showModal({
- title: '提示',
- content: '一键登录为收费功能,当前环境暂不支持。请在HBuilderX中新建Hello uni-app x项目真机运行体验!',
- showCancel: false
- })
- return
- }
- 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.openDialogPage({
- url: url,
- animationType: 'slide-in-bottom',
- success(res) {
- console.log("成功打开自定义登录页面");
- },
- fail(err) {
- console.log(err);
- }
- })
- },
- verify(fullScreen : boolean) {
- if('production' === process.env.NODE_ENV && '__UNI__HelloUniAppX'===uni.getAppBaseInfo().appId){
- uni.showModal({
- title: '提示',
- content: '一键登录为收费功能,当前环境暂不支持。请在HBuilderX中新建Hello uni-app x项目真机运行体验!',
- showCancel: false
- })
- return
- }
- // 校验预登录是否有效
- const isPreLoginValid = this.uniVerifyManager?.isPreLoginValid() ?? false;
- if (isPreLoginValid) {
- // 预登录有效,执行登录
- this.login(fullScreen);
- } else {
- // 预登录无效,执行预登录
- this.preLogin(() => {
- this.login(fullScreen);
- })
- }
- },
- preLogin(callback: (() => void)) {
- 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();
- },
- fail: (err) => {
- console.error("pre login fail => " + JSON.stringify(err));
- const hasCauseMessage = (err.cause?.cause?.message ?? '').length > 0
- uni.showModal({
- title: '预登录失败',
- content: hasCauseMessage ? JSON.parseObject(err.cause?.cause?.message ?? '')?.getString("errorDesc") : err.errMsg,
- showCancel: false
- });
- }
- });
- },
- login(fullScreen : boolean) {
- this.uniVerifyManager?.login({
- uniVerifyStyle:{
- fullScreen: fullScreen,
- loginBtnText: "一键登录",
- logoPath: "/static/logo.png"
- },
- success: (res) => {
- console.log("login success => " + JSON.stringify(res));
- this.takePhoneNumber(res.accessToken,res.openId);
- },
- fail: (err) => {
- console.error("login fail => " + err);
- const hasCauseMessage = (err.cause?.cause?.message ?? '').length > 0
- uni.showModal({
- title: '登录失败',
- content: hasCauseMessage ? 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);
- });
- }
- }
- }
- </script>
- <style>
- </style>/style>
|