企业微信自建应用授权登录demo

版权声明:本文为博主原创文章,未经博主允许不得转载。有问题可加微信meizu_mx4 https://blog.csdn.net/sinat_15955423/article/details/82181833
<?php
/**
 * Created by PhpStorm.
 * User: liubao
 * Date: 2018/8/29
 * Time: 10:49
 */

namespace Qywx\Controller;


use Think\Controller;

class BaseController extends Controller
{
    /*
    *
    * 微信授权登录
    *
    * */
    public $appId = 'wwd51800cfc7708391';
    public $appSecret = 't5fQh7lSMg8FPEqQrwcfPOOmWXz0cu2xqjey4BBDd28';
    public $agentid = '1000002';
    public $code = '';
    public $access_token = '';
    public $user_ticket = '';
    public $UserId = '';
    public $openid = '';

    public function index()
    {
        $uri = urlencode('http://www.crm-dev.com/index.php?s=/Qywx/Index/back'); //授权成功返回地址
        //下面$url请求授权登录地址,设置的是手动授权
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->appId . '&redirect_uri=' . $uri . '&response_type=code&scope=snsapi_privateinfo&agentid=' . $this->agentid . '&state=STATE#wechat_redirect';
        header('Location:' . $url); //不同框架用不同方法,yii 用$this->redirect(); tp直接header();
    }

    public function back()
    {
        $this->code = $_GET['code']; //接收上面url返回code,5分钟有效期
        try {
            $access_token = $this->getAccessToken();//获取token
            $user_ticket = $this->getUserInfoList();//获取基本信息里的user_ticket值
            $userInfo = $this->getUserInfoDetail();//获取登录人的详情信息
            if (isset($access_token) && isset($user_ticket) && isset($userInfo)) {
                //$openId = $this->getOpenId();//获取openID(——暂时不用)
                $name = $userInfo['name'];
                $user = M("db_user")->where("username='$name'")->find();
                session('user', $user);
                //授权成功!
                header('Location:http://www.baidu.com');
            }
        } catch (\Exception $e) {

        }
    }

    //获取access_token
    public function getAccessToken()
    {
        $code_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" . $this->appId . "&corpsecret=" . $this->appSecret;
        $access_token_str = $this->curl_get($code_url);
        $access_token_list = json_decode($access_token_str, true);
        $this->access_token = $access_token_list["access_token"];//获取access_token
        if ($access_token_list['errcode'] == 0) {
            return $this->access_token;
        }
        return false;
    }

    //获取登录人基本信息
    public function getUserInfoList()
    {
        $user_url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" . $this->access_token . "&code=" . $this->code;
        $userInfo_str = $this->curl_get($user_url);
        $userInfo_list = json_decode($userInfo_str, true);//get获取企业登录人基本信息
        $this->user_ticket = $userInfo_list['user_ticket'];
        $this->UserId = $userInfo_list['UserId'];
        if ($userInfo_list['errcode'] == 0) {
            return $userInfo_list;
        }
        return false;
    }

    //获取登录人的详情信息包括昵称邮箱和头像等
    public function getUserInfoDetail()
    {
        $detail_url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=" . $this->access_token;//post获取详细信息
        $param = array('user_ticket' => $this->user_ticket);
        $data = json_decode($this->curl_post($detail_url, $param), true);
        if ($data['errcode'] == 0) {
            return $data;
        }
        return false;
    }

    public function getOpenId()
    {
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=" . $this->access_token;
        $param = array('userid' => $this->UserId);
        $data = json_decode($this->curl_post($url, $param), true);
        if ($data['errcode'] == 0) {
            $this->openid = $data['openid'];
        }
        return $this->openid;
    }

    public function curl_get($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }

    public function curl_post($url, $param)
    {
        $param = json_encode($param);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);
        return $data;
    }


}

猜你喜欢

转载自blog.csdn.net/sinat_15955423/article/details/82181833