uniapp 获取微信手机号码

20 篇文章 1 订阅
17 篇文章 0 订阅

 前端授权uniapp

<button type="default" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号码</button>
//授权手机号
decryptPhoneNumber (e) {
    let code = e.detail.code
    this.$http.https("post", "/index/login/phone", {
        'openid':uni.getStorageSync('openid'),//这里读缓存已获取到微信用户
        'code': code
    }).then(req => {
        // console.log(req)
        if(req.data.code == 200){
             //存缓存,我这里是为了不用每次都请求获取号码,如果只是单纯入库可以不要
            uni.setStorageSync('user', req.data.data); 
        }else{
            uni.showToast({
                title: req.data.message,
                icon: "none",
                duration: 2000
            })
        }
    })
},

后端 tp6 

public function phone(Request $reques){
        $appid = '';//小程序的appid
        $secret = '';//小程序的AppSecret
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $res = curl_exec($ch);
        curl_close($ch);
        
        $tmptoken = json_decode($res);
        $token = $tmptoken->access_token;
        
        $openid = $reques->param("openid");//前端传的用户
        $code = $reques->param("code");//前端传的code,必须的
        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$token";
        $data['code'] = $code;
        $date = $this->http_request($url,json_encode($data));
        $date = json_decode($date,true);
        $user = User::where('openid',$openid)->find();//查询是否存在
        if(!empty(user )){
            $user->phoneNumber = $date['phone_info']['phoneNumber'];
            $user->save();
            return json(['code'=>200,'data'=>$date,'msg'=>'ok']);
        }else{
            return json(['code'=>200,'msg'=>'用户不存在']);
        }
    }
    
    public function http_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
 
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json'
            ));
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
 
        return $output;
        exit();
 
    }

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值