作者 李美松

v6优化程序 OA绑定AICC 优化程序

@@ -53,7 +53,7 @@ class ProjectAssociationController extends BaseController @@ -53,7 +53,7 @@ class ProjectAssociationController extends BaseController
53 53
54 /** 54 /**
55 * 获取aicc用户列表 并返回绑定的数据 55 * 获取aicc用户列表 并返回绑定的数据
56 - * @return array|void 56 + * @return void
57 */ 57 */
58 public function check() 58 public function check()
59 { 59 {
@@ -65,55 +65,23 @@ class ProjectAssociationController extends BaseController @@ -65,55 +65,23 @@ class ProjectAssociationController extends BaseController
65 $status = (int)$status ? ProjectAssociation::STATUS_NORMAL : ProjectAssociation::STATUS_DISABLED; 65 $status = (int)$status ? ProjectAssociation::STATUS_NORMAL : ProjectAssociation::STATUS_DISABLED;
66 } 66 }
67 $isRes = $this->ProjectAssociationLogic->normal($project_id); 67 $isRes = $this->ProjectAssociationLogic->normal($project_id);
68 - DB::beginTransaction();  
69 - try {  
70 - // 当数据不存在时并开启状态,自动添加一条数据  
71 - if (is_null($isRes) && (!is_null($status) && $status)) {  
72 - $isRes = $this->ProjectAssociationLogic->disabled($project_id);  
73 - if (is_null($isRes)) {  
74 - $isRes = new ProjectAssociation();  
75 - }  
76 - $isRes->project_id = $project_id;  
77 - $isRes->user_id = (int)env('AICC_WECHAT_USER_ID');  
78 - $isRes->status = $status;  
79 - $isRes->save();  
80 - DB::commit();  
81 - } // 关闭状态  
82 - elseif (!is_null($isRes) && (!is_null($status) && empty($status))) {  
83 - $isRes->status = $status;  
84 - $isRes->save();  
85 - DB::commit();  
86 - return [  
87 - 'code' => Code::SUCCESS,  
88 - 'data' => [],  
89 - 'message' => '关闭AICC绑定成功!',  
90 - ]; 68 + // 当数据不存在时并开启状态,自动添加一条数据
  69 + if (is_null($isRes) && (!is_null($status) && $status)) {
  70 + $bool = $this->ProjectAssociationLogic->saveProject($project_id, $status);
  71 + if (empty($bool)) {
  72 + // 保存数据失败
  73 + $this->response('error', Code::SERVER_ERROR);
91 } 74 }
92 - } catch (\Exception $exception) {  
93 - DB::rollBack();  
94 - // 数据错误,请重试  
95 - $this->response('error', Code::SERVER_ERROR);  
96 - }  
97 - if (is_null($isRes)) { 75 + } // 关闭状态
  76 + elseif (!is_null($isRes) && (!is_null($status) && empty($status))) {
  77 + $res = $this->ProjectAssociationLogic->closedState($isRes, $status);
  78 + $this->response($res['message'], $res['code']);
  79 + } elseif (is_null($isRes)) {
98 // 请开启AICC绑定 80 // 请开启AICC绑定
99 - $this->response('success', Code::SERVER_ERROR);  
100 - }  
101 - $redis_key = 'aicc_friend_lists_' . (int)env('AICC_WECHAT_USER_ID');  
102 - $result = isset($cache) ? false : redis_get($redis_key);  
103 - if (empty($result)) {  
104 - $url = env('AICC_URL') . env('AICC_WECHAT_FRIEND_API_URL');  
105 - $result = curlGet($url);  
106 - redis_set($redis_key, json_encode($result), 60);  
107 - } else {  
108 - $result = json_decode($result, true); 81 + $this->response('error', Code::SERVER_ERROR);
109 } 82 }
110 -  
111 - $result['info'] = [  
112 - 'friend_id' => $isRes->friend_id ?? 0,  
113 - 'nickname' => $isRes->nickname ?? '',  
114 - 'user_name' => $isRes->user_name ?? '',  
115 - 'image' => $isRes->image ?? '',  
116 - ]; 83 + $cache = isset($cache);
  84 + $result = $this->ProjectAssociationLogic->getAiccWechatLists($isRes, $cache);
117 $this->response('success', Code::SUCCESS, $result); 85 $this->response('success', Code::SUCCESS, $result);
118 } 86 }
119 } 87 }
@@ -47,4 +47,84 @@ class ProjectAssociationLogic extends Logic @@ -47,4 +47,84 @@ class ProjectAssociationLogic extends Logic
47 { 47 {
48 return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_DISABLED)->first(); 48 return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_DISABLED)->first();
49 } 49 }
  50 +
  51 + /**
  52 + * 初始化数据/修改数据
  53 + * @param int $project_id
  54 + * @param int $status
  55 + * @return bool
  56 + */
  57 + public function saveProject($project_id, $status)
  58 + {
  59 + $bool = false;
  60 + DB::beginTransaction();
  61 + $isRes = $this->disabled($project_id);
  62 + if (is_null($isRes)) {
  63 + $isRes = new ProjectAssociation();
  64 + }
  65 + $isRes->project_id = $project_id;
  66 + $isRes->user_id = (int)env('AICC_WECHAT_USER_ID');
  67 + $isRes->status = $status;
  68 + try {
  69 + $bool = $isRes->save();
  70 + DB::commit();
  71 + } catch (\Exception $exception) {
  72 + DB::rollBack();
  73 + }
  74 + return $bool;
  75 + }
  76 +
  77 + /**
  78 + * 获取AICC微信列表数据
  79 + * @param ProjectAssociation $res
  80 + * @param bool $cache
  81 + * @return mixed
  82 + */
  83 + public function getAiccWechatLists($res, $cache = false)
  84 + {
  85 + $redis_key = 'aicc_friend_lists_' . (int)env('AICC_WECHAT_USER_ID');
  86 + $result = $cache ? false : redis_get($redis_key);
  87 + if (empty($result)) {
  88 + $url = env('AICC_URL') . env('AICC_WECHAT_FRIEND_API_URL');
  89 + $result = curlGet($url);
  90 + redis_set($redis_key, json_encode($result), 60);
  91 + } else {
  92 + $result = json_decode($result, true);
  93 + }
  94 + $result['info'] = [
  95 + 'friend_id' => $res->friend_id ?? 0,
  96 + 'nickname' => $res->nickname ?? '',
  97 + 'user_name' => $res->user_name ?? '',
  98 + 'image' => $res->image ?? '',
  99 + ];
  100 + return $result;
  101 + }
  102 +
  103 + /**
  104 + * 关闭状态
  105 + * @param ProjectAssociation $res
  106 + * @param int $status 1 - 正常, 0 - 禁用
  107 + * @return array
  108 + */
  109 + public function closedState($res, $status)
  110 + {
  111 + DB::beginTransaction();
  112 + try {
  113 + $res->status = $status;
  114 + $res->save();
  115 + DB::commit();
  116 + $code = Code::SUCCESS;
  117 + $message = '关闭AICC绑定成功!';
  118 + } catch (\Exception $exception) {
  119 + DB::rollBack();
  120 + // 数据错误,请重试
  121 + $code = Code::SERVER_ERROR;
  122 + $message = 'error';
  123 + }
  124 + return [
  125 + 'code' => $code,
  126 + 'data' => [],
  127 + 'message' => $message,
  128 + ];
  129 + }
50 } 130 }