sd-account

水滴登录模块

Usage no npm install needed!

<script type="module">
  import sdAccount from 'https://cdn.skypack.dev/sd-account';
</script>

README

sd-account

INSTALL

$ yarn add sd-account

USEAGE

import account from 'sd-account' 

// appid(项目appid)
// userThirdScope(授权方式base 或者 user_info)
// thirdType(1,hz;3,sdc;7,sdb)
// codeExtend; 一个对象,用来扩展错误码拦截
Vue.use(account, {
  appid: 'wx536456456fsfsd',
  userThirdScope: 'base',
  thirdType: 1,
  codeExtend: {
    23230: () => {
      // action
    },
    20021: () => {
      // action
    }
  }
})

this.$sdAccount.login().then(() => {
  console.log('登录成功')
})

// 或者重置参数:
this.$sdAccount.login({
  url: `https://www.shuidix.com`,
  userThirdScope: `base` || `user_info`
})

API

//登录
// setting:可选,会重写Vue.use时传入的参数
// 允许重置两个参数: url, userThirdScope
login(setting)

//拦截错误码
interceptHttpCode(code)

//发送验证码, phone 是手机号, done是成功回调, fail是失败回调
sendVerifyCode({
 'mobile': mobile
}).then()

//config 包含{mobile: config.phone, verifyCode:config.verifyCode}. done, fail同上
loginWithMobile(config, done, fail)

// 是否登录
isLogin()

// 使用手机号+code登录
loginWithLongCode({
  code: '',
  mobile: '',
  thirdType: ''
}).then().catch()
...
更多接口在index.js查看

TIPS

// 方法需替换原来interceptHttpCode, 在'api/utils/index.js'
Vue.sdAccount.interceptHttpCode(code)

// 该字段在每次执微信行授权行为时都会刷新,用于标识当次授权行为是静默授权还是显式授权
// value值为user_info 或者 base
storage.get('tokenType')

// 推荐写法(in App.vue)
this.$router.afterEach((to, from) => {
  const needLogin = to.meta.login
  const plat = platform()

  if (needLogin) {
    if (plat === 1) {
      this.$sdAccount.login().then(() => {
        this.renderView = true
      }).catch(e => {
        console.log(e)
      })
    } else {
      if (!this.$sdAccount.isLogin()) {
        this.$showLogin(() => {
          this.renderView = true
        })
      } else {
        this.renderView = true
      }
    }
  } else {
    this.renderView = true
  }
})