Menus.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) || $this->wechatcfg == '[]') {
  30. try {
  31. $ret = WechatService::menuService()->list();
  32. $this->wechatcfg = json_encode($ret['menu']['button'], JSON_UNESCAPED_UNICODE);
  33. Db::name('weixin_cache')->where(['key' => 'wechat_menus'])->update([
  34. 'result' => $this->wechatcfg, 'add_time' => time()
  35. ]);
  36. } catch (\Exception $e) {
  37. //$this->error($e->getMessage());
  38. }
  39. }
  40. $this->assign('menu', (array)json_decode($this->wechatcfg, true));
  41. return $this->fetch();
  42. }
  43. /**
  44. * 修改
  45. */
  46. public function edit($ids = null)
  47. {
  48. $menu = $this->request->post("menu");
  49. $menu = (array)json_decode($menu, true);
  50. foreach ($menu as $index => &$item) {
  51. if (isset($item['sub_button'])) {
  52. foreach ($item['sub_button'] as &$subitem) {
  53. if ($subitem['type'] == 'view') {
  54. $allowFields = ['type', 'name', 'url'];
  55. $subitem = ['type' => $subitem['type'], 'name' => $subitem['name'], 'url' => $subitem['url']];
  56. } else {
  57. if ($subitem['type'] == 'miniprogram') {
  58. $allowFields = ['type', 'name', 'url', 'appid', 'pagepath'];
  59. $subitem = [
  60. 'type' => $subitem['type'], 'name' => $subitem['name'], 'url' => $subitem['url'],
  61. 'appid' => $subitem['appid'], 'pagepath' => $subitem['pagepath']
  62. ];
  63. } else {
  64. $allowFields = ['type', 'name', 'key'];
  65. $subitem = [
  66. 'type' => $subitem['type'], 'name' => $subitem['name'], 'key' => $subitem['key']
  67. ];
  68. }
  69. }
  70. $subitem = array_intersect_key($subitem, array_flip($allowFields));
  71. }
  72. } else {
  73. if ($item['type'] == 'view') {
  74. $allowFields = ['type', 'name', 'url'];
  75. } else {
  76. if ($item['type'] == 'miniprogram') {
  77. $allowFields = ['type', 'name', 'url', 'appid', 'pagepath'];
  78. } else {
  79. $allowFields = ['type', 'name', 'key'];
  80. }
  81. }
  82. $item = array_intersect_key($item, array_flip($allowFields));
  83. }
  84. }
  85. try {
  86. Db::name('weixin_cache')->where('key', 'wechat_menus')->update([
  87. 'result' => json_encode($menu, JSON_UNESCAPED_UNICODE), 'add_time' => time()
  88. ]);
  89. } catch (\Exception $e) {
  90. $this->error($e->getMessage());
  91. }
  92. $this->success('修改成功!');
  93. }
  94. /**
  95. * 同步
  96. */
  97. public function sync()
  98. {
  99. $hasError = false;
  100. $menu = (array)json_decode($this->wechatcfg, true);
  101. foreach ($menu as $k => $v) {
  102. if (isset($v['sub_button'])) {
  103. if (empty($v['sub_button'])) {
  104. unset($menu[$k]['sub_button']);
  105. continue;
  106. }
  107. foreach ($v['sub_button'] as $m => $n) {
  108. if ($n['type'] == 'click' && isset($n['key']) && !$n['key']) {
  109. $hasError = true;
  110. break 2;
  111. }
  112. }
  113. } else {
  114. if ($v['type'] == 'click' && isset($v['key']) && !$v['key']) {
  115. $hasError = true;
  116. break;
  117. }
  118. }
  119. }
  120. if (!$hasError) {
  121. try {
  122. WechatService::menuService()->delete();
  123. WechatService::menuService()->create($menu);
  124. } catch (Exception $e) {
  125. $this->error($e->getMessage());
  126. }
  127. } else {
  128. $this->error(__('Invalid parameters'));
  129. }
  130. $this->success('同步成功!');
  131. }
  132. }