xhWxMenuService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace common\services;
  3. use biz\message\services\TMessageService;
  4. use biz\wx\services\WxOpenService;
  5. use Yii;
  6. use common\components\wxUtil;
  7. use common\components\configDict;
  8. use common\models\xhWxMenu;
  9. class xhWxMenuService
  10. {
  11. /**
  12. * 初始化菜单
  13. */
  14. public static function applyInit($merchant, $isOpen = 0)
  15. {
  16. $merchantId = isset($merchant['id']) ? $merchant['id'] : 0;
  17. //删除之前存在的菜单
  18. self::delMenuList($merchantId);
  19. $menuOption = configDict::getConfig('wxMenuOption');
  20. $mallDomain = Yii::$app->params['mallDomain'];
  21. $shopDomain = Yii::$app->params['shopDomain'];
  22. //强制转成https方式
  23. if (strpos($mallDomain, 'https') === false) {
  24. $mallDomain = str_replace('http', 'https', $mallDomain);
  25. $shopDomain = str_replace('http', 'https', $shopDomain);
  26. }
  27. if ($isOpen == 0) {
  28. // $button = [
  29. // ["type" => 'view', "name" => "鲜花速递", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
  30. // ["type" => 'view', "name" => "会员服务", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
  31. // ["type" => 'view', "name" => "我的订单", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
  32. // ];
  33. $button = [
  34. ["type" => 'miniprogram', "name" => "鲜花速递", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/index", "url" => $mallDomain . '/#/?account=' . $merchantId,],
  35. ["type" => 'miniprogram', "name" => "会员服务", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/user", "url" => $mallDomain . '/#/?account=' . $merchantId,],
  36. ["type" => 'miniprogram', "name" => "我的订单", "appid" => $merchant['miniAppId'], "pagepath" => "pages/order/list", "url" => $mallDomain . '/#/?account=' . $merchantId,],
  37. ];
  38. } else {
  39. // $button = [
  40. // ["type" => "view", "name" => "官网", "menuOption" => $menuOption['redirectToCustomUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
  41. // ["type" => "view", "name" => "后台", "menuOption" => $menuOption['redirectToCustomUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
  42. // ];
  43. $button = [
  44. ["type" => 'miniprogram', "name" => "官网", "appid" => $merchant['miniAppId'], "pagepath" => "admin/official/index", "url" => $mallDomain . '/#/?account=' . $merchantId,],
  45. ["type" => 'miniprogram', "name" => "后台", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/console", "url" => $mallDomain . '/#/?account=' . $merchantId,],
  46. ];
  47. }
  48. $wxUtil = new wxUtil($merchant);
  49. $menuList = $button;
  50. foreach ($button as $key => $val) {
  51. unset($button[$key]['keyName']);
  52. }
  53. $menu['button'] = $button;
  54. $info = $wxUtil->userMenuToWeiXin($menu);
  55. $info = json_decode($info, true);
  56. print_r($info);
  57. die;
  58. xhMerchantExtendService::updateByMerchantId($merchantId, ['wxMenuInit' => 1]);
  59. $now = time();
  60. $date = date("Y-m-d H:i:s", $now);
  61. $mapped = configDict::getConfig('commonUseUrlMapped');
  62. $redirectUrl = configDict::getConfig('replyType', 'redirectUrl');//回复类型 跳转链接
  63. if ($info['errcode'] != 0) {
  64. return false;
  65. }
  66. foreach ($menuList as $key => $val) {
  67. $addData = [];
  68. $addData['parentId'] = 0;
  69. $addData['merchantId'] = $merchantId;
  70. $addData['menuOrder'] = $key;
  71. $addData['menuOption'] = $val['menuOption'];
  72. $addData['hasSonMenu'] = isset($val['sub_button']) && !empty($val['sub_button']) ? 1 : 0;
  73. $addData['name'] = $val['name'];
  74. $addData['menuKey'] = $merchantId . '_' . $key;
  75. $addData['createTime'] = $date;
  76. $addData['replyType'] = $redirectUrl;//回复类型:常用链接、自定义网址、文字、报名类活动
  77. $addData['targetId'] = isset($val['keyName']) ? $mapped[$val['keyName']] : 0;//常用链接的链接选项id;活动id等
  78. $addData['replyUrl'] = isset($val['url']) ? $val['url'] : '';
  79. $parentMenu = self::add($addData);
  80. if (isset($val['sub_button']) && !empty($val['sub_button'])) {
  81. foreach ($val['sub_button'] as $sonKey => $sonVal) {
  82. $sonAddData = [];
  83. $sonAddData['parentId'] = $parentMenu['id'];
  84. $sonAddData['merchantId'] = $merchantId;
  85. $sonAddData['menuOrder'] = $sonKey;
  86. $sonAddData['menuOption'] = $sonVal['menuOption'];//回复类型
  87. $sonAddData['hasSonMenu'] = 0;
  88. $sonAddData['name'] = $sonVal['name'];
  89. $sonAddData['menuKey'] = $merchantId . '_' . $key . '_' . $sonKey;
  90. $sonAddData['createTime'] = $date;
  91. $sonAddData['replyType'] = $redirectUrl;//回复类型:常用链接、自定义网址、文字、报名类活动
  92. $sonAddData['targetId'] = isset($sonVal['keyName']) ? $mapped[$sonVal['keyName']] : 0;//常用链接选项id、活动id
  93. $sonAddData['replyUrl'] = isset($sonVal['url']) ? $sonVal['url'] : '';
  94. self::add($sonAddData);
  95. }
  96. }
  97. }
  98. return true;
  99. }
  100. public static function getByMenuKey($menuKey)
  101. {
  102. return xhWxMenu::find()->where(['menuKey' => $menuKey])->asArray()->one();
  103. }
  104. public static function getMenuList($merchantId)
  105. {
  106. $list = self::getAllMenu($merchantId);
  107. return $list;
  108. }
  109. /**
  110. * 取出商家所有菜单
  111. */
  112. public static function getAllMenu($merchantId, $getKeyName = false)
  113. {
  114. $list = self::getMenuById(0, $merchantId, $getKeyName);
  115. return $list;
  116. }
  117. /**
  118. * 根据菜单id取子菜单
  119. * $getKeyName false 默认不带键名 true 返回带键名的
  120. */
  121. public static function getMenuById($parentId, $merchantId, $getKeyName = false)
  122. {
  123. $condition = ['parentId' => $parentId, 'merchantId' => $merchantId];
  124. $wxMenu = xhWxMenu::getAllByCondition($condition, 'menuOrder asc');
  125. $data = [];
  126. if (empty($wxMenu)) {
  127. return $data;
  128. }
  129. foreach ($wxMenu as $k => $v) {
  130. $id = $v['id'];
  131. $cate = [
  132. 'id' => $id,
  133. 'name' => $v['name'],
  134. 'menuKey' => $v['menuKey'],
  135. 'menuOption' => $v['menuOption'],
  136. 'parentId' => $v['parentId'],
  137. 'merchantId' => $v['merchantId'],
  138. 'replyType' => $v['replyType'],
  139. 'replyUrl' => $v['replyUrl'],
  140. 'replyText' => $v['replyText'],
  141. 'replyNews' => $v['replyNews'],
  142. 'replyMultiNews' => $v['replyMultiNews'],
  143. 'hasSonMenu' => $v['hasSonMenu'],
  144. 'menuOrder' => $v['menuOrder'],
  145. 'createTime' => $v['createTime'],
  146. 'updateTime' => $v['updateTime'],
  147. 'child' => self::getMenuById($id, $merchantId, $getKeyName),
  148. ];
  149. if ($getKeyName) {
  150. $data[$id] = $cate;
  151. } else {
  152. $data[] = $cate;
  153. }
  154. }
  155. return $data;
  156. }
  157. public static function delMenuList($merchantId)
  158. {
  159. xhWxMenu::deleteByCondition(['merchantId' => $merchantId]);
  160. }
  161. public static function add($data)
  162. {
  163. return xhWxMenu::add($data);
  164. }
  165. public static function getFormatData($wxMenuContent, $wxMenuOption, $merchant)
  166. {
  167. $addData = [];
  168. $wxMenuOptionList = configDict::getConfig('wxMenuOption');
  169. $commonUseUrl = configDict::getConfig('commonUseUrl');
  170. switch ($wxMenuOption) {
  171. case $wxMenuOptionList['redirectToCommonUseUrl']://跳转到常用链接
  172. foreach ($commonUseUrl as $comKey => $comVal) {
  173. if ($comKey == $wxMenuContent) {
  174. $addData['replyUrl'] = Yii::$app->params['frontUrl'] . $comVal['url'] . '?account=' . $merchant['id'];
  175. $addData['targetId'] = $wxMenuContent;
  176. }
  177. }
  178. break;
  179. case $wxMenuOptionList['redirectToCustomUrl']://跳转到自定义网址
  180. $addData['replyUrl'] = urldecode($wxMenuContent);
  181. break;
  182. case $wxMenuOptionList['showText']://回复文字
  183. $addData['replyText'] = $wxMenuContent;
  184. break;
  185. default:
  186. }
  187. return $addData;
  188. }
  189. }