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); $tags = \json_decode($obj->tags, true); $tag_arr = $this->strTagArr($tags); $obj->tags_arr = $tag_arr; $obj->gzh_names = $gzhInfo->pluck('nick_name')->toArray(); } return $obj; } private function strTagArr($tags) { $tagFields = [ 't1' => [ 'attention_hour' => '关注时间(小时)', 'in_48_hour' => '仅48小时内和公众号有互动的粉丝才能收到', 'interact_hour' => '上次互动时间(小时)', 'last_watch_day' => '最近观看时间(天)', 'register_day' => '用户注册日期(天)', 'total_watch_day' => '累计观看天数', 'video_watch' => '历史观看短剧', 'video_charge' => '充值过短剧', ], 't2' => [ 'charge_type' => '付费类型', 'total_charge_money' => '累计充值金额', 'avg_charge_money' => '平均充值金额', 'charge_num' => '充值次数', 'remain_coin' => '看币余额', 'last_charge_day' => '上次充值日期', ] ]; $t1 = []; $t2 = []; foreach ($tags as $key => $val) { if($val) { $valStr = $this->strVal($key, $val); if(in_array($key, array_keys($tagFields['t1']))) { $t1[] = $tagFields['t1'][$key] . ':'. $valStr; } else { $t2[] = $tagFields['t2'][$key]. ':'. $valStr; } } } return [ 'active' => $t1, 'pay' => $t2 ]; } private function strVal($key, $val) { if('charge_type' == $key) { return ['1' => '未付费', '2' => '待支付', '3' => '已付费', '4' => 'VIP用户'][$val] ?? ''; } if(in_array($key, ['video_watch', 'video_charge'])) { return DB::table('videos') ->whereIn('id', $val) ->select('name')->get()->pluck('name')->join(', '); } if('in_48_hour' == $key) { return ['1' => '是', '2' => '否'][$val] ?? ''; } $arr = explode('-', $val); if($arr[0] == $arr[1]) { return $arr[0]; } if($arr[1] == 0) { return $arr[0] . '-不限'; } return $val; } /** * 添加用户分群 * @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' ]); $data = [ 'name' => $request->input('name'), 'uid' => $this->getLoginUserId(), 'tags' => \json_encode($request->input('tags', []), JSON_UNESCAPED_UNICODE), ]; if($request->input('remark')) { $data['remark'] = $request->integer('remark'); } $model = UserGroupModel::create($data); 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); } if(time() - 60 > $request->integer('timestamp')) { CommonBusinessException::throwError(Errors::OPENPLATFORM_UG_SIGN_TIMESTAMP_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 ]; } }