PHP code example of lspbupt / yii2-dingtalk

1. Go to this page and download the library: Download lspbupt/yii2-dingtalk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lspbupt / yii2-dingtalk example snippets


return [
    'components' => [
        'dingtalk' => [
             'class' => '\lspbupt\dingtalk\Dingtalk',
             'agentid' => '', //您的应用的agentid
             'corpid' => '',  //您的企业corpid
             'corpsecret' => '', //您的企业的corpsecret
        ],
        // ....
    ],
];

//在代码中实现了少量的接口,后续会不断补充,示例如:

// 下述代码会获取当前企业的部门列表
$data = Yii::$app->dingtalk->getDepartmentList();

//下述代码会给用户id为1的用户发一条测试的钉钉消息
$data = Yii::$app->dingtalk->sendTextMsg(1, "", "测试一下企业消息");

//通过GET方法获取dingding的企业部门列表
$data = Yii::$app->dingtalk->setGet()->httpExec("/department/list", []);
//通过POST JOSN来发送钉钉消息
$data = Yii::$app->dingtalk->setPostJson()->httpExec("/message/send", [
    'touser' => $user,
    'toparty' => $toparty,
    'agentid' => Yii::$app->dingtalk->agentid,
    'msgtype' => 'text',
    'text' => [
        'content' => '测试一下企业消息'
    ],
]);

echo \lspbupt\dingtalk\JsapiConfig::widget([
    'jsApiList' => ["runtime.permission.requestAuthCode"], //本页面需要使用的jsapi,本例中为免登服务
    'successJs' => 'function(){ //jsapi配置好后执行的JS回调,我们可以在此处开始写执行的代码
         dd.runtime.permission.requestAuthCode({
             corpId: "'.\Yii::$app->dingtalk->corpid.'",
             onSuccess: function(result) {
                 $.ajax({
                     url: "", //此处填上根据code登录的url
                     data: {
                         code: result.code
                     },
                     success: function(data){  //处理成功请求
                     },
                 });
             },
             onFail : function(err) {
                 //alert(err.errmsg);
             }
         });
    }',
    //'errorJs' => 'function(){}', //错误时的JS,默认会输出错误的信息
]);

    echo \lspbupt\dingtalk\widgets\JsapiPcConfig::widget([
        'jsApiList' => ["runtime.permission.requestAuthCode"],
        'successJs' => 'function(){
            //参见上节
        }',
    ]);
  

return [
    'components' => [
        'dingapp' => [
             'class' => '\lspbupt\dingtalk\DingApp',
             'appid' => '',     // appid
             'appsecret' => '',  // appsecret
             'redirectUrl' => "", //登录成功后跳回的地址
         ],
        // ....
    ],
];

$ret = Yii::$app->dingapp->getUserInfoByTmpCode($code, $userInfo);
if($ret) {
    //代表不成功,$ret代表错误信息
}
// $userInfo中有用户的信息,可以根据$userInfo来获取用户,并执行登录操作

return [
    'components' => [
        'dingcrypt' => [
            'class' => '\lspbupt\dingtalk\helpers\CryptHelper',
            'aesKey' => '', // aeskey
            'token' => '',  //加密的token
        ],
        // 其它配置 
    ],
]

public class xxController extends Controller
{
    public function behaviors()
    {
        return [
            'dingcallback' => [
                'class' => \lspbupt\dingtalk\filters\CallbackFilter::className(),
                'actions' => ['index'], //你需要钉钉处理解密的请求
            ],
        ],
    }
    
    public function xxAction()
    {
        //获取钉钉传过来的数据进行处理
        $postArr = Yii::$app->request->post();
    }
}