Menu.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 Menu extends Backend
  14. {
  15. /**
  16. * Wechat模型对象
  17. * @var \app\admin\model\shopro\wechat\Wechat
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\shopro\wechat\Wechat;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = false;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $total = $this->model
  46. ->where($where)
  47. ->where('type', 'menu')
  48. ->order($sort, $order)
  49. ->count();
  50. $list = $this->model
  51. ->where($where)
  52. ->where('type', 'menu')
  53. ->order($sort, $order)
  54. ->limit($offset, $limit)
  55. ->select();
  56. $list = collection($list)->toArray();
  57. $currentMenu = $this->getCurrentMenu();
  58. $result = array("total" => $total, "rows" => $list, 'currentMenu' => $currentMenu);
  59. $this->success('查看菜单', null, $result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add()
  67. {
  68. if ($this->request->isPost()) {
  69. $params = $this->request->post();
  70. if ($params) {
  71. $params = json_decode($params['data'], true);
  72. $result = false;
  73. Db::startTrans();
  74. if ($this->request->get('act') === 'publish') {
  75. $publish = $this->publishMenu($params['content']);
  76. if (false == $publish['result']) {
  77. return $this->error($publish['msg']);
  78. }
  79. }
  80. try {
  81. $params['content'] = json_encode($params['content'], JSON_UNESCAPED_UNICODE);
  82. $result = $this->model->allowField(true)->save($params);
  83. Db::commit();
  84. } catch (ValidateException $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. } catch (PDOException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (Exception $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. }
  94. if ($result !== false) {
  95. $this->success();
  96. } else {
  97. $this->error(__('No rows were inserted'));
  98. }
  99. }
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. $this->setWechatLinkEnv();
  103. return $this->view->fetch();
  104. }
  105. /**
  106. * 编辑
  107. */
  108. public function edit($id = null)
  109. {
  110. $row = $this->model->get($id);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. if ($this->request->isPost()) {
  115. $params = $this->request->post();
  116. if ($params) {
  117. $params = json_decode($params['data'], true);
  118. $result = false;
  119. Db::startTrans();
  120. if ($this->request->get('act') === 'publish') {
  121. $publish = $this->publishMenu($params['content']);
  122. if (!$publish['result']) {
  123. $this->error($publish['msg']);
  124. }
  125. }
  126. try {
  127. $params['content'] = json_encode($params['content']);
  128. $result = $row->allowField(true)->save($params);
  129. Db::commit();
  130. } catch (ValidateException $e) {
  131. Db::rollback();
  132. $this->error($e->getMessage());
  133. } catch (PDOException $e) {
  134. Db::rollback();
  135. $this->error($e->getMessage());
  136. } catch (Exception $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage());
  139. }
  140. if ($result !== false) {
  141. $this->success();
  142. } else {
  143. $this->error(__('No rows were updated'));
  144. }
  145. }
  146. $this->error(__('Parameter %s can not be empty', ''));
  147. }
  148. $this->setWechatLinkEnv();
  149. $this->assignconfig("row", $row);
  150. return $this->view->fetch();
  151. }
  152. /**
  153. * 复制
  154. */
  155. public function copy($id)
  156. {
  157. if ($id == 0) {
  158. $data = [
  159. 'name' => '复制 当前菜单',
  160. 'type' => 'menu',
  161. 'content' => json_encode($this->getCurrentMenu())
  162. ];
  163. } else {
  164. $copyMenu = $this->model->get($id);
  165. $data = [
  166. 'name' => '复制 ' . $copyMenu['name'],
  167. 'type' => 'menu',
  168. 'content' => $copyMenu['content']
  169. ];
  170. }
  171. $menu = new \app\admin\model\shopro\wechat\Wechat;
  172. $menu->allowField(true)->save($data);
  173. $this->success('复制成功');
  174. }
  175. /**
  176. * 发布菜单
  177. */
  178. public function publish($id)
  179. {
  180. $row = $this->model->get($id);
  181. if (!$row) {
  182. $this->error(__('No Results were found'));
  183. }
  184. $publish = $this->publishMenu(json_decode($row->content, true));
  185. if (!$publish['result']) {
  186. return $this->error($publish['msg']);
  187. }
  188. return $this->success('发布成功');
  189. }
  190. //发布指定菜单
  191. private function publishMenu($buttons)
  192. {
  193. $wechat = new \addons\shopro\library\Wechat('wxOfficialAccount');
  194. try {
  195. $res = $wechat->menu('create', $buttons);
  196. } catch (Exception $e) {
  197. if (strpos($e->getMessage(), 'ip')) {
  198. return [
  199. 'result' => false,
  200. 'msg' => '请将当前IP地址加入公众号后台IP白名单'
  201. ];
  202. }
  203. }
  204. if ($res['errcode'] !== 0) {
  205. return [
  206. 'result' => false,
  207. 'msg' => '请检查您的菜单格式'
  208. ];
  209. }
  210. return ['result' => true, 'msg' => '发布成功'];
  211. }
  212. //获取当前菜单
  213. private function getCurrentMenu()
  214. {
  215. $wechat = new \addons\shopro\library\Wechat('wxOfficialAccount');
  216. $currentMenuInfo = $wechat->menu('current');
  217. if (isset($currentMenuInfo['selfmenu_info']['button'])) {
  218. $buttons = $currentMenuInfo['selfmenu_info']['button'];
  219. foreach($buttons as &$b) {
  220. if(isset($b['sub_button'])) {
  221. $b['sub_button'] = $b['sub_button']['list'];
  222. }
  223. }
  224. return $buttons;
  225. } else {
  226. return [];
  227. }
  228. }
  229. //设置微信环境域名和appid
  230. private function setWechatLinkEnv()
  231. {
  232. $shopro = json_decode(\app\admin\model\shopro\Config::get(['name' => 'shopro'])->value, true);
  233. $wxMiniProgram = json_decode(\app\admin\model\shopro\Config::get(['name' => 'wxMiniProgram'])->value, true);
  234. $this->assignconfig('shopro', $shopro);
  235. $this->assignconfig('wxMiniProgram', $wxMiniProgram);
  236. }
  237. }