ProjectAssociationLogic.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace App\Http\Logic\Aside\ProjectAssociation;
use App\Enums\Common\Code;
use App\Http\Logic\Logic;
use App\Models\ProjectAssociation\ProjectAssociation;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ProjectAssociationLogic extends Logic
{
public function saveWeChatData($data)
{
$wx = new ProjectAssociation();
DB::beginTransaction();
try {
$status = $wx->saveData($data);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$e->getMessage();
errorLog('V6与AICC关联失败', $wx, $e);
$this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);
}
return $status;
}
/**
* status - 正常
* @param $project_id
* @return ProjectAssociation|Builder|Model|object|null
*/
public function normal($project_id)
{
return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_NORMAL)->first();
}
/**
* status - 禁用
* @param $project_id
* @return ProjectAssociation|Builder|Model|object|null
*/
public function disabled($project_id)
{
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,
];
}
}