WechatMenuService.php 12 KB

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