input('name'); return DB::table('user_groups') ->when($name, function ($query, $name){ return $query->where('name', 'like', '%'.$name.'%'); })->where([ 'uid' => $this->getLoginUserId(), 'is_enabled' => 1 ])->orderBy('id', 'desc') ->paginate($request->input('limit', 20)); } /** * 详情 * @param Request $request * @throws \Illuminate\Validation\ValidationException */ public function detail(Request $request) { $this->validate($request, ['id' => 'required']); $uid = $this->getLoginUserId(); $obj = DB::table('user_groups') ->where([ 'uid' => $uid, 'is_enabled' => 1, 'id' => $request->input('id') ])->first(); if($obj) { $gzhInfo = DB::table('gzh_ug_maps as a') ->join('wechat_authorization_infos as b', function($query) use ($uid) { $query->on('a.gzh_id', '=', 'b.id') ->where([ 'b.is_enabled' => 1, 'b.user_id' => $uid ]); })->where([ 'a.ug_id' => $request->input('id') ])->select('b.nick_name', 'b.authorizer_appid')->get(); $tags = \json_decode($obj->tags, true); $tags['gzh_appids'] = $gzhInfo->pluck('authorizer_appid')->toArray(); $tags['in_48_hour'] = 1; $uids = UserGroupService::getSQL($tags)->distinct()->select('a.uid')->get()->pluck('uid')->toArray(); $uids = UserGroupService::dealHistoryReadBooks(UserGroupService::dealPayVideo($uids, $tags), $tags); $obj->user_num = count($uids); $obj->tags_arr = $tags; $obj->gzh_names = $gzhInfo->pluck('nick_name')->toArray(); } return $obj; } /** * 添加用户分群 * @param Request $request * @return string * @throws \Illuminate\Validation\ValidationException */ public function add(Request $request){ $this->validate($request, [ 'name' => 'required|string|max:64', 'gzh_ids' => 'required|array', 'tags' => 'required|array', 'remark' => 'nullable|string|max:140' ]); $model = UserGroupModel::create([ 'name' => $request->input('name'), 'uid' => $this->getLoginUserId(), 'tags' => \json_encode($request->input('tags', []), JSON_UNESCAPED_UNICODE), 'remark' => $request->input('remark', '') ]); foreach ($request->input('gzh_ids') as $gzh_id) { GzhUgMapModel::create([ 'ug_id' => $model->id, 'gzh_id' => $gzh_id ]); } return 'ok'; } /** * 批量删除 * @param Request $request * @return string * @throws \Illuminate\Validation\ValidationException */ public function delete(Request $request) { $this->validate($request, ['ids' => 'required|array']); foreach ($request->input('ids') as $id) { $model = UserGroupModel::where([ 'uid' => $this->getLoginUserId(), 'id' => $id, 'is_enabled' => 1, ])->first(); if($model) { $model->is_enabled = 0; $model->save(); GzhUgMapModel::where([ 'ug_id' => $model->id, 'is_enabled' => 1 ])->update([ 'is_enabled' => 0 ]); } } return 'ok'; } /** * 获取符合条件的用户分群列表 * @param Request $request */ public function listUser(Request $request) { $this->validate($request, [ 'timestamp' => 'required', 'sign' => 'required' ]); $signKey = config('audience.ug.signKey'); if($request->input('sign') != md5( $signKey.$request->input('timestamp') )) { CommonBusinessException::throwError(Errors::OPENPLATFORM_UG_SIGN_ERROR); } $tags = DB::table('user_groups') ->where([ ['id' , '=', $request->input('ugId')], ['is_enabled', '=', 1] ])->value('tags'); if(!$tags) { CommonBusinessException::throwError(Errors::OPENPLATFORM_UG_NOT_EXISTS); } $tagsArr = \json_decode($tags, true); $authorizer_appid = DB::table('wechat_authorization_infos') ->where([ ['id', '=', $request->input('gzhId')], ['is_enabled', '=', 1] ])->value('authorizer_appid'); if(!$authorizer_appid) { CommonBusinessException::throwError(Errors::OPENPLATFORM_GZH_SHOUQUAN_ERROR); } $tagsArr['gzh_appids'] = [$authorizer_appid]; $sql = UserGroupService::getSQL($tagsArr); $res = $sql->distinct()->select('b.uid', 'b.mp_openid') ->get(); $uids = $res->pluck('uid')->toArray(); if(count($uids)) { $uids = UserGroupService::dealHistoryReadBooks(UserGroupService::dealPayVideo($uids, $tags), $tags); } $openid = []; foreach ($res as $item) { if(in_array($item->uid, $uids)) { $openid[] = $item->mp_openid; } } return [ 'openid' => $openid ]; } }