169 lines
5.5 KiB
PHP
169 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace app\chukebao\controller;
|
|
|
|
use app\chukebao\model\FriendSettings;
|
|
use library\ResponseHelper;
|
|
use think\Db;
|
|
|
|
class WechatFriendController extends BaseController
|
|
{
|
|
|
|
public function getList()
|
|
{
|
|
$page = $this->request->param('page', 1);
|
|
$limit = $this->request->param('limit', 10);
|
|
$accountId = $this->getUserInfo('s2_accountId');
|
|
if (empty($accountId)) {
|
|
return ResponseHelper::error('请先登录');
|
|
}
|
|
$query = Db::table('s2_wechat_friend')
|
|
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
|
->order('id desc');
|
|
$total = $query->count();
|
|
$list = $query->page($page, $limit)->select();
|
|
|
|
// 提取所有好友ID
|
|
$friendIds = array_column($list, 'id');
|
|
|
|
$aiTypeData = [];
|
|
if (!empty($friendIds)) {
|
|
$aiTypeData = FriendSettings::where('friendId', 'in', $friendIds)->column('friendId,type');
|
|
}
|
|
|
|
|
|
// 处理每个好友的数据
|
|
foreach ($list as $k => &$v) {
|
|
$v['labels'] = json_decode($v['labels'], true);
|
|
$v['siteLabels'] = json_decode($v['siteLabels'], true);
|
|
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s', $v['createTime']) : '';
|
|
$v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s', $v['updateTime']) : '';
|
|
$v['passTime'] = !empty($v['passTime']) ? date('Y-m-d H:i:s', $v['passTime']) : '';
|
|
$v['aiType'] = isset($aiTypeData[$v['id']]) ? $aiTypeData[$v['id']] : 0;
|
|
}
|
|
unset($v);
|
|
|
|
return ResponseHelper::success(['list' => $list, 'total' => $total]);
|
|
}
|
|
|
|
/**
|
|
* 获取单个好友详情
|
|
* @return \think\response\Json
|
|
*/
|
|
public function getDetail()
|
|
{
|
|
$friendId = $this->request->param('id');
|
|
$accountId = $this->getUserInfo('s2_accountId');
|
|
|
|
if (empty($accountId)) {
|
|
return ResponseHelper::error('请先登录');
|
|
}
|
|
|
|
if (empty($friendId)) {
|
|
return ResponseHelper::error('好友ID不能为空');
|
|
}
|
|
|
|
// 查询好友详情
|
|
$friend = Db::table('s2_wechat_friend')
|
|
->where(['id' => $friendId, 'isDeleted' => 0])
|
|
->find();
|
|
|
|
if (empty($friend)) {
|
|
return ResponseHelper::error('好友不存在');
|
|
}
|
|
|
|
// 处理好友数据
|
|
$friend['labels'] = json_decode($friend['labels'], true);
|
|
$friend['siteLabels'] = json_decode($friend['siteLabels'], true);
|
|
$friend['createTime'] = !empty($friend['createTime']) ? date('Y-m-d H:i:s', $friend['createTime']) : '';
|
|
$friend['updateTime'] = !empty($friend['updateTime']) ? date('Y-m-d H:i:s', $friend['updateTime']) : '';
|
|
$friend['passTime'] = !empty($friend['passTime']) ? date('Y-m-d H:i:s', $friend['passTime']) : '';
|
|
|
|
// 获取AI类型设置
|
|
$aiTypeSetting = FriendSettings::where('friendId', $friendId)->find();
|
|
$friend['aiType'] = $aiTypeSetting ? $aiTypeSetting['type'] : 0;
|
|
|
|
return ResponseHelper::success(['detail' => $friend]);
|
|
}
|
|
|
|
/**
|
|
* 更新好友资料(公司、姓名、手机号等字段可单独更新)
|
|
* @return \think\response\Json
|
|
*/
|
|
public function updateFriendInfo()
|
|
{
|
|
$friendId = $this->request->param('id');
|
|
$accountId = $this->getUserInfo('s2_accountId');
|
|
|
|
if (empty($accountId)) {
|
|
return ResponseHelper::error('请先登录');
|
|
}
|
|
|
|
if (empty($friendId)) {
|
|
return ResponseHelper::error('好友ID不能为空');
|
|
}
|
|
|
|
$friend = Db::table('s2_wechat_friend')
|
|
->where(['id' => $friendId, 'accountId' => $accountId, 'isDeleted' => 0])
|
|
->find();
|
|
|
|
if (empty($friend)) {
|
|
return ResponseHelper::error('好友不存在或无权限操作');
|
|
}
|
|
|
|
$requestData = $this->request->param();
|
|
$updatableColumns = [
|
|
'phone',
|
|
];
|
|
$columnUpdates = [];
|
|
|
|
foreach ($updatableColumns as $field) {
|
|
if (array_key_exists($field, $requestData)) {
|
|
$columnUpdates[$field] = $requestData[$field];
|
|
}
|
|
}
|
|
|
|
$extendFieldsData = [];
|
|
if (!empty($friend['extendFields'])) {
|
|
$decodedExtend = json_decode($friend['extendFields'], true);
|
|
$extendFieldsData = is_array($decodedExtend) ? $decodedExtend : [];
|
|
}
|
|
|
|
$extendFieldKeys = [
|
|
'company',
|
|
'name',
|
|
'position',
|
|
'email',
|
|
'address',
|
|
'wechat',
|
|
'qq',
|
|
'remark'
|
|
];
|
|
$extendFieldsUpdated = false;
|
|
|
|
foreach ($extendFieldKeys as $key) {
|
|
if (array_key_exists($key, $requestData)) {
|
|
$extendFieldsData[$key] = $requestData[$key];
|
|
$extendFieldsUpdated = true;
|
|
}
|
|
}
|
|
|
|
if ($extendFieldsUpdated) {
|
|
$columnUpdates['extendFields'] = json_encode($extendFieldsData, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
if (empty($columnUpdates)) {
|
|
return ResponseHelper::error('没有可更新的字段');
|
|
}
|
|
|
|
$columnUpdates['updateTime'] = time();
|
|
|
|
try {
|
|
Db::table('s2_wechat_friend')->where('id', $friendId)->update($columnUpdates);
|
|
} catch (\Exception $e) {
|
|
return ResponseHelper::error('更新失败:' . $e->getMessage());
|
|
}
|
|
|
|
return ResponseHelper::success(['id' => $friendId]);
|
|
}
|
|
} |