xhWxMenuService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace common\services;
  3. use bizHd\wx\classes\WxOpenClass;
  4. use common\components\util;
  5. use Yii;
  6. use common\components\wxUtil;
  7. use common\components\dict;
  8. use common\models\xhWxMenu;
  9. class xhWxMenuService
  10. {
  11. /**
  12. * 初始化菜单
  13. */
  14. public static function menuInit()
  15. {
  16. $ptStyle = dict::getDict('ptStyle');
  17. if (empty($ptStyle)) {
  18. util::stop('没有找到平台');
  19. }
  20. foreach ($ptStyle as $style) {
  21. if ($style == dict::getDict('ptStyle', 'hd')) {
  22. $merchant = WxOpenClass::getWxInfo();
  23. $url = Yii::$app->params['hdDomain'];
  24. self::menuGenerate($style, $merchant, $url);
  25. } elseif ($style == dict::getDict('ptStyle', 'ghs')) {
  26. $merchant = WxOpenClass::getGhsWxInfo();
  27. $url = Yii::$app->params['ghsDomain'];
  28. self::menuGenerate($style, $merchant, $url);
  29. } elseif ($style == dict::getDict('ptStyle', 'mall')) {
  30. $merchant = WxOpenClass::getMallWxInfo();
  31. $url = Yii::$app->params['mallDomain'];
  32. self::menuGenerate($style, $merchant, $url);
  33. }
  34. }
  35. }
  36. public static function menuGenerate($style, $merchant, $url)
  37. {
  38. if ($style == dict::getDict('ptStyle', 'mall')) {
  39. $button = [
  40. ["type" => 'miniprogram', "name" => "最近访问", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/recent", "url" => $url,],
  41. ];
  42. } else {
  43. $button = [
  44. ["type" => 'miniprogram', "name" => "官网", "appid" => $merchant['miniAppId'], "pagepath" => "pagesClient/official/index", "url" => $url,],
  45. ["type" => 'miniprogram', "name" => "后台", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/workbench", "url" => $url,],
  46. ];
  47. }
  48. foreach ($button as $key => $val) {
  49. unset($button[$key]['keyName']);
  50. }
  51. $menu['button'] = $button;
  52. $info = wxUtil::userMenuToWeiXin($menu, $merchant, $style);
  53. $info = json_decode($info, true);
  54. print_r($info);
  55. }
  56. public static function getByMenuKey($menuKey)
  57. {
  58. return xhWxMenu::find()->where(['menuKey' => $menuKey])->asArray()->one();
  59. }
  60. public static function getMenuList($sjId)
  61. {
  62. $list = self::getAllMenu($sjId);
  63. return $list;
  64. }
  65. /**
  66. * 取出商家所有菜单
  67. */
  68. public static function getAllMenu($sjId, $getKeyName = false)
  69. {
  70. $list = self::getMenuById(0, $sjId, $getKeyName);
  71. return $list;
  72. }
  73. /**
  74. * 根据菜单id取子菜单
  75. * $getKeyName false 默认不带键名 true 返回带键名的
  76. */
  77. public static function getMenuById($parentId, $sjId, $getKeyName = false)
  78. {
  79. $condition = ['parentId' => $parentId, 'sjId' => $sjId];
  80. $wxMenu = xhWxMenu::getAllByCondition($condition, 'menuOrder asc');
  81. $data = [];
  82. if (empty($wxMenu)) {
  83. return $data;
  84. }
  85. foreach ($wxMenu as $k => $v) {
  86. $id = $v['id'];
  87. $cate = [
  88. 'id' => $id,
  89. 'name' => $v['name'],
  90. 'menuKey' => $v['menuKey'],
  91. 'menuOption' => $v['menuOption'],
  92. 'parentId' => $v['parentId'],
  93. 'sjId' => $v['sjId'],
  94. 'replyType' => $v['replyType'],
  95. 'replyUrl' => $v['replyUrl'],
  96. 'replyText' => $v['replyText'],
  97. 'replyNews' => $v['replyNews'],
  98. 'replyMultiNews' => $v['replyMultiNews'],
  99. 'hasSonMenu' => $v['hasSonMenu'],
  100. 'menuOrder' => $v['menuOrder'],
  101. 'createTime' => $v['createTime'],
  102. 'updateTime' => $v['updateTime'],
  103. 'child' => self::getMenuById($id, $sjId, $getKeyName),
  104. ];
  105. if ($getKeyName) {
  106. $data[$id] = $cate;
  107. } else {
  108. $data[] = $cate;
  109. }
  110. }
  111. return $data;
  112. }
  113. public static function delMenuList($sjId)
  114. {
  115. xhWxMenu::deleteByCondition(['sjId' => $sjId]);
  116. }
  117. public static function add($data)
  118. {
  119. return xhWxMenu::add($data);
  120. }
  121. public static function getFormatData($wxMenuContent, $wxMenuOption, $merchant)
  122. {
  123. $addData = [];
  124. $wxMenuOptionList = dict::getDict('wxMenuOption');
  125. $commonUseUrl = dict::getDict('commonUseUrl');
  126. switch ($wxMenuOption) {
  127. case $wxMenuOptionList['redirectToCommonUseUrl']://跳转到常用链接
  128. foreach ($commonUseUrl as $comKey => $comVal) {
  129. if ($comKey == $wxMenuContent) {
  130. $addData['replyUrl'] = Yii::$app->params['frontUrl'] . $comVal['url'] . '?account=' . $merchant['id'];
  131. $addData['targetId'] = $wxMenuContent;
  132. }
  133. }
  134. break;
  135. case $wxMenuOptionList['redirectToCustomUrl']://跳转到自定义网址
  136. $addData['replyUrl'] = urldecode($wxMenuContent);
  137. break;
  138. case $wxMenuOptionList['showText']://回复文字
  139. $addData['replyText'] = $wxMenuContent;
  140. break;
  141. default:
  142. }
  143. return $addData;
  144. }
  145. }