Menu.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 Menu extends Common
  8. {
  9. protected $wechat = null;
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->wechat = Wechat::officialAccountManage();
  15. $this->model = new \app\admin\model\shopro\wechat\Menu;
  16. }
  17. /**
  18. * 公众号配置
  19. */
  20. public function index()
  21. {
  22. if (!$this->request->isAjax()) {
  23. return $this->view->fetch();
  24. }
  25. $current = $this->getCurrentMenu();
  26. $data = $this->model->sheepFilter()->paginate(request()->param('list_rows', 10));
  27. $this->success('操作成功', null, ['current' => $current, 'list' => $data]);
  28. }
  29. /**
  30. * 详情
  31. *
  32. * @param $id
  33. * @return \think\Response
  34. */
  35. public function detail($id)
  36. {
  37. $detail = $this->model->get($id);
  38. if (!$detail) {
  39. $this->error(__('No Results were found'));
  40. }
  41. $this->success('获取成功', null, $detail);
  42. }
  43. /**
  44. * 添加菜单
  45. *
  46. * @param int $publish 发布状态:0=不发布,1=直接发布
  47. * @return \think\Response
  48. */
  49. public function add()
  50. {
  51. if (!$this->request->isAjax()) {
  52. return $this->view->fetch();
  53. }
  54. $publish = $this->request->param('publish', 0);
  55. $params = $this->request->only(['name', 'rules']);
  56. // 参数校验
  57. $this->svalidate($params, '.add');
  58. Db::transaction(function () use ($params, $publish) {
  59. $menu = $this->model->save($params);
  60. if ($menu && $publish) {
  61. $this->publishMenu($this->model->id);
  62. }
  63. return $menu;
  64. });
  65. $this->success('保存成功');
  66. }
  67. /**
  68. * 编辑
  69. *
  70. * @param $id
  71. * @return \think\Response
  72. */
  73. public function edit($id = null)
  74. {
  75. if (!$this->request->isAjax()) {
  76. return $this->view->fetch('add');
  77. }
  78. $menu = $this->model->get($id);
  79. $publish = $this->request->param('publish', 0);
  80. $params = $this->request->only(['name', 'rules']);
  81. // 参数校验
  82. $this->svalidate($params);
  83. $menu = Db::transaction(function () use ($params, $menu, $publish) {
  84. $menu->save($params);
  85. if ($publish) {
  86. $this->publishMenu($menu->id);
  87. }
  88. return $menu;
  89. });
  90. $this->success('更新成功');
  91. }
  92. /**
  93. * 删除
  94. *
  95. * @param $id
  96. * @return \think\Response
  97. */
  98. public function delete($id)
  99. {
  100. Db::transaction(function () use ($id) {
  101. $menu = $this->model->get($id);
  102. $menu->delete();
  103. });
  104. $this->success('删除成功');
  105. }
  106. /**
  107. * 发布菜单
  108. *
  109. * @param $id
  110. * @return \think\Response
  111. */
  112. public function publish($id)
  113. {
  114. Db::transaction(function () use ($id) {
  115. return $this->publishMenu($id);
  116. });
  117. $this->success('发布成功');
  118. }
  119. /**
  120. * 复制菜单
  121. *
  122. * @return Response
  123. */
  124. public function copy($id = 0)
  125. {
  126. if ($id == 0) {
  127. $data = [
  128. 'name' => '复制 当前菜单',
  129. 'rules' => $this->getCurrentMenu(),
  130. ];
  131. } else {
  132. $menu = $this->model->get($id);
  133. $data = [
  134. 'name' => '复制 ' . $menu->name,
  135. 'rules' => $menu->rules
  136. ];
  137. }
  138. $menu = $this->model->save($data);
  139. $this->success('复制成功');
  140. }
  141. // 发布菜单
  142. private function publishMenu($id)
  143. {
  144. $menu = $this->model->get($id);
  145. if ($this->setCurrentMenu($menu->rules)) {
  146. $this->model->where('id', '<>', $menu->id)->update(['status' => 0]);
  147. return $menu->save([
  148. 'status' => 1,
  149. 'publishtime' => time()
  150. ]);
  151. }
  152. return false;
  153. }
  154. // 获取当前菜单
  155. private function getCurrentMenu()
  156. {
  157. try {
  158. $currentMenu = $this->wechat->menu->current();
  159. } catch (\Exception $e) {
  160. $this->error($e->getMessage());
  161. }
  162. if (isset($currentMenu['selfmenu_info']['button'])) {
  163. $buttons = $currentMenu['selfmenu_info']['button'];
  164. foreach ($buttons as &$button) {
  165. if (isset($button['sub_button'])) {
  166. $button['sub_button'] = $button['sub_button']['list'];
  167. }
  168. }
  169. return $buttons;
  170. } else {
  171. return [];
  172. }
  173. }
  174. // 设置菜单
  175. private function setCurrentMenu($rules)
  176. {
  177. try {
  178. $result = $this->wechat->menu->create($rules);
  179. } catch (\Exception $e) {
  180. $this->error($e->getMessage());
  181. }
  182. if (isset($result['errcode']) && $result['errcode'] === 0) {
  183. return true;
  184. } else {
  185. $this->error($result['errmsg'] ?? '发布失败');
  186. }
  187. }
  188. }