Subscribe.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\admin\controller\weixin\reply;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 微信关键字回复管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Subscribe extends Backend
  14. {
  15. /**
  16. * Subscribe模型对象
  17. * @var \app\admin\model\weixin\reply\Subscribe
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\weixin\Reply;
  24. $this->view->assign("typeList", $this->model->getTypeList());
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. $row = $this->model->where(['key' => 'subscribe'])->find();
  32. if (!$row) {
  33. $params['key'] = 'subscribe';
  34. $params['type'] = 'text';
  35. $this->model->allowField(true)->save($params);
  36. $row = $this->model->get($this->model->id);
  37. }
  38. if (!$row) {
  39. $this->error(__('No Results were found'));
  40. }
  41. $adminIds = $this->getDataLimitAdminIds();
  42. if (is_array($adminIds)) {
  43. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  44. $this->error(__('You have no permission'));
  45. }
  46. }
  47. if ($this->request->isPost()) {
  48. $params = $this->request->post("row/a");
  49. if ($params) {
  50. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  51. $result = false;
  52. Db::startTrans();
  53. try {
  54. $method = 'tidy' . ucfirst($params['type']);
  55. $params['data'] = $row->$method($params['data'], $params['type']);
  56. //是否采用模型验证
  57. if ($this->modelValidate) {
  58. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  59. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  60. $row->validateFailException(true)->validate($validate);
  61. }
  62. $result = $row->allowField(true)->save($params);
  63. Db::commit();
  64. } catch (ValidateException $e) {
  65. Db::rollback();
  66. $this->error($e->getMessage());
  67. } catch (PDOException $e) {
  68. Db::rollback();
  69. $this->error($e->getMessage());
  70. } catch (Exception $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. }
  74. if ($result !== false) {
  75. $this->success();
  76. } else {
  77. $this->error(__('No rows were updated'));
  78. }
  79. }
  80. $this->error(__('Parameter %s can not be empty', ''));
  81. }
  82. $this->view->assign("row", $row);
  83. $this->view->assign("data", json_decode($row['data'], true));
  84. return $this->view->fetch();
  85. }
  86. }