Menu.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 = \app\common\model\WechatConfig::get(['name' => 'menu']);
  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', (array) json_decode($this->wechatcfg->value, TRUE));
  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. $this->wechatcfg->value = json_encode($menu, JSON_UNESCAPED_UNICODE);
  45. $this->wechatcfg->save();
  46. $this->code = 1;
  47. return;
  48. }
  49. /**
  50. * 同步
  51. */
  52. public function sync($ids = NULL)
  53. {
  54. $this->code = -1;
  55. $app = new Application(Config::get('wechat'));
  56. try
  57. {
  58. $ret = $app->menu->add(json_decode($this->wechatcfg->value, TRUE));
  59. if ($ret->errcode == 0)
  60. {
  61. $this->code = 1;
  62. }
  63. else
  64. {
  65. $this->content = $ret->errmsg;
  66. }
  67. }
  68. catch (Exception $e)
  69. {
  70. $this->content = $e->getMessage();
  71. }
  72. return;
  73. }
  74. }