thinphp6 对接飞书系统接口与更新数据
添加链接描述
第一步:定义类与方法变量
<?php
namespace app\api\controller;
class Feishu
{
public function index(){
try{
$class=request()->param('class/s')?:'';
$action=request()->param('action/s')?:'';
$class='app\util\feishu\\'.$class;
$class=new $class();
$class->$action();
return json(['code'=>200,'msg'=>'success']);
}catch (\Exception $e){
setLog($e->getLine().$e->getMessage(),'feishu_error','Feishu');
return json(['code'=>400,'msg'=>$e->getLine().$e->getMessage()]);
}
}
public function submit(){
try{
$class=request()->param('class/s')?:'';
$action=request()->param('action/s')?:'';
$class='app\util\feishu\people_admin\\'.$class;
$class=new $class();
$res=$class->$action();
dump($res);
}catch (\Exception $e){
setLog($e->getLine().$e->getMessage(),'feishu_error','Feishu');
return json(['code'=>400,'msg'=>$e->getLine().$e->getMessage()]);
}
}
}
第二步:方法:逻辑层操作
<?php
namespace app\util\feishu\people_admin;
use app\index\model\FeishuUser;
use app\index\model\User;
use app\index\model\UserJob;
use app\util\feishu\feishu;
use app\util\feishu\Token;
use app\util\Http;
class Employees extends feishu
{
public $url = 'https://open.feishu.cn/open-apis/people_admin/v1/employees';
public $jobId = 'xxx';
public $aliasId = 'xxx';
public function oaUser()
{
try {
$userList = User::where([['status', '=', 1], ['IsTest', '=', 0]])->select();
$tmp = [];
foreach ($userList as $k => $v) {
$data = [];
$data['oaId'] = $v['id'];
$data['fsId'] = 'fsyg_' . $v->id;;
$tmp[] = $data;
}
FeishuUser::insertAll($tmp);
setLog('完成', 'SyncStaff');
dump('完成');
} catch (\Exception $e) {
setLog($e->getLine() . '.' . $e->getMessage(), 'SyncStaff_err');
dump($e->getLine() . '.' . $e->getMessage());
}
}
public function CreateStaff()
{
try {
$data = [];
$data['external_id'] = 'ceshi_1';
$data['i18n_name'] = [];
$data['i18n_name']['zh_CN'] = '测试1';
$data['mobile'] = [];
$data['mobile']['phone'] = '16638233773';
$data['employee_type_id'] = "1";
$data['department_ids'] = [];
$data['department_ids'][] = "0";
$Token = $this->getToken('ceshi1');
if (isset($Token['code']) && $Token['code'] == 200) {
$head[] = 'Authorization:Bearer ' . $Token['tenant_access_token'];
$head[] = 'Content-Type:application/json';
} else {
throw new \Exception('6001.' . $Token['msg']);
}
$res = (new Http())->setArrData($data)->setHeaders($head)->setUrl($this->url)->post();
setLog(json_encode($res), 'CreateStaff', 'Employees');
$res = json_decode($res, true);
if (isset($res['code']) && $res['code'] == 0) {
return ['code' => 0, 'msg' => '创建员工成功,id为:' . $res['data']['id']];
} else {
return ['code' => 6003, 'msg' => '创建员工失败:' . $res['msg']];
}
} catch (\Exception $e) {
setLog($e->getLine() . '.' . $e->getMessage(), 'CreateStaff', 'Employees');
throw new \Exception($e->getLine() . '.' . $e->getMessage(), '6002');
}
}
public function