Material.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace app\admin\controller\xmwechat\offiaccount;
  3. use app\common\controller\Backend;
  4. use app\common\library\xmwechat\offiaccount\MaterialService;
  5. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  6. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  7. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  8. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  9. use Throwable;
  10. /**
  11. * 微信公众号素材
  12. */
  13. class Material extends Backend
  14. {
  15. /**
  16. * Material模型对象
  17. * @var object
  18. * @phpstan-var \app\admin\model\xmwechat\offiaccount\Material
  19. */
  20. protected object $model;
  21. protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];
  22. protected string|array $quickSearchField = ['id', 'type'];
  23. public function initialize(): void
  24. {
  25. parent::initialize();
  26. $this->model = new \app\admin\model\xmwechat\offiaccount\Material;
  27. $this->request->filter('trim,htmlspecialchars');
  28. }
  29. public function index(): void
  30. {
  31. $this->request->filter(['strip_tags', 'trim']);
  32. $params = $this->request->param();
  33. if (!isset($params['type'])) {
  34. $search = $this->request->get("search/a", []);
  35. $params['type'] = 'text';
  36. foreach ($search as $field) {
  37. if (!is_array($field) || !isset($field['operator']) || !isset($field['field']) || !isset($field['val'])) {
  38. continue;
  39. }
  40. $val = $field['val'];
  41. $field = $field['field'];
  42. $params[$field] = $val;
  43. }
  44. }
  45. if ($this->request->param('select')) {
  46. $this->select();
  47. }
  48. list($where, $alias, $limit, $order) = $this->queryBuilder();
  49. if ($params['type'] == 'text') {
  50. $res = $this->model
  51. ->field($this->indexField)
  52. ->alias($alias)
  53. ->where($where)
  54. ->order($order)
  55. ->paginate($limit);
  56. $list = $res->items();
  57. $total = $res->total();
  58. } else if ($params['type'] == 'news') {
  59. $page = $this->request->get('page');
  60. if (!isset($page)) $page = 1;
  61. $data = MaterialService::getInstance()->getMaterialFreePublish($page, $limit);
  62. $list = $data['list'];
  63. $total = $data['total'];
  64. } else {
  65. $page = $this->request->get('page');
  66. if (!isset($page)) $page = 1;
  67. $data = MaterialService::getInstance()->getMaterialList($params['type'], $page, $limit);
  68. $list = $data['list'];
  69. $total = $data['total'];
  70. }
  71. $this->success('', [
  72. 'list' => $list,
  73. 'total' => $total,
  74. ]);
  75. }
  76. public function add(): void
  77. {
  78. if ($this->request->isPost()) {
  79. $data = $this->request->post();
  80. if (!$data) {
  81. $this->error(__('Parameter %s can not be empty', ['']));
  82. }
  83. $data = $this->excludeFields($data);
  84. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  85. $data[$this->dataLimitField] = $this->auth->id;
  86. }
  87. $this->checkMaterial($data);
  88. $result = false;
  89. $this->model->startTrans();
  90. try {
  91. // 模型验证
  92. if ($this->modelValidate) {
  93. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  94. if (class_exists($validate)) {
  95. $validate = new $validate;
  96. if ($this->modelSceneValidate) $validate->scene('add');
  97. $validate->check($data);
  98. }
  99. }
  100. $materialData['content'] = $data['content'];
  101. $materialData['event_key'] = $this->generateRandomString(30);
  102. $materialData['type'] = $data['type'];
  103. $result = $this->model->save($materialData);
  104. $this->model->commit();
  105. } catch (Throwable $e) {
  106. $this->model->rollback();
  107. $this->error($e->getMessage());
  108. }
  109. if ($result !== false) {
  110. $this->success(__('Added successfully'));
  111. } else {
  112. $this->error(__('No rows were added'));
  113. }
  114. }
  115. $this->error(__('Parameter error'));
  116. }
  117. public function edit(): void
  118. {
  119. $id = $this->request->param($this->model->getPk());
  120. $row = $this->model->find($id);
  121. if (!$row) {
  122. $this->error(__('Record not found'));
  123. }
  124. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  125. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  126. $this->error(__('You have no permission'));
  127. }
  128. if ($this->request->isPost()) {
  129. $data = $this->request->post();
  130. if (!$data) {
  131. $this->error(__('Parameter %s can not be empty', ['']));
  132. }
  133. $data = $this->excludeFields($data);
  134. $this->checkMaterial($data);
  135. $result = false;
  136. $this->model->startTrans();
  137. try {
  138. // 模型验证
  139. if ($this->modelValidate) {
  140. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  141. if (class_exists($validate)) {
  142. $validate = new $validate;
  143. if ($this->modelSceneValidate) $validate->scene('edit');
  144. $validate->check($data);
  145. }
  146. }
  147. $materialData['content'] = $data['content'];
  148. $materialData['type'] = $data['type'];
  149. $result = $row->save($materialData);
  150. $this->model->commit();
  151. } catch (Throwable $e) {
  152. $this->model->rollback();
  153. $this->error($e->getMessage());
  154. }
  155. if ($result !== false) {
  156. $this->success(__('Update successful'));
  157. } else {
  158. $this->error(__('No rows updated'));
  159. }
  160. }
  161. $this->success('', [
  162. 'row' => $row
  163. ]);
  164. }
  165. public function checkMaterial(array $data)
  166. {
  167. $type = $data['type'];
  168. if ($type == 'text') {
  169. if (empty($data['content'])) {
  170. $this->error('请填写内容');
  171. }
  172. }
  173. }
  174. public function select(): void
  175. {
  176. $this->request->filter(['strip_tags', 'trim']);
  177. $params = $this->request->param();
  178. list($where, $alias, $limit, $order) = $this->queryBuilder();
  179. $where['type'] = $params['type'];
  180. if ($params['type'] == 'text') {
  181. $res = $this->model
  182. ->field($this->indexField)
  183. ->alias($alias)
  184. ->where($where)
  185. ->order($order)
  186. ->paginate($limit);
  187. $list = $res->items();
  188. $total = $res->total();
  189. } else if ($params['type'] == 'news') {
  190. $page = $this->request->get('page');
  191. if (!isset($page)) $page = 1;
  192. $data = MaterialService::getInstance()->getMaterialFreePublish($page, $limit);
  193. $list = $data['list'];
  194. $total = $data['total'];
  195. } else {
  196. $page = $this->request->get('page');
  197. if (!isset($page)) $page = 1;
  198. $data = MaterialService::getInstance()->getMaterialList($params['type'], $page, $limit);
  199. $list = $data['list'];
  200. $total = $data['total'];
  201. }
  202. $this->success('', [
  203. 'list' => $list,
  204. 'total' => $total,
  205. ]);
  206. }
  207. public function generateRandomString($length = 35): string
  208. {
  209. $characters = '12356789abcdefghijklmnopqrstuvwxyz';
  210. $characterLength = strlen($characters);
  211. $randomString = '';
  212. for ($i = 0; $i < $length; $i++) {
  213. $randomString .= $characters[rand(0, $characterLength - 1)];
  214. }
  215. return $randomString;
  216. }
  217. /**
  218. * 获取已选中/添加的菜单内容
  219. * @return void
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\DbException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. * @author jsjxsz
  224. */
  225. public function getMenuContent(): void
  226. {
  227. $params = $this->request->param();
  228. if (isset($params['key']) && !empty($params['key'])) {
  229. $content = $this->model->where('event_key', $params['key'])->where('type', 'text')->value('content');
  230. $data = [
  231. 'content' => $content
  232. ];
  233. $this->success('success', $data);
  234. }
  235. if (isset($params['media_id']) && !empty($params['media_id'])) {
  236. try {
  237. $data = MaterialService::getInstance()->getMaterialByMediaId($params['media_id']);
  238. $this->success('success', $data);
  239. } catch (ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $e) {
  240. $this->error($e->getMessage());
  241. }
  242. }
  243. if (isset($params['article_id']) && !empty($params['article_id'])) {
  244. try {
  245. $data = MaterialService::getInstance()->getMaterialByArticleId($params['article_id']);
  246. $this->success('success', $data);
  247. } catch (ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $e) {
  248. $this->error($e->getMessage());
  249. }
  250. }
  251. $this->success('success');
  252. }
  253. }