Template.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\admin\controller\vbot;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Hook;
  9. /**
  10. * 通知模板管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Template extends Backend
  15. {
  16. /**
  17. * VbotTemplate模型对象
  18. * @var \app\admin\model\VbotTemplate
  19. */
  20. protected $model = null;
  21. protected $multiFields = 'openswitch';
  22. protected $modelValidate = true; //开启Validate验证
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\VbotTemplate;
  27. $this->view->assign("typelistList", $this->model->getTypelistList());
  28. $this->view->assign("isAtallList", $this->model->getIsAtallList());
  29. }
  30. public function msg_test()
  31. {
  32. $post_data = $this->request->only('ids');
  33. /*$res = \think\Hook::listen('vbot_send_msg', $post_data['ids'], [], true);
  34. if ($res['errcode'] != 0) {
  35. $this->error($res['errmsg'] . '(' . $res['errcode'] . ')');
  36. } else {
  37. $this->success('发送成功!');
  38. }*/
  39. $vbot = new \addons\vbot\Vbot();
  40. $res = $vbot->vbotSendMsg($post_data['ids'], [], [
  41. 'dynamic_variable' => '赋值成功~'
  42. ]);
  43. if ($res['errcode'] != 0) {
  44. $this->error($res['errmsg'] . '(' . $res['errcode'] . ')');
  45. } else {
  46. $this->success('发送成功!');
  47. }
  48. }
  49. /**
  50. * 查看
  51. */
  52. public function index()
  53. {
  54. //当前是否为关联查询
  55. $this->relationSearch = true;
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags']);
  58. if ($this->request->isAjax()) {
  59. //如果发送的来源是Selectpage,则转发到Selectpage
  60. if ($this->request->request('keyField')) {
  61. return $this->selectpage();
  62. }
  63. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  64. $total = $this->model
  65. ->with(['vbotrobot'])
  66. ->where($where)
  67. ->order($sort, $order)
  68. ->count();
  69. $list = $this->model
  70. ->with(['vbotrobot'])
  71. ->where($where)
  72. ->order($sort, $order)
  73. ->limit($offset, $limit)
  74. ->select();
  75. $list = addtion($list, [
  76. [
  77. 'field' => 'robot_ids',
  78. 'name' => 'vbot_robot'
  79. ]
  80. ]);
  81. $list = collection($list)->toArray();
  82. $result = array("total" => $total, "rows" => $list);
  83. return json($result);
  84. }
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 添加
  89. */
  90. public function add()
  91. {
  92. if ($this->request->isPost()) {
  93. $params = $this->request->post("row/a");
  94. if ($params) {
  95. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  96. if ($params['code']) {
  97. $code_exist = Db::name('vbot_template')->where('code', $params['code'])->value('id');
  98. if ($code_exist) {
  99. $this->error(__('Code exist'));
  100. }
  101. }
  102. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  103. $params[$this->dataLimitField] = $this->auth->id;
  104. }
  105. $result = false;
  106. Db::startTrans();
  107. try {
  108. //是否采用模型验证
  109. if ($this->modelValidate) {
  110. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  111. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  112. $this->model->validateFailException(true)->validate($validate);
  113. }
  114. $result = $this->model->allowField(true)->save($params);
  115. Db::commit();
  116. } catch (ValidateException $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. } catch (PDOException $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. } catch (Exception $e) {
  123. Db::rollback();
  124. $this->error($e->getMessage());
  125. }
  126. if ($result !== false) {
  127. $this->success();
  128. } else {
  129. $this->error(__('No rows were inserted'));
  130. }
  131. }
  132. $this->error(__('Parameter %s can not be empty', ''));
  133. }
  134. return $this->view->fetch();
  135. }
  136. /**
  137. * 编辑
  138. */
  139. public function edit($ids = null)
  140. {
  141. $row = $this->model->get($ids);
  142. if (!$row) {
  143. $this->error(__('No Results were found'));
  144. }
  145. $adminIds = $this->getDataLimitAdminIds();
  146. if (is_array($adminIds)) {
  147. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  148. $this->error(__('You have no permission'));
  149. }
  150. }
  151. if ($this->request->isPost()) {
  152. $params = $this->request->post("row/a");
  153. if ($params) {
  154. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  155. if ($params['code'] && $params['code'] != $row['code']) {
  156. $code_exist = Db::name('vbot_template')->where('code', $params['code'])->value('id');
  157. if ($code_exist) {
  158. $this->error(__('Code exist'));
  159. }
  160. }
  161. $result = false;
  162. Db::startTrans();
  163. try {
  164. //是否采用模型验证
  165. if ($this->modelValidate) {
  166. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  167. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  168. $row->validateFailException(true)->validate($validate);
  169. }
  170. $result = $row->allowField(true)->save($params);
  171. Db::commit();
  172. } catch (ValidateException $e) {
  173. Db::rollback();
  174. $this->error($e->getMessage());
  175. } catch (PDOException $e) {
  176. Db::rollback();
  177. $this->error($e->getMessage());
  178. } catch (Exception $e) {
  179. Db::rollback();
  180. $this->error($e->getMessage());
  181. }
  182. if ($result !== false) {
  183. $this->success();
  184. } else {
  185. $this->error(__('No rows were updated'));
  186. }
  187. }
  188. $this->error(__('Parameter %s can not be empty', ''));
  189. }
  190. $this->view->assign("row", $row);
  191. return $this->view->fetch();
  192. }
  193. }