Univerify.uts 4.0 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
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)
// })

A
Anne_LXM 已提交
20
// #ifdef uniVersion < 3.98
DCloud_JSON's avatar
DCloud_JSON 已提交
21 22 23

// #endif

A
Anne_LXM 已提交
24
// #ifdef APP && uniVersion >= 3.98
DCloud_JSON's avatar
DCloud_JSON 已提交
25 26 27 28

const univerifyManager = uni.getUniverifyManager();
export default class Univerify {
  verify(fullScreen : boolean) {
A
Anne_LXM 已提交
29 30 31 32 33 34 35
    // 校验预登录是否有效
    const isPreLoginValid = univerifyManager.isPreLoginValid();
    if (isPreLoginValid) {
      // 预登录有效,执行登录
      this.login(fullScreen);
    } else {
      // 预登录无效,执行预登录
DCloud_JSON's avatar
DCloud_JSON 已提交
36
      univerifyManager.preLogin({
A
Anne_LXM 已提交
37
        success() {
DCloud_JSON's avatar
DCloud_JSON 已提交
38
          console.log("pre login success");
A
Anne_LXM 已提交
39 40 41
          this.login(fullScreen);
        },
        fail(err : PreLoginFail) {
DCloud_JSON's avatar
DCloud_JSON 已提交
42 43 44 45 46 47
          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
            });
          }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)
                }
              })
            }
          }
A
Anne_LXM 已提交
73 74 75
        }
      } as PreLoginOptions);
    }
DCloud_JSON's avatar
DCloud_JSON 已提交
76 77
  }
  login(fullScreen : boolean) {
A
Anne_LXM 已提交
78 79 80 81 82 83
    univerifyManager.login({
      // 登录页样式
      univerifyStyle: {
        fullScreen: fullScreen,
        backgroundColor: "#FFFFFF",
        loginBtnText: "一键登录",
DCloud_JSON's avatar
DCloud_JSON 已提交
84 85 86 87
        logoPath: "/static/logo.png",
        // buttons:{
        //   "iconWidth": "45px", // 图标宽度(高度等比例缩放) 默认值:45px
        //   "list":buttonsList
A
Anne_LXM 已提交
88 89 90 91
        // }
      } as UniverifyStyle,
      success(res : LoginSuccess) {
        console.log("login success => " + JSON.stringify(res));
DCloud_JSON's avatar
DCloud_JSON 已提交
92 93
        // 云函数取号
        const uniIdCo = uniCloud.importObject("uni-id-co")
A
Anne_LXM 已提交
94 95 96
        uniIdCo.loginByUniverify({
          access_token: res.accessToken, // 客户端一键登录接口返回的access_token
          openid: res.openId // 客户端一键登录接口返回的openid
DCloud_JSON's avatar
DCloud_JSON 已提交
97 98 99 100
        }).then(res => {
          console.log('res',res)
          loginSuccess(res)
          univerifyManager.close();
A
Anne_LXM 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        }).catch(err => {
          console.error(JSON.stringify(err));
          // 关闭登录页
          univerifyManager.close();
        });
      },
      fail(err : LoginFail) {
        console.error("login fail => " + err);
        // uni.showToast({
        //   title: "登录失败",
        //   icon: "error"
        // });
      }
    } as LoginOptions);
  }
DCloud_JSON's avatar
DCloud_JSON 已提交
116 117
}
// #endif