作者 刘锟

Merge remote-tracking branch 'origin/master' into akun

... ... @@ -53,7 +53,7 @@ class ProjectAssociationController extends BaseController
/**
* 获取aicc用户列表 并返回绑定的数据
* @return array|void
* @return void
*/
public function check()
{
... ... @@ -65,55 +65,23 @@ class ProjectAssociationController extends BaseController
$status = (int)$status ? ProjectAssociation::STATUS_NORMAL : ProjectAssociation::STATUS_DISABLED;
}
$isRes = $this->ProjectAssociationLogic->normal($project_id);
DB::beginTransaction();
try {
// 当数据不存在时并开启状态,自动添加一条数据
if (is_null($isRes) && (!is_null($status) && $status)) {
$isRes = $this->ProjectAssociationLogic->disabled($project_id);
if (is_null($isRes)) {
$isRes = new ProjectAssociation();
}
$isRes->project_id = $project_id;
$isRes->user_id = (int)env('AICC_WECHAT_USER_ID');
$isRes->status = $status;
$isRes->save();
DB::commit();
} // 关闭状态
elseif (!is_null($isRes) && (!is_null($status) && empty($status))) {
$isRes->status = $status;
$isRes->save();
DB::commit();
return [
'code' => Code::SUCCESS,
'data' => [],
'message' => '关闭AICC绑定成功!',
];
// 当数据不存在时并开启状态,自动添加一条数据
if (is_null($isRes) && (!is_null($status) && $status)) {
$bool = $this->ProjectAssociationLogic->saveProject($project_id, $status);
if (empty($bool)) {
// 保存数据失败
$this->response('error', Code::SERVER_ERROR);
}
} catch (\Exception $exception) {
DB::rollBack();
// 数据错误,请重试
$this->response('error', Code::SERVER_ERROR);
}
if (is_null($isRes)) {
} // 关闭状态
elseif (!is_null($isRes) && (!is_null($status) && empty($status))) {
$res = $this->ProjectAssociationLogic->closedState($isRes, $status);
$this->response($res['message'], $res['code']);
} elseif (is_null($isRes)) {
// 请开启AICC绑定
$this->response('success', Code::SERVER_ERROR);
}
$redis_key = 'aicc_friend_lists_' . (int)env('AICC_WECHAT_USER_ID');
$result = isset($cache) ? false : redis_get($redis_key);
if (empty($result)) {
$url = env('AICC_URL') . env('AICC_WECHAT_FRIEND_API_URL');
$result = curlGet($url);
redis_set($redis_key, json_encode($result), 60);
} else {
$result = json_decode($result, true);
$this->response('error', Code::SERVER_ERROR);
}
$result['info'] = [
'friend_id' => $isRes->friend_id ?? 0,
'nickname' => $isRes->nickname ?? '',
'user_name' => $isRes->user_name ?? '',
'image' => $isRes->image ?? '',
];
$cache = isset($cache);
$result = $this->ProjectAssociationLogic->getAiccWechatLists($isRes, $cache);
$this->response('success', Code::SUCCESS, $result);
}
}
... ...
... ... @@ -47,4 +47,84 @@ class ProjectAssociationLogic extends Logic
{
return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_DISABLED)->first();
}
/**
* 初始化数据/修改数据
* @param int $project_id
* @param int $status
* @return bool
*/
public function saveProject($project_id, $status)
{
$bool = false;
DB::beginTransaction();
$isRes = $this->disabled($project_id);
if (is_null($isRes)) {
$isRes = new ProjectAssociation();
}
$isRes->project_id = $project_id;
$isRes->user_id = (int)env('AICC_WECHAT_USER_ID');
$isRes->status = $status;
try {
$bool = $isRes->save();
DB::commit();
} catch (\Exception $exception) {
DB::rollBack();
}
return $bool;
}
/**
* 获取AICC微信列表数据
* @param ProjectAssociation $res
* @param bool $cache
* @return mixed
*/
public function getAiccWechatLists($res, $cache = false)
{
$redis_key = 'aicc_friend_lists_' . (int)env('AICC_WECHAT_USER_ID');
$result = $cache ? false : redis_get($redis_key);
if (empty($result)) {
$url = env('AICC_URL') . env('AICC_WECHAT_FRIEND_API_URL');
$result = curlGet($url);
redis_set($redis_key, json_encode($result), 60);
} else {
$result = json_decode($result, true);
}
$result['info'] = [
'friend_id' => $res->friend_id ?? 0,
'nickname' => $res->nickname ?? '',
'user_name' => $res->user_name ?? '',
'image' => $res->image ?? '',
];
return $result;
}
/**
* 关闭状态
* @param ProjectAssociation $res
* @param int $status 1 - 正常, 0 - 禁用
* @return array
*/
public function closedState($res, $status)
{
DB::beginTransaction();
try {
$res->status = $status;
$res->save();
DB::commit();
$code = Code::SUCCESS;
$message = '关闭AICC绑定成功!';
} catch (\Exception $exception) {
DB::rollBack();
// 数据错误,请重试
$code = Code::SERVER_ERROR;
$message = 'error';
}
return [
'code' => $code,
'data' => [],
'message' => $message,
];
}
}
... ...