2018-02-16 02:26:14 +08:00

154 lines
4.3 KiB
PHP

<?php
/**
*
* @since 2018-02-11
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
namespace app\admin\controller;
use app\model\ApiApp;
use app\model\ApiMenu;
use app\model\ApiList;
use app\model\ApiGroup;
use app\util\ReturnCode;
use app\util\Strs;
class App extends Base {
/**
* 获取应用列表
* @return array
* @throws \think\exception\DbException
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function index() {
$limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
$start = $limit * ($this->request->get('page', 1) - 1);
$keywords = $this->request->get('keywords', '');
$type = $this->request->get('type', '');
$status = $this->request->get('status', '');
$where = [];
if ($status === '1' || $status === '0') {
$where['app_status'] = $status;
}
if ($type) {
switch ($type) {
case 1:
$where['app_id'] = $keywords;
break;
case 2:
$where['app_name'] = ['like', "%{$keywords}%"];
break;
}
}
$listInfo = (new ApiApp())->where($where)->order('app_addTime', 'DESC')->limit($start, $limit)->select();
$count = (new ApiApp())->where($where)->count();
$listInfo = $this->buildArrFromObj($listInfo);
return $this->buildSuccess([
'list' => $listInfo,
'count' => $count
]);
}
/**
* 获取AppId,AppSecret,接口列表,应用接口权限细节
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
* @throws \think\exception\DbException
*/
public function getAppInfo() {
$apiArr = ApiList::all();
foreach ($apiArr as $api) {
$res['apiList'][$api['groupId']][] = $api;
}
$groupArr = ApiGroup::all();
$groupArr = $this->buildArrFromObj($groupArr);
$res['groupInfo'] = array_column($groupArr, 'name', 'id');
$res['groupInfo'][0] = '默认分组';
$id = $this->request->get('id', 0);
if ($id) {
} else {
$res['app_id'] = Strs::randString(8, 1);
$res['app_secret'] = Strs::randString(32);
}
return $this->buildSuccess($res);
}
/**
* 新增菜单
* @return array
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function add() {
$postData = $this->request->post();
$res = ApiMenu::create($postData);
if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
} else {
return $this->buildSuccess([]);
}
}
/**
* 应用状态编辑
* @return array
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function changeStatus() {
$id = $this->request->get('id');
$status = $this->request->get('status');
$res = ApiApp::update([
'app_status' => $status
], [
'id' => $id
]);
if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
} else {
return $this->buildSuccess([]);
}
}
/**
* 编辑菜单
* @return array
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function edit() {
$postData = $this->request->post();
$res = ApiMenu::update($postData);
if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
} else {
return $this->buildSuccess([]);
}
}
/**
* 删除菜单
* @return array
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function del() {
$id = $this->request->get('id');
if (!$id) {
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
}
$childNum = ApiMenu::where(['fid' => $id])->count();
if ($childNum) {
return $this->buildFailed(ReturnCode::INVALID, '当前菜单存在子菜单,不可以被删除!');
} else {
ApiMenu::destroy($id);
return $this->buildSuccess([]);
}
}
}