ADD:添加后台管理端接口;
DEL:删除不必要的头部注释; FIX:修正若干已知错误;
This commit is contained in:
@@ -1,12 +1,5 @@
|
||||
<?php
|
||||
|
||||
# @Author: 嗨噜客(三亚) <fm453>
|
||||
# @Date: 2022-05-22T07:39:14+08:00
|
||||
# @Email: fm453@lukegzs.com
|
||||
# @Last modified by: fm453
|
||||
# @Last modified time: 2024-08-09T09:19:45+08:00
|
||||
# @Copyright: www.hiluker.cn
|
||||
|
||||
namespace api\controllers\gm\v1;
|
||||
|
||||
use Yii;
|
||||
@@ -19,230 +12,277 @@
|
||||
//主界面
|
||||
public function actionIndex()
|
||||
{
|
||||
return $this->render('../layouts/dev', []);
|
||||
$apis = [
|
||||
'list' => '平台清单',
|
||||
'detail' => '平台详情',
|
||||
'add'=>'添加',
|
||||
'edit'=>'编辑',
|
||||
'exts'=>'扩展参数',
|
||||
'delete'=>'删除',
|
||||
'show'=>'显示',
|
||||
'hide'=>'隐藏',
|
||||
'ajax'=>'ajax操作'
|
||||
];
|
||||
$this->result('您正使用CMTS-GM系统平台管理接口!', $apis, 200);
|
||||
}
|
||||
|
||||
public function actionList()
|
||||
{
|
||||
$s = $this->search();
|
||||
$res = $s['res'];
|
||||
if (!$res) {
|
||||
$this->result('没有查询到相应的数据!', [], 0);
|
||||
}
|
||||
|
||||
$data= [
|
||||
'total'=>$s['query']->count(),
|
||||
'plats' => $res,
|
||||
'stores' => $s['stores'],
|
||||
'page'=>$this->page
|
||||
];
|
||||
$this->result('平台查询成功!', $data, 200);
|
||||
}
|
||||
|
||||
public function actionDetail()
|
||||
{
|
||||
$pid = $this->pid;
|
||||
$post = $this->postdata;
|
||||
|
||||
$id = isset($post['id']) ? $post['id'] : 0;
|
||||
if ($id <= 0) {
|
||||
$this->result('查询参数错误!');
|
||||
}
|
||||
$model = new AcPlat();
|
||||
$search = $where = [];
|
||||
$detail = $model->findOne($id)->toArray();
|
||||
if(!$detail) $this->result('未查询到相应数据');
|
||||
$exts = $this->showExt($detail);
|
||||
foreach ($exts as $k => $v) {
|
||||
$detail[$k] = $v;
|
||||
}
|
||||
$this->result('查询成功!', $detail, 200);
|
||||
}
|
||||
|
||||
public function actionAdd()
|
||||
{
|
||||
//数据预检查、编排
|
||||
$data = $this->preSave('add');
|
||||
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
|
||||
|
||||
//保存资料
|
||||
$model = new AcPlat();
|
||||
foreach ($data as $key=>$val) {
|
||||
$model->$key = $val;
|
||||
}
|
||||
$res = $model->save();
|
||||
$msg = '数据保存失败!';
|
||||
if(!$res) $this->result($msg, [], 100);
|
||||
|
||||
$msg = '数据保存成功!';
|
||||
$return = [];
|
||||
$return['id']= $model->attributes['id']; //获取插入后id;
|
||||
$this->result($msg,$return, 200);
|
||||
}
|
||||
|
||||
public function actionEdit()
|
||||
{
|
||||
$detail = $this->preUpdate();
|
||||
//数据预检查、编排
|
||||
$data = $this->preSave('edit');
|
||||
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
|
||||
|
||||
//保存资料
|
||||
foreach ($data as $key=>$val) {
|
||||
$detail->$key = $val;
|
||||
}
|
||||
$res = $detail->save();
|
||||
$msg = '数据编辑失败!';
|
||||
if(!$res) $this->result($msg, [], 100);
|
||||
|
||||
$msg = '数据编辑成功!';
|
||||
$return = [];
|
||||
$return['data']= $detail;
|
||||
$this->result($msg,$return, 200);
|
||||
}
|
||||
|
||||
//编辑平台参数
|
||||
public function actionExts()
|
||||
{
|
||||
$pid = $this->pid;
|
||||
if(!$pid) $this->result('您正使用本系统内部接口,禁止非法链接使用!');
|
||||
if($this->user_id !=1) $this->result('修改失败,平台配置仅支持超级管理员修改!');
|
||||
$detail = $this->preUpdate();
|
||||
//数据预检查、编排
|
||||
$data = $this->preSave('edit');
|
||||
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
|
||||
$exts = $this->saveExt();
|
||||
if(!$exts) $this->result('参数为空,保存失败', [], 100);
|
||||
$detail->remark = $exts;
|
||||
$res = $detail->save();
|
||||
$msg = '参数编辑失败!';
|
||||
if(!$res) $this->result($msg, [], 100);
|
||||
|
||||
$msg = '参数编辑成功!';
|
||||
$this->result($msg,[], 200);
|
||||
}
|
||||
|
||||
public function actionDelete()
|
||||
{
|
||||
$detail = $this->preUpdate();
|
||||
$data = [];
|
||||
$data['update_at'] = time();
|
||||
$data['deleted'] = $detail->deleted + 1;
|
||||
|
||||
//保存资料
|
||||
foreach ($data as $key=>$val) {
|
||||
$detail->$key = $val;
|
||||
}
|
||||
$res = $detail->save();
|
||||
$msg = '平台删除失败!';
|
||||
if(!$res) $this->result($msg, [], 100);
|
||||
|
||||
$msg = '平台删除成功!';
|
||||
$this->result($msg,[], 200);
|
||||
}
|
||||
|
||||
public function actionAjax()
|
||||
{
|
||||
$detail = $this->preUpdate();
|
||||
$get = Yii::$app->request->get();
|
||||
$msg = '';
|
||||
$errorCode = 0;
|
||||
$detail->update_at = time();
|
||||
|
||||
switch ($get['do']) {
|
||||
case 'hide':
|
||||
$detail->status_code = 0;
|
||||
$res = $detail->save();
|
||||
if ($res) {
|
||||
$msg = '平台隐藏标记成功';
|
||||
$errorCode = 200;
|
||||
} else {
|
||||
$msg = '平台隐藏标记失败';
|
||||
}
|
||||
break;
|
||||
case 'show':
|
||||
$detail->status_code = 1;
|
||||
$res = $detail->save();
|
||||
if ($res) {
|
||||
$msg = '平台显示标记成功';
|
||||
$errorCode = 200;
|
||||
} else {
|
||||
$msg = '平台显示标记失败';
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
$this->result($msg,[], $errorCode);
|
||||
}
|
||||
|
||||
private function search(){
|
||||
$return = [];
|
||||
$model = new AcPlat();
|
||||
$where = [];
|
||||
$where[] = 'and';
|
||||
if (isset($_POST['title']) && $_POST['title'] != '') {
|
||||
$search['title'] = $_POST['title'];
|
||||
$post = $this->postdata;
|
||||
$search = $post['search'] ?? [];
|
||||
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
|
||||
if (!$search['title']) {
|
||||
$where[] = ['LIKE', 'title', $search['title']];
|
||||
}
|
||||
$where[] = ['=', 'deleted', 0];
|
||||
$data = $model->find()->where($where);
|
||||
|
||||
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '5']);
|
||||
$res = $data->orderby('orderby DESC')->offset($pages->offset)->limit($pages->limit)->all();
|
||||
$status = Yii::$app->params['CommonStatus'];
|
||||
$plats = [];
|
||||
foreach ($res as $r) {
|
||||
$plats[$r->id] = $r->toArray();
|
||||
}
|
||||
return $this->render('list', [
|
||||
'plats' => $plats,
|
||||
'pager' => $pages,
|
||||
'search' => $search,
|
||||
'status' => $status
|
||||
]);
|
||||
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
|
||||
$_orderby = 'orderby DESC,update_at DESC';
|
||||
$res = $data->offset($pages->offset)->limit($pages->limit)->orderBy($_orderby)->indexBy('id')->all();
|
||||
|
||||
return ['res'=>$res,'query'=>$data];
|
||||
}
|
||||
|
||||
public function actionNew()
|
||||
{
|
||||
return $this->render('modify', []);
|
||||
}
|
||||
|
||||
public function actionEdit()
|
||||
/*
|
||||
* 数据更新前的预检查,返回对应关联数据
|
||||
* 必须确保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 AcPlat();
|
||||
$id = Yii::$app->request->get('id');
|
||||
$res = $model->findOne($id);
|
||||
if ($res) {
|
||||
$res = $res->toArray();
|
||||
}
|
||||
return $this->render('modify', ['detail' => $res]);
|
||||
if(!$res) $this->result('未查询到相应数据',[],404);
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function actionSave()
|
||||
{
|
||||
$post = Yii::$app->request->post();
|
||||
/*
|
||||
* 数据保存前的预检查(查重、参数校验等)
|
||||
* 要保存的数据[],直接从post中取出
|
||||
* @op,操作类型(add,edit……)
|
||||
* 校验机制:
|
||||
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
|
||||
* 查重:禁止录入重复数据
|
||||
* 返回:校验重组后的数据
|
||||
* */
|
||||
private function preSave($op)
|
||||
{
|
||||
$post = $this->postdata;
|
||||
$title = trim($post['title']) ?? '';
|
||||
if(!$title) $this->result('请传入平台名称');
|
||||
|
||||
$data = [];
|
||||
$id = $post['id'];
|
||||
//格式化数据
|
||||
//目标键=》POST键
|
||||
$cols = ['title' => 'title', 'thumb' => 'thumb'];
|
||||
|
||||
$model = new AcPlat();
|
||||
switch ($op) {
|
||||
case 'add':
|
||||
$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;
|
||||
}
|
||||
$cols = ['thumb' => 'thumb'];
|
||||
foreach ($cols as $col => $key) {
|
||||
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
|
||||
}
|
||||
$cols = ['orderby' => 'orderby', 'deleted' => 'deleted'];
|
||||
$cols = ['status_code' => 'status_code', 'deleted' => 'deleted','order_by'=>'order_by'];
|
||||
foreach ($cols as $col => $key) {
|
||||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
||||
}
|
||||
$data['update_at'] = time();
|
||||
$data['create_at'] = $data['update_at'];
|
||||
|
||||
$model = new AcPlat();
|
||||
foreach ($data as $key => $val) {
|
||||
$model->$key = $val;
|
||||
}
|
||||
if ($id) {
|
||||
$model->id = $id;
|
||||
$res = $model->updateAll($data, ['id' => $id]);
|
||||
} else {
|
||||
$res = $model->save();
|
||||
$id = $model->attributes['id']; //获取插入后id
|
||||
}
|
||||
|
||||
if ($res) {
|
||||
\Yii::$app->getSession()->setFlash('success', '平台保存成功!');
|
||||
return $this->redirect(['edit', 'id' => $id]);
|
||||
} else {
|
||||
\Yii::$app->getSession()->setFlash('warning', '平台保存失败!');
|
||||
return $this->goBack();
|
||||
}
|
||||
$exts = $this->saveExt();
|
||||
if($exts) $data['remark'] = $exts;
|
||||
return $data;
|
||||
}
|
||||
|
||||
/*
|
||||
* 格式化显示扩展字段
|
||||
* @data 平台数据[]
|
||||
* 返回[]
|
||||
* */
|
||||
private function showExt($data){
|
||||
$json = $data['remark'];
|
||||
$res = json_decode($json,true);
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function actionExt()
|
||||
private function saveExt()
|
||||
{
|
||||
$session = Yii::$app->session;
|
||||
if (isset($_GET['pid']) && (int)$_GET['pid'] > 0) {
|
||||
$session->set('pid', (int)$_GET['pid']);
|
||||
}
|
||||
$pid = $session->get('pid');
|
||||
if (!$pid) {
|
||||
$url = Url::toRoute(['index/index', 'pid' => 0]);
|
||||
header('location:' . $url);
|
||||
exit;
|
||||
}
|
||||
|
||||
$model = new AcPlat();
|
||||
$res = $model->findOne($pid);
|
||||
$detail = [];
|
||||
if ($res) {
|
||||
$detail = json_decode($res->remark, TRUE);
|
||||
}
|
||||
return $this->render('ext', ['detail' => $detail]);
|
||||
}
|
||||
|
||||
public function actionExtsave()
|
||||
{
|
||||
$user_id = Yii::$app->user->identity->getId();
|
||||
if ($user_id != 1) {
|
||||
\Yii::$app->getSession()->setFlash('warning', '修改失败,平台配置仅支持超级管理员修改!');
|
||||
return $this->redirect(['plat/ext']);
|
||||
}
|
||||
$post = Yii::$app->request->post();
|
||||
$session = Yii::$app->session;
|
||||
$pid = $session->get('pid');
|
||||
|
||||
$post = $this->postdata;
|
||||
$data = [];
|
||||
//目标键=》POST键
|
||||
$cols = ['cw_phone' => 'cw_phone'];
|
||||
foreach ($cols as $col => $key) {
|
||||
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
|
||||
}
|
||||
$remark = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$model = new AcPlat();
|
||||
$model->remark = $remark;
|
||||
$res = $model->updateAll(['remark' => $remark], ['id' => $pid]);
|
||||
|
||||
if ($res) {
|
||||
\Yii::$app->getSession()->setFlash('success', '参数保存成功!');
|
||||
} else {
|
||||
\Yii::$app->getSession()->setFlash('warning', '参数保存失败!');
|
||||
}
|
||||
return $this->redirect(['plat/ext']);
|
||||
}
|
||||
|
||||
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;
|
||||
$data['update_at'] = time();
|
||||
$data['deleted'] = 1;
|
||||
|
||||
$model = new AcPlat();
|
||||
foreach ($data as $key => $val) {
|
||||
$model->$key = $val;
|
||||
}
|
||||
|
||||
$model->id = $id;
|
||||
$res = $model->updateAll($data, $where);
|
||||
|
||||
$return = [];
|
||||
$return['url'] = Url::toRoute('plat/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));
|
||||
}
|
||||
|
||||
public function actionAjax()
|
||||
{
|
||||
$get = Yii::$app->request->get();
|
||||
$post = Yii::$app->request->post();
|
||||
$id = (int)$get['id'];
|
||||
$data = $where = [];
|
||||
$where['id'] = $id;
|
||||
$data['update_at'] = time();
|
||||
|
||||
$model = new AcPlat();
|
||||
$res = $model->findOne($id);
|
||||
$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));
|
||||
}
|
||||
|
||||
switch ($get['do']) {
|
||||
case 'hide':
|
||||
$data['status_code'] = 0;
|
||||
$model->id = $id;
|
||||
$res = $model->updateAll($data, $where);
|
||||
if ($res) {
|
||||
$return['msg'] = '平台隐藏成功';
|
||||
$return['errorcode'] = 200;
|
||||
$return['data'] = ['css' => 'info'];
|
||||
} else {
|
||||
$return['msg'] = '平台隐藏失败';
|
||||
$return['errorcode'] = 0;
|
||||
}
|
||||
break;
|
||||
case 'show':
|
||||
$data['status_code'] = 1;
|
||||
$model->id = $id;
|
||||
$res = $model->updateAll($data, $where);
|
||||
if ($res) {
|
||||
$return['msg'] = '平台显示成功';
|
||||
$return['errorcode'] = 200;
|
||||
$return['data'] = ['css' => 'default'];
|
||||
} else {
|
||||
$return['msg'] = '平台显示失败';
|
||||
$return['errorcode'] = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
exit(json_encode($return));
|
||||
if(!$data) return false;
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user