微信公众号开发获取code,拿openid和绑定手机

 

https://mp.weixin.qq.com/wiki     微信公众号开发文档

 拿code是要在配置了appID,redirect_uri之后页面跳转回来的页面url上携带着code,需要截取。

将公众号代码让布到了服务器上  可以看效果,微信开发者工具很不方便,

getUrl() {
      let appID = "公众号的appId";
      let redirectUri = encodeURIComponent("这里是f跳转回来的页面");
      let strUrl =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
        appID +
        "&redirect_uri=" +
        redirectUri +
        "%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

      this.$nextTick(() => {
        window.location.href = strUrl;
      });
    },

appId和response_type 是自己定义的参数

跳回的页面的url 需要加encodeURIComponent()方法来处理

截取code

//截取code
    getCode() {
      let url = window.location.search;
      let start = window.location.search.indexOf("=");
      let end = window.location.search.indexOf("&");
      let code = url.substring(start + 1, end);
      return code;
    },

拿到code之后去请求后端,后端会返回openId 也可以拿到用户的昵称 头像

 

绑定手机号 另起页面 开通手机验证码服务

拿到数据发送给后端验证

getCode(formName){
            this.msg = '';
            const TIME_COUNT = 60;
            if (this.inputPhone) {
                 this.$http.post('这里是调取的后端接口名?phone='+ this.inputPhone, this.inputPhone)
                    .then(({ data }) => {
                        if(data.succeed) {
                            this.count = TIME_COUNT;
                            this.show = false;
                            this.btnShow = true;
                            this.timer = setInterval(() => {
                            if (this.count > 0 && this.count <= TIME_COUNT) {
                                this.count--;
                                } else {
                                this.btnShow = false;
                                this.show = true;
                                clearInterval(this.timer);
                                this.timer = null;
                                }
                            }, 1000)
                        } else {
                            //展示错误信息给用户
                            let errINFO = document.getElementById('errInfo')
                            this.msg = data.msg
                            this.changeMsg()
                        }
                    })
                
            } else {
                this.msg = '请输入手机号'
                this.changeMsg();
            }
        },
        //提交绑定
        submitInfo() {
           this.$http.post('这里是调取的后端接口名?phone='+ this.inputPhone +'&code='+ this.inputCode +'&openId='+ localStorage.getItem('openId'))
            .then(({ data }) => {
                if(data.succeed) {
                    this.$router.push('/production?userOK')
                } else {
                    this.msg = data.msg
                }
            })
        }

总结: 

开发时需项目上线自动布项目,之后拿到域名跳域名测试,前端push代码,服务器自动布项目。这也是开发的不便。

 

  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值