123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- namespace App\Http\Controllers\SImage;
- use App\Handlers\ImageUploadHandler;
- use App\Http\Controllers\Controller;
- use App\Models\Image;
- use App\Models\ImageFolder;
- use App\Models\TravelImage\TravelFaceInfo;
- use App\Models\TravelImage\TravelFaceName;
- use App\Models\TravelImage\TravelImage;
- use App\Models\TravelImage\TravelImageClass;
- use App\Models\TravelImage\TravelImageFolder;
- use App\Models\TravelImage\TravelInfo;
- use App\Models\TravelImage\TravelUser;
- use App\Models\User;
- use GuzzleHttp\Client;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class SImageController extends Controller
- {
- /**时光相册**/
- public function TravelImageList(Request $request)
- {
- $input = $request->all();
- $page_size = $input['page_size'];
- $page_index = $input['page_index'];
- $num = $page_size * ($page_index - 1);
- // $array=TravelUser::pluck('travel_id')->toArray();//报名的情况
- $array = [1];
- $image = TravelImageClass::where('is_deleted', 0)->whereIn('travel_id', $array)->with(['get_image' => function ($q) use ($array, $type) {
- $q->where('is_cover', 1)->orderByDesc('created_at')->whereIn('travel_id', $array); //只展示用户报名的赛季
- }, 'get_folder' => function ($q) use ($array, $type) {
- $q->orderByDesc('created_at')->whereIn('travel_id', $array); //只展示用户报名的赛季
- }])->orderByDesc('travel_id')->orderBy('img_type');
- $count = $image->get()->count();
- $image = $image->orderByDesc('created_at')->take($page_size)->skip($num)->get();
- $re = [];
- foreach ($image as $k => $v) {
- if ($v->img_type == 1) {
- $re[$k]['imgclass_name'] = $v->imgclass_name;
- $re[$k]['created_at'] = $v->created_at->toDateTimeString();
- $re[$k]['count'] = $v->count;
- $re[$k]['id'] = $v->id;
- $re[$k]['op_name'] = $v->op_name;
- $re[$k]['images'] = $v->get_image;
- $re[$k]['img_type'] = $v->img_type;
- $re[$k]['travel_id'] = $v->travel_id;
- $re[$k]['is_old'] = true;
- } else {
- $re[$k]['imgclass_name'] = $v->imgclass_name;
- $re[$k]['created_at'] = $v->created_at->toDateTimeString();
- $re[$k]['count'] = $v->count;
- $re[$k]['id'] = $v->id;
- $re[$k]['op_name'] = $v->op_name;
- $re[$k]['images'] = $v->get_image;
- $re[$k]['img_type'] = $v->img_type;
- $re[$k]['travel_id'] = $v->travel_id;
- $re[$k]['is_old'] = true;
- }
- }
- return $this->success_list($re, '', $count);
- }
- /**查看所有*/
- public function GetTravelImgDetail(Request $request)
- {
- $input = $request->all();
- $page_size = $input['page_size'];
- $page_index = $input['page_index'];
- $num = $page_size * ($page_index - 1);
- // $user_id=Auth::user()->id;
- // $res=TravelUser::where('dwbs_id',$user_id)->first();
- // if (!$res){
- // return $this->error('500214','','你没有权限查看');
- // }
- // $ids=Like::where('user_id',Auth::user()->id)->where('activity_id',$request->activity_id)->pluck('image_id')->toArray();
- $images = TravelImage::where('class_id', $request->id);
- $count = $images->get()->count();
- $images = $images->orderByDesc('id')->take($page_size)->skip($num)->get();
- return $this->success_list($images, '', $count);
- }
- public function get_folder(Request $request)
- {
- $id = $request->input('id', 1);
- $type = $request->input('type') ?? 0;
- if ($type == 0) {
- $re = ImageFolder::orderBy('sort')->where('activity_id', $id)->OrderBy('id', 'desc')->get();
- foreach ($re as $k => $v) {
- $re[$k]['cover'] = Image::where('img_folder_id', $v->id)->whereNotNull('thumbnail')->limit(1)->value('thumbnail');
- }
- } else {
- $re = Image::where('img_type', 1)->where('activity_id', $id)->get();
- }
- return $this->success_list($re);
- }
- public function get_img_detail(Request $request)
- {
- $input = $request->all();
- $page_size = $input['page_size'];
- $page_index = $input['page_index'];
- $num = $page_size * ($page_index - 1);
- $id = $request->input('id');
- $images = Image::where('img_folder_id', $id);
- $count = $images->get()->count();
- $re = $images->select('img_url', 'file_name', 'url', 'id')->orderByDesc('id')->take($page_size)->skip($num)->get();
- foreach ($re as $k => $v) {
- // $re[$k]->img_url=$re[$k]->img_url.'?imageView2/0/q/20';
- $re[$k]->url = $re[$k]->img_url . '?imageMogr2/thumbnail/!10p';
- // $re[$k]->img_url=$re[$k]->img_url.'?imageMogr2/thumbnail/999999@';
- // $re[$k]->img_url=$re[$k]->img_url.'?imageMogr2/thumbnail/!40p';
- }
- return $this->success_list($re, '', $count);
- }
- /*
- * 获取openid
- */
- public function getUserOpenid($Code)
- {
- $client = new Client();
- $AppId = config('wechat.official_account.default.app_id');
- $Secret = config('wechat.official_account.default.secret');
- $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $AppId . '&secret=' . $Secret . '&code=' . $Code . '&grant_type=authorization_code';
- $data = $client->request('get', $url);
- $da = $data->getBody()->getContents();
- $da = json_decode($da, true);
- $da = (array)$da;
- return $da;
- }
- public function wx_Login(Request $request)
- {
- $Code = $request->code;
- $data = $this->getUserOpenid($Code);
- if (isset($data['openid'])) {
- $user = User::where('openid', $data['openid'])->first();
- if ($user) {
- $id = TravelUser::where('id', '>', 0)->pluck('dwbs_id')->toArray();
- if (!in_array($user->id, $id)) {
- return $this->error(500214, '', '你没有权限访问!');
- }
- $token = Auth::guard('api')->fromUser($user);
- $da['token'] = $token;
- $da['openid'] = $data['openid'];
- return $this->success($da);
- }
- return $this->error(450001, '', '请先登录个人中心');
- } else {
- return $this->error(450001, $data, '你没有权限访问');
- }
- }
- /***账号密码登录**/
- public function Login(Request $request)
- {
- $mobile = $request->input('mobile');
- $password = $request->input('password');
- $user = User::where('mobile', $mobile)->first();
- if (!$user) {
- return $this->error(50014, '', '账号不存在');
- }
- if (!$password) {
- return $this->error(500014, '', '账号密码不正确');
- }
- if ($user->password != md5($password)) {
- return $this->error(500014, '', '账号密码不正确');
- }
- $token = Auth::guard('api')->fromUser($user);
- return $this->success(['token' => $token]);
- }
- public function szyImg()
- {
- return view('szyImg');
- }
- /**获取每次活动的相册**/
- public function GetTravelList(Request $request)
- {
- $input = $request->all();
- $page_size = $input['page_size'];
- $page_index = $input['page_index'];
- $num = $page_size * ($page_index - 1);
- $id = Auth::user()->id;
- $re = \DB::table('image_user')->where('dwbs_id', $id)->first();
- if ($re) {
- $res = DB::table('image_user')->where('dwbs_id', $id)->orderByDesc('id')->groupBy('travel_id')->get();
- $data = [];
- $time = [
- '76' => ['time_start' => '2024-11-19 08:00:00', 'end_time' => '2024-11-20 22:00:00','name'=>'大卫博士实战营'],
- '75' => ['time_start' => '2024-10-23 08:00:00', 'end_time' => '2024-10-24 22:00:00','name'=>'大卫博士实战营'],
- '74' => ['time_start' => '2024-10-09 08:00:00', 'end_time' => '2024-10-10 22:00:00','name'=>'大卫博士实战营'],
- '73' => ['time_start' => '2024-09-25 08:00:00', 'end_time' => '2024-09-26 22:00:00','name'=>'爱丫丫团队领袖密训营'],
- '72' => ['time_start' => '2024-08-13 08:00:00', 'end_time' => '2024-08-14 22:00:00','name'=>'大卫博士实战营'],
- '71' => ['time_start' => '2024-07-10 08:00:00', 'end_time' => '2024-07-11 22:00:00','name'=>'大卫博士实战营'],
- '70' => ['time_start' => '2024-05-15 08:00:00', 'end_time' => '2024-05-16 22:00:00','name'=>'大卫博士实战营'],
- '69' => ['time_start' => '2024-03-05 08:00:00', 'end_time' => '2024-03-06 22:00:00','name'=>'大卫博士实战营'],
- '68' => ['time_start' => '2023-12-27 08:00:00', 'end_time' => '2023-12-28 22:00:00','name'=>'大卫博士实战营'],
- '67' => ['time_start' => '2023-10-24 08:00:00', 'end_time' => '2023-10-25 22:00:00','name'=>'大卫博士实战营'],
- ];
- foreach ($res as $kk => $v) {
- $get_img = Image::where('img_type', 0)->where('activity_id', $v->travel_id)->whereNotNull('thumbnail')->limit(8)->select('thumbnail')->get();
- $season = ImageFolder::where('activity_id', $v->travel_id)->value('season');
- $data[] = [
- 'cover_url' => null,
- 'time_start' => $time[$v->travel_id]['time_start'],
- 'time_end' => $time[$v->travel_id]['end_time'],
- 'travel_name' => '第' . $season . '届'. $time[$v->travel_id]['name'],
- // 'travel_name' => '第' . $season . '届大卫博士实战营',
- 'id' => $v->travel_id,
- 'get_img' => $get_img,
- ];
- }
- } else {
- $data[] = [];
- }
- return $this->success_list($data, '', 1);
- }
- /**上传相片**/
- public function UploadImg(Request $request, ImageUploadHandler $handler)
- {
- return $this->error(50214, '', '暂停使用');
- $travel_id = $request->input('travel_id');
- if (!$request->file('file')) {
- return $this->error(50214, '', '请使用正确的参数');
- }
- $re = $handler->save($request->file('file'), 'headImg', Auth::user()->id);
- if (isset($re['path'])) {
- $res = $this->GetFaceInfo($travel_id, $re['path']);
- if (isset($res['group_id'])) {
- $face = TravelUser::where('dwbs_id', Auth::user()->id)->first();
- $face->img_url = $re['path'];
- $face->group_id = $res['group_id'];
- $face->save();
- return $this->success(['group_id' => $res['group_id']]);
- }
- return $this->error($res['code'], '', $res['msg']);
- }
- }
- public function GetImg(Request $request)
- {
- return $this->success_list([], '', 0);
- $travel_id = $request->input('travel_id');
- $re = TravelUser::where('travel_id', $travel_id)->where('dwbs_id', Auth::user()->id)->first();
- $avatar = '';
- $group_id = '';
- $user = ['nickname' => Auth::user()->remark_name, 'avatar' => Auth::user()->headimgurl, 'mobile' => Auth::user()->mobile];
- if ($re) {
- if (is_numeric($re->group_id)) {
- return $this->success(['url' => $re->img_url, 'group_id' => $re->group_id, 'user' => $user]);
- }
- }
- return $this->success(['url' => $avatar, 'group_id' => $group_id, 'user' => $user]);
- }
- public function GetUserImage(Request $request)
- {
- return $this->success_list([], '', 0);
- $page_index = $request->input('page_index');
- $page_size = $request->input('page_size');
- $group_id = $request->input('group_id');
- $travel_id = $request->input('travel_id');
- $ids = TravelFaceInfo::where('groupid', $group_id)->where('travel_id', $travel_id)->groupBy('img_id')->pluck('img_id');
- $count = count($ids);
- $re = TravelImage::whereIn('id', $ids)->select('id', 'url', 'download_url', 'file_name', 'thumbnail', 'img_url', 'url')->take($page_size)->skip(($page_index - 1) * $page_size)->orderBy('id')->get();
- return $this->success_list($re, '', $count);
- }
- public function GetFaceInfo($travel_id, $url_img)
- {
- try {
- $re = TravelFaceName::where('travel_id', $travel_id)->first();
- $msg = '成功';
- $code = 200;
- if (!$re) {
- $msg = '暂未开启相册聚类';
- $code = 3000;
- return ['msg' => $msg, 'code' => $code];
- }
- $facename = $re->face_name;
- $appkey = config('faceplus.api_key');
- $appsecret = config('faceplus.api_secret');
- $url = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/addimage';
- $client = new Client();
- $array = [
- 'form_params' => [
- 'api_secret' => $appsecret,
- 'api_key' => $appkey,
- 'facealbum_token' => $facename,
- 'image_url' => $url_img
- ]
- ];
- try {
- $re = $client->post($url, $array);
- $res = $re->getBody()->getContents();
- $ress = json_decode($res, true);
- } catch (\Exception $exception) {
- $error = $exception->getMessage();
- $msg = '识别失败,请重新上传';
- $code = 30001;
- return ['msg' => $msg, 'code' => $code];
- }
- $face_token = $ress['faces'][0]['face_token'];
- $url1 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupface';
- $client1 = new Client();
- $array1 = [
- 'form_params' => [
- 'api_secret' => $appsecret,
- 'api_key' => $appkey,
- 'facealbum_token' => $facename,
- ]
- ];
- $re1 = $client1->post($url1, $array1);
- $res1 = $re1->getBody()->getContents();
- $ress1 = json_decode($res1, true);
- $task_id = $ress1['task_id'];
- sleep(5);
- $url2 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupfacetaskquery';
- $client2 = new Client();
- $array2 = [
- 'form_params' => [
- 'api_secret' => $appsecret,
- 'api_key' => $appkey,
- 'task_id' => $task_id,
- ]
- ];
- $facess = $client2->post($url2, $array2);
- $face = $facess->getBody()->getContents();
- $faces = json_decode($face, true);
- if ($faces['status'] == 0) {
- sleep(4);
- }
- if ($faces['status'] == 1) {
- $url2 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupfacetaskquery';
- $client2 = new Client();
- $array2 = [
- 'form_params' => [
- 'api_secret' => $appsecret,
- 'api_key' => $appkey,
- 'task_id' => $task_id,
- ]
- ];
- $facess_1 = $client2->post($url2, $array2);
- $face_1 = $facess_1->getBody()->getContents();
- Log::error($face_1);
- $faces_1 = json_decode($face_1, true);
- }
- if (!isset($faces_1['group_result'])) {
- $msg = '分组失败,请上传新的照片';
- $code = 30002;
- }
- $arr = $faces_1['group_result'];
- foreach ($arr as $k => $v) {
- $s = TravelFaceInfo::where('face_token', $v['face_token'])->first();
- if ($s) {
- $s->groupid = $v['group_id'];
- }
- if ($v['face_token'] == $face_token) {
- $group_id = $v['group_id'];
- }
- }
- return ['group_id' => $group_id, 'msg' => $msg, 'code' => 200];
- } catch (\Exception $exception) {
- $msg = '识别失败,请上传新的照片';
- $code = 30002;
- return ['msg' => $msg, 'code' => $code];
- }
- }
- }
|