Protocol.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * app协议政策
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Protocol extends Backend
  13. {
  14. CONST TYPES = [
  15. '1' => '使用条款',
  16. '2' => '隐私政策',
  17. ];
  18. /**
  19. * Protocol模型对象
  20. * @var \app\admin\model\Protocol
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\Protocol;
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags', 'trim']);
  35. if ($this->request->isAjax()) {
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. if ($this->request->request('keyField')) {
  38. return $this->selectpage();
  39. }
  40. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  41. $list = $this->model
  42. ->where($where)
  43. ->order($sort, $order)
  44. ->paginate($limit);
  45. foreach($list as &$item) {
  46. $item['url'] = addHost('/protocol/index/privacy/t/').$item['type'];
  47. $item['type'] = self::TYPES[$item['type']];
  48. }
  49. $result = array("total" => $list->total(), "rows" => $list->items());
  50. return json($result);
  51. }
  52. return $this->view->fetch();
  53. }
  54. /**
  55. * 添加
  56. */
  57. public function add(): string
  58. {
  59. if ($this->request->isPost()) {
  60. $params = $this->request->post("row/a");
  61. if ($params) {
  62. $params = $this->preExcludeFields($params);
  63. //查询相同类型的是否存在
  64. $one = $this->model->where('type', $params['type'])->count();
  65. if($one) $this->error('同类型协议已存在');
  66. $result = false;
  67. Db::startTrans();
  68. try {
  69. $result = $this->model->allowField(true)->save($params);
  70. Db::commit();
  71. } catch (ValidateException | PDOException | Exception $e) {
  72. Db::rollback();
  73. $this->error($e->getMessage());
  74. }
  75. if ($result !== false) {
  76. $this->success();
  77. } else {
  78. $this->error(__('No rows were inserted'));
  79. }
  80. }
  81. $this->error(__('Parameter %s can not be empty', ''));
  82. }
  83. return $this->view->fetch();
  84. }
  85. }