WechatMenuService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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\Common\Services\BaseService;
  11. use Modules\WechatPlatform\Models\WechatAccountMenuDetail;
  12. use Modules\WechatPlatform\Models\WechatMenu;
  13. use function PHPUnit\Framework\isNan;
  14. class WechatMenuService extends BaseService
  15. {
  16. /**
  17. * 添加菜单
  18. * name: addMenu
  19. * @param $param
  20. * date 2023/07/11 11:38
  21. */
  22. public static function addMenu($param)
  23. {
  24. $res = WechatMenu::create($param);
  25. if ($res) {
  26. return ['msg' => '操作成功'];
  27. }
  28. self::throwErrMsg('操作失败');
  29. }
  30. /**
  31. * 更新菜单
  32. * name: updateMenu
  33. * @param $id
  34. * @param $param
  35. * @return string[]
  36. * date 2023/07/12 07:17
  37. */
  38. public static function updateMenu($id, $param)
  39. {
  40. $info = WechatMenu::where('id', $id)->where('is_del', 0)->first();
  41. if (is_empty($info)) {
  42. self::throwErrMsg('该菜单不存在或已删除');
  43. }
  44. $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('id')->toArray();
  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. self::createWechatMenus($appIds);
  54. return ['msg' => '操作成功'];
  55. }
  56. /**
  57. * 创建微信菜单
  58. * name: createWechatMenus
  59. * @param array $appIds
  60. * @return bool
  61. * date 2023/07/12 07:16
  62. */
  63. private static function createWechatMenus(array $appIds)
  64. {
  65. if (empty($appIds)) {
  66. return true;
  67. }
  68. $info = WechatAccountMenuDetail::query()->whereIn('id', $appIds)->get();
  69. foreach ($info as $val) {
  70. }
  71. return true;
  72. }
  73. /**
  74. * 菜单详情
  75. * name: detail
  76. * @param $id
  77. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  78. * date 2023/07/12 07:17
  79. */
  80. public static function detail($id)
  81. {
  82. return WechatMenu::query()->where('id', $id)->first();
  83. }
  84. /**
  85. * 公众号列表
  86. * name: authList
  87. * @param $id
  88. * @param $userId
  89. * @return mixed
  90. * date 2023/07/12 07:16
  91. */
  92. public static function authList($id, $userId)
  93. {
  94. $list = DB::table('wechat_authorization_infos')
  95. ->where('user_id', $userId)
  96. ->select('id', 'nick_name', 'is_enabled')->get();
  97. $authList = WechatAccountMenuDetail::where('menu_id', $id)->get();
  98. if (!$list->isEmpty()) {
  99. foreach ($list as $val) {
  100. $val->is_auth = 0;
  101. $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
  102. if (getProp($info, 'status') == 1) {
  103. $val->is_auth = 1;
  104. }
  105. if ($val->is_enabled != 1) {
  106. $val->nick_name .= "(取消授权)";
  107. }
  108. unset($val->is_enabled);
  109. }
  110. }
  111. return $list;
  112. }
  113. /**
  114. * 删除
  115. * name: del
  116. * @param $ids
  117. * @return string[]
  118. * date 2023/07/12 07:14
  119. */
  120. public static function del($ids)
  121. {
  122. WechatMenu::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);
  123. $applyIds = WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->where('status', 1)->pluck('id')->toArray();
  124. if ($applyIds) {
  125. WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->update(['status' => 0]);
  126. // 删除菜单
  127. self::delWechatAccountMenu($applyIds);
  128. }
  129. return ['msg' => "操作成功"];
  130. }
  131. private static function delWechatAccountMenu(array $applyIds)
  132. {
  133. $info = WechatAccountMenuDetail::query()->whereIn('id', $applyIds)->get();
  134. foreach ($info as $val) {
  135. }
  136. return "操作成功";
  137. }
  138. /**
  139. * 分配公众号
  140. * name: allocation
  141. * @param $id
  142. * @param array $wxAuthIds
  143. * date 2023/07/12 07:20
  144. */
  145. public static function allocation($id, $wxAuthIds)
  146. {
  147. $info = WechatMenu::query()->where('id', $id)->where('is_del', 0)->first();
  148. if (is_empty($info)) {
  149. self::throwErrMsg('该菜单不存在或已删除');
  150. }
  151. // 已分配的
  152. $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('id')->toArray();
  153. $list = [];
  154. $delAppId = [];
  155. $createAddIp = [];
  156. if (empty($wxAuthIds)) {
  157. $data['wechat_accounts'] = [];
  158. $delAppId = $appIds;
  159. } else {
  160. $wechatAccountInfos = DB::table('wechat_authorization_infos')
  161. ->whereIn('id', $wxAuthIds)
  162. ->where('is_enabled', 1)
  163. ->where('user_id', $info->user_id)
  164. ->get();
  165. if ($wechatAccountInfos->isEmpty()) {
  166. self::throwErrMsg("优化师对提交的公众号没有使用权限");
  167. }
  168. $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
  169. $canNotUsed = array_diff($wxAuthIds, $canNotUsed);
  170. if (count($canNotUsed) > 0) {
  171. self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
  172. }
  173. foreach ($appIds as $val){
  174. if (!in_array($val,$wxAuthIds)){
  175. $delAppId[] = $val;
  176. }
  177. }
  178. // $allSet = WechatAccountMenuDetail::query()->where('user_id', $info->user_id)->get();
  179. foreach ($wechatAccountInfos as $val) {
  180. if (!in_array($val->id,$appIds)){
  181. $createAddIp[] = $val->id;
  182. }
  183. $data['wechat_accounts'][] = [
  184. 'id' => $val->id,
  185. 'appid' => $val->authorizer_appid,
  186. 'nick_name' => $val->nick_name,
  187. 'component_appid' => $val->component_appid
  188. ];
  189. $appIds[] = $val->authorizer_appid;
  190. $list[] = [
  191. 'menu_id' => $info->id,
  192. 'user_id' => $info->user_id,
  193. 'puser_id' => $info->puser_id,
  194. 'miniprogram_id' => $info->miniprogram_id,
  195. 'appid' => $val->authorizer_appid,
  196. 'wechat_authorization_info_id' => $val->id,
  197. 'nick_name' => $val->nick_name,
  198. 'content' => $info->content,
  199. 'msg_content' => $info->msg_content,
  200. 'status' => 1,
  201. ];
  202. }
  203. unset($wechatAccountInfos,$appIds,$val,$canNotUsed);
  204. }
  205. DB::beginTransaction();
  206. try {
  207. if (!empty($list)){
  208. foreach ($list as $val){
  209. WechatAccountMenuDetail::updateOrCreate(
  210. [
  211. 'menu_id' => $val['menu_id'],
  212. 'miniprogram_id' => $val['miniprogram_id'],
  213. 'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
  214. ],$val);
  215. }
  216. }else{
  217. WechatAccountMenuDetail::where('menu_id',$id)->update(['status' => 0]);
  218. }
  219. if(!empty($delAppId)){
  220. WechatAccountMenuDetail::whereIn('id',$delAppId)->update(['status' => 0]);
  221. }
  222. WechatMenu::query()->where('id',$id)->update($data);
  223. DB::commit();
  224. }catch (\Exception $exception){
  225. self::throwErrMsg('操作失败');
  226. }
  227. if (!empty($createAddIp)){
  228. self::createWechatMenus($createAddIp);
  229. }
  230. if(!empty($delAppId)){
  231. self::delWechatAccountMenu($delAppId);
  232. }
  233. return ['msg' => "操作成功"];
  234. }
  235. /**
  236. * 列表
  237. * name: list
  238. * @param array $param
  239. * date 2023/07/12 07:43
  240. */
  241. public static function list(array $param)
  242. {
  243. $sql = self::getQuery($param)->orderBy('id','desc');
  244. $isAll = getProp($param,'is_all',false);
  245. if($isAll){
  246. $list = $sql->get();
  247. }else{
  248. $list = $sql->paginate(getProp($param,'limit',10));
  249. }
  250. return $list;
  251. }
  252. private static function getQuery(array $param)
  253. {
  254. $sql = WechatMenu::query()->where('is_del',0);
  255. if (getProp($param, 'puser_id')) {
  256. $sql->where('puser_id', $param['puser_id']);
  257. }
  258. if (getProp($param, 'user_id')) {
  259. $sql->where('user_id', $param['user_id']);
  260. }
  261. if (getProp($param, 'keyword')) {
  262. $sql->where('keyword', "like", "%" . $param['keyword'] . "%");
  263. }
  264. if(getProp($param,'miniprogram_id')){
  265. $sql->where('miniprogram_id', $param['miniprogram_id']);
  266. }
  267. if(getProp($param,'wechat_authorization_info_id')){
  268. $sql->whereJsonContains('wechat_accounts->id',$param['wechat_authorization_info_id']);
  269. }
  270. return $sql;
  271. }
  272. }