thinphp6 对接飞书系统接口与更新数据

本文介绍了如何使用 ThinPHP6 框架对接飞书系统的接口并进行数据更新。详细步骤包括定义类与方法变量、逻辑层操作、继承父类、调用token公共方法、创建Http和Redis公共工具类,以及配置feishu.php和redis.php文件。
摘要由CSDN通过智能技术生成

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
/**
 * Created by PhpStorm
 * User: Gordon
 * Date-Time: 2022/7/12 10:06
 * Desc:飞书员工相关处理
 */


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';    //职位id
    public $aliasId = 'xxx';  //别名id


    //刷oa的用户数据到关联表中
    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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值