h5实现 微信的授权登录

本文重点

判断是不是微信环境
localstorage设置一个值
微信授权登录
获取一个时间戳 new Date().getTime()
const wx = (function () {
    return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
})()
if (wx) {
    const platformValue = 'wx'
    localStorage.setItem('temp', platformValue)   //如果现在是微信环境的话,做一个标识
}

const http = function (api, data = {}, withoutToken = false) {
    // 拼接接口参数
    const {
        url,
        method,
    } = isObject(api) ? api : {
        url: api,
        method: 'post',
    }
    const accessToken = vuex.getters.getAccessToken
    const v = '1.0'
    const platform = localStorage.getItem('temp')
    return axios({
        url: withoutToken ? `${url}?platform=${platform}&v=${v}` : `${url}?accessToken=${accessToken}&platform=${platform}&v=${v}`,
        method,
        data,
    }).catch(e => {
        throw new Error('网络异常')
    }).then(res => {
        if (res.data.code === 502) {
            this.$loadingEnd()
            throw new Error('未登录')
        } else if (res.data.code !== 200) {
            throw new Error(res.data.message[0].details)
        }
        return res.data
    })
}

export default http      //这一段是封装了一个http
Vue.prototype.$errorHandler = function (err) {
    if (err.message === '未登录') {  //判断现在如果是未登录状态并且是微信环境跳转到下面的链接,即微信授权登录页面,传需要传的参数就可以了,拼接到后面即可
        if (platform === 'wx') {
            const path = encodeURIComponent(location.href)
            location.replace('https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx34284214d7a79ba1&redirect_uri=' + path + '&response_type=code&scope=snsapi_userinfo&state=' + new Date().getTime() + '#wechat_redirect')
        } else { 
            this.$vux.toast.text('请先登录', 'middle')
            this.$router.replace({ path: '/login' })
        }
    } else {
        this.$vux.alert.show({
            title: '鲸保科技提示',
            content: `${err.message}`,
        })
    }
}

转载于:https://www.cnblogs.com/lml-lml/p/9836918.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值