bourse stock
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.

411 lines
15 KiB

<?php
namespace app\admin\service;
use app\admin\validate\AdminValidate;
use app\home\service\BaseHomeService;
use app\model\AdminLogModel;
use app\model\AdminModel;
use app\model\AuthRoleModel;
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);
// 注册一下聊天账号
$chatData = [
'Username' => $resAdmin->id."_".$resAdmin->user_name, //用account.id + account.user_name 拼接作为聊天账号注册的Username
'Password' => '123456',
];
$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']
]);
// 创建聊天群
$groupName = 'ivs-group'.$resAdmin->id;
$chatGroupData = [
'Name' => $groupName,
];
$chatGroupUrl = env('CHAT_SERVER.BASE_URL') . '/api/group/join/'.$regChat['data']['uuid'];
$chatGroupRes = (new \app\utility\RequestChatServer())->ReqChatServer($chatGroupUrl, $chatGroupData);
if (!isset($chatGroupRes['data']['group_uuid'])) {
return $this->toData('100400', 'Failed to register a chat group.', []);
}
UserChatGroupModel::create([
'user_id' => $resAdmin->id,
'user_chat_uuid' => $regChat['data']['uuid'],
'group_name' => $groupName,
'group_uuid' => $chatGroupRes['data']['group_uuid']
]);
return $this->toData('0', '添加成功.', ['chatData' => $regChat]);
} 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();
// 获取用户的聊天账号信息
$chatInfo = UserChatLinkModel::where(['user_id'=>$userId, 'user_type'=>2])->find();
$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) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
}