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

230 lines
8.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller;
use app\model\Admins;
use app\model\Onlines;
use app\model\Orders;
use app\model\Products;
use support\Log;
use support\Request;
use support\Redis;
class AdminController extends base
{
const ROUTE_LISTS = ['境内' => 10, '境外' => 20];
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);
}
if($typeDesc = $request->get('type_desc')) {
//$routeType = self::ROUTE_LISTS[mb_substr($typeDesc, 0, 2)] ?? 10;
//$query->where('route_type', $routeType);
}
try {
if ($request->admin->is_franchisee == 1) {
$query->where('pid', $request->admin->id);
}
} catch (\Exception $exception) {
}
$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);
$is_franchisee = $request->post('is_franchisee',0);
$product_ids = $request->post('product_ids','');
$routeType = $request->post('route_type','10');
$type = $request->post('type','0');
if (in_array($type, [2, 3])) { // 主播/中控默认密码123456
$password = '123456';
}
if($id) {
$item = (new Admins())->find($id);
}else{
$item = new Admins();
$item->pid = $request->admin->id;
}
if(!($request->admin->is_super || $request->admin->is_franchisee)) return $this->error(2000, '超级管理员或加盟商才可以管理人员');
if($item->id > 0) {
if(!empty($name) && $item->name != $name) {
$item->name = $name;
}
if(!empty($username) && $item->username != $username) {
$item->username = $username;
}
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;
$item->is_franchisee = $is_franchisee;
$item->product_ids = $product_ids;
$item->route_type = $routeType;
$item->type = $type;
$item->wechat = $request->post('wechat','');
$item->wechat_pic = $request->post('wechat_pic','');
$item->dy_nickname = $request->post('dy_nickname','');
Log::info('logs:' . json_encode($item));
$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 = [];
$users = Admins::where('status',1)->select();
foreach ($users as $user){
$adminId = $user->id;
$list[$adminId]['isEndWork'] = 0;
if ($user->is_order == 1){
$list[$adminId]['isEndWork'] = 1;//是否分单
}
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));
}else{
$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;
}
if ($user->start_work_time <=0){
$list[$adminId]['onlineTime'] = 0;
}
$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;//在线时不用展示
}
$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;
$list[$adminId]['id'] = $adminId;
$list[$adminId]['username'] = $user->username;
$list[$adminId]['route_type'] = $user->route_type;
$list[$adminId]['routes'] = [];
if ($user->product_ids) {
$list[$adminId]['routes'] = Products::query()->field(['product_name'])->whereIn('id', explode(',', $user->product_ids))->select();
}
}
return $this->success(array_values($list));
}
public function editInfo(Request $request)
{
$post = $request->post();
if (empty($post['id'])){
$post['id'] = $request->admin->id;
}
$post['end_work_time'] = time();
if ($post['is_order'] == 1){
$post['end_work_time'] = 0;
}
try {
Admins::update($post);
}catch (\Exception $e){
return $this->error(2001,$e->getMessage());
}
return $this->success(true);
}
}