WechatMenuService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('id')->toArray();
  46. WechatMenu::where('id', $id)->update($param);
  47. $data = [
  48. 'type' => $param['type'],
  49. 'content' => $param['content'],
  50. 'msg_content' => $param['msg_content']
  51. ];
  52. WechatAccountMenuDetail::where('menu_id', $id)->update($data);
  53. // 更新公众号菜单
  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 == "iso" ? "1" : "2",
  81. ];
  82. $res = $app->menu->create($val->content, $matchRule);
  83. $wxMenuId = getProp($res, 'menuid', "");
  84. myLog('wx-meun')->info(['res' => $res,'mid' => $wxMenuId]);
  85. if ($wxMenuId) {
  86. WechatAccountMenuDetail::query()->where('id', $val->id)->update(['wx_menuid' => $wxMenuId]);
  87. }
  88. }
  89. return true;
  90. }
  91. public static function getofficialAccount($id)
  92. {
  93. $info = WechatOpenPlatformService::getAppInfoById($id);
  94. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($info->component_appid);
  95. $openPlatform = WechatOpenPlatformService::buildApplication($componentInfo);
  96. $app = $openPlatform->officialAccount($info->authorizer_appid, getProp($info, 'authorizer_refresh_token'));
  97. unset($appInfo);
  98. return $app;
  99. }
  100. /**
  101. * 菜单详情
  102. * name: detail
  103. * @param $id
  104. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  105. * date 2023/07/12 07:17
  106. */
  107. public static function detail($id)
  108. {
  109. return WechatMenu::query()->where('id', $id)->first();
  110. }
  111. /**
  112. * 公众号列表
  113. * name: authList
  114. * @param $id
  115. * @param $userId
  116. * @return mixed
  117. * date 2023/07/12 07:16
  118. */
  119. public static function authList($id, $userId)
  120. {
  121. $list = DB::table('wechat_authorization_infos')
  122. ->where('user_id', $userId)
  123. ->select('id', 'nick_name', 'is_enabled')->get();
  124. $authList = WechatAccountMenuDetail::where('menu_id', $id)->get();
  125. if (!$list->isEmpty()) {
  126. foreach ($list as $val) {
  127. $val->is_auth = 0;
  128. $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
  129. if (getProp($info, 'status') == 1) {
  130. $val->is_auth = 1;
  131. }
  132. if ($val->is_enabled != 1) {
  133. $val->nick_name .= "(取消授权)";
  134. }
  135. unset($val->is_enabled);
  136. }
  137. }
  138. return $list;
  139. }
  140. /**
  141. * 删除
  142. * name: del
  143. * @param $ids
  144. * @return string[]
  145. * date 2023/07/12 07:14
  146. */
  147. public static function del($ids)
  148. {
  149. WechatMenu::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);
  150. $applyIds = WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->where('status', 1)->pluck('id')->toArray();
  151. if ($applyIds) {
  152. WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->update(['status' => 0]);
  153. // 删除菜单
  154. self::delWechatAccountMenu($applyIds);
  155. }
  156. return ['msg' => "操作成功"];
  157. }
  158. private static function delWechatAccountMenu(array $applyIds)
  159. {
  160. $info = WechatAccountMenuDetail::query()->whereIn('id', $applyIds)->get();
  161. foreach ($info as $val) {
  162. if ($val->wx_menuid) {
  163. // 有菜单则删除菜单
  164. $app = self::getofficialAccount($val->wechat_authorization_info_id);
  165. $app->menu->delete($info->wx_menuid);
  166. }
  167. WechatAccountMenuDetail::query()->where('wechat_authorization_info_id', $val->id)->update(['wx_menuid' => ""]);
  168. }
  169. return "操作成功";
  170. }
  171. /**
  172. * 分配公众号
  173. * name: allocation
  174. * @param $id
  175. * @param array $wxAuthIds
  176. * date 2023/07/12 07:20
  177. */
  178. public static function allocation($id, $wxAuthIds)
  179. {
  180. $info = WechatMenu::query()->where('id', $id)->where('is_del', 0)->first();
  181. if (is_empty($info)) {
  182. self::throwErrMsg('该菜单不存在或已删除');
  183. }
  184. // 已分配的
  185. $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('wechat_authorization_info_id')->toArray();
  186. $list = [];
  187. $delAppId = [];
  188. $createAddIp = [];
  189. if (empty($wxAuthIds)) {
  190. $data['wechat_accounts'] = [];
  191. $delAppId = $appIds;
  192. } else {
  193. $wechatAccountInfos = DB::table('wechat_authorization_infos')
  194. ->whereIn('id', $wxAuthIds)
  195. ->where('is_enabled', 1)
  196. ->where('user_id', $info->user_id)
  197. ->get();
  198. if ($wechatAccountInfos->isEmpty()) {
  199. self::throwErrMsg("优化师对提交的公众号没有使用权限");
  200. }
  201. $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
  202. $canNotUsed = array_diff($wxAuthIds, $canNotUsed);
  203. if (count($canNotUsed) > 0) {
  204. self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
  205. }
  206. foreach ($appIds as $val) {
  207. if (!in_array($val, $wxAuthIds)) {
  208. $delAppId[] = $val;
  209. }
  210. }
  211. // $allSet = WechatAccountMenuDetail::query()->where('user_id', $info->user_id)->get();
  212. foreach ($wechatAccountInfos as $val) {
  213. if (!in_array($val->id, $appIds)) {
  214. $createAddIp[] = $val->id;
  215. }
  216. $data['wechat_accounts'][] = [
  217. 'id' => $val->id,
  218. 'appid' => $val->authorizer_appid,
  219. 'nick_name' => $val->nick_name,
  220. 'component_appid' => $val->component_appid
  221. ];
  222. $appIds[] = $val->authorizer_appid;
  223. $list[] = [
  224. 'menu_id' => $info->id,
  225. 'user_id' => $info->user_id,
  226. 'puser_id' => $info->puser_id,
  227. 'miniprogram_id' => $info->miniprogram_id,
  228. 'appid' => $val->authorizer_appid,
  229. 'wechat_authorization_info_id' => $val->id,
  230. 'nick_name' => $val->nick_name,
  231. 'content' => $info->content,
  232. 'msg_content' => $info->msg_content,
  233. 'status' => 1,
  234. ];
  235. }
  236. unset($wechatAccountInfos, $appIds, $val, $canNotUsed);
  237. }
  238. DB::beginTransaction();
  239. try {
  240. if (!empty($list)) {
  241. foreach ($list as $val) {
  242. WechatAccountMenuDetail::updateOrCreate(
  243. [
  244. 'menu_id' => $val['menu_id'],
  245. 'miniprogram_id' => $val['miniprogram_id'],
  246. 'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
  247. ], $val);
  248. }
  249. } else {
  250. WechatAccountMenuDetail::where('menu_id', $id)->update(['status' => 0]);
  251. }
  252. if (!empty($delAppId)) {
  253. WechatAccountMenuDetail::whereIn('id', $delAppId)->update(['status' => 0]);
  254. }
  255. WechatMenu::query()->where('id', $id)->update($data);
  256. DB::commit();
  257. } catch (\Exception $exception) {
  258. self::throwErrMsg('操作失败');
  259. }
  260. myLog('wx-meun')->info(['c' => $createAddIp,'del' => $delAppId]);
  261. if (!empty($createAddIp)) {
  262. self::createWechatMenus($createAddIp,$id);
  263. }
  264. if (!empty($delAppId)) {
  265. self::delWechatAccountMenuByMeunId($delAppId,$id);
  266. }
  267. return ['msg' => "操作成功"];
  268. }
  269. /**
  270. * 列表
  271. * name: list
  272. * @param array $param
  273. * date 2023/07/12 07:43
  274. */
  275. public static function list(array $param)
  276. {
  277. $sql = self::getQuery($param)->orderBy('id', 'desc');
  278. $isAll = getProp($param, 'is_all', false);
  279. if ($isAll) {
  280. $list = $sql->get();
  281. } else {
  282. $list = $sql->paginate(getProp($param, 'limit', 10));
  283. }
  284. return $list;
  285. }
  286. private static function getQuery(array $param)
  287. {
  288. $sql = WechatMenu::query()->where('is_del', 0);
  289. if (getProp($param, 'puser_id')) {
  290. $sql->where('puser_id', $param['puser_id']);
  291. }
  292. if (getProp($param, 'user_id')) {
  293. $sql->where('user_id', $param['user_id']);
  294. }
  295. if (getProp($param, 'keyword')) {
  296. $sql->where('keyword', "like", "%" . $param['keyword'] . "%");
  297. }
  298. if (getProp($param, 'miniprogram_id')) {
  299. $sql->where('miniprogram_id', $param['miniprogram_id']);
  300. }
  301. if (getProp($param, 'wechat_authorization_info_id')) {
  302. $sql->whereJsonContains('wechat_accounts->id', $param['wechat_authorization_info_id']);
  303. }
  304. return $sql;
  305. }
  306. private static function delWechatAccountMenuByMeunId(array $delAppId, $menuId)
  307. {
  308. $info = WechatAccountMenuDetail::query()->whereIn('wechat_authorization_info_id', $delAppId)->where('menu_id',$menuId)->get();
  309. foreach ($info as $val) {
  310. if ($val->wx_menuid) {
  311. // 有菜单则删除菜单
  312. $app = self::getofficialAccount($info->wechat_authorization_info_id);
  313. $app->menu->delete($val->wx_menuid);
  314. }
  315. WechatAccountMenuDetail::query()->where('wechat_authorization_info_id', $val->id)->update(['wx_menuid' => ""]);
  316. }
  317. return "操作成功";
  318. }
  319. }