Menu.php 2.0 KB

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