WechatMenuService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * 公众号菜单
  4. * @file:WechatMenuService.php
  5. * @Date: 2023/7/11
  6. * @Time: 10:53
  7. */
  8. namespace Modules\WechatPlatform\Services;
  9. use Illuminate\Support\Facades\DB;
  10. use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
  11. use Modules\Common\Services\BaseService;
  12. use Modules\WechatPlatform\Models\WechatAccountMenuDetail;
  13. use Modules\WechatPlatform\Models\WechatMenu;
  14. use function PHPUnit\Framework\isNan;
  15. class WechatMenuService extends BaseService
  16. {
  17. /**
  18. * 添加菜单
  19. * name: addMenu
  20. * @param $param
  21. * date 2023/07/11 11:38
  22. */
  23. public static function addMenu($param)
  24. {
  25. $res = WechatMenu::create($param);
  26. if ($res) {
  27. return ['msg' => '操作成功'];
  28. }
  29. self::throwErrMsg('操作失败');
  30. }
  31. /**
  32. * 更新菜单
  33. * name: updateMenu
  34. * @param $id
  35. * @param $param
  36. * @return string[]
  37. * date 2023/07/12 07:17
  38. */
  39. public static function updateMenu($id, $param)
  40. {
  41. $info = WechatMenu::where('id', $id)->where('is_del', 0)->first();
  42. if (is_empty($info)) {
  43. self::throwErrMsg('该菜单不存在或已删除');
  44. }
  45. WechatMenu::where('id', $id)->update($param);
  46. $data = [
  47. 'type' => $param['type'],
  48. 'content' => $param['content'],
  49. 'msg_content' => $param['msg_content']
  50. ];
  51. WechatAccountMenuDetail::where('menu_id', $id)->update($data);
  52. // 更新公众号菜单
  53. $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('wechat_authorization_info_id')->toArray();
  54. self::createWechatMenus($appIds,$id);
  55. return ['msg' => '操作成功'];
  56. }
  57. /**
  58. * 创建微信菜单
  59. * name: createWechatMenus
  60. * @param array $appIds
  61. * @param $menuId
  62. * @return bool
  63. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  64. * @throws \GuzzleHttp\Exception\GuzzleException
  65. * date 2023/07/12 14:29
  66. */
  67. private static function createWechatMenus(array $appIds,$menuId)
  68. {
  69. if (empty($appIds)) {
  70. return true;
  71. }
  72. $info = WechatAccountMenuDetail::query()->whereIn('wechat_authorization_info_id', $appIds)->where('menu_id',$menuId)->get();
  73. foreach ($info as $val) {
  74. $app = self::getofficialAccount($val->wechat_authorization_info_id);
  75. if ($val->wx_menuid) {
  76. // 有菜单则删除菜单
  77. $app->menu->delete($val->wx_menuid);
  78. }
  79. $matchRule = [
  80. "client_platform_type" => $val->type == "ios" ? "1" : "2",
  81. ];
  82. $res = $app->menu->create($val->content, $matchRule);
  83. // myLog('wx-meun')->info(['res' => $res]);
  84. $errcode = getProp($res,'errcode',"");
  85. if (intval($errcode) == 65303){
  86. $res = $app->menu->create($val->content);
  87. }
  88. // myLog('wx-meun')->info(['res' => $res]);
  89. $wxMenuId = getProp($res, 'menuid', "");
  90. $res = $app->menu->create($val->content, $matchRule);
  91. myLog('wx-meun')->info(['res' => $res,'mid' => $wxMenuId]);
  92. if ($wxMenuId) {
  93. WechatAccountMenuDetail::query()->where('id', $val->id)->update(['wx_menuid' => $wxMenuId]);
  94. }
  95. }
  96. return true;
  97. }
  98. public static function getofficialAccount($id)
  99. {
  100. $info = WechatOpenPlatformService::getAppInfoById($id);
  101. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($info->component_appid);
  102. $openPlatform = WechatOpenPlatformService::buildApplication($componentInfo);
  103. $app = $openPlatform->officialAccount($info->authorizer_appid, getProp($info, 'authorizer_refresh_token'));
  104. unset($appInfo);
  105. return $app;
  106. }
  107. /**
  108. * 菜单详情
  109. * name: detail
  110. * @param $id
  111. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  112. * date 2023/07/12 07:17
  113. */
  114. public static function detail($id)
  115. {
  116. return WechatMenu::query()->where('id', $id)->first();
  117. }
  118. /**
  119. * 公众号列表
  120. * name: authList
  121. * @param $id
  122. * @param $userId
  123. * @return mixed
  124. * date 2023/07/12 07:16
  125. */
  126. public static function authList($id, $userId)
  127. {
  128. $list = DB::table('wechat_authorization_infos')
  129. ->where('user_id', $userId)
  130. ->select('id', 'nick_name', 'is_enabled')->get();
  131. $authList = WechatAccountMenuDetail::where('menu_id', $id)->where('status' , 1)->distinct('appid')->get();
  132. if (!$list->isEmpty()) {
  133. foreach ($list as $val) {
  134. $val->is_auth = 0;
  135. $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
  136. if (getProp($info, 'status') == 1) {
  137. $val->is_auth = 1;
  138. }
  139. if ($val->is_enabled != 1) {
  140. $val->nick_name .= "(取消授权)";
  141. }
  142. unset($val->is_enabled);
  143. }
  144. }
  145. return $list;
  146. }
  147. /**
  148. * 删除
  149. * name: del
  150. * @param $ids
  151. * @return string[]
  152. * date 2023/07/12 07:14
  153. */
  154. public static function del($ids)
  155. {
  156. WechatMenu::whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);
  157. $applyIds = WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->where('status', 1)->pluck('id')->toArray();
  158. if ($applyIds) {
  159. WechatAccountMenuDetail::whereIn('menu_id', $ids)->update(['status' => 0]);
  160. // 删除菜单
  161. self::delWechatAccountMenu($applyIds);
  162. }
  163. return ['msg' => "操作成功"];
  164. }
  165. private static function delWechatAccountMenu(array $applyIds)
  166. {
  167. $info = WechatAccountMenuDetail::query()->whereIn('id', $applyIds)->get();
  168. foreach ($info as $val) {
  169. if ($val->wx_menuid) {
  170. // 有菜单则删除菜单
  171. $app = self::getofficialAccount($val->wechat_authorization_info_id);
  172. $app->menu->delete($info->wx_menuid);
  173. }
  174. WechatAccountMenuDetail::query()->where('wechat_authorization_info_id', $val->id)->update(['wx_menuid' => ""]);
  175. }
  176. return "操作成功";
  177. }
  178. /**
  179. * 分配公众号
  180. * name: allocation
  181. * @param $id
  182. * @param array $wxAuthIds
  183. * date 2023/07/12 07:20
  184. */
  185. public static function allocation($id, $wxAuthIds)
  186. {
  187. $info = WechatMenu::query()->where('id', $id)->where('is_del', 0)->first();
  188. if (is_empty($info)) {
  189. self::throwErrMsg('该菜单不存在或已删除');
  190. }
  191. // 已分配的
  192. $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('wechat_authorization_info_id')->toArray();
  193. $list = [];
  194. $delAppId = [];
  195. $createAddIp = [];
  196. if (empty($wxAuthIds)) {
  197. $data['wechat_accounts'] = [];
  198. $delAppId = $appIds;
  199. } else {
  200. $wechatAccountInfos = DB::table('wechat_authorization_infos')
  201. ->whereIn('id', $wxAuthIds)
  202. ->where('is_enabled', 1)
  203. ->where('user_id', $info->user_id)
  204. ->get();
  205. if ($wechatAccountInfos->isEmpty()) {
  206. self::throwErrMsg("优化师对提交的公众号没有使用权限");
  207. }
  208. $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
  209. $canNotUsed = array_diff($wxAuthIds, $canNotUsed);
  210. if (count($canNotUsed) > 0) {
  211. self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
  212. }
  213. foreach ($appIds as $val) {
  214. if (!in_array($val, $wxAuthIds)) {
  215. $delAppId[] = $val;
  216. }
  217. }
  218. // $allSet = WechatAccountMenuDetail::query()->where('user_id', $info->user_id)->get();
  219. $appIds = [];
  220. foreach ($wechatAccountInfos as $val) {
  221. $data['wechat_accounts'][] = [
  222. 'id' => $val->id,
  223. 'appid' => $val->authorizer_appid,
  224. 'nick_name' => $val->nick_name,
  225. 'component_appid' => $val->component_appid
  226. ];
  227. $appIds[] = $val->authorizer_appid;
  228. $list[] = [
  229. 'menu_id' => $info->id,
  230. 'user_id' => $info->user_id,
  231. 'puser_id' => $info->puser_id,
  232. 'miniprogram_id' => $info->miniprogram_id,
  233. 'appid' => $val->authorizer_appid,
  234. 'wechat_authorization_info_id' => $val->id,
  235. 'nick_name' => $val->nick_name,
  236. 'content' => $info->content,
  237. 'msg_content' => $info->msg_content,
  238. 'status' => 1,
  239. ];
  240. }
  241. unset($wechatAccountInfos, $appIds, $val, $canNotUsed);
  242. }
  243. DB::beginTransaction();
  244. try {
  245. if (!empty($list)) {
  246. foreach ($list as $val) {
  247. WechatAccountMenuDetail::updateOrCreate(
  248. [
  249. 'menu_id' => $val['menu_id'],
  250. 'miniprogram_id' => $val['miniprogram_id'],
  251. 'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
  252. ], $val);
  253. }
  254. } else {
  255. WechatAccountMenuDetail::where('menu_id', $id)->update(['status' => 0]);
  256. }
  257. if (!empty($delAppId)) {
  258. WechatAccountMenuDetail::whereIn('id', $delAppId)->update(['status' => 0]);
  259. }
  260. WechatMenu::query()->where('id', $id)->update($data);
  261. DB::commit();
  262. } catch (\Exception $exception) {
  263. self::throwErrMsg('操作失败');
  264. }
  265. $createAddIp = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('wechat_authorization_info_id')->toArray();
  266. myLog('wx-meun')->info(['c' => $createAddIp,'del' => $delAppId]);
  267. if (!empty($createAddIp)) {
  268. self::createWechatMenus($createAddIp,$id);
  269. }
  270. if (!empty($delAppId)) {
  271. self::delWechatAccountMenuByMeunId($delAppId,$id);
  272. }
  273. return ['msg' => "操作成功"];
  274. }
  275. /**
  276. * 列表
  277. * name: list
  278. * @param array $param
  279. * date 2023/07/12 07:43
  280. */
  281. public static function list(array $param)
  282. {
  283. $sql = self::getQuery($param)->orderBy('id', 'desc');
  284. $isAll = getProp($param, 'is_all', false);
  285. if ($isAll) {
  286. $list = $sql->get();
  287. } else {
  288. $list = $sql->paginate(getProp($param, 'limit', 10));
  289. }
  290. foreach ($list as $val){
  291. $val->wechat_accounts = WechatAccountMenuDetail::query()->where(
  292. ['menu_id' => $val->id,
  293. 'status' => 1
  294. ])->distinct('appid')->select('wechat_authorization_info_id as id',"appid",'nick_name')->get();
  295. }
  296. return $list;
  297. }
  298. private static function getQuery(array $param)
  299. {
  300. $sql = WechatMenu::query()->where('is_del', 0);
  301. if (getProp($param, 'puser_id')) {
  302. $sql->where('puser_id', $param['puser_id']);
  303. }
  304. if (getProp($param, 'user_id')) {
  305. $sql->where('user_id', $param['user_id']);
  306. }
  307. if (getProp($param, 'title')) {
  308. $sql->where('title', "like", "%" . $param['title'] . "%");
  309. }
  310. if (getProp($param, 'type')) {
  311. $sql->where('user_id', $param['type']);
  312. }
  313. if (getProp($param, 'miniprogram_id')) {
  314. $sql->where('miniprogram_id', $param['miniprogram_id']);
  315. }
  316. if (getProp($param, 'wechat_authorization_info_id')) {
  317. $ids = WechatAccountMenuDetail::query()->where([
  318. 'wechat_authorization_info_id' => $param['wechat_authorization_info_id'],
  319. 'status' => 1,
  320. ])->distinct('menu_id')->pluck('menu_id')->toArray();
  321. $sql->whereIn('id',$ids);
  322. }
  323. return $sql;
  324. }
  325. private static function delWechatAccountMenuByMeunId(array $delAppId, $menuId)
  326. {
  327. $info = WechatAccountMenuDetail::query()->whereIn('wechat_authorization_info_id', $delAppId)->where('menu_id',$menuId)->get();
  328. foreach ($info as $val) {
  329. if ($val->wx_menuid) {
  330. // 有菜单则删除菜单
  331. $app = self::getofficialAccount($info->wechat_authorization_info_id);
  332. $app->menu->delete($val->wx_menuid);
  333. }
  334. WechatAccountMenuDetail::query()->where('wechat_authorization_info_id', $val->id)->update(['wx_menuid' => ""]);
  335. }
  336. return "操作成功";
  337. }
  338. /**
  339. * 点击事件回复
  340. * name: getClickInfoContent
  341. * @param $wechatInfoId
  342. * date 2023/07/12 16:37
  343. */
  344. public static function getClickInfoContent($wechatAppId,$key)
  345. {
  346. $info = WechatAccountMenuDetail::query()->where([
  347. 'status' => 1,
  348. 'wechat_authorization_info_id' => $wechatAppId,
  349. ])->first();
  350. if (is_empty($info)){
  351. return "";
  352. }
  353. $content = $info->msg_content;
  354. if (empty($content)){
  355. return "";
  356. }
  357. foreach ($content as $val){
  358. if (isset($val[$key])){
  359. $content = $val[$key];
  360. break ;
  361. }
  362. }
  363. return $content ?: "";
  364. }
  365. }