ProjectAssociationServices.php
6.8 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?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;
use Illuminate\Support\Facades\Http;
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;
}
/**
* 获取AI客服列表
* @author zbj
* @date 2024/9/7
*/
public function getWorkChatRoomList($search = '', $friend_id = 0){
$param = [
'search' => $search,
'time' => time(),
];
if(!$search && $friend_id){
$param['friend_id'] = $friend_id;
}
$param['sign'] = $this->getSign($param);
$url = 'https://hub.ai.cc/api/globalso_ai_customer_service/chatroom_list';
$result = Http::withoutVerifying()->withOptions(['proxy' => env('CURL_PROXY')])->timeout(20)->post($url, $param)->json();
if(empty($result) || $result['status'] != 200){
return [];
}
return $result;
}
/**
* 获取AI客服列表
* @throws \Exception
* @author zbj
* @date 2024/9/7
*/
public function sendMessage($friend_id, $content, $type = 'Text'){
$param = [
'friend_id' => $friend_id,
'content' => $content,
'type' => $type,
];
$param['sign'] = $this->getSign($param);
$url = 'https://hub.ai.cc/api/globalso_ai_customer_service/send_msg';
$result = Http::withoutVerifying()->withOptions(['proxy' => env('CURL_PROXY')])->timeout(30)->post($url, $param)->json();
if(empty($result) || $result['status'] != 200){
throw new \Exception(json_encode($result));
}
return true;
}
/**
* @param $data
* @return string
*/
public function getSign($data)
{
if (empty($data) || FALSE == is_array($data))
return '';
unset($data['sign']);
ksort($data);
// 放弃http_build_query 会将空数据字段抛弃
// $string = http_build_query($data);
$tem = [];
foreach ($data as $key => $val) {
$tem[] = $key . '=' . urlencode($val);
}
$string = implode('&', $tem);
$key = md5('quanqiusou.com');
$sign = md5($string . $key);
return $sign;
}
}