使用的框架Thinkphp3.2.3版本
微信公众平台地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
扫二维码进入的页面地址:AppWeb-report-certifiScope
控制器ReportController.classs.php代码:
/**
* 扫码进入的页面,引导用户跳转到统计页面
*/
public function certifScope(){
$common = new CommonController();
$res = $common->wxOauthGetInfo();
//如果session里面有数据,则不会再进行微信授权操作
if($res['is_redirect'] == 1){
redirect(U("Report-certificate"));
exit();
}
}
/**
* 邮政金融证书统计
* 扫码之后的跳转页面
*/
public function certificate(){
$common = new CommonController();
$res = $common->wxOauthGetInfo();
$userid = $res['userid'];
//查询用户是否已经填写过信息
$info = $common->getUserAllInfo($userid);
$is_add = $info['is_add'];//是否添加过用户信息 0 没有添加 1 添加过
if($is_add == 1){
//查看页面
redirect(U("Report-showcertificate?uid={$userid}"));
exit();
}else{
$data['uid'] = $userid;
$this->assign($data);
$this->display();
}
}
微信授权CommonController.class.php代码:
namespace AppWebCommon;
use ThinkController;
class CommonController extends Controller
{
var $openid;
var $unionid;
var $appid;
var $appsecret;
protected function _initialize(){
session_start();
$cg_wx = C("WEIXIN_OPTIONS");
$this->appid = $cg_wx['appid'];
$this->appsecret = $cg_wx['appsecret'];
}
public function wxOauthGetInfo(){
$scope = C("WEIXIN_WEB_OPTIONS")['scope'];
$this->user = session("user");
$this->openid = session("openid");
if(!$this->openid){
if (!isset($_GET['code'])){//没有code,去微信接口获取code码
$callback = "http://www.jinpeiwang.cn/AppWeb-report-certificate";//微信服务器回调url
$this->get_code($callback,$scope);
} else {//获取code后跳转回来到这里了
if($scope == 'snsapi_userinfo'){
//用户认证授权方式
$code = $_GET['code'];
$data = $this->get_access_token($code);//获取网页授权access_token和用户openid
session('openid', $data['openid']);
$data_all = $this->get_user_info($data['access_token'],$data['openid']);//获取微信用户信息
//查询用户是否已经存在openid
$has = $this->getInfoByOpenid($data_all['openid']);
if(isset($has) && !empty($has)){
//已存在
$this->updateCertificateUserByWx($has['id'],$data_all['openid'],$data_all['nickname'],$data_all['unionid'],$data_all['headimgurl']);
$addUserID = $has['id'];
}else{
$addUserID = $this->addCertificateUserByWx($data_all['openid'],$data_all['nickname'],$data_all['unionid'],$data_all['headimgurl']);
}
session('user', $addUserID);
}elseif($scope == 'snsapi_base'){
//静默授权方式
$code = $_GET['code'];
$data = $this->get_access_token($code);//获取网页授权access_token和用户openid
session('openid', $data['openid']);
//查询用户是否已经存在openid
$has = $this->getInfoByOpenid($data['openid']);
if(isset($has) && !empty($has)){
//已存在
$addUserID = $has['id'];
}else{
$addUserID = $this->addCertificateUserByWx($data['openid'],'','','');
}
session('user', $addUserID);
}
}
$is_redirect = 0;
}else{
if(!$this->user){
$has = $this->getInfoByOpenid($this->openid);
$addUserID = $has['id'];
session('user', $addUserID);
}else{
$addUserID = $this->user;
}
$is_redirect = 1;
}
return array('userid' => $addUserID,'is_redirect'=>$is_redirect);
}
/**
* @param $callback
* 获取code
*/
private function get_code($callback,$scope){
$appid = $this->appid;
$state = md5(uniqid(rand(), TRUE));//唯一ID标识符绝对不会重复
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
header("Location:$url");
}
/**
* @param $code
* @return mixed
* 根据code获取access_token
*/
private function get_access_token($code){
$appid = $this->appid;
$appsecret = $this->appsecret;
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
$user = json_decode(file_get_contents($url));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode.'<hr>msg :' . $user->errmsg;exit;
}
$data = json_decode(json_encode($user),true);//返回的json数组转换成array数组
return $data;
}
/**
* @param $access_token
* @param $openid
* @return mixed
* 获取授权用户的信息
*/
private function get_user_info($access_token,$openid){
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
$user = json_decode(file_get_contents($url));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode.'<hr>msg :' . $user->errmsg;exit;
}
$data = json_decode(json_encode($user),true);//返回的json数组转换成array数组
return $data;
}
/**
* @param $openid
* @param $nickname
* @param $unionid
* @param $headimgurl
* @return mixed
* 添加获取微信授权用户信息到数据表
*/
public function addCertificateUserByWx($openid,$nickname,$unionid,$headimgurl){
$cuser = M("certificate_user");
$data = array(
'openid' => $openid,
'nickname' => $nickname,
'unionid' => $unionid,
'headimgurl' => $headimgurl,
'is_add' => 0
);
$addID = $cuser->add($data);
return $addID;
}
/**
* @param $openid
* @return mixed
* 根据openid查询用户信息
*/
public function getInfoByOpenid($openid){
$cuser = M("certificate_user");
$res = $cuser->where(array('openid' => $openid))->field("id,openid")->find();
return $res;
}
/**
* @param $uid
* @param $openid
* @param $nickname
* @param $unionid
* @param $headimgurl
* @return bool
* 修改用户微信网页授权获取的信息
*/
public function updateCertificateUserByWx($uid,$openid,$nickname,$unionid,$headimgurl){
$cuser = M("certificate_user");
$data = array(
'nickname' => $nickname,
'unionid' => $unionid,
'headimgurl' => $headimgurl
);
$res = $cuser->where(array('id'=>$uid,'openid'=>$openid))->save($data);
return $res;
}
/**
* @return array|false|mixed|PDOStatement|string| hinkCollection
* 获取所有的证书
*/
public function getAllCertificates(){
$data = F("certificates_data");//数据放入到快缓存中
$data = json_decode($data,true);
if(!empty($data)){
$result = $data;
}else{
$certificate = M("certificate");
$res = $certificate->alias("c1")
->join("LEFT JOIN certificate c2 on c1.id=c2.parent")
->where(array('c1.`status`'=>1,'c1.parent'=>0,'c2.`status`'=>1))
->field("c1.id as pid,c1.`name` as pname,c1.parent,c2.id as cid,c2.`name` as cname,c2.parent as cparent")
->select();
$result = array();
if(isset($res) && !empty($res)){
foreach($res as $k => $v){
if(isset($result[$v['pid']])){
$arr = array('id'=>$v['cid'],'name'=>$v['cname']);
array_push($result[$v['pid']]['child'],$arr);
}else{
$result[$v['pid']] = array(
'id' => $v['pid'],
'name' => $v['pname'],
'child' => array(array(
'id' => $v['cid'],
'name' => $v['cname']
))
);
}
}
$result = array_merge($result);
F("certificates_data",json_encode($result));
}
}
return $result;
}
/**
* @param $uid
* @return array
* 根据uid获取用户信息
*/
public function getUserAllInfo($uid){
$certificate = M("certificate_user");
$res = $certificate->alias("cuser")
->where(array('cuser.id'=>$uid))
->field("cuser.id as uid,cuser.`name`,cuser.sex,cuser.idcard,cuser.education,cuser.birthday,cuser.phone,cuser.financial_time,cuser.post_status,cuser.province,cuser.city,cuser.area,cuser.branch,cuser.is_add,cuser.employ_type")
->find();
if(isset($res) && !empty($res)){
$result = $res;
$result['code'] = 1;
$result['birthdate'] = !empty($val['birthday'])?date('Y-m-d',$val['birthday']):'';
$result['financialdate'] = !empty($val['financial_time'])?date('Y-m-d',$val['financial_time']):'';
$result['list'] = $this->getCertificatesByUid($uid);
}else{
$result['code'] = 0;
}
return $result;
}
/**
* @param $uid
* @return Model|ThinkModel
* 根据会员编号获取用户证书
*/
public function getCertificatesByUid($uid){
$relation = M("certificate_user_relation");
$res = $relation->alias("relation")
->join("LEFT JOIN `certificate` c on relation.cid=c.id")
->join("LEFT JOIN certificate c1 on c.parent=c1.id")
->where(array('relation.uid'=>$uid))
->field("relation.id,relation.cid,relation.`cname`,c.`name`,relation.number,c1.`name` as pname")
->order("relation.id desc")
->select();
if(isset($res) && !empty($res)){
$result = array();
foreach($res as $k => $v){
if(empty($v['cid']) && $v['cname'] != ''){
$cname = $v['cname'];
}elseif(!empty($v['pname'])){
$cname = $v['pname'].'-'.$v['name'];
}else{
$cname = $v['name'];
}
$result[] = array(
'id' => $v['cid'],
'cname' => $cname,
'number' => $v['number']
);
}
$result = array_merge($result);
}else{
$result = array();
}
return $result;
}
}
转载于:https://www.cnblogs.com/lfjblog/p/10892800.html