Material.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\admin\controller\shopro\wechat;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. /**
  9. * 微信管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Material extends Backend
  14. {
  15. /**
  16. * Wechat模型对象
  17. * @var \app\admin\model\shopro\Wechat
  18. */
  19. protected $model = null;
  20. protected $noNeedLogin = [];
  21. protected $noNeedRight = ['detail'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\shopro\wechat\Wechat;
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. $params = $this->request->param();
  41. extract($params);
  42. if ($type === 'text' || $type === 'link') {
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $total = $this->model
  45. ->where($where)
  46. ->where('type', $type)
  47. ->order($sort, $order)
  48. ->count();
  49. $list = $this->model
  50. ->where($where)
  51. ->where('type', $type)
  52. ->order($sort, $order)
  53. ->limit($offset, $limit)
  54. ->select();
  55. $list = collection($list)->toArray();
  56. $result = array("total" => $total, "rows" => $list);
  57. } else {
  58. $res = $this->getMaterialList($type, $offset = 0, $count = 20);
  59. $result = array("total" => $res['total_count'], "rows" => $res['item']);
  60. }
  61. $this->success('查看素材', null, $result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 添加
  67. */
  68. public function add()
  69. {
  70. if ($this->request->isPost()) {
  71. $params = $this->request->post();
  72. if ($params) {
  73. $params = json_decode($params['data'], true);
  74. $result = false;
  75. Db::startTrans();
  76. try {
  77. $params['content'] = json_encode($params['content'], JSON_UNESCAPED_UNICODE);
  78. $result = $this->model->allowField(true)->save($params);
  79. Db::commit();
  80. } catch (ValidateException $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. } catch (PDOException $e) {
  84. Db::rollback();
  85. $this->error($e->getMessage());
  86. } catch (Exception $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. }
  90. if ($result !== false) {
  91. $this->success();
  92. } else {
  93. $this->error(__('No rows were inserted'));
  94. }
  95. }
  96. $this->error(__('Parameter %s can not be empty', ''));
  97. }
  98. return $this->view->fetch();
  99. }
  100. /**
  101. * 编辑
  102. */
  103. public function edit($id = null)
  104. {
  105. $row = $this->model->get($id);
  106. if (!$row) {
  107. $this->error(__('No Results were found'));
  108. }
  109. if ($this->request->isPost()) {
  110. $params = $this->request->post();
  111. if ($params) {
  112. $params = json_decode($params['data'], true);
  113. $result = false;
  114. Db::startTrans();
  115. try {
  116. $params['content'] = json_encode($params['content'], JSON_UNESCAPED_UNICODE);
  117. $result = $row->allowField(true)->save($params);
  118. Db::commit();
  119. } catch (ValidateException $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. } catch (PDOException $e) {
  123. Db::rollback();
  124. $this->error($e->getMessage());
  125. } catch (Exception $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. }
  129. if ($result !== false) {
  130. $this->success();
  131. } else {
  132. $this->error(__('No rows were updated'));
  133. }
  134. }
  135. $this->error(__('Parameter %s can not be empty', ''));
  136. }
  137. $this->assignconfig("row", $row);
  138. return $this->view->fetch();
  139. }
  140. /**
  141. * 素材详情
  142. */
  143. public function detail($media_id)
  144. {
  145. $wechat = new \addons\shopro\library\Wechat('wxOfficialAccount');
  146. $app = $wechat->getApp();
  147. $resource = $app->material->get($media_id);
  148. if ($resource instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  149. return response($resource->getBody(), 200, ['Content-Length' => strlen($resource)])->contentType('audio/mp3');
  150. } else {
  151. // 小程序码获取失败
  152. $msg = isset($content['errcode']) ? $content['errcode'] : '-';
  153. $msg .= isset($content['errmsg']) ? $content['errmsg'] : '';
  154. \think\Log::write('wxacode-error' . $msg);
  155. $this->error('获取失败', $msg);
  156. }
  157. $this->success('素材详情', null, $resource);
  158. }
  159. /**
  160. * 选择素材
  161. */
  162. public function select()
  163. {
  164. if ($this->request->isAjax()) {
  165. $params = $this->request->param();
  166. extract($params);
  167. if ($type === 'text' || $type === 'link') {
  168. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  169. $total = $this->model
  170. ->where($where)
  171. ->where('type', $type)
  172. ->order($sort, $order)
  173. ->count();
  174. $list = $this->model
  175. ->where($where)
  176. ->where('type', $type)
  177. ->order($sort, $order)
  178. ->limit($offset, $limit)
  179. ->select();
  180. $list = collection($list)->toArray();
  181. $result = array("total" => $total, "rows" => $list);
  182. } else {
  183. $res = $this->getMaterialList($type, $offset = 0, $count = 20);
  184. $result = array("total" => $res['total_count'], "rows" => $res['item']);
  185. }
  186. $this->success('选择素材', null, $result);
  187. }
  188. }
  189. private function getMaterialList($type, $offset, $count)
  190. {
  191. $wechat = new \addons\shopro\library\Wechat('wxOfficialAccount');
  192. $app = $wechat->getApp();
  193. $res = $app->material->list($type, $offset, $count);
  194. return $res;
  195. }
  196. }