Wx.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\admin\controller\guide;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use think\Config;
  6. use think\Controller;
  7. use think\Log;
  8. use think\Request;
  9. /**
  10. * 导粉公众号
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Wx extends Backend
  15. {
  16. /**
  17. * GuideWx模型对象
  18. */
  19. protected $model = null;
  20. /**
  21. * 是否开启数据限制
  22. * 支持auth/personal
  23. * 表示按权限判断/仅限个人
  24. * 默认为禁用,若启用请务必保证表中存在admin_id字段
  25. */
  26. protected $dataLimit = 'personal';
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = model('GuideWx');
  31. $this->view->assign("stateList", $this->model->getStateList());
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. */
  41. public function index()
  42. {
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags']);
  45. if ($this->request->isAjax())
  46. {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('pkey_name'))
  49. {
  50. return $this->selectpage();
  51. }
  52. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53. $total = $this->model
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where($where)
  59. ->order($sort, $order)
  60. ->limit($offset, $limit)
  61. ->select();
  62. $result = array("total" => $total, "rows" => $list);
  63. return json($result);
  64. }
  65. $sub_url = getCurrentDomain($this->auth->id,'/index/book/sub');
  66. $this->assign('sub_url',$sub_url);
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 公众号关注回复设置
  71. * @return string
  72. * @throws \Exception
  73. */
  74. public function dfguide(){
  75. $sub_url = getCurrentDomain($this->auth->id,'/index/book/sub');
  76. $this->assign('sub_url',$sub_url);
  77. return $this->view->fetch();
  78. }
  79. /**
  80. * 功能说明
  81. */
  82. public function desc(){
  83. return $this->view->fetch();
  84. }
  85. /**
  86. * 添加
  87. */
  88. public function add()
  89. {
  90. if ($this->request->isPost())
  91. {
  92. $params = $this->request->post("row/a");
  93. if ($params)
  94. {
  95. if (isset($params['subscribe_url']) && $params['subscribe_url'] = trim($params['subscribe_url'])) {
  96. if (!preg_match('/(http:\/\/)|(https:\/\/)/i', $params['subscribe_url'])) {
  97. $this->error("引导关注链接格式错误");
  98. }
  99. }
  100. if ($this->dataLimit)
  101. {
  102. $params[$this->dataLimitField] = $this->auth->id;
  103. }
  104. try
  105. {
  106. //是否采用模型验证
  107. if ($this->modelValidate)
  108. {
  109. $name = basename(str_replace('\\', '/', get_class($this->model)));
  110. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  111. $this->model->validate($validate);
  112. }
  113. $result = $this->model->allowField(true)->save($params);
  114. if ($result !== false)
  115. {
  116. //删除redis缓存
  117. $admin_key = 'GW:'.$this->auth->id;
  118. $redis = Redis::instance();
  119. $redis->del($admin_key);
  120. $this->success();
  121. }
  122. else
  123. {
  124. $this->error($this->model->getError());
  125. }
  126. }
  127. catch (\think\exception\PDOException $e)
  128. {
  129. $this->error($e->getMessage());
  130. }
  131. }
  132. $this->error(__('Parameter %s can not be empty', ''));
  133. }
  134. return $this->view->fetch();
  135. }
  136. /**
  137. * 编辑
  138. * //redis GW:admin_id; set结构
  139. */
  140. public function edit($ids = NULL)
  141. {
  142. $row = $this->model->get($ids);
  143. if (!$row)
  144. $this->error(__('No Results were found'));
  145. $adminIds = $this->getDataLimitAdminIds();
  146. if (is_array($adminIds))
  147. {
  148. if (!in_array($row[$this->dataLimitField], $adminIds))
  149. {
  150. $this->error(__('You have no permission'));
  151. }
  152. }
  153. if ($this->request->isPost())
  154. {
  155. $params = $this->request->post("row/a");
  156. if ($params)
  157. {
  158. if (isset($params['subscribe_url']) && $params['subscribe_url'] = trim($params['subscribe_url'])) {
  159. if (!preg_match('/(http:\/\/)|(https:\/\/)/i', $params['subscribe_url'])) {
  160. $this->error("引导关注链接格式错误");
  161. }
  162. }
  163. try
  164. {
  165. //是否采用模型验证
  166. if ($this->modelValidate)
  167. {
  168. $name = basename(str_replace('\\', '/', get_class($this->model)));
  169. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  170. $row->validate($validate);
  171. }
  172. $result = $row->allowField(true)->save($params);
  173. if ($result !== false)
  174. {
  175. //删除redis缓存
  176. $admin_key = 'GW:'.$this->auth->id;
  177. $redis = Redis::instance();
  178. $redis->del($admin_key);
  179. $this->success();
  180. }
  181. else
  182. {
  183. $this->error($row->getError());
  184. }
  185. }
  186. catch (\think\exception\PDOException $e)
  187. {
  188. $this->error($e->getMessage());
  189. }
  190. }
  191. $this->error(__('Parameter %s can not be empty', ''));
  192. }
  193. $this->view->assign("row", $row);
  194. return $this->view->fetch();
  195. }
  196. }