Univerify.uts 4.1 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import { loginSuccess } from '@/uni_modules/uni-id-pages-x/common/common.uts';
// 导入配置
import config from '@/uni_modules/uni-id-pages-x/config.uts'
const loginTypes = config.getArray<string>('loginTypes');

// import {servicesList,Services} from '@/uni_modules/uni-id-pages-x/lib/servicesList.uts';
// type Buttons = {
//   provider:string,
//   iconPath:string
// }
// const buttonsList:Buttons[] = []
// servicesList.forEach(services=>{
//   let buttons:Buttons = {
//     "provider": "apple",
//     "iconPath": "/static/apple.png" // 图标路径仅支持本地图片
//   }
//   buttonsList.push(buttons)
// })

// #ifdef uniVersion < 3.99

// #endif

雪洛's avatar
雪洛 已提交
24
// #ifdef APP && (uniVersion > 3.99 || uniVersion == 3.99)
DCloud_JSON's avatar
DCloud_JSON 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

const univerifyManager = uni.getUniverifyManager();
export default class Univerify {
  verify(fullScreen : boolean) {
    // 校验预登录是否有效
    const isPreLoginValid = univerifyManager.isPreLoginValid();
    if (isPreLoginValid) {
      // 预登录有效,执行登录
      this.login(fullScreen);
    } else {
      // 预登录无效,执行预登录
      univerifyManager.preLogin({
        success() {
          console.log("pre login success");
          this.login(fullScreen);
        },
        fail(err : PreLoginFail) {
          console.error("pre login fail => " + JSON.stringify(err));
          // 当前页面不是登录页面时,一键登录不可用,且还存在一键登录以外的登录方式,就跳转至此登录方式
          const pages = getCurrentPages()
          const currentPage = pages[pages.length - 1]
          const loginPageRoute = "uni_modules/uni-id-pages-x/pages/login/login"
          if(currentPage.route === loginPageRoute){
DCloud_JSON's avatar
DCloud_JSON 已提交
48 49 50 51
            uni.showModal({
              title: '预登录失败',
              content: JSON.parseObject(err.cause?.cause?.message ?? "")?.getString("errorDesc") ?? err.errMsg,
              showCancel: false
DCloud_JSON's avatar
DCloud_JSON 已提交
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
            });
          }else{
            // 获取除一键登录以外的登录方式
            let otherLoginTypes = loginTypes?.filter((type:string):Boolean => type != 'univerify')
            if(currentPage.route != loginPageRoute && otherLoginTypes!.length > 0){
              const type = otherLoginTypes[0]
              uni.navigateTo({
                url: "/" + loginPageRoute + "?type=" + type,
                fail(err){
                  console.log('err',err)
                  uni.showModal({
                    content: err.errMsg,
                    showCancel: false
                  });
                },
                success(){
                  console.warn("一键登录不可用,已自动切换登录方式,type:" + type)
                }
              })
            }
          }
        }
      } as PreLoginOptions);
    }
  }
  login(fullScreen : boolean) {
    univerifyManager.login({
      // 登录页样式
      univerifyStyle: {
        fullScreen: fullScreen,
        backgroundColor: "#FFFFFF",
        loginBtnText: "一键登录",
        logoPath: "/static/logo.png",
        // buttons:{
        //   "iconWidth": "45px", // 图标宽度(高度等比例缩放) 默认值:45px
        //   "list":buttonsList
        // }
      } as UniverifyStyle,
      success(res : LoginSuccess) {
        console.log("login success => " + JSON.stringify(res));
        // 云函数取号
        const uniIdCo = uniCloud.importObject("uni-id-co")
        uniIdCo.loginByUniverify({
          access_token: res.accessToken, // 客户端一键登录接口返回的access_token
          openid: res.openId // 客户端一键登录接口返回的openid
        }).then(res => {
          console.log('res',res)
          loginSuccess(res)
          univerifyManager.close();
        }).catch(err => {
          console.error(JSON.stringify(err));
          // 关闭登录页
          univerifyManager.close();
        });
      },
      fail(err : LoginFail) {
        console.error("login fail => " + err);
        // uni.showToast({
        //   title: "登录失败",
        //   icon: "error"
        // });
      }
    } as LoginOptions);
  }
}
// #endif