xhWxMenuService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/home/console", "url" => $mallDomain . '/#/?account=' . $merchantId,],
  45. ["type" => 'miniprogram', "name" => "申请", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/console", "url" => $mallDomain . '/#/?account=' . $merchantId,],
  46. ["type" => 'miniprogram', "name" => "后台", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/console", "url" => $mallDomain . '/#/?account=' . $merchantId,],
  47. ];
  48. }
  49. $wxUtil = new wxUtil($merchant);
  50. $menuList = $button;
  51. foreach ($button as $key => $val) {
  52. unset($button[$key]['keyName']);
  53. }
  54. $menu['button'] = $button;
  55. $info = $wxUtil->userMenuToWeiXin($menu);
  56. $info = json_decode($info, true);
  57. print_r($info);
  58. die;
  59. xhMerchantExtendService::updateByMerchantId($merchantId, ['wxMenuInit' => 1]);
  60. $now = time();
  61. $date = date("Y-m-d H:i:s", $now);
  62. $mapped = configDict::getConfig('commonUseUrlMapped');
  63. $redirectUrl = configDict::getConfig('replyType', 'redirectUrl');//回复类型 跳转链接
  64. if ($info['errcode'] != 0) {
  65. return false;
  66. }
  67. foreach ($menuList as $key => $val) {
  68. $addData = [];
  69. $addData['parentId'] = 0;
  70. $addData['merchantId'] = $merchantId;
  71. $addData['menuOrder'] = $key;
  72. $addData['menuOption'] = $val['menuOption'];
  73. $addData['hasSonMenu'] = isset($val['sub_button']) && !empty($val['sub_button']) ? 1 : 0;
  74. $addData['name'] = $val['name'];
  75. $addData['menuKey'] = $merchantId . '_' . $key;
  76. $addData['createTime'] = $date;
  77. $addData['replyType'] = $redirectUrl;//回复类型:常用链接、自定义网址、文字、报名类活动
  78. $addData['targetId'] = isset($val['keyName']) ? $mapped[$val['keyName']] : 0;//常用链接的链接选项id;活动id等
  79. $addData['replyUrl'] = isset($val['url']) ? $val['url'] : '';
  80. $parentMenu = self::add($addData);
  81. if (isset($val['sub_button']) && !empty($val['sub_button'])) {
  82. foreach ($val['sub_button'] as $sonKey => $sonVal) {
  83. $sonAddData = [];
  84. $sonAddData['parentId'] = $parentMenu['id'];
  85. $sonAddData['merchantId'] = $merchantId;
  86. $sonAddData['menuOrder'] = $sonKey;
  87. $sonAddData['menuOption'] = $sonVal['menuOption'];//回复类型
  88. $sonAddData['hasSonMenu'] = 0;
  89. $sonAddData['name'] = $sonVal['name'];
  90. $sonAddData['menuKey'] = $merchantId . '_' . $key . '_' . $sonKey;
  91. $sonAddData['createTime'] = $date;
  92. $sonAddData['replyType'] = $redirectUrl;//回复类型:常用链接、自定义网址、文字、报名类活动
  93. $sonAddData['targetId'] = isset($sonVal['keyName']) ? $mapped[$sonVal['keyName']] : 0;//常用链接选项id、活动id
  94. $sonAddData['replyUrl'] = isset($sonVal['url']) ? $sonVal['url'] : '';
  95. self::add($sonAddData);
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. public static function getByMenuKey($menuKey)
  102. {
  103. return xhWxMenu::find()->where(['menuKey' => $menuKey])->asArray()->one();
  104. }
  105. public static function getMenuList($merchantId)
  106. {
  107. $list = self::getAllMenu($merchantId);
  108. return $list;
  109. }
  110. /**
  111. * 取出商家所有菜单
  112. */
  113. public static function getAllMenu($merchantId, $getKeyName = false)
  114. {
  115. $list = self::getMenuById(0, $merchantId, $getKeyName);
  116. return $list;
  117. }
  118. /**
  119. * 根据菜单id取子菜单
  120. * $getKeyName false 默认不带键名 true 返回带键名的
  121. */
  122. public static function getMenuById($parentId, $merchantId, $getKeyName = false)
  123. {
  124. $condition = ['parentId' => $parentId, 'merchantId' => $merchantId];
  125. $wxMenu = xhWxMenu::getAllByCondition($condition, 'menuOrder asc');
  126. $data = [];
  127. if (empty($wxMenu)) {
  128. return $data;
  129. }
  130. foreach ($wxMenu as $k => $v) {
  131. $id = $v['id'];
  132. $cate = [
  133. 'id' => $id,
  134. 'name' => $v['name'],
  135. 'menuKey' => $v['menuKey'],
  136. 'menuOption' => $v['menuOption'],
  137. 'parentId' => $v['parentId'],
  138. 'merchantId' => $v['merchantId'],
  139. 'replyType' => $v['replyType'],
  140. 'replyUrl' => $v['replyUrl'],
  141. 'replyText' => $v['replyText'],
  142. 'replyNews' => $v['replyNews'],
  143. 'replyMultiNews' => $v['replyMultiNews'],
  144. 'hasSonMenu' => $v['hasSonMenu'],
  145. 'menuOrder' => $v['menuOrder'],
  146. 'createTime' => $v['createTime'],
  147. 'updateTime' => $v['updateTime'],
  148. 'child' => self::getMenuById($id, $merchantId, $getKeyName),
  149. ];
  150. if ($getKeyName) {
  151. $data[$id] = $cate;
  152. } else {
  153. $data[] = $cate;
  154. }
  155. }
  156. return $data;
  157. }
  158. public static function delMenuList($merchantId)
  159. {
  160. xhWxMenu::deleteByCondition(['merchantId' => $merchantId]);
  161. }
  162. public static function add($data)
  163. {
  164. return xhWxMenu::add($data);
  165. }
  166. public static function getFormatData($wxMenuContent, $wxMenuOption, $merchant)
  167. {
  168. $addData = [];
  169. $wxMenuOptionList = configDict::getConfig('wxMenuOption');
  170. $commonUseUrl = configDict::getConfig('commonUseUrl');
  171. switch ($wxMenuOption) {
  172. case $wxMenuOptionList['redirectToCommonUseUrl']://跳转到常用链接
  173. foreach ($commonUseUrl as $comKey => $comVal) {
  174. if ($comKey == $wxMenuContent) {
  175. $addData['replyUrl'] = Yii::$app->params['frontUrl'] . $comVal['url'] . '?account=' . $merchant['id'];
  176. $addData['targetId'] = $wxMenuContent;
  177. }
  178. }
  179. break;
  180. case $wxMenuOptionList['redirectToCustomUrl']://跳转到自定义网址
  181. $addData['replyUrl'] = urldecode($wxMenuContent);
  182. break;
  183. case $wxMenuOptionList['showText']://回复文字
  184. $addData['replyText'] = $wxMenuContent;
  185. break;
  186. default:
  187. }
  188. return $addData;
  189. }
  190. }