Menus.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\admin\controller\weixin;
  3. use app\common\controller\Backend;
  4. use addons\weixin\library\WechatService;
  5. use Exception;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 微信菜单 控制器
  11. * Class Menus
  12. * @package app\admin\controller\wechat
  13. */
  14. class Menus extends Backend
  15. {
  16. protected $wechatcfg = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->wechatcfg = Db::name('weixin_cache')->where('key', 'wechat_menus')->value('result');
  21. if (empty($this->wechatcfg)) {
  22. Db::name('weixin_cache')->insert([
  23. 'result' => json_encode([], JSON_UNESCAPED_UNICODE), 'add_time' => time(), 'key' => 'wechat_menus'
  24. ]);
  25. }
  26. }
  27. public function index()
  28. {
  29. if (empty($this->wechatcfg)) {
  30. try {
  31. $ret = WechatService::menuService()->all();
  32. $ret = (array)json_decode($ret, true);
  33. $this->wechatcfg = json_encode($ret['menu']['button'], JSON_UNESCAPED_UNICODE);
  34. Db::name('weixin_cache')->where(['key' => 'wechat_menus'])->update([
  35. 'result' => $this->wechatcfg, 'add_time' => time()
  36. ]);
  37. } catch (\Exception $e) {
  38. //$this->error($e->getMessage());
  39. }
  40. }
  41. $this->assign('menu', (array)json_decode($this->wechatcfg, true));
  42. return $this->fetch();
  43. }
  44. /**
  45. * 修改
  46. */
  47. public function edit($ids = null)
  48. {
  49. $menu = $this->request->post("menu");
  50. $menu = (array)json_decode($menu, true);
  51. foreach ($menu as $index => &$item) {
  52. if (isset($item['sub_button'])) {
  53. foreach ($item['sub_button'] as &$subitem) {
  54. if ($subitem['type'] == 'view') {
  55. $allowFields = ['type', 'name', 'url'];
  56. $subitem = ['type' => $subitem['type'], 'name' => $subitem['name'], 'url' => $subitem['url']];
  57. } else {
  58. if ($subitem['type'] == 'miniprogram') {
  59. $allowFields = ['type', 'name', 'url', 'appid', 'pagepath'];
  60. $subitem = [
  61. 'type' => $subitem['type'], 'name' => $subitem['name'], 'url' => $subitem['url'],
  62. 'appid' => $subitem['appid'], 'pagepath' => $subitem['pagepath']
  63. ];
  64. } else {
  65. $allowFields = ['type', 'name', 'key'];
  66. $subitem = [
  67. 'type' => $subitem['type'], 'name' => $subitem['name'], 'key' => $subitem['key']
  68. ];
  69. }
  70. }
  71. $subitem = array_intersect_key($subitem, array_flip($allowFields));
  72. }
  73. } else {
  74. if ($item['type'] == 'view') {
  75. $allowFields = ['type', 'name', 'url'];
  76. } else {
  77. if ($item['type'] == 'miniprogram') {
  78. $allowFields = ['type', 'name', 'url', 'appid', 'pagepath'];
  79. } else {
  80. $allowFields = ['type', 'name', 'key'];
  81. }
  82. }
  83. $item = array_intersect_key($item, array_flip($allowFields));
  84. }
  85. }
  86. try {
  87. Db::name('weixin_cache')->where('key', 'wechat_menus')->update([
  88. 'result' => json_encode($menu, JSON_UNESCAPED_UNICODE), 'add_time' => time()
  89. ]);
  90. } catch (\Exception $e) {
  91. $this->error($e->getMessage());
  92. }
  93. $this->success('修改成功!');
  94. }
  95. /**
  96. * 同步
  97. */
  98. public function sync()
  99. {
  100. $hasError = false;
  101. $menu = (array)json_decode($this->wechatcfg, true);
  102. foreach ($menu as $k => $v) {
  103. if (isset($v['sub_button'])) {
  104. if (empty($v['sub_button'])) {
  105. unset($menu[$k]['sub_button']);
  106. continue;
  107. }
  108. foreach ($v['sub_button'] as $m => $n) {
  109. if ($n['type'] == 'click' && isset($n['key']) && !$n['key']) {
  110. $hasError = true;
  111. break 2;
  112. }
  113. }
  114. } else {
  115. if ($v['type'] == 'click' && isset($v['key']) && !$v['key']) {
  116. $hasError = true;
  117. break;
  118. }
  119. }
  120. }
  121. if (!$hasError) {
  122. try {
  123. WechatService::menuService()->destroy();
  124. WechatService::menuService()->add($menu);
  125. } catch (Exception $e) {
  126. $this->error($e->getMessage());
  127. }
  128. } else {
  129. $this->error(__('Invalid parameters'));
  130. }
  131. $this->success('同步成功!');
  132. }
  133. }