Material.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\admin\controller\shopro\wechat;
  3. use app\admin\controller\shopro\Common;
  4. use addons\shopro\facade\Wechat;
  5. use think\Db;
  6. use addons\shopro\exception\ShoproException;
  7. class Material extends Common
  8. {
  9. protected $wechat = null;
  10. protected $model = null;
  11. protected $noNeedRight = ['select'];
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->wechat = Wechat::officialAccountManage();
  16. $this->model = new \app\admin\model\shopro\wechat\Material;
  17. }
  18. /**
  19. * 素材列表
  20. */
  21. public function index()
  22. {
  23. if (!$this->request->isAjax()) {
  24. return $this->view->fetch();
  25. }
  26. $list_rows = intval($this->request->param('list_rows', 10));
  27. $page = intval($this->request->param('page', 1));
  28. $type = $this->request->param('type', 'news');
  29. $offset = intval(($page - 1) * $list_rows);
  30. if (in_array($type, ['text', 'link'])) {
  31. $data = $this->model->sheepFilter()->where('type', $type)->paginate(request()->param('list_rows', 10));
  32. } else {
  33. // 使用微信远程素材列表
  34. try {
  35. $res = $this->wechat->material->list($type, $offset, $list_rows);
  36. } catch (\Exception $e) {
  37. $this->error($e->getMessage());
  38. }
  39. $data = [
  40. 'current_page' => $page,
  41. 'data' => $res['item'],
  42. 'last_page' => intval(ceil($res['total_count'] / $list_rows)),
  43. 'per_page' => $list_rows,
  44. 'total' => intval($res['total_count']),
  45. ];
  46. }
  47. $this->success('', null, $data);
  48. }
  49. /**
  50. * 详情
  51. *
  52. * @param $id
  53. * @return \think\Response
  54. */
  55. public function detail($id)
  56. {
  57. $detail = $this->model->get($id);
  58. if (!$detail) {
  59. $this->error(__('No Results were found'));
  60. }
  61. $this->success('获取成功', null, $detail);
  62. }
  63. /**
  64. * 添加
  65. *
  66. * @return \think\Response
  67. */
  68. public function add()
  69. {
  70. if (!$this->request->isAjax()) {
  71. return $this->view->fetch();
  72. }
  73. $params = $this->request->only(['type', 'content']);
  74. if ($params['type'] === 'text') {
  75. $params['content'] = urldecode($params['content']);
  76. }
  77. Db::transaction(function () use ($params) {
  78. $this->model->save($params);
  79. });
  80. $this->success('保存成功');
  81. }
  82. /**
  83. * 编辑
  84. *
  85. * @param $id
  86. * @return \think\Response
  87. */
  88. public function edit($id = null)
  89. {
  90. if (!$this->request->isAjax()) {
  91. return $this->view->fetch('add');
  92. }
  93. $material = $this->model->get($id);
  94. $params = $this->request->only(['type', 'content']);
  95. if ($params['type'] === 'text') {
  96. $params['content'] = urldecode($params['content']);
  97. }
  98. Db::transaction(function () use ($params, $material) {
  99. $material->save($params);
  100. });
  101. $this->success('更新成功');
  102. }
  103. /**
  104. * 删除
  105. *
  106. * @param $id
  107. * @return \think\Response
  108. */
  109. public function delete($id)
  110. {
  111. $is_real = $this->request->param('is_real', 0);
  112. Db::transaction(function () use ($id, $is_real) {
  113. $menu = $this->model->get($id);
  114. if ($is_real) {
  115. $menu->force()->delete();
  116. } else {
  117. $menu->delete();
  118. }
  119. });
  120. $this->success('删除成功');
  121. }
  122. /**
  123. * 菜单列表
  124. *
  125. * @return Response
  126. */
  127. public function select()
  128. {
  129. $list_rows = intval($this->request->param('list_rows', 10));
  130. $page = intval($this->request->param('page', 1));
  131. $type = $this->request->param('type', 'news');
  132. $offset = intval(($page - 1) * $list_rows);
  133. if (in_array($type, ['text', 'link'])) {
  134. $data = $this->model->where('type', $type)->order('id desc')->paginate(request()->param('list_rows', 10));
  135. } else {
  136. // 使用微信远程素材列表
  137. try {
  138. $res = $this->wechat->material->list($type, $offset, $list_rows);
  139. } catch (\Exception $e) {
  140. $this->error($e->getMessage());
  141. }
  142. $data = [
  143. 'current_page' => $page,
  144. 'data' => $res['item'],
  145. 'last_page' => intval(ceil($res['total_count'] / $list_rows)),
  146. 'per_page' => $list_rows,
  147. 'total' => intval($res['total_count']),
  148. ];
  149. }
  150. $this->success('获取成功', null, $data);
  151. }
  152. }