<?php
declare (strict_types = 1);
namespace app\controller;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use think\facade\Config;
/**
* 禅道
*/
class ChanDao
{
/**
* 禅道域名
* @var string
*/
private string $baseUrl;
public function __construct()
{
$this->baseUrl = Config::get('chandao.baseurl');
}
/**
* 禅道免密登录
* @return string
*/
public function sso(string $account)
{
$time = time();
$code = Config::get('chandao.code');
$key = Config::get('chandao.key');
$token = md5($code . $key . $time);
$params = [
'm' => 'user',
'f' => 'apilogin',
'account' => $account,
'code' => $code,
'time' => $time,
'token' => $token,
];
return $this->baseUrl . '?' . http_build_query($params);
}
/**
* 获取禅道的token,用于后续操作
* @return false|mixed|\Redis|string
* @throws GuzzleException
*/
private function getToken()
{
$redis = \app\Pedis::instance();
$redisKey = \app\RedisKey::chandaoToken();
try {
if ($redis->exists($redisKey)) {
return $redis->get($redisKey);
} else {
$client = new Client();
$response = $client->Request('POST', $this->baseUrl . '/v1/tokens', [
'headers' => [
'content-type' => 'application/x-www-form-urlencoded'
],
'body' => '{"account": "admin", "password": "Sujrex@32"}'
]
);
$data = $response->getBody()->getContents();
$data = json_decode($data, true);
$redis->set($redisKey, $data['token']);
$redis->expireAt($redisKey, strtotime(date('Y-m-d') . ' 23:59:59'));
return $data['token'];
}
} catch (\Exception $e) {
return false;
}
}
/**
* 创建禅道用户
* 需要保存禅道用户id,便于后续的操作
* @param string $account
* @param string $pwd
* @return string|void
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function save(string $account, #[\SensitiveParameter] string $pwd)
{
$client = new Client();
try {
$response = $client->request('POST', $this->baseUrl . '/v1/users', [
'headers' => [
'token' => $this->getToken(),
],
'body' => "{'account': '{$account}', 'password': '{$pwd}', 'realname': '{$account}', 'visions':'rnd'}"
]);
return $response->getBody()->getContents();
} catch (ClientException $e) {
print_r($e->getMessage());
}
}
/**
* @param int $id
* @return string|void
* @throws GuzzleException
*/
public function read(int $id)
{
$client = new Client();
try {
$response = $client->request('GET', $this->baseUrl . '/v1/users/' . $id, [
'headers' => [
'token' => $this->getToken(),
]
]);
return $response->getBody()->getContents();
} catch (ClientException $e) {
print_r($e->getMessage());
}
}
/**
* 跟新禅道用户信息
*
* @param int $id
* @param array $info <br/>
* <p>dept int 否 所属部门</p>
* <p>role string 否 角色</p>
* <p>mobile string 否 手机号</p>
* <p>realname string 否 真实姓名</p>
* <p>email string 否 邮箱</p>
* <p>phone string 否 电话号码</p>
* @return string
* @throws GuzzleException
*/
public function update(int $id, array $info)
{
$client = new Client();
try {
$response = $client->request('PUT', $this->baseUrl . '/v1/users/' . $id, [
'headers' => [
'token' => $this->getToken(),
],
'body' => json_encode($info)
]);
return $response->getBody()->getContents();
} catch (ClientException $e) {
print_r($e->getMessage());
}
}
/**
* 删除禅道用户
* @param $id
* @return string|void
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function delete(int $id)
{
$client = new Client();
try {
$response = $client->request('DELETE', $this->baseUrl . '/v1/users/' . $id, [
'headers' => [
'token' => $this->getToken(),
]
]);
return $response->getBody()->getContents();
} catch (ClientException $e) {
print_r($e->getMessage());
}
}
}