p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

580 lines
24 KiB

5 months ago
<?php
namespace app\admin\service;
4 months ago
use app\admin\middleware\AdminLog;
5 months ago
use app\admin\validate\AdminValidate;
use app\home\service\BaseHomeService;
use app\model\AdminLogModel;
use app\model\AdminModel;
use app\model\AuthRoleModel;
4 months ago
use app\model\TranslatorCustomerModel;
4 months ago
use app\model\UserAccessLogModel;
5 months ago
use app\model\UserChatGroupModel;
use app\model\UserChatLinkModel;
use phpDocumentor\Reflection\Type;
use think\exception\ValidateException;
use app\utility\UnqId;
use think\facade\Log;
use function Sodium\compare;
use think\facade\Request;
class AdminService extends AdminBaseService
{
public function addUser($param): array
{
try {
// 参数校验
validate(AdminValidate::class)->scene('addUser')->check($param);
// 查找用户是否存在
$username = $param['user_name'];
$user = AdminModel::getByUserName($username);
if (!empty($user)) {
return $this->toData('200100', '该用户已存在', []);
}
$param['password'] = (new UnqId())->encryptPassword($param['password'], env('ENCRYPT.ADMINSALT'));
$param['invite_code'] = (new BaseHomeService())->getUniqInviteCode();
$resAdmin = AdminModel::create($param);
4 months ago
if (!$resAdmin) {
return $this->toData('1', '添加失败');
}
// 检测一下是否注册过聊天账号,没有的话就注册一下聊天账号
$isRegChat = UserChatLinkModel::where(['user_id'=>$resAdmin->id, 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
if (empty($isRegChat)) {
$chatData = [
'Username' => $resAdmin->id."_".$resAdmin->user_name, //用account.id + account.user_name 拼接作为聊天账号注册的Username
'Password' => '123456',
'Nickname' => $resAdmin->user_name,
'Avatar' => env('USER.DEFAULT_HEAD_IMG_PATH'),
];
$chatUrl = env('CHAT_SERVER.BASE_URL') . '/api/user/register';
$regChat = (new \app\utility\RequestChatServer())->ReqChatServer($chatUrl, $chatData);
if (!isset($regChat['data']['uuid'])) {
return $this->toData('0', '注册聊天账号失败.', ['chat_response' => $regChat]);
}
UserChatLinkModel::create([
'user_id' => $resAdmin->id,
'user_type' => 2,
'chat_uuid' => $regChat['data']['uuid'],
'chat_name' => $regChat['data']['username']
]);
}
// 检测一下是否创建过聊天群组,没有的话创建一个
$chatGroup = UserChatGroupModel::where(['user_id'=>$resAdmin->id,'remark'=>UserChatGroupModel::USER_CHAT_GROUP_REMARK_ADMIN_INIT])->find();
if (empty($chatGroup)) {
$groupName = 'ivs-group'.$resAdmin->id;
$chatGroupData = [
'Name' => $groupName,
];
$chatGroupUrl = env('CHAT_SERVER.BASE_URL') . '/api/group/'.$regChat['data']['uuid'];
$chatGroupRes = (new \app\utility\RequestChatServer())->ReqChatServer($chatGroupUrl, $chatGroupData);
if (!isset($chatGroupRes['data']['group_uuid'])) {
return $this->toData('100400', '创建聊天群组失败');
}
UserChatGroupModel::create([
'user_id' => $resAdmin->id,
'user_chat_uuid' => $regChat['data']['uuid'],
'group_name' => $groupName,
'group_uuid' => $chatGroupRes['data']['group_uuid'],
'remark' => UserChatGroupModel::USER_CHAT_GROUP_REMARK_ADMIN_INIT,
]);
}
return $this->toData('0', '添加成功');
5 months ago
} catch (ValidateException $validateException) {
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', '系统繁忙.', [$exception->getMessage()]);
}
}
/**
* 获取用户列表
* @return array
*/
public function getUserList($param)
{
try {
$adminModel = new AdminModel();
//查询条件
if (isset($param['user_name']) && !empty($param['user_name'])) {
$adminModel = $adminModel->where('user_name', $param['user_name']);
}
if (isset($param['nick_name']) && !empty($param['nick_name'])) {
$adminModel = $adminModel->where('nick_name', $param['nick_name']);
}
// 总数
$total = $adminModel->count();
$list = $adminModel->withoutField('password,token')->order('id', 'asc')->page($param['page'], $param['limit'])->select()->toArray();
$roleIds = array_column($list, "role_id");
$roleList = AuthRoleModel::where('id', 'in', $roleIds)->field('id,name')->select()->toArray();
$host = Request::host();
$host = str_replace('api.', '', $host);
if (!empty($list)) {
for ($i = 0; $i < count($list); $i++) {
$index = array_search($list[$i]['role_id'], array_column($roleList, 'id'));
if ($index !== false) {
$list[$i]['role_name'] = $roleList[$index]['name'];
}
$list[$i]['url'] = $host . '/register/index?=agent_code='.$list[$i]['invite_code'];
}
}
return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $list]);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
/**
* @desc 获取用户信息
* @param $userId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserInfo($userId)
{
try {
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first', []);
}
$info = AdminModel::find($userId);
if (empty($info)) {
return $this->toData('100400', 'The user does not exist.', []);
}
$infoArr = $info->toArray();
// 获取用户的聊天账号信息
4 months ago
$chatInfo = UserChatLinkModel::where(['user_id'=>$userId, 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
5 months ago
$chat_uuid = 0;
if (!empty($chatInfo)) {
$chat_uuid = $chatInfo->chat_uuid;
}
$infoArr['chat_uuid'] = $chat_uuid;
// 返回数据
return $this->toData('0', 'Modification successful.', $infoArr);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
/**
* @desc 设置用户信息
* @param $userId
* @param $param
* @return array
*/
public function updateInfo($userId, $param): array
{
try {
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('200403', 'Please log in first.', []);
}
// 参数校验
validate(AdminValidate::class)->scene('updateInfo')->check($param);
// 查找用户信息
$user = AdminModel::find($userId);
if (empty($user)) {
return $this->toData('200403', 'Please log in first.', []);
}
$user['nick_name'] = $param['nick_name'];
$user['email'] = $param['email'];
$user['mobile'] = isset($param['mobile']) ? $param['mobile'] : null;
$user['desc'] = isset($param['desc']) ? $param['mobile'] : null;
$user->save();
// 返回
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
$message = $validateException->getError();
return $this->toData('200400', $message);
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* 编辑账号信息
* @param $accountId
* @param $param
* @return array
*/
public function updateAccount($accountId, $param): array
{
try {
// 主键
if (empty($accountId) || $accountId <= 0) {
return $this->toData('200404', 'accountId lose.', []);
}
// 参数校验
validate(AdminValidate::class)->scene('updateAccount')->check($param);
// 查找用户信息
$user = AdminModel::find($accountId);
if (empty($user)) {
return $this->toData('200403', '不存在该用户.', []);
}
if (isset($param['password'])) {
$user['password'] = (new UnqId())->encryptPassword($param['password'], env('ENCRYPT.ADMINSALT'));
}
$user['role_id'] = $param['role_id'];
$user['user_name'] = $param['user_name'];
$user['nick_name'] = $param['nick_name'];
$user['email'] = isset($param['email']) ? $param['email'] : null;
$user['remark'] = isset($param['remark']) ? $param['remark'] : null;
$user->save();
// 返回
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
$message = $validateException->getError();
return $this->toData('200400', $message);
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function updateAccountStauts($accountId, $param): array
{
try {
// 主键
if (empty($accountId) || $accountId <= 0) {
return $this->toData('200404', 'accountId lose.', []);
}
// 参数校验
validate(AdminValidate::class)->scene('updateAccountStatus')->check($param);
// 查找用户信息
$user = AdminModel::find($accountId);
if (empty($user)) {
return $this->toData('200403', 'Please log in first.', []);
}
$user['status'] = $param['status'];
$user->save();
// 返回
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
$message = $validateException->getError();
return $this->toData('200400', $message);
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function updatePassword($userId, $param): array
{
try {
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('200403', 'Please log in first.', []);
}
// 参数校验
validate(AdminValidate::class)->scene('updatePassword')->check($param);
// 查找用户信息
$user = AdminModel::find($userId);
if (empty($user)) {
return $this->toData('200403', 'Please log in first.', []);
}
//检查原始密码
$flag = (new UnqId())->checkPassword($param['old_password'], $user['password'], env('ENCRYPT.ADMINSALT'));
if (!$flag) {
return $this->toData('200401', '原始密码错误', []);
}
//设置新密码
$newPassword = (new UnqId())->encryptPassword($param['new_password'], env('ENCRYPT.ADMINSALT'));
$user['password'] = $newPassword;
$user->save();
// 返回
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
$message = $validateException->getError();
return $this->toData('200400', $message);
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function del($id): array
{
try {
$adminModel = AdminModel::find($id);
$adminModel->delete();
// 返回
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
$message = $validateException->getError();
return $this->toData('200400', $message);
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* 查询用户权限代码
* @param $id
* @return array
*/
public function getPermCode($id): array
{
try {
$adminModel = AdminModel::find($id);
//根据角色查询权限代码
$roleModel = AuthRoleModel::find($adminModel->role_id);
$rules = $roleModel->rules;
if ($rules) {
$rules = explode(",", $rules);
//字符串转换为数字类型
// $rules=array_map(function ($value){
// return (int)$value;
// },$rules);
}
// 返回
return $this->toData('0', 'Modification successful.', $rules);
} catch (ValidateException $validateException) {
$message = $validateException->getError();
return $this->toData('200400', $message);
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 退出登陆
* @param $userId
* @return array
*/
public function logout($userId)
{
try {
// 获取用户
$user = AdminModel::find($userId);
if (empty($user)) {
return $this->toData('200300', '用户不存在', []);
}
$user->token = "";
$user->save();
return $this->toData('0', 'Modification successful.');
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function logList($param)
{
try {
$adminLogModel = new AdminLogModel();
//查询条件
if (!empty($param['create_date'])) {
$adminLogModel = $adminLogModel->where('create_date', $param['create_date']);
}
// 总数
$total = $adminLogModel->count();
$list = $adminLogModel->order('id', 'desc')->page($param['page'], $param['limit'])->select()->toArray();
$accountIds = array_column($list, "admin_id");
$accountList = AdminModel::where('id', 'in', $accountIds)->column('user_name', 'id');
// $menuList = AuthRuleModel::column('title', 'path');
if (!empty($list)) {
foreach ($list as &$v) {
$v['admin_name'] = $accountList[$v['admin_id']] ?? '-';
$v['path_text'] = '用户上分';
}
}
return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $list]);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
public function inviteCode()
{
try {
$user = AdminModel::select();
foreach ($user as $v){
$inviteCode = (new BaseHomeService())->getUniqInviteCode();
AdminModel::where('id',$v->id)->update(['invite_code'=>$inviteCode]);
}
} catch (\Exception $exception) {
4 months ago
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
// 获取翻译员列表
public function getTranslatorList($param)
{
try {
if (empty($param['page']) || empty($param['limit'])) {
return $this->toData('400', '缺少分页参数');
}
// 查询角色表中翻译角色的id
$role = AuthRoleModel::where(['name'=>'翻译'])->find();
if (empty($role)) {
return $this->toData('400', '系统不存在翻译角色,请先创建');
}
// 查询属于翻译角色的所有账号
$where = ['role_id'=>$role->id];
if (!empty($param['user_name'])) {
$list = AdminModel::where($where)->whereLike('user_name', '%'.$param['user_name'].'%')->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'page' => $param['page'],
]);
} else {
$list = AdminModel::where($where)->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'page' => $param['page'],
]);
}
return $this->toData('0', 'SUCCESS', [
'list' => $list->items(), // 当前页的数据
'page' => $list->currentPage(), // 当前页码
'total' => $list->total(), // 总记录数
'last_page' => $list->lastPage(), // 最后一页页码
]);
} catch (\Exception $exception) {
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
// 翻译员添加客服聊天好友
public function translatorAddCustomer($param)
{
try {
if (empty($param['translator_id']) || empty($param['customer_id'])) {
return $this->toData('400', '缺少参数');
}
// 记录好友关系
$translatorCustomer = TranslatorCustomerModel::where(['translator_id'=>$param['translator_id'], 'customer_id'=>$param['customer_id']])->find();
if (empty($translatorCustomer)) {
TranslatorCustomerModel::create(['translator_id'=>$param['translator_id'], 'customer_id'=>$param['customer_id']]);
}
// 请求聊天服添加好友关系
$translatorChatInfo = UserChatLinkModel::where(['user_id'=>$param['translator_id'], 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find(); //查询翻译员的聊天账号uuid
if (empty($translatorChatInfo)) {
return $this->toData('500', '翻译员的聊条账号信息错误');
}
$customerChatInfo = UserChatLinkModel::where(['user_id'=>$param['customer_id'], 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find(); //查询客服的聊天账号uuid
if (empty($customerChatInfo)) {
return $this->toData('500', '客服的聊天账号信息错误');
}
$chatFriendsData = [
'UserUuid' => $translatorChatInfo->chat_uuid,
'CustomerUuid' => $customerChatInfo->chat_uuid,
];
$chatFriendsUrl = env('CHAT_SERVER.BASE_URL') . '/api/eachOtherFriends';
$chatFriendsRes = (new \app\utility\RequestChatServer())->ReqChatServer($chatFriendsUrl, $chatFriendsData);
Log::info("翻译员添加客服好友结果:". json_encode($chatFriendsRes));
// 将客服添加到翻译员的聊天群组中
$chatGroup = UserChatGroupModel::where(['user_id'=>$param['translator_id'], 'remark'=>UserChatGroupModel::USER_CHAT_GROUP_REMARK_ADMIN_INIT])->find();
if (empty($chatGroup)) {
return $this->toData('500', 'The chat group is error.');
}
$joinChatGroupUrl = env('CHAT_SERVER.BASE_URL') . '/api/group/join/'.$translatorChatInfo->chat_uuid.'/'.$chatGroup->group_uuid;
$joinChatGroupRes = (new \app\utility\RequestChatServer())->ReqChatServer($joinChatGroupUrl, []);
Log::info("翻译员添加客服到聊天群组结果:". json_encode($joinChatGroupRes));
return $this->toData('0', 'SUCCESS', [$joinChatGroupRes]);
} catch (\Exception $exception) {
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
// 翻译员以绑定的客服列表
public function translatorBindCustomerList($param)
{
try {
if (empty($param['translator_id'])) {
return $this->toData('400', '缺少参数');
}
if (empty($param['page']) || empty($param['limit'])) {
return $this->toData('400', '缺少分页参数');
}
$list = TranslatorCustomerModel::where(['translator_id'=>$param['translator_id']])->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'page' => $param['page'],
]);
return $this->toData('0', 'SUCCESS', [
'list' => $list->items(), // 当前页的数据
'page' => $list->currentPage(), // 当前页码
'total' => $list->total(), // 总记录数
'last_page' => $list->lastPage(), // 最后一页页码
]);
} catch (\Exception $exception) {
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
5 months ago
}
}
4 months ago
// 用户访问页面的记录
public function getUserAccessLog($param)
{
try {
if (empty($param['page']) || empty($param['limit'])) {
return $this->toData('400', '缺少分页参数');
}
$where = [];
if (!empty($param['module'])) {
$where['module'] = $param['module'];
}
4 months ago
if (!empty($param['user_id'])) {
$where['user_id'] = $param['user_id'];
}
4 months ago
$list = UserAccessLogModel::where($where)->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'page' => $param['page'],
]);
return $this->toData('0', 'SUCCESS', [
'list' => $list->items(),
'page' => $list->currentPage(),
'total' => $list->total(),
'last_page' => $list->lastPage(),
]);
} catch (\Exception $exception) {
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
public function getAdminOperationLog($param)
{
try {
if (empty($param['page']) || empty($param['limit'])) {
return $this->toData('400', '缺少分页参数');
}
$where = [];
if (!empty($param['admin_id'])) {
$where['admin_id'] = $param['admin_id'];
}
$list = AdminLogModel::where($where)->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'page' => $param['page'],
]);
return $this->toData('0', 'SUCCESS', [
'list' => $list->items(),
'page' => $list->currentPage(),
'total' => $list->total(),
'last_page' => $list->lastPage(),
]);
} catch (\Exception $exception) {
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
5 months ago
}