微信授权登录后,把获取的信息存储!

微信授权登录后,把获取的信息存储到本地数据库中

<?php
namespace Home\Controller;
use Think\Controller;
class YiMuDiController extends Controller
{
    /**
     * 构造方法
     */
    public function __construct()
 
    {
        parent::__construct();
        $this->modelUserAccount = M('yimudi_account'); // 账户表
        $this->wxUserInfo = session('wxUserInfo');
        $this->uid = session('uid');
        $this->mobile = session('mobile');
        if (!$this->uid)
        {
            $this->uid = $this->getUserId()['user_id'];
            $this->mobile = $this->getUserId()['mobile'];
            session('uid', $this->uid);
            session('mobile', $this->mobile);
        }
    }
 
    /**
     * 获取微信用户信息
     * @return mixed
     */
    protected function getWxUserInfo()
    {
        $AppId = C('AppId'); //访问配置文件的appId
        $AppSecret = C('AppSecret'); //访问配置文件的appSecret
 
        // 获取微信 code
        $code = I('get.code');
        if (!$code)
        {
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            $redirectUri = $protocol . $_SERVER['HTTP_HOST'] . __SELF__;
            $redirectUri = urlencode($redirectUri);
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$AppId&redirect_uri=$redirectUri&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
            redirect($url);
        }
 
        // 获取微信网页授权 access_token
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$AppId&secret=$AppSecret&code=$code&grant_type=authorization_code";
        $userInfoJson = file_get_contents($url);
        $userInfo = json_decode($userInfoJson, true);
 
        // 获取微信用户信息
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$userInfo['access_token']}&openid={$userInfo['openid']}&lang=zh_CN";
        $userDetailInfoJson = file_get_contents($url);
        $userDetailInfo = json_decode($userDetailInfoJson, true);
        return $userDetailInfo;
    }
 
    /**
     * 获取用户 ID
     * @return mixed
     */
    protected function getUserId()
    {
        if (!$this->wxUserInfo)
        {
            $this->wxUserInfo = $this->getWxUserInfo();
            session('wxUserInfo', $this->wxUserInfo);
        }
        $this->openid = $this->wxUserInfo['openid'];
 
        // 1. 通过 openid,查询账户表获取 user_id
        $where = array(
            'openid' => $this->openid,
            'disabled' => 0,
        );
        $info = $this->yimudiAccount->where($where)->getField('user_id,mobile');
        if ($info)
        {
            $datainfo=[];
            $datainfo['user_id'] = $info['user_id];
             $datainfo['mobile'] = $info['mobile'];
            return $datainfo;
        }
        
    }
    /**
     * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @name 获取微信js-sdk配置参数
     * @method 传参请求
     * @param    当前执行程序的完整的URL
     * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     */
    public function getWxjsSdkConfig(){
        $appId = C('AppId');//访问配置文件的appId
        $appSecret = C('AppSecret');//访问配置文件的appSecret
        $nonceStr = $this->getRandomString(16);//签名参数1:随机字符串
        $jsapiTicket = $this->getJsApiTicket($appId,$appSecret);
        $timestamp = time();
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $actionUrl = $protocol . $_SERVER['HTTP_HOST'] . __SELF__;
        $signString = stripcslashes('jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$actionUrl);
        $signature = sha1($signString);
        $configData['AppId'] = $appId;
        $configData['AppSecret'] = $appSecret;
        $configData['timestamp'] = $timestamp;
        $configData['nonceStr'] = $nonceStr;
        $configData['signString'] = $signString;
        $configData['signature'] = $signature;
        return $configData;
    }
    
   
    /**
     * 模拟提交
     * @param string $url
     * @param array $data
     * @return bool|mixed
     */
    public function requestPost($url='', $data=array()) {
        if(empty($url) || empty($data)){
            return false;
        }
        $o="";
        foreach($data as $k=>$v){
            $o.="$k=".$v."&";
        }
        $param=substr($o,0,-1);
        $ch=curl_init();//初始化curl
        curl_setopt($ch,CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch,CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch,CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch,CURLOPT_POSTFIELDS, $param);
        $return=curl_exec($ch);//运行curl
        curl_close($ch);
        return $return;
    }
    /**
     * 文件日志
     * @param string $type
     * @param string $data
     */
    public function makeLog($type='',$data=''){
        if(!empty($type)){
            @file_put_contents(C('DIR_LOG').$type."/".$type.'_'.date('YmdH').'.txt',$data."\n",FILE_APPEND);
        }
    }
}

 

posted @ 2018-12-22 11:34  爱跑步的乌龟  阅读(888)  评论(0编辑  收藏  举报