Menu.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\controller\Backend;
  4. use app\common\model\Configvalue;
  5. use app\common\model\WechatResponse;
  6. use EasyWeChat\Foundation\Application;
  7. use think\Exception;
  8. /**
  9. * 菜单管理
  10. *
  11. * @icon fa fa-list-alt
  12. */
  13. class Menu extends Backend
  14. {
  15. protected $wechatcfg = NULL;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->wechatcfg = Configvalue::get('wechat');
  20. }
  21. /**
  22. * 查看
  23. */
  24. public function index()
  25. {
  26. $responselist = array();
  27. $all = WechatResponse::all();
  28. foreach ($all as $k => $v)
  29. {
  30. $responselist[$v['eventkey']] = $v['title'];
  31. }
  32. $this->view->assign('responselist', $responselist);
  33. $this->view->assign('menu', $this->wechatcfg->content['menu']);
  34. return $this->view->fetch();
  35. }
  36. /**
  37. * 修改
  38. */
  39. public function edit($ids = NULL)
  40. {
  41. $menu = $this->request->post("menu");
  42. $menu = (array) json_decode($menu, TRUE);
  43. $content = $this->wechatcfg->content;
  44. $content['menu'] = $menu;
  45. $this->wechatcfg->content = $content;
  46. $this->wechatcfg->save();
  47. $this->code = 1;
  48. return;
  49. }
  50. /**
  51. * 同步
  52. */
  53. public function sync($ids = NULL)
  54. {
  55. $this->code = -1;
  56. $app = new Application(Config::get('wechat')->toArray());
  57. try
  58. {
  59. $ret = $app->menu->add($this->wechatcfg->content['menu']);
  60. if ($ret->errcode == 0)
  61. {
  62. $this->code = 1;
  63. }
  64. else
  65. {
  66. $this->content = $ret->errmsg;
  67. }
  68. }
  69. catch (Exception $e)
  70. {
  71. $this->content = $e->getMessage();
  72. }
  73. return;
  74. }
  75. }