• php 微信授权登录,获取微信用户信息



    public function _initialize(){
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,Authorization");
    header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');

    }

    //微信授权登录

    public function is_login(){
    $openid = session('weixin_user.openid');
    // dump($openid);exit;
    if(empty($openid)){
    return json(['code'=>2,'msg'=>'未授权登录']);
    }else{
    $data = db('weixin_user')->where(['openid'=>$openid])->find();
    $data['nickname'] = base64_decode($data['nickname']);
    return json(['code'=>1,'data'=>$data]);
    }
    }


    public function index($value='')
    {
    return $this->fetch();
    }


    /**
    * =========获取微信用户详细信息=====================
    **/
    public function wechatLogin($my_redirect_url=''){
    //游戏支付
    $AppID='   ';
    $AppSecret ='   ';


    $param = input('param.');
    // $my_redirect_url = $param['my_redirect_url'];

    // $my_redirect_url = 'http://demo01.mjgxym.cn/zjjy/zjjy/index.php/index/index/index';

    $callback = 'http://www.zhanluo.top/h5game/index.php/index/index/wechatCllBack';
    //url('index/index/wechatCllBack'); //回调地址
    // $callback = 'http://client.rup-china.com/bd20180409/index.php/index/index/wechatCllBack';
    //微信登录
    //-------生成唯一随机串防CSRF攻击
    $state = md5(uniqid(rand(), TRUE));
    $_SESSION["wx_state"] = $state; //存到SESSION snsapi_userinfo snsapi_base
    $callback = urlencode($callback);
    $wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$AppID."&redirect_uri=".$callback."&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
    header("Location: $wxurl");
    exit;

    }
    //微信登录回调函数
    public function wechatCllBack() {
    //游戏支付
    $AppID='   ';
    $AppSecret ='   ';

    // $my_redirect_url = input('param.my_redirect_url');
    // $my_redirect_url = 'http://client.rup-china.com/bd20180409/weixin/index.html';
    $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    $json = curl_exec($ch);
    curl_close($ch);
    $arr=json_decode($json,1);
    //得到 access_token 与 openid

    $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';
    // dump($url);exit;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    $json = curl_exec($ch);
    curl_close($ch);
    $arr=json_decode($json,1);

    header("content-type:text/html;charset=utf-8"); //设置编码

    $this->_login_handle($arr);
    }

    private function _login_handle($user_info,$my_redirect_url='',$from=''){
    $user_oauth = Db('weixin_user')->where(array("openid"=>$user_info['openid']))->find();
    if(empty($user_oauth)){
    /*二【如果用户不存在】
    * @1添加该用户到users
    */
    $nickname = base64_encode($user_info['nickname']);
    $new_user_data = array(
    'openid' => $user_info['openid'],
    'nickname' => $nickname, //名称
    'sex' => $user_info['sex'],//性别
    'city' => $user_info['city'],//城市
    'province' => $user_info['province'],//省份
    'country' => $user_info['country'], //国家
    'headimgurl' => $user_info['headimgurl'],//头像
    'create_time' => date("Y-m-d H:i:s"),
    );
    Db('weixin_user')->insertGetId($new_user_data);

    // $new_user_data['id'] = Db('weixin_user')->insertGetId($new_user_data);
    $weixin_user_data = Db('weixin_user')->where('openid',$user_info['openid'])->find();

    $user_oauth = $weixin_user_data;
    }

    session('weixin_user',$user_oauth);

    $openid = session('weixin_user.openid');
    // dump($openid);exit;
    // return $this->view('index/index');
    // if(empty($my_redirect_url)){
    // // $this->redirect(url('index'));
    // }else{
    $my_redirect_url = 'http://www.zhanluo.top/h5game/index.php/index/index/index';
    header("Location: $my_redirect_url");exit;

    // }
    }

    //获取用户信息    编码昵称头像

    public function login_info()
    {
    $data1 = db('result_details')->alias('r')->join('weixin_user w','w.pid=r.id')->order('w.id desc')->paginate(15);
    // dump($data1);exit;

    $data = [];
    foreach ($data1 as $k => $v) {
    $data[$k]['nickname'] = base64_decode($v['nickname']);
    $data[$k]['headimgurl'] = $v['headimgurl'];
    $data[$k]['answer'] = $v['answer'];
    $data[$k]['id'] = $v['id'];
    $data[$k]['pid'] = $v['pid'];
    $data[$k]['name'] = $v['name'];
    $data[$k]['character'] = $v['character'];
    $data[$k]['product'] = $v['product'];
    $data[$k]['nature'] = $v['nature'];
    $data[$k]['evaluation'] = $v['evaluation'];
    $data[$k]['career1'] = $v['career1'];
    $data[$k]['career2'] = $v['career2'];
    $data[$k]['pic'] = $v['pic'];
    }

    // dump($data);exit;
    $page = $data1->render();


    $this->assign('data', $data);
    $this->assign('page', $page);

    return $this->fetch();
    }

    //页面展示html

    {volist name="data" id="vo"}
    <tr>
    <td data-field="" id="sub" ><img src="{$vo.headimgurl}" style="height: 60px"></td>
    <td data-field="" id="sub" >{$vo.nickname}</td>
    <td data-field="" id="sub" >{$vo.answer}</td>
    <td data-field="" id="sub" ><a href="{:url('Index/details',['pid'=>$vo.pid])}">点击查看</a></td>
    <th>
    <a href="###" id="{$vo.id}" onclick="del(this)"><i class="layui-icon">删除 &#xe640;</i></a>
    </th>
    </tr>
    {/volist}

  • 相关阅读:
    Springboot整合dubbo搭建基本的消费、提供和负载均衡
    SpringBoot与Dubbo整合的三种方式
    Dubbo整合SpringBoot
    Java 如何有效地避免OOM:善于利用软引用和弱引用
    finalize的作用
    垃圾回收
    不同JDK版本之间的intern()方法的区别-JDK6 VS JDK6+
    Java8内存模型—永久代(PermGen)和元空间(Metaspace)
    to meet you
    Atomic long 和long的区别
  • 原文地址:https://www.cnblogs.com/mcll/p/9455182.html
Copyright © 2020-2023  润新知