企業微信登錄Demo PHP


企業微信登錄Demo

 

1、開發文檔
https://qydev.weixin.qq.com/wiki/index.php

 

 

第一步:

在企業微信管理平台創建應用,獲取

agentid
Secret

第二步:

設置回調域名

具體步驟....  ???

 

第三步:

代碼邏輯實現

 

 

<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * 微信授權登錄
     *
     * */
    private $appId = 'wx90d3be38cf782f12';//我的企業ID
    private $appSecret = 'wp6hg8trJSTqjDDorwN5s5LP9_6_ha8YjmwHiYR8mMw';//自建應用里的Secret
    private $agentid = '1000049';//AgentId 手動授權時scope值改為snsapi_privateinfo 后面必須有AgentId
    private $common_url = 'http://test-qywx/back'; // 回調地址
    private $code = '';
    private $access_token = '';
    private $user_ticket = '';
    private $UserId = '';
    private $openid = '';

    public function index()
    {
        $uri = urlencode($this->common_url); //授權成功返回地址
//        $uri = urlencode($this->common_url . 'index.php?s=' . $action); //授權成功返回地址
        //下面$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);
    }

    public function back()
    {
        $this->code = $_GET['code']; //接收上面url返回code,5分鍾有效期
        try {
            $userInfo = $this->common();
            if (isset($userInfo)) {
                //$openId = $this->getOpenId();//獲取openID(——暫時不用)
                echo '<PRE>';print_r($userInfo);exit();
                return $userInfo;
            }
        } catch (\Exception $e) {
            return false;
        }
    }

    //獲取企業微信用戶信息
    public function common()
    {
        $access_token = $this->getAccessToken();//獲取token
        $user_ticket = $this->getUserInfoList();//獲取基本信息里的user_ticket值
        $userInfo = $this->getUserInfoDetail();//獲取登錄人的詳情信息
        if (isset($access_token) && isset($user_ticket) && isset($userInfo)) {
            return $userInfo;
        }
        return false;
    }

    //獲取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;
    }

    //根據userID獲取openID
    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;
    }

    /**
     * 文本消息推送
     * touser、toparty、totag不能同時為空
     * touser 成員ID列表(消息接收者,多個接收者用‘|’分隔,最多支持1000個)。特殊情況:指定為@all,則向該企業應用的全部成員發送
     * toparty 部門ID列表,多個接收者用‘|’分隔,最多支持100個。當touser為@all時忽略本參數
     * totag 標簽ID列表,多個接收者用‘|’分隔,最多支持100個。當touser為@all時忽略本參數
     * safe 表示是否是保密消息,0表示否,1表示是,默認0
     * @param $touser
     * @param $toparty
     * @param string $message //推送內容
     * @return true or false
     */
    public function push_message($touser, $toparty, $message)
    {
        $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" . $this->access_token;
        $params = array("touser" => $touser, "toparty" => $toparty, "totag" => "", "msgtype" => "text", "agentid" => $this->agentid, "text" => array("content" => $message), "safe" => 0);
        $data = json_decode($this->curl_post($url, $params), true);
        if ($data['errcode'] == 0) {
            return true;
        }
        return false;
    }

    /**
     * //發送推送文本卡片消息展現
     * @param $touser
     * @param $toparty
     * @param $title
     * @param $description
     * @param $url
     * @return bool|mixed
     */
    public function push_card($touser, $toparty, $title, $description, $url)
    {
        $access_token = $this->getAccessToken();
        $push_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" . $access_token;
        $params = array("touser" => $touser, "toparty" => $toparty, "totag" => "", "msgtype" => "textcard", "agentid" => $this->agentid, "textcard" => array("title" => $title, "description" => $description, "url" => $url, "btntxt" => ""));
        $data = json_decode($this->curl_post($push_url, $params), true);
        if ($data['errcode'] == 0) {
            return $data;
        }
        return false;

    }

    /**
     * 獲取部門列表
     * @param $department_id //部門id。獲取指定部門及其下的子部門。 如果不填,默認獲取全量組織架構
     * @return
     */
    public function get_department($department_id)
    {
        $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=" . $this->access_token . "&id=" . $department_id;
        $departmentInfo = $this->curl_get($url);
        $departmentInfo = json_decode($departmentInfo, true);//只能拉取token對應的應用的權限范圍內的部門列表
        if ($departmentInfo['errcode'] == 0) {
            return $departmentInfo['department'];//所有的部門信息
        }
        return false;

    }

    //curl get方式
    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;
    }

    //curl post方式
    public function curl_post($url, $param)
    {
        $param = json_encode($param,true);
        $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;
    }

    //判斷是否是企業微信登錄
    public function isQyWx()
    {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'wxwork') !== false) {
            return true;
        } else {
            return false;
        }
    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM