作者 lyh
... ... @@ -259,6 +259,7 @@ class ProjectUpdate extends Command
}
//分类
$category_id = '';
$category_arr = [];
if ($item['category'] ?? []) {
$category_arr = $category_model->list(['original_id' => ['in', array_column($item['category'], 'id')]]);
$category_id = $logic->getLastCategory(array_column($category_arr, 'id'));
... ... @@ -291,7 +292,6 @@ class ProjectUpdate extends Command
'route' => $route
]);
$this->set_map($route, RouteMap::SOURCE_PRODUCT, $id, $project_id);
CategoryRelated::saveRelated($id, array_column($category_arr, 'id'));
CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_PRODUCT, $id, $link_type, $language_list, $page_list);
} else {
$id = $product['id'];
... ... @@ -309,6 +309,10 @@ class ProjectUpdate extends Command
]),
'sort' => $item['sort'] ?? 0,
], ['id' => $id]);
}
//关联分类
if($category_arr){
CategoryRelated::saveRelated($id, array_column($category_arr, 'id'));
}
... ...
... ... @@ -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();
$bool = $this->ProjectAssociationLogic->saveProject($project_id, $status);
if (empty($bool)) {
// 保存数据失败
$this->response('error', Code::SERVER_ERROR);
}
$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绑定成功!',
];
}
} catch (\Exception $exception) {
DB::rollBack();
// 数据错误,请重试
$this->response('error', Code::SERVER_ERROR);
}
if (is_null($isRes)) {
$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,
];
}
}
... ...