Version.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 版本管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Version extends Backend
  13. {
  14. /**
  15. * Version模型对象
  16. * @var \app\admin\model\setting\Version
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\setting\Version;
  23. $this->view->assign("platformList", $this->model->getPlatformList());
  24. }
  25. public function import()
  26. {
  27. parent::import();
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $wherePlatform = [];
  45. $filter = json_decode(input('filter'), true);
  46. if (!$filter) {
  47. $wherePlatform = ['platform' => 'android'];
  48. }
  49. $list = $this->model
  50. ->field($this->indexField, $this->indexFieldExcept)
  51. ->where($where)
  52. ->where($wherePlatform)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. $this->assignconfig('tag_default', '#tab-default-android');
  59. return $this->view->fetch();
  60. }
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $params = $this->request->post("row/a");
  65. if ($params) {
  66. $params = $this->preExcludeFields($params);
  67. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  68. $params[$this->dataLimitField] = $this->auth->id;
  69. }
  70. // 查询已存在的版本号
  71. $lastVersion = $this->model->where('platform', $params['platform'])->order('id', 'desc')->value('version');
  72. $lastVersion = $lastVersion ?: '0.0.0.1';
  73. if (version_compare($params['version'], $lastVersion, '<=')) {
  74. $this->error('版本号必须大于' . $lastVersion);
  75. }
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  83. $this->model->validateFailException(true)->validate($validate);
  84. }
  85. $result = $this->model->allowField(true)->save($params);
  86. Db::commit();
  87. } catch (ValidateException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (PDOException $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. } catch (\Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result !== false) {
  98. $this->success();
  99. } else {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. }
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. return $this->view->fetch();
  106. }
  107. }