Template.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller\sms;
  3. use app\admin\model\UserGroup;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. *
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Template extends Backend
  15. {
  16. /**
  17. * Template模型对象
  18. * @var \app\admin\model\sms\Template
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\sms\Template;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. public function getType()
  28. {
  29. $list[0]['id']=0;
  30. $list[0]['name']='分';
  31. $list[1]['id']=1;
  32. $list[1]['name']='时';
  33. $list[2]['id']=2;
  34. $list[2]['name']='天';
  35. $i=$this->request->request('keyValue');
  36. if ($i) {
  37. return json(['list' => $list[$i], 'total' => count($list[$i])]);
  38. }
  39. return json(['list' => $list, 'total' => count($list)]);
  40. }
  41. /**
  42. * 添加
  43. *
  44. * @return string
  45. * @throws \think\Exception
  46. */
  47. public function add()
  48. {
  49. if (false === $this->request->isPost()) {
  50. return $this->view->fetch();
  51. }
  52. $params = $this->request->post('row/a');
  53. if($params['type']==2){
  54. $params['sendtime']=$params['times']*24*60*60;
  55. }elseif($params['type']==1){
  56. $params['sendtime']=$params['times']*60*60;
  57. }else{
  58. $params['sendtime']=$params['times']*60;
  59. }
  60. if (empty($params)) {
  61. $this->error(__('Parameter %s can not be empty', ''));
  62. }
  63. $params = $this->preExcludeFields($params);
  64. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  65. $params[$this->dataLimitField] = $this->auth->id;
  66. }
  67. $result = false;
  68. Db::startTrans();
  69. try {
  70. //是否采用模型验证
  71. if ($this->modelValidate) {
  72. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  73. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  74. $this->model->validateFailException()->validate($validate);
  75. }
  76. $result = $this->model->allowField(true)->save($params);
  77. Db::commit();
  78. } catch (ValidateException|PDOException|Exception $e) {
  79. Db::rollback();
  80. $this->error($e->getMessage());
  81. }
  82. if ($result === false) {
  83. $this->error(__('No rows were inserted'));
  84. }
  85. $this->success();
  86. }
  87. /**
  88. * 编辑
  89. *
  90. * @param $ids
  91. * @return string
  92. * @throws DbException
  93. * @throws \think\Exception
  94. */
  95. public function edit($ids = null)
  96. {
  97. $row = $this->model->get($ids);
  98. if (!$row) {
  99. $this->error(__('No Results were found'));
  100. }
  101. $adminIds = $this->getDataLimitAdminIds();
  102. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  103. $this->error(__('You have no permission'));
  104. }
  105. if (false === $this->request->isPost()) {
  106. $this->view->assign('row', $row);
  107. return $this->view->fetch();
  108. }
  109. $params = $this->request->post('row/a');
  110. if (empty($params)) {
  111. $this->error(__('Parameter %s can not be empty', ''));
  112. }
  113. if($params['type']==2){
  114. $params['sendtime']=$params['times']*24*60*60;
  115. }elseif($params['type']==1){
  116. $params['sendtime']=$params['times']*60*60;
  117. }else{
  118. $params['sendtime']=$params['times']*60;
  119. }
  120. $params = $this->preExcludeFields($params);
  121. $result = false;
  122. Db::startTrans();
  123. try {
  124. //是否采用模型验证
  125. if ($this->modelValidate) {
  126. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  127. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  128. $row->validateFailException()->validate($validate);
  129. }
  130. $result = $row->allowField(true)->save($params);
  131. Db::commit();
  132. } catch (ValidateException|PDOException|Exception $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. }
  136. if (false === $result) {
  137. $this->error(__('No rows were updated'));
  138. }
  139. $this->success();
  140. }
  141. /**
  142. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  143. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  144. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  145. */
  146. }