Vue 移动端开发实现微信登录

1 打包app,需要去微信开放者平台申请(https://open.weixin.qq.com/) appid 和 appsecret

    // 跳转到微信
    into_weixin (headImage) {
      let ts = this
      var auths = null
      var aweixin = null
      plus.oauth.getServices(function (services) {
        auths = services
        var s
        for (var i = 0; i < auths.length; i++) {
          if (auths[i].id == 'weixin') {
            s = auths[i]
            break
          }
        }
        if (!s.authResult) {
          console.log('准备拉起微信')
          s.login(function (e) {
            console.log('e的内容' + JSON.stringify(e))
            var info = plus.push.getClientInfo()
            console.log('推送:' + info.clientid)
            var access_token = e.target.authResult.access_token
            var openid = e.target.authResult.openid
            console.log(access_token + '|||' + openid)
            window.localStorage.opendId = openid

            var ajax = new XMLHttpRequest()
            // 使用post请求
            ajax.open('get', 'http://59.111.110.250:8011/users/tbUser/weixin/login/?openid=' + openid + '&access_token=' + access_token + '&cid=' + info.clientid)
            // 如果 使用post发送数据 必须 设置 如下内容
            // 修改了 发送给 服务器的 请求报文的 内容
            // 如果需要像 HTML 表单那样 POST 数据,请使用 setRequestHeader() 来添加 HTTP 头。然后在 send() 方法中规定您希望发送的数据:
            ajax.setRequestHeader('Content-type', 'application/json; charset=utf-8')
            // 发送
            // post请求 发送的数据 写在 send方法中
            // 格式 name=jack&age=18 字符串的格式
            ajax.send('')
            // 注册事件
            ajax.onreadystatechange = function () {
              if (ajax.readyState == 4 && ajax.status == 200) {
                var returnData = JSON.parse(ajax.responseText)
                // console.log('ajas数据' + returnData)
                // var user =   JSON.stringify(returnData.body.user)
                // var token =    JSON.stringify(returnData.body.user)
                localStorage.setItem('users', JSON.stringify(returnData.body))
                console.log(JSON.stringify(returnData.body))
                // windows.href="https://www.baidu.com";
              }
            }
            ts.login()
          }, function (e) {
            console.log('登录认证失败')
            // console.log('返回数据' + JSON.stringify(e))
            console.log('返回数据' + JSON.stringify(s))
          })
        } else {
          // 已经登录认证
          // console.log('返回数据' + JSON.stringify(e))
          console.log('返回数据' + JSON.stringify(s))
          console.log('登录成功')
          ts.login() // 自己的登录结口
          // let parm = {}
          // parm.openId = s.authResult.openid
          // parm.phone = ''
          // parm.password = ''
          // loginForUser(JSON.stringify(parm), function (res) {
          //   if (parseInt(res.code) == 608) {
          //     Toast({
          //       message: '授权成功,您还未注册,请点击注册。您已注册可直接登录'
          //     })
          //   }
          //   if (res.success == true) {
          //     window.localStorage.userinfo = JSON.stringify(res.object)
          //     window.localStorage.authorization = res.addition
          //     ts.$router.push('/Home')
          //   }
          // })
        }
      }, function (e) {
        Toast({
          message: '登录认证失败'
        })
        console.log('获取登录失败:' + e.message + ' - ' + e.code)
        console.log('登录认证失败')
      })
      // this.$router.push({
      //   path: '/index',
      //   name: 'index'
      // })
    },
发布了70 篇原创文章 · 获赞 67 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_37896578/article/details/96481098