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

206 lines
7.1 KiB
PHP
Raw Normal View History

2024-06-24 11:52:30 +08:00
<?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
{
2024-08-08 21:08:23 +08:00
const ROUTE_LISTS = ['境内' => 10, '境外' => 20];
2024-06-24 11:52:30 +08:00
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);
}
2024-08-08 21:14:11 +08:00
2024-08-08 21:08:23 +08:00
if($typeDesc = $request->get('type_desc')) {
$routeType = self::ROUTE_LISTS[mb_substr($typeDesc, 0, 2)] ?? 10;
$query->where('route_type', $routeType);
}
2024-06-24 11:52:30 +08:00
$list = $query->paginate($request->get('limit',10));
return $this->success($list->hidden(['password','remember_token']),null,['oss' => Orders::OSS]);
}
2024-08-08 21:08:23 +08:00
public function edit(Request $request) {
2024-06-24 11:52:30 +08:00
$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);
2024-07-31 16:45:54 +08:00
$is_franchisee = $request->post('is_franchisee',0);
$product_ids = $request->post('product_ids','');
2024-08-08 21:08:23 +08:00
$routeType = $request->post('route_type','10');
2024-06-24 11:52:30 +08:00
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;
}
2024-06-27 14:09:12 +08:00
if(!empty($username) && $item->username != $username) {
$item->username = $username;
}
2024-06-24 11:52:30 +08:00
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;
2024-07-31 16:45:54 +08:00
$item->is_franchisee = $is_franchisee;
$item->product_ids = $product_ids;
2024-08-08 21:08:23 +08:00
$item->route_type = $routeType;
Log::info('logs:' . json_encode($item));
2024-06-24 11:52:30 +08:00
$back = $item->save();
2024-08-08 21:08:23 +08:00
if($back)
2024-06-24 11:52:30 +08:00
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 = [];
$users = Admins::where('status',1)->select();
foreach ($users as $user){
$adminId = $user->id;
2024-06-24 20:58:23 +08:00
2024-06-26 15:38:38 +08:00
$list[$adminId]['isEndWork'] = 0;
2024-06-24 20:58:23 +08:00
if ($user->is_order == 1){
2024-06-24 11:52:30 +08:00
$list[$adminId]['isEndWork'] = 1;//是否分单
2024-06-26 15:38:38 +08:00
}
if (!empty($user->end_work_time)) {
$list[$adminId]['onlineTime'] = strtotime(date('Y-m-d H:i:00',$user->end_work_time)) - strtotime(date('Y-m-d H:i:00',$user->start_work_time));
2024-06-24 11:52:30 +08:00
}else{
2024-06-26 15:38:38 +08:00
$list[$adminId]['onlineTime'] = $user->last_work_time > 0 ? strtotime(date('Y-m-d H:i:00',$user->last_work_time)) - strtotime(date('Y-m-d H:i:00',$user->start_work_time)) : 0;
2024-06-24 11:52:30 +08:00
}
2024-06-26 16:50:38 +08:00
if ($user->start_work_time <=0){
$list[$adminId]['onlineTime'] = 0;
}
2024-06-26 15:38:38 +08:00
2024-06-26 16:50:38 +08:00
$list[$adminId]['last_work_time'] = $user->last_work_time;
$list[$adminId]['isOnline'] = 0;
if (Redis::get('CRM:USER:ONLINE:'.$adminId)){
$list[$adminId]['isOnline'] = 1;
$list[$adminId]['last_work_time'] = 0;//在线时不用展示
}
2024-06-24 20:58:23 +08:00
$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;
2024-06-26 15:38:38 +08:00
$list[$adminId]['id'] = $adminId;
$list[$adminId]['username'] = $user->username;
2024-06-24 11:52:30 +08:00
}
return $this->success(array_values($list));
}
2024-06-24 20:58:23 +08:00
public function editInfo(Request $request)
{
$post = $request->post();
2024-06-26 15:38:38 +08:00
if (empty($post['id'])){
$post['id'] = $request->admin->id;
}
$post['end_work_time'] = time();
if ($post['is_order'] == 1){
$post['end_work_time'] = 0;
}
2024-06-24 20:58:23 +08:00
try {
Admins::update($post);
}catch (\Exception $e){
return $this->error(2001,$e->getMessage());
}
return $this->success(true);
}
2024-06-24 11:52:30 +08:00
}