194 lines
6.6 KiB
PHP
194 lines
6.6 KiB
PHP
<?php
|
|
namespace app\admin\controller;
|
|
|
|
use app\model\Admins;
|
|
use app\model\Onlines;
|
|
use app\model\Orders;
|
|
use support\Log;
|
|
use support\Request;
|
|
use support\Redis;
|
|
|
|
class AdminController extends base
|
|
{
|
|
public function Index(Request $request) {
|
|
|
|
$query = Admins::order('status', 'desc')->order('id', 'desc');
|
|
if($username = $request->get('username')) {
|
|
$query->where('username', $username);
|
|
}
|
|
if($status = $request->get('status')) {
|
|
$query->where('status', $status);
|
|
}
|
|
if($is_order = $request->get('is_order')) {
|
|
$query->where('is_order', $is_order);
|
|
}
|
|
|
|
$list = $query->paginate($request->get('limit',10));
|
|
return $this->success($list->hidden(['password','remember_token']),null,['oss' => Orders::OSS]);
|
|
}
|
|
|
|
public function edit(Request $request) {
|
|
$id = $request->get('id', 0);
|
|
|
|
$info = (new Admins())->find($id);
|
|
|
|
return $this->success($info->hidden(['password','remember_token']));
|
|
}
|
|
|
|
public function pwd(Request $request) {
|
|
$oldpwd = $request->post('oldpwd', null);
|
|
$pwd = $request->post('pwd', null);
|
|
|
|
if(empty($pwd) || empty($oldpwd)) return $this->error(2003, '请填写密码');
|
|
|
|
$admin = Admins::where('id', $request->admin->id)->find();
|
|
if($admin->checkPwd($oldpwd)) {
|
|
$admin->password = $pwd;
|
|
$back = $admin->save();
|
|
if($back) {
|
|
return $this->success("");
|
|
}
|
|
}else{
|
|
return $this->error(2003, '密码输入错误');
|
|
}
|
|
return $this->error(2003, '修改密码失败');
|
|
}
|
|
|
|
public function save(Request $request) {
|
|
$id = $request->post('id',0);
|
|
$username = $request->post('username');
|
|
$name = $request->post('name');
|
|
$mobile = $request->post('mobile');
|
|
$password = $request->post('password');
|
|
$is_order = $request->post('is_order',0);
|
|
$is_anchor = $request->post('is_anchor',0);
|
|
|
|
if($id) {
|
|
$item = (new Admins())->find($id);
|
|
}else{
|
|
$item = new Admins();
|
|
}
|
|
|
|
if(!$request->admin->is_super) return $this->error(2000, '超级管理员才可以管理人员');
|
|
|
|
if($item->id > 0) {
|
|
if(!empty($name) && $item->name != $name) {
|
|
$item->name = $name;
|
|
}
|
|
if(!empty($password) && strlen($password) < 6) return $this->error(2001, '密码长度错误');
|
|
if(!empty($password)) {
|
|
$item->password = $password;
|
|
}
|
|
}else{
|
|
if(empty($username)) return $this->error(2002);
|
|
if(empty($name)) return $this->error(2003);
|
|
if(empty($password) || strlen($password) < 6) return $this->error(2004, '密码长度错误');
|
|
if(!empty($password)) {
|
|
$item->password = $password;
|
|
}
|
|
$has = Admins::where('username', $username)->find();
|
|
if($has) {
|
|
return $this->error(2004, '用户已经存在,换个用户名');
|
|
}
|
|
$item->username = $username;
|
|
$item->name = $name;
|
|
}
|
|
$item->mobile = $mobile;
|
|
$item->is_order = $is_order;
|
|
$item->is_anchor = $is_anchor;
|
|
$back = $item->save();
|
|
if($back)
|
|
return $this->success($item->hidden(['password','remember_token']));
|
|
return $this->error(2003, '写入数据库错误');
|
|
}
|
|
|
|
//开启和关闭
|
|
public function disabled(Request $request) {
|
|
$id = $request->post('id',0);
|
|
$status = $request->post('status',0);
|
|
$status = $status == 1? 1:0;
|
|
|
|
if(!$request->admin->is_super) return $this->error(2000, '超级管理员才可以管理人员');
|
|
|
|
if($id) {
|
|
$item = Admins::find($id);
|
|
$item->status = $status;
|
|
$back = $item->save();
|
|
if($back) {
|
|
if($status == 0) {
|
|
Redis::set('Admin:disabled:'.$item->id, time() , 'EX', 3600*24*10);
|
|
}else{
|
|
Redis::del('Admin:disabled:'.$item->id);
|
|
}
|
|
return $this->success($item->hidden(['password','remember_token']));
|
|
}else{
|
|
return $this->error(2001); //找不到管理员
|
|
}
|
|
}
|
|
return $this->error(2001); //找不到管理员
|
|
}
|
|
|
|
public function getOnlineList(Request $request)
|
|
{
|
|
$list = [];
|
|
$todayStart = strtotime(date('Y-m-d'));
|
|
$todayEnd = $todayStart + 86399;
|
|
|
|
// 获取今天在线的记录
|
|
$onlines = Onlines::alias('o')
|
|
->field('o.*, a.id as admin_id, a.name') // 选择需要的字段
|
|
->join('admins a', 'o.admin_id = a.id', 'LEFT') // 关联admins表
|
|
->where('start','>=',$todayStart)
|
|
->where('end','<=',$todayEnd)
|
|
->select();
|
|
|
|
foreach ($onlines as $online) {
|
|
$adminId = $online->admin_id;
|
|
$list[$adminId]['id'] = $adminId;
|
|
$list[$adminId]['name'] = $online->name;
|
|
$list[$adminId]['onlineTime'] = $list[$adminId]['onlineTime'] ?? 0 + (($online->end ? $online->end : $todayEnd) - $online->start);
|
|
$list[$adminId]['isOnline'] = Redis::get('CRM:USER:ONLINE:'.$online->admin_id) ? 1 : 0;
|
|
}
|
|
|
|
$users = Admins::where('status',1)->select();
|
|
foreach ($users as $user){
|
|
$adminId = $user->id;
|
|
if (empty($list[$user->id])){
|
|
$list[$adminId]['id'] = $adminId;
|
|
$list[$adminId]['name'] = $user->name;
|
|
$list[$adminId]['onlineTime'] = 0;
|
|
$list[$adminId]['isOnline'] = 2;
|
|
}
|
|
|
|
if (!isset($list[$adminId]['isOnline'])){
|
|
$list[$adminId]['isOnline'] = 0;
|
|
}
|
|
|
|
// if ($user->is_order == 1 && $list[$adminId]['isOnline'] ==1){
|
|
if ($user->is_order == 1){
|
|
$list[$adminId]['isEndWork'] = 1;//是否分单
|
|
}else{
|
|
$list[$adminId]['isEndWork'] = 0;
|
|
}
|
|
$list[$adminId]['start_work_time'] = $user->start_work_time;
|
|
$list[$adminId]['end_work_time'] = $user->end_work_time;
|
|
$list[$adminId]['order_num'] = $user->order_num;
|
|
}
|
|
|
|
return $this->success(array_values($list));
|
|
}
|
|
|
|
public function editInfo(Request $request)
|
|
{
|
|
$post = $request->post();
|
|
if (empty($post['id'])) return $this->error(500, '参数错误');
|
|
|
|
try {
|
|
Admins::update($post);
|
|
}catch (\Exception $e){
|
|
return $this->error(2001,$e->getMessage());
|
|
}
|
|
|
|
return $this->success(true);
|
|
}
|
|
} |