MenuService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace addons\Wechat\services;
  3. use Yii;
  4. use common\components\Service;
  5. use addons\Wechat\common\models\Menu;
  6. use addons\Wechat\common\enums\MenuTypeEnum;
  7. /**
  8. * Class MenuService
  9. * @package addons\Wechat\services
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class MenuService extends Service
  13. {
  14. /**
  15. * @param Menu $model
  16. * @param $data
  17. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  18. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  19. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  20. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  21. * @throws \GuzzleHttp\Exception\GuzzleException
  22. * @throws \Psr\SimpleCache\InvalidArgumentException
  23. * @throws \yii\web\UnprocessableEntityHttpException
  24. */
  25. public function createSave(Menu $model, $data)
  26. {
  27. $buttons = [];
  28. foreach ($data as &$button) {
  29. $arr = [];
  30. // 判断是否有子菜单
  31. if (isset($button['sub_button'])) {
  32. $arr['name'] = $button['name'];
  33. foreach ($button['sub_button'] as &$sub) {
  34. $sub_button = $this->mergeButton($sub);
  35. $sub_button['name'] = $sub['name'];
  36. $sub_button['type'] = $sub['type'];
  37. $arr['sub_button'][] = $sub_button;
  38. }
  39. } else {
  40. $arr = $this->mergeButton($button);
  41. $arr['name'] = $button['name'];
  42. $arr['type'] = $button['type'];
  43. }
  44. $buttons[] = $arr;
  45. }
  46. $model->menu_data = $buttons;
  47. // 判断写入是否成功
  48. !$model->validate() && $this->error($model);
  49. // 个性化菜单
  50. if ($model->type == MenuTypeEnum::INDIVIDUATION) {
  51. $matchRule = [
  52. "tag_id" => $model->tag_id,
  53. "client_platform_type" => $model->client_platform_type,
  54. ];
  55. // 创建自定义菜单
  56. $menuResult = Yii::$app->wechat->app->menu->create($buttons, $matchRule);
  57. Yii::$app->services->base->getWechatError($menuResult);
  58. $model->menu_id = $menuResult['menuid'];
  59. } else {
  60. // 验证微信报错
  61. Yii::$app->services->base->getWechatError(Yii::$app->wechat->app->menu->create($buttons));
  62. }
  63. !$model->save() && $this->error($model);
  64. Menu::updateAll(['menu_data' => $buttons], ['id' => $model->id]);
  65. }
  66. /**
  67. * 合并前端过来的数据
  68. *
  69. * @param array $button
  70. * @return array
  71. */
  72. protected function mergeButton(array $button)
  73. {
  74. $arr = [];
  75. $menuTypes = MenuTypeEnum::type();
  76. if ($button['type'] == 'click' || $button['type'] == 'view') {
  77. $arr[$menuTypes[$button['type']]['meta']] = $button['content'];
  78. } elseif ($button['type'] == 'miniprogram') {
  79. $arr['appid'] = $button['appid'];
  80. $arr['pagepath'] = $button['pagepath'];
  81. $arr['url'] = $button['url'];
  82. } else {
  83. $arr[$menuTypes[$button['type']]['meta']] = $menuTypes[$button['type']]['value'];
  84. }
  85. return $arr;
  86. }
  87. /**
  88. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  89. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  90. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  91. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  92. * @throws \Psr\SimpleCache\InvalidArgumentException
  93. * @throws \yii\web\UnprocessableEntityHttpException
  94. */
  95. public function sync()
  96. {
  97. // 获取菜单列表
  98. $list = Yii::$app->wechat->app->menu->list();
  99. // 解析微信接口是否报错
  100. Yii::$app->services->base->getWechatError($list);
  101. // 开始获取自定义菜单同步
  102. if (!empty($list['menu'])) {
  103. $model = new Menu;
  104. $model->title = "默认菜单";
  105. $model = $model->loadDefaultValues();
  106. $model->menu_data = $list['menu']['button'];
  107. $model->menu_id = isset($list['menu']['menuid']) ? $list['menu']['menuid'] : '';
  108. $model->save();
  109. }
  110. // 个性化菜单
  111. if (!empty($list['conditionalmenu'])) {
  112. foreach ($list['conditionalmenu'] as $menu) {
  113. if (!($model = Menu::findOne(['menu_id' => $menu['menuid']]))) {
  114. $model = new Menu;
  115. $model = $model->loadDefaultValues();
  116. }
  117. $model->title = "个性化菜单";
  118. $model->attributes = $menu['matchrule'];
  119. $model->type = MenuTypeEnum::INDIVIDUATION;
  120. $model->tag_id = isset($menu['group_id']) ? $menu['group_id'] : '';
  121. $model->menu_data = $menu['button'];
  122. $model->menu_id = $menu['menuid'];
  123. $model->save();
  124. }
  125. }
  126. }
  127. }
粤ICP备19079148号