ADD:添加后台管理端接口;
DEL:删除不必要的头部注释; FIX:修正若干已知错误;
This commit is contained in:
@@ -1,268 +1,279 @@
|
||||
<?php
|
||||
|
||||
# @Author: 嗨噜客(三亚) <fm453>
|
||||
# @Date: 2022-05-22T07:35:09+08:00
|
||||
# @Email: fm453@lukegzs.com
|
||||
# @Last modified by: fm453
|
||||
# @Last modified time: 2024-08-09T09:17:49+08:00
|
||||
# @Copyright: www.hiluker.cn
|
||||
|
||||
namespace api\controllers\gm\v1;
|
||||
|
||||
use Yii;
|
||||
use yii\data\Pagination;
|
||||
use yii\helpers\Url;
|
||||
use addons\models\AcDriver;
|
||||
use common\models\Member as User;
|
||||
use common\models\UserExt;
|
||||
|
||||
class DriverController extends Common
|
||||
{
|
||||
public function beforeAction($action)
|
||||
{
|
||||
if (!$this->pid) {
|
||||
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
|
||||
}
|
||||
return parent::beforeAction($action);
|
||||
}
|
||||
public function actionIndex()
|
||||
{
|
||||
return $this->render('index');
|
||||
$apis = [
|
||||
'list'=>'司机列表',
|
||||
'detail'=>'详情',
|
||||
'get-status'=>'状态字典',
|
||||
'add'=>'添加',
|
||||
'edit'=>'编辑',
|
||||
'delete'=>'删除',
|
||||
'show'=>'显示',
|
||||
'hide'=>'隐藏',
|
||||
'ajax'=>'ajax操作'
|
||||
];
|
||||
$this->result('您正使用CMTS-GM系统司机管理接口!', $apis, 200);
|
||||
}
|
||||
|
||||
//司机列表
|
||||
public function actionList()
|
||||
{
|
||||
$model = new AcDriver();
|
||||
$where = $where2 = [];
|
||||
$where2[] = 'and';
|
||||
$where['pid'] = Yii::$app->session->get('pid');
|
||||
$post = Yii::$app->request->post();
|
||||
$search = [];
|
||||
$search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : '';
|
||||
if (!empty($search['title'])) {
|
||||
$where2[] = ['LIKE', 'name', $search['title']];
|
||||
}
|
||||
$where['deleted'] = 0;
|
||||
$data = $model->find()->where($where);
|
||||
if ($search['title']) {
|
||||
$data = $data->andwhere($where2);
|
||||
}
|
||||
$s = $this->search();
|
||||
$res = $s['res'];
|
||||
if (!$res) {
|
||||
$this->result('没有查询到相应的数据!', [], 0);
|
||||
}
|
||||
|
||||
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
|
||||
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
||||
$status = Yii::$app->params['DriverStatus'];
|
||||
|
||||
return $this->render('list', [
|
||||
'drivers' => $res,
|
||||
'pager' => $pages,
|
||||
'status' => $status,
|
||||
'search' => $search
|
||||
]);
|
||||
$data= [
|
||||
'total'=>$s['query']->count(),
|
||||
'drivers' => $res,
|
||||
'page'=>$this->page
|
||||
];
|
||||
$this->result('驾驶员查询成功!', $data, 200);
|
||||
}
|
||||
|
||||
public function actionSelect()
|
||||
public function actionDetail()
|
||||
{
|
||||
$id = (int)Yii::$app->request->get('id');
|
||||
if(!$id) $this->result('请求错误,未携带ID参数');
|
||||
$model = new AcDriver();
|
||||
$where = $where2 = [];
|
||||
$where2[] = 'and';
|
||||
$where['pid'] = Yii::$app->session->get('pid');
|
||||
$post = Yii::$app->request->post();
|
||||
$search = [];
|
||||
$search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : '';
|
||||
if (!empty($search['title'])) {
|
||||
$where2[] = ['LIKE', 'name', $search['title']];
|
||||
}
|
||||
$where['deleted'] = 0;
|
||||
$data = $model->find()->where($where);
|
||||
if ($search['title']) {
|
||||
$data = $data->andwhere($where2);
|
||||
}
|
||||
|
||||
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
|
||||
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
||||
$status = Yii::$app->params['DriverStatus'];
|
||||
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'driver';
|
||||
return $this->render('select', [
|
||||
'drivers' => $res,
|
||||
'pager' => $pages,
|
||||
'status' => $status,
|
||||
'search' => $search,
|
||||
'callback' => $callback
|
||||
]);
|
||||
$res = $model->findOne($id)->toArray();
|
||||
if(!$res) $this->result('未查询到相应数据');
|
||||
$this->result('驾驶员信息查询完成', $res, 200);
|
||||
}
|
||||
|
||||
public function actionNew()
|
||||
public function actionGetStatus()
|
||||
{
|
||||
return $this->render('modify', []);
|
||||
$status = Yii::$app->params['DriverStatus'];
|
||||
$this->result('司机状态字典获取成功', $status, 200);
|
||||
}
|
||||
|
||||
public function actionAdd()
|
||||
{
|
||||
//数据预检查、编排
|
||||
$data = $this->preSave('add');
|
||||
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
|
||||
|
||||
//保存资料
|
||||
$model = new AcDriver();
|
||||
foreach ($data as $key=>$val) {
|
||||
$model->$key = $val;
|
||||
}
|
||||
$res = $model->save();
|
||||
$msg = '数据保存失败!';
|
||||
if(!$res) $this->result($msg, [], 100);
|
||||
//驾驶员资料注册系统用户(驾驶员需要可以登陆指定客户端以进行业务操作)
|
||||
$this->driverToUser($data);
|
||||
$msg = '数据保存成功!';
|
||||
$return = [];
|
||||
$return['id']= $model->attributes['id']; //获取插入后id;
|
||||
$this->result($msg,$return, 200);
|
||||
}
|
||||
|
||||
//编辑司机资料
|
||||
public function actionEdit()
|
||||
{
|
||||
$model = new AcDriver();
|
||||
$id = Yii::$app->request->get('id');
|
||||
$res = $model->find()->where(['id' => $id])->one();
|
||||
if ($res) {
|
||||
$res = $res->toArray();
|
||||
}
|
||||
$detail = $this->preUpdate();
|
||||
//数据预检查、编排
|
||||
$data = $this->preSave('edit');
|
||||
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
|
||||
|
||||
return $this->render('modify', ['detail' => $res]);
|
||||
}
|
||||
|
||||
//保存司机资料
|
||||
public function actionSave()
|
||||
{
|
||||
$post = Yii::$app->request->post();
|
||||
$id = (int)$post['id'];
|
||||
$pid = Yii::$app->session->get('pid');
|
||||
//格式化数据
|
||||
//目标键=》POST键
|
||||
$cols = ['name' => 'name', 'mobile' => 'mobile', 'idcard' => 'idcard'];
|
||||
foreach ($cols as $col => $key) {
|
||||
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
|
||||
}
|
||||
//查询司机手机号是否在库
|
||||
$hasDriver = AcDriver::find()->where(['mobile' => $data['mobile']])->one();
|
||||
if ($hasDriver) {
|
||||
if ($id && $id == $hasDriver->id) {
|
||||
} else {
|
||||
$id = $hasDriver->id;
|
||||
\Yii::$app->getSession()->setFlash('danger', '保存失败,该手机号对应的司机已在库,系统自动现在为您调出该司机资料!');
|
||||
return $this->redirect(['edit', 'id' => $id]);
|
||||
}
|
||||
}
|
||||
$cols = ['status_code' => 'status_code', 'deleted' => 'deleted'];
|
||||
foreach ($cols as $col => $key) {
|
||||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
||||
}
|
||||
$data['create_at'] = time();
|
||||
$data['update_at'] = $data['create_at'];
|
||||
$data['pid'] = $pid;
|
||||
|
||||
//保存司机资料
|
||||
$AcDriver = new AcDriver();
|
||||
foreach ($data as $key => $val) {
|
||||
$AcDriver->$key = $val;
|
||||
}
|
||||
|
||||
if ($id) {
|
||||
$AcDriver->id = $id;
|
||||
unset($data['create_at']);
|
||||
$res = $AcDriver->updateAll($data, ['id' => $id]);
|
||||
} else {
|
||||
$res = $AcDriver->save();
|
||||
$id = $AcDriver->attributes['id']; //获取插入后id
|
||||
}
|
||||
|
||||
//查询司机对应的系统用户
|
||||
$userModel = new User();
|
||||
$user = $userModel->find()->where(['mobile' => $data['mobile']])->one();
|
||||
if (empty($user)) {
|
||||
$userModel->username = !$data['name'] ? $data['mobile'] : $data['name'];
|
||||
$userModel->mobile = $data['mobile'];
|
||||
$userModel->email = $data['mobile'] . '@hiluker.com';
|
||||
$userModel->setPassword(Yii::$app->params['defaultUserPassword']);
|
||||
$userModel->generateAuthKey();
|
||||
$userModel->avatar = Yii::$app->params['defaultUserAvatar'];
|
||||
$userModel->created_at = time();
|
||||
$userModel->status = 1;
|
||||
$userModel->save();
|
||||
$user_id = $userModel->attributes['id']; //获取插入后id
|
||||
}
|
||||
|
||||
\Yii::$app->getSession()->setFlash('info', '已成功注册该用户!');
|
||||
if ($res) {
|
||||
\Yii::$app->getSession()->setFlash('success', '司机信息保存成功!');
|
||||
return $this->redirect(['edit', 'id' => $id]);
|
||||
} else {
|
||||
\Yii::$app->getSession()->setFlash('warning', '司机信息保存失败!');
|
||||
return $this->goBack();
|
||||
}
|
||||
//保存资料
|
||||
foreach ($data as $key=>$val) {
|
||||
$detail->$key = $val;
|
||||
}
|
||||
$res = $detail->save();
|
||||
$msg = '数据编辑失败!';
|
||||
if(!$res) $this->result($msg, [], 100);
|
||||
//驾驶员资料注册系统用户(驾驶员需要可以登陆指定客户端以进行业务操作)
|
||||
$this->driverToUser($data);
|
||||
$msg = '数据编辑成功!';
|
||||
$return = [];
|
||||
$return['data']= $detail;
|
||||
$this->result($msg,$return, 200);
|
||||
}
|
||||
|
||||
//删除员工
|
||||
public function actionDelete()
|
||||
{
|
||||
$get = Yii::$app->request->get();
|
||||
$post = Yii::$app->request->post();
|
||||
$id = (int)$get['id'];
|
||||
$data = $where = [];
|
||||
$where['pid'] = Yii::$app->session->get('pid');
|
||||
$where['id'] = $id;
|
||||
$detail = $this->preUpdate();
|
||||
$data = [];
|
||||
$data['update_at'] = time();
|
||||
$data['deleted'] = 1;
|
||||
$data['deleted'] = $detail->deleted + 1;
|
||||
|
||||
$model = new AcDriver();
|
||||
foreach ($data as $key => $val) {
|
||||
$model->$key = $val;
|
||||
}
|
||||
//保存资料
|
||||
foreach ($data as $key=>$val) {
|
||||
$detail->$key = $val;
|
||||
}
|
||||
$res = $detail->save();
|
||||
$msg = '驾驶员数据删除失败!';
|
||||
if(!$res) $this->result($msg, [], 100);
|
||||
|
||||
$model->id = $id;
|
||||
$res = $model->updateAll($data, $where);
|
||||
|
||||
$return = [];
|
||||
$return['url'] = Url::toRoute('driver/list');
|
||||
$return['timeout'] = 3; //3秒后自动跳转
|
||||
$return['status'] = 0;
|
||||
if ($res) {
|
||||
$return['msg'] = '司机删除成功';
|
||||
$return['errorcode'] = 200;
|
||||
$return['data'] = $id;
|
||||
} else {
|
||||
$return['msg'] = '司机删除失败';
|
||||
$return['errorcode'] = 0;
|
||||
}
|
||||
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
||||
$msg = '驾驶员数据删除成功!';
|
||||
$this->result($msg,[], 200);
|
||||
}
|
||||
|
||||
public function actionAjax()
|
||||
{
|
||||
$detail = $this->preUpdate();
|
||||
$get = Yii::$app->request->get();
|
||||
$post = Yii::$app->request->post();
|
||||
$id = (int)$get['id'];
|
||||
$data = $where = [];
|
||||
$where['pid'] = Yii::$app->session->get('pid');
|
||||
$where['id'] = $id;
|
||||
$data['update_at'] = time();
|
||||
|
||||
$model = new AcDriver();
|
||||
$res = $model->find()->where(['id' => $id])->one();
|
||||
$return = [];
|
||||
$return['timeout'] = 1; //3秒后自动跳转
|
||||
$return['status'] = 0;
|
||||
$return['ajax'] = 1;
|
||||
if ($res) {
|
||||
$res = $res->toArray();
|
||||
} else {
|
||||
$return['msg'] = '司机数据不存在';
|
||||
$return['errorcode'] = 404;
|
||||
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
$msg = '';
|
||||
$errorCode = 0;
|
||||
$detail->update_at = time();
|
||||
|
||||
switch ($get['do']) {
|
||||
case 'out':
|
||||
$data['status'] = 0;
|
||||
$model->id = $id;
|
||||
$res = $model->updateAll($data, $where);
|
||||
$detail->status_code = 0;
|
||||
$res = $detail->save();
|
||||
if ($res) {
|
||||
$return['msg'] = '司机离职标记成功';
|
||||
$return['errorcode'] = 200;
|
||||
$return['data'] = ['css' => 'info'];
|
||||
$msg = '司机离职标记成功';
|
||||
$errorCode = 200;
|
||||
} else {
|
||||
$return['msg'] = '司机离职标记失败';
|
||||
$return['errorcode'] = 0;
|
||||
$msg = '司机离职标记失败';
|
||||
}
|
||||
break;
|
||||
case 'in':
|
||||
$data['status'] = 1;
|
||||
$model->id = $id;
|
||||
$res = $model->updateAll($data, $where);
|
||||
$detail->status_code = 1;
|
||||
$res = $detail->save();
|
||||
if ($res) {
|
||||
$return['msg'] = '司机在职标记成功';
|
||||
$return['errorcode'] = 200;
|
||||
$return['data'] = ['css' => 'default'];
|
||||
$msg = '司机在职标记成功';
|
||||
$errorCode = 200;
|
||||
} else {
|
||||
$return['msg'] = '司机在职标记失败';
|
||||
$return['errorcode'] = 0;
|
||||
$msg = '司机在职标记失败';
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
||||
$this->result($msg,[], $errorCode);
|
||||
}
|
||||
|
||||
private function search(){
|
||||
$pid = $this->pid;
|
||||
$return = [];
|
||||
$model = new AcDriver();
|
||||
$where = $where2 = [];
|
||||
$where['pid'] = $pid;
|
||||
$post = $this->postdata;
|
||||
$search = $post['search'] ?? [];
|
||||
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
|
||||
if (!empty($search['title'])) {
|
||||
$where2[] = 'or';
|
||||
$where2[] = ['LIKE', 'name', $search['title']];
|
||||
$where2[] = ['LIKE', 'mobile', $search['title']];
|
||||
}
|
||||
|
||||
$where['deleted'] = 0;
|
||||
$data = $model->find()->where($where);
|
||||
if ($search['title']) {
|
||||
$data = $data->andwhere($where2);
|
||||
}
|
||||
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
|
||||
$res = $data->offset($pages->offset)->limit($pages->limit)->orderBy('order_by DESC')->all();
|
||||
|
||||
return ['res'=>$res,'query'=>$data];
|
||||
}
|
||||
|
||||
/*
|
||||
* 数据更新前的预检查,返回对应关联数据
|
||||
* 必须确保get与post数据中均包含需更新的数据id且一致
|
||||
* */
|
||||
private function preUpdate()
|
||||
{
|
||||
$id = (int)Yii::$app->request->get('id');
|
||||
if(!$id) $this->result('请求错误,未携带ID参数');
|
||||
$post = $this->postdata;
|
||||
if($post['id'] != $id) $this->result('传参id与请求数据不匹配',[],403);
|
||||
$model = new AcDriver();
|
||||
$res = $model->findOne($id);
|
||||
if(!$res) $this->result('未查询到相应数据',[],404);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/*
|
||||
* 数据保存前的预检查(查重、参数校验等)
|
||||
* 要保存的数据[],直接从post中取出
|
||||
* @op,操作类型(add,edit……)
|
||||
* 校验机制:
|
||||
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
|
||||
* 查重:禁止录入重复数据
|
||||
* 返回:校验重组后的数据
|
||||
* */
|
||||
private function preSave($op)
|
||||
{
|
||||
$post = $this->postdata;
|
||||
$name = trim($post['name']) ?? '';
|
||||
if(!$name) $this->result('请传入司机姓名');
|
||||
|
||||
$data = [];
|
||||
//格式化数据
|
||||
//目标键=》POST键
|
||||
|
||||
$model = new AcDriver();
|
||||
switch ($op) {
|
||||
case 'add':
|
||||
$mobile = trim($post['mobile']) ?? '';
|
||||
if(!$mobile) $this->result('请传入手机号');
|
||||
//查询通讯录手机号是否在库(如果在库,则取出相应资料,剩余逻辑由前端完成)
|
||||
$hasOne = AcDriver::find()->where(['mobile' => $mobile])->one();
|
||||
if($hasOne){
|
||||
$detail = $hasOne->toArray();
|
||||
$this->result('保存失败,该手机号对应的驾驶员已在库,系统自动现在为您调出该司机资料!',$detail,100);
|
||||
}
|
||||
$data['mobile'] = $mobile;
|
||||
$data['create_at'] = time();
|
||||
$data['update_at'] = $data['create_at'];
|
||||
break;
|
||||
case 'edit':
|
||||
//编辑模式下,没有改mobile,避免校验重复性
|
||||
$id = (int)Yii::$app->request->get('id');
|
||||
$_id = (int)$post['id'];
|
||||
if(!$id != $_id) $this->result('id参数不匹配,请检查');
|
||||
$data['update_at'] = time();
|
||||
break;
|
||||
}
|
||||
|
||||
$data['pid'] = $this->pid;
|
||||
$cols = ['status_code' => 'status_code', 'deleted' => 'deleted'];
|
||||
foreach ($cols as $col => $key) {
|
||||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
//驾驶员资料注册系统会员
|
||||
private function driverToUser($data){
|
||||
//查询司机对应的系统用户
|
||||
$userModel = new User();
|
||||
$user = $userModel->find()->where(['mobile' => $data['mobile']])->one();
|
||||
if($user) return $user->id;
|
||||
$userModel->username = !$data['name'] ? $data['mobile'] : $data['name'];
|
||||
$userModel->mobile = $data['mobile'];
|
||||
$userModel->email = $data['mobile'] . '@hiluker.com';
|
||||
$userModel->setPassword(Yii::$app->params['defaultUserPassword']);
|
||||
$userModel->generateAuthKey();
|
||||
$userModel->avatar = Yii::$app->params['defaultUserAvatar'];
|
||||
$userModel->created_at = time();
|
||||
$userModel->status = 1;
|
||||
$userModel->save();
|
||||
return $userModel->attributes['id']; //获取插入后id
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user