upgrade/application/admin/controller/Version.php

369 lines
11 KiB
PHP

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Controller;
use think\Request;
use think\Db;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use Exception;
use think\Validate;
/**
* 版本管理
*
* @icon fa fa-circle-o
*/
class Version extends Backend
{
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = model('Version');
$this->view->assign("sysList", self::getSysList());
$this->assignconfig("sysList", self::getSysList());
$this->view->assign("cpuList", self::getCpuList());
$this->assignconfig("cpuList", self::getCpuList());
$this->view->assign("oemList", self::getOemList());
$this->assignconfig("oemList", self::getOemList());
$this->view->assign("appList", self::getAppList());
$this->assignconfig("appList", self::getAppList());
$this->view->assign("dev_typeList", self::getDev_typeList());
$this->assignconfig("dev_typeList", self::getDev_typeList());
}
/**
* 批量更新
* @internal
*/
public function multi($ids = "")
{
// 管理员禁止批量操作
$this->error();
}
public static function getSysList()
{
$data = config('site.sys') ?? [];
foreach ($data as $index => &$datum) {
$datum = __($datum);
}
return $data;
}
public static function getCpuList()
{
$data = config('site.cpu') ?? [];
foreach ($data as $index => &$datum) {
$datum = __($datum);
}
return $data;
}
public static function getOemList()
{
$data = config('site.oem') ?? [];
foreach ($data as $index => &$datum) {
$datum = __($datum);
}
return $data;
}
public static function getAppList()
{
$data = config('site.app') ?? [];
foreach ($data as $index => &$datum) {
$datum = __($datum);
}
return $data;
}
public static function getDev_typeList()
{
$data = config('site.dev_type') ?? [];
foreach ($data as $index => &$datum) {
$datum = __($datum);
}
return $data;
}
/**
* 添加
*
* @return string
* @throws \think\Exception
*/
public function add()
{
if (false === $this->request->isPost()) {
return $this->view->fetch();
}
$params = $this->request->post('row/a');
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
$params = $this->preExcludeFields($params);
if(empty($params['version'])){
$this->error(__('版本号不能为空', ''));
}
if(empty($params['size'])){
$this->error(__('包大小不能为空', ''));
}
if(empty($params['downloadurl'])){
$this->error(__('文件路径不能为空', ''));
}
if(empty($params['md5'])){
$this->error(__('MD5不能为空', ''));
}
// if(!empty($params['version'])){
// $count = $this->model->where(['version' => $params['version']])->count();
// if($count > 0){
// $this->error(__('该版本号已存在,请核验后继续添加!'));
// }
// }
$params['status'] = 'normal';
$params['operator'] = $this->auth->username;
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException()->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result === false) {
$this->error(__('No rows were inserted'));
}
//更新缓存
$this->updateCac();
$this->success();
}
/**
* 编辑
*
* @param $ids
* @return string
* @throws DbException
* @throws \think\Exception
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
if (false === $this->request->isPost()) {
$this->view->assign('row', $row);
return $this->view->fetch();
}
$params = $this->request->post('row/a');
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
if(empty($params['version'])){
$this->error(__('版本号不能为空', ''));
}
if(empty($params['size'])){
$this->error(__('包大小不能为空', ''));
}
if(empty($params['downloadurl'])){
$this->error(__('文件路径不能为空', ''));
}
if(empty($params['md5'])){
$this->error(__('MD5不能为空', ''));
}
// if($params['status'] == 'hidden'){
// //判断是否还存在其他未作废版本,因为必须存在一个未作废的
// $count = $this->model->where(['status' => 'normal'])->where('id', '<>', $ids)->count();
// if($count == '0'){
// $this->error(__('必须存在一个正常版本,才能进行作废操作', ''));
// }
// }
$params['operator'] = $this->auth->username;
$params = $this->preExcludeFields($params);
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validateFailException()->validate($validate);
}
$result = $row->allowField(true)->save($params);
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (false === $result) {
$this->error(__('No rows were updated'));
}
//更新缓存
$this->updateCac();
$this->success();
}
/**
* 删除
*
* @param $ids
* @return void
* @throws DbException
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
public function del($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post("ids");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $item) {
$count += $item->delete();
}
Db::commit();
} catch (PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->updateCac();
$this->success();
}
$this->error(__('No rows were deleted'));
}
/**
* 真实删除
*
* @param $ids
* @return void
*/
public function destroy($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post('ids');
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$this->model->where($pk, 'in', $ids);
$count = 0;
Db::startTrans();
try {
$list = $this->model->onlyTrashed()->select();
foreach ($list as $item) {
$count += $item->delete(true);
}
Db::commit();
} catch (PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
}
$this->error(__('No rows were deleted'));
}
public function cancel($ids = null){
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
//判断状态是否为非撤回状态
if($row->status == 'normal'){
//判断是否还存在其他未作废版本,因为必须存在一个未作废的
$count = $this->model->where(['status' => 'normal'])->where('id', '<>', $ids)->count();
if($count == '0'){
$this->error(__('必须存在一个正常版本,才能进行作废操作', ''));
}
$params['status'] = 'hidden';
$params['operator'] = $this->auth->username;
$result = $row->allowField(true)->save($params);
if (false === $result) {
$this->error(__('No rows were updated'));
}
//更新缓存
$this->updateCac();
$this->cancelVerUpCac($ids);
$this->success('作废版本成功!', null, 'success');
}
$this->error(__('请刷新后再次操作'));
}
private function updateCac(){
$redis = getRedis();
$redis->del('version_last');
}
/**
* 作废版本(更新废弃版本缓存)
* @desc 对某个版本作废,作废后将版本号加入到接口缓存的hash中便于接口判断查询
* $id 需要操作的版本_id
* $isCancel 作废true,还是取消作废false
*/
private function cancelVerUpCac($id, $isCancel = true){
$list = collection($this->model->where('id' ,'=', $id)->limit(1)->field('version')->select())->toArray();
$version = $list[0]['version'] ?? '';
if($isCancel){
// 增加
if(!empty($version) && $version != ''){
$redis = getRedis();
$redis->hset('client_cancel', $version, 1);
}
}else{
// 取消
$redis = getRedis();
$redis->hdel('client_cancel', $version);
}
}
}