ProjectAssociationServices.php
4.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace App\Services\Aside\ProjectAssociation;
use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Models\ProjectAssociation\ProjectAssociation;
use App\Services\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ProjectAssociationServices extends BaseService
{
/**
* 保存aicc与项目关系数据
* @param array $data
* @return bool
* @throws BsideGlobalException
*/
public function save(array $data)
{
$bool = false;
DB::beginTransaction();
try {
# V6项目ID
$project_id = (int)$data['project_id'] ?? 0;
$isRes = ProjectAssociation::query()->where('project_id', $project_id)->first();
if (!$isRes) {
$isRes = new ProjectAssociation();
$isRes->project_id = $project_id;
}
# AICC朋友ID
$isRes->friend_id = $data['friend_id'] ?? 0;
# AICC朋友昵称
$isRes->nickname = $data['nickname'] ?? 0;
# AICC用户ID
$isRes->user_id = $data['user_id'] ?? 0;
# AICC用户姓名
$isRes->user_name = $data['user_name'] ?? '';
# AICC朋友头像
$isRes->image = $data['image'] ?? '';
$isRes->binding_app = $data['app'] ?? 1;
$bool = $isRes->save();
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$e->getMessage();
errorLog('V6与AICC关联失败', $data, $e);
$this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);
}
return $bool;
}
/**
* status - 正常
* @param $project_id
* @param $app
* @return ProjectAssociation|Builder|Model|object|null
*/
public function normal($project_id, $app)
{
return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_NORMAL)->whereBindingApp($app)->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 $project_id
* @param $status
* @param $app
* @return array
*/
public function saveProject($project_id, $status, $app)
{
$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;
$isRes->binding_app = $app;
try {
$bool = $isRes->save();
DB::commit();
} catch (\Exception $exception) {
DB::rollBack();
}
return compact('bool', 'isRes');
}
/**
* 关闭状态
* @param ProjectAssociation $res
* @param int $status 1 - 正常, 0 - 禁用
* @return bool
*/
public function closedState($res, $status)
{
DB::beginTransaction();
$bool = false;
try {
$res->status = $status;
$bool = $res->save();
DB::commit();
} catch (\Exception $exception) {
DB::rollBack();
}
return $bool;
}
/**
* 获取AICC微信列表数据
* @param ProjectAssociation $res
* @param int $type 绑定app应用类型
* @param bool $cache 是否缓存
* @return mixed
*/
public function getAiccWechatLists($res, $type = ProjectAssociation::PERSONAL_WECHAT, $cache = false)
{
$redis_key = 'aicc_friend_lists_' . $type . '_' . (int)env('AICC_WECHAT_USER_ID');
$result = $cache ? false : redis_get($redis_key);
if (empty($result)) {
$apiUrl = ProjectAssociation::getApplicationApiUrl($type);
$url = env('AICC_URL') . $apiUrl;
$result = curlGet($url);
if (!empty($result['data'])) {
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;
}
}