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