travel/service/app/admin/controller/AdminController.php

172 lines
6.1 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;
}
Log::warning('===bug:',['bug'=> (!config('app.debug', true))]);
if ($user->is_order == 1 && $list[$adminId]['isOnline'] ==1 && empty(Redis::get("CRM:USER:ENDWORK:".$user->id)) && (!config('app.debug', true))){
$list[$adminId]['isEndWork'] = 1;//是否分单
}else{
$list[$adminId]['isEndWork'] = 0;
}
}
return $this->success(array_values($list));
}
}