orderBy('id','desc'); $isAll = getProp($param,'is_all',false); if($isAll){ $list = $sql->get(); }else{ $list = $sql->paginate(getProp($param,'limit',10)); } $list->makeHidden(array_merge(self::$hideField,['content'])); return $list; } private static function getQuery(array $param) { $sql = WechatKeywords::query()->where('is_del',0); if (getProp($param, 'puser_id')) { $sql->where('puser_id', $param['puser_id']); } if (getProp($param, 'user_id')) { $sql->where('user_id', $param['user_id']); } if (getProp($param, 'keyword')) { $sql->where('keyword', "like", "%" . $param['keyword'] . "%"); } if(getProp($param,'wechat_authorization_info_id')){ $sql->whereJsonContains('wechat_accounts->id',$param['wechat_authorization_info_id']); } return $sql; } /** * 详情 * name: detail * @param $id * date 2023/07/06 13:55 */ public static function detail($id) { return WechatKeywords::where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField,['wechat_accounts'])); } /** * 保存关键字 * name: updateKeyWords * @param $id * @param mixed $param * @return string * date 2023/07/06 14:51 */ public static function updateKeyWords($id, mixed $param) { $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first(); if (is_empty($info)) { self::throwErrMsg('关键词不存在或已删除'); } if ($param['keyword'] != $info->keyword) { // 修改关键词,需要查询词关键词是否已配置 if ($info->wechat_accounts) { $appIds = WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->where('status', 1)->pluck('appid')->get(); if ($appIds) { $hasUsed = WechatAccountKeywordLog::where('keyword', $param['keyword'])->whereIn('appid', $appIds)->where('status', 1)->pluck('nick_name')->get(); if ($hasUsed) { self::throwErrMsg("关键字{$param['keyword']}已配置了以下公众号" . implode(',', $hasUsed) . "请先解绑后再编辑"); } } } } DB::beginTransaction(); try { $info->save($param); $data = [ 'content' => $param['send_content'], 'keyword' => $param['keyword'], ]; WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->update($data); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); self::throwErrMsg('编辑失败'); } return "操作成功"; } /** * 配置公众号列表 * name: WechaAccountAuthListInfo * @param $id * @param mixed $userId * date 2023/07/06 15:31 */ public static function WechaAccountAuthListInfo($id, mixed $userId) { $list = DB::table('wechat_authorization_infos') ->where('user_id', $userId) ->select('id', 'nick_name', 'is_enabled')->get(); $authList = WechatAccountKeywordLog::where('weacht_keyworld_id', $id)->get(); if (!$list->isEmpty()) { foreach ($list as $val) { $val->is_auth = 0; $info = $authList->where('wechat_authorization_info_id', $val->id)->first(); if (getProp($info, 'status') == 1) { $val->is_auth = 1; } if ($val->is_enabled != 1) { $val->nick_name .= "(取消授权)"; } unset($val->is_enabled); } } return $list; } /** * 删除关键词 * name: delKeywords * @param array $ids * date 2023/07/06 15:54 */ public static function delKeywords(array $ids) { if (empty($ids)) { self::throwErrMsg('要删除的数据不能为空'); } DB::beginTransaction(); try { WechatKeywords::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]); WechatAccountKeywordLog::query()->whereIn('weacht_keyworld_id', $ids)->update(['status' => 0]); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); self::throwErrMsg('删除失败'); } return "操作成功"; } public static function allocation($id, $wxAuthIds) { $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first(); if (is_empty($info)) { self::throwErrMsg('关键词不存在或已删除'); } $data = []; $list = []; if (empty($wxAuthIds)){ $data['wechat_accounts'] = []; }else{ $wechatAccountInfos = DB::table('wechat_authorization_infos') ->whereIn('id',$wxAuthIds) ->where('is_enabled',1) ->where('user_id',$info->user_id) ->get(); if($wechatAccountInfos->isEmpty()){ self::throwErrMsg("优化师对提交的公众号没有使用权限"); } $canNotUsed = $wechatAccountInfos->pluck('id')->toArray(); $canNotUsed = array_diff($wxAuthIds,$canNotUsed); if (count($canNotUsed) > 0){ self::throwErrMsg("优化师对id:为:".implode(',',$canNotUsed)."的公众号没有使用权限"); } foreach ($wechatAccountInfos as $val){ $data['wechat_accounts'][] = [ 'id' => $val->id, 'appid' =>$val->authorizer_appid, 'nick_name' => $val->nick_name, 'component_appid' => $val->component_appid ]; $list[] = [ 'weacht_keyworld_id' => $info->id, 'appid' => $val->authorizer_appid, 'wechat_authorization_info_id'=> $val->id, 'nick_name' => $val->nick_name,'keyword' => $info->keyword, 'content' => $info->send_content, 'status' => 1, ]; } } DB::beginTransaction(); try { if (!empty($list)){ foreach ($list as $val){ WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'],'wechat_authorization_info_id' => $val['wechat_authorization_info_id']],$val); } }else{ WechatAccountKeywordLog::where('weacht_keyworld_id',$id)->update(['status' => 0]); } WechatKeywords::where('id',$id)->update($data); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); self::throwErrMsg('操作失败'); } return "操作成功"; } }