Wechat.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\admin\controller\weixin\template;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. use think\Cache;
  9. use addons\weixin\library\WechatTemplateService;
  10. /**
  11. * 微信模板
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Wechat extends Backend
  16. {
  17. /**
  18. * Wechat模型对象
  19. * @var \app\admin\model\weixin\template\Wechat
  20. */
  21. protected $model = null;
  22. protected $cacheTag = '_system_wechat';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\weixin\template\Wechat;
  27. //得到所属行业
  28. $industry = Cache::tag($this->cacheTag)->remember('_wechat_industry', function () {
  29. try {
  30. $cache = WechatTemplateService::getIndustry();
  31. if (!$cache) {
  32. return [];
  33. }
  34. Cache::tag($this->cacheTag, ['_wechat_industry']);
  35. return $cache;
  36. } catch (\Exception $e) {
  37. return $e->getMessage();
  38. }
  39. }, 0) ?: [];
  40. !is_array($industry) && $industry = [];
  41. $this->assign('industry', $industry);
  42. }
  43. /**
  44. * 查看
  45. */
  46. public function index()
  47. {
  48. //设置过滤方法
  49. $this->request->filter(['strip_tags']);
  50. if ($this->request->isAjax()) {
  51. //如果发送的来源是Selectpage,则转发到Selectpage
  52. if ($this->request->request('keyField')) {
  53. return $this->selectpage();
  54. }
  55. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  56. $total = $this->model
  57. ->where($where)
  58. ->order($sort, $order)
  59. ->count();
  60. $list = $this->model
  61. ->where($where)
  62. ->order($sort, $order)
  63. ->limit($offset, $limit)
  64. ->select();
  65. $list = collection($list)->toArray();
  66. $result = array("total" => $total, "rows" => $list);
  67. return json($result);
  68. }
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 添加
  73. */
  74. public function add()
  75. {
  76. if ($this->request->isPost()) {
  77. $params = $this->request->post("row/a");
  78. if ($params) {
  79. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  80. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  81. $params[$this->dataLimitField] = $this->auth->id;
  82. }
  83. $result = false;
  84. Db::startTrans();
  85. try {
  86. //是否采用模型验证
  87. if ($this->modelValidate) {
  88. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  89. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  90. $this->model->validateFailException(true)->validate($validate);
  91. }
  92. $result = $this->model->allowField(true)->save($params);
  93. Db::commit();
  94. } catch (ValidateException $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. } catch (PDOException $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. } catch (Exception $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. if ($result !== false) {
  105. $this->success();
  106. } else {
  107. $this->error(__('No rows were inserted'));
  108. }
  109. }
  110. $this->error(__('Parameter %s can not be empty', ''));
  111. }
  112. return $this->view->fetch();
  113. }
  114. /**
  115. * 编辑
  116. */
  117. public function edit($ids = null)
  118. {
  119. $row = $this->model->get($ids);
  120. if (!$row) {
  121. $this->error(__('No Results were found'));
  122. }
  123. $adminIds = $this->getDataLimitAdminIds();
  124. if (is_array($adminIds)) {
  125. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  126. $this->error(__('You have no permission'));
  127. }
  128. }
  129. if ($this->request->isPost()) {
  130. $params = $this->request->post("row/a");
  131. if ($params) {
  132. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  133. $result = false;
  134. Db::startTrans();
  135. try {
  136. //是否采用模型验证
  137. if ($this->modelValidate) {
  138. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  139. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  140. $row->validateFailException(true)->validate($validate);
  141. }
  142. $result = $row->allowField(true)->save($params);
  143. Db::commit();
  144. } catch (ValidateException $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. } catch (PDOException $e) {
  148. Db::rollback();
  149. $this->error($e->getMessage());
  150. } catch (Exception $e) {
  151. Db::rollback();
  152. $this->error($e->getMessage());
  153. }
  154. if ($result !== false) {
  155. $this->success();
  156. } else {
  157. $this->error(__('No rows were updated'));
  158. }
  159. }
  160. $this->error(__('Parameter %s can not be empty', ''));
  161. }
  162. $this->view->assign("row", $row);
  163. return $this->view->fetch();
  164. }
  165. /**
  166. * 删除
  167. */
  168. public function del($ids = "")
  169. {
  170. if ($ids) {
  171. $pk = $this->model->getPk();
  172. $adminIds = $this->getDataLimitAdminIds();
  173. if (is_array($adminIds)) {
  174. $this->model->where($this->dataLimitField, 'in', $adminIds);
  175. }
  176. $list = $this->model->where($pk, 'in', $ids)->select();
  177. $count = 0;
  178. Db::startTrans();
  179. try {
  180. foreach ($list as $k => $v) {
  181. $count += $v->delete();
  182. }
  183. Db::commit();
  184. } catch (PDOException $e) {
  185. Db::rollback();
  186. $this->error($e->getMessage());
  187. } catch (Exception $e) {
  188. Db::rollback();
  189. $this->error($e->getMessage());
  190. }
  191. if ($count) {
  192. $this->success();
  193. } else {
  194. $this->error(__('No rows were deleted'));
  195. }
  196. }
  197. $this->error(__('Parameter %s can not be empty', 'ids'));
  198. }
  199. }