controller.tpl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\<model> as <model>Model;
  11. use app\admin\validate\<model>Validate;
  12. use think\exception\ValidateException;
  13. use think\facade\Db;
  14. use think\facade\View;
  15. class <controller> extends BaseController
  16. {
  17. /**
  18. * 构造函数
  19. */
  20. public function __construct()
  21. {
  22. $this->model = new <model>Model();
  23. $this->uid = get_login_admin('id');
  24. }
  25. /**
  26. * 数据列表
  27. */
  28. public function datalist()
  29. {
  30. if (request()->isAjax()) {
  31. $param = get_params();
  32. $where = [];
  33. <wheremap>
  34. $list = $this->model->get<model>List($where,$param);
  35. return table_assign(0, '', $list);
  36. }
  37. else{
  38. return view();
  39. }
  40. }
  41. /**
  42. * 添加
  43. */
  44. public function add()
  45. {
  46. if (request()->isAjax()) {
  47. $param = get_params();
  48. // 检验完整性
  49. try {
  50. validate(<model>Validate::class)->check($param);
  51. } catch (ValidateException $e) {
  52. // 验证失败 输出错误信息
  53. return to_assign(1, $e->getError());
  54. }
  55. <timeadd>
  56. $this->model->add<model>($param);
  57. }else{
  58. return view();
  59. }
  60. }
  61. /**
  62. * 编辑
  63. */
  64. public function edit()
  65. {
  66. $param = get_params();
  67. if (request()->isAjax()) {
  68. // 检验完整性
  69. try {
  70. validate(<model>Validate::class)->check($param);
  71. } catch (ValidateException $e) {
  72. // 验证失败 输出错误信息
  73. return to_assign(1, $e->getError());
  74. }
  75. <timeedit>
  76. $this->model->edit<model>($param);
  77. }else{
  78. $<pk> = isset($param['<pk>']) ? $param['<pk>'] : 0;
  79. $detail = $this->model->get<model>ById($<pk>);
  80. if (!empty($detail)) {
  81. View::assign('detail', $detail);
  82. return view();
  83. }
  84. else{
  85. throw new \think\exception\HttpException(404, '找不到页面');
  86. }
  87. }
  88. }
  89. /**
  90. * 查看信息
  91. */
  92. public function read()
  93. {
  94. $param = get_params();
  95. $<pk> = isset($param['<pk>']) ? $param['<pk>'] : 0;
  96. $detail = $this->model->get<model>ById($<pk>);
  97. if (!empty($detail)) {
  98. View::assign('detail', $detail);
  99. return view();
  100. }
  101. else{
  102. throw new \think\exception\HttpException(404, '找不到页面');
  103. }
  104. }
  105. /**
  106. * 删除
  107. * type=0,逻辑删除,默认
  108. * type=1,物理删除
  109. */
  110. public function del()
  111. {
  112. $param = get_params();
  113. $<pk> = isset($param['<pk>']) ? $param['<pk>'] : 0;
  114. $type = isset($param['type']) ? $param['type'] : 0;
  115. $this->model->del<model>ById($<pk>,$type);
  116. }
  117. }