Menu.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\controller\Backend;
  4. use app\common\model\WechatResponse;
  5. use EasyWeChat\Foundation\Application;
  6. use think\Config;
  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 = \app\common\model\WechatConfig::get(['name' => 'menu']);
  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', (array) json_decode($this->wechatcfg->value, TRUE));
  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. $this->wechatcfg->value = json_encode($menu, JSON_UNESCAPED_UNICODE);
  44. $this->wechatcfg->save();
  45. $this->code = 1;
  46. return;
  47. }
  48. /**
  49. * 同步
  50. */
  51. public function sync($ids = NULL)
  52. {
  53. $this->code = -1;
  54. $app = new Application(Config::get('wechat'));
  55. try
  56. {
  57. $hasError = false;
  58. $menu = json_decode($this->wechatcfg->value, TRUE);
  59. foreach ($menu as $k => $v)
  60. {
  61. if (isset($v['sub_button']))
  62. {
  63. foreach ($v['sub_button'] as $m => $n)
  64. {
  65. if (isset($n['key']) && !$n['key'])
  66. {
  67. $hasError = true;
  68. break 2;
  69. }
  70. }
  71. }
  72. else if (isset($v['key']) && !$v['key'])
  73. {
  74. $hasError = true;
  75. break;
  76. }
  77. }
  78. if (!$hasError)
  79. {
  80. $ret = $app->menu->add($menu);
  81. if ($ret->errcode == 0)
  82. {
  83. $this->code = 1;
  84. }
  85. else
  86. {
  87. $this->msg = $ret->errmsg;
  88. }
  89. }
  90. else
  91. {
  92. $this->msg = __('Invalid parameters');
  93. }
  94. }
  95. catch (Exception $e)
  96. {
  97. $this->msg = $e->getMessage();
  98. }
  99. return;
  100. }
  101. }