WechatMenuService.php 11 KB

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