SImageController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. namespace App\Http\Controllers\SImage;
  3. use App\Handlers\ImageUploadHandler;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Image;
  6. use App\Models\ImageFolder;
  7. use App\Models\TravelImage\TravelFaceInfo;
  8. use App\Models\TravelImage\TravelFaceName;
  9. use App\Models\TravelImage\TravelImage;
  10. use App\Models\TravelImage\TravelImageClass;
  11. use App\Models\TravelImage\TravelImageFolder;
  12. use App\Models\TravelImage\TravelInfo;
  13. use App\Models\TravelImage\TravelUser;
  14. use App\Models\User;
  15. use GuzzleHttp\Client;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. class SImageController extends Controller
  21. {
  22. /**时光相册**/
  23. public function TravelImageList(Request $request)
  24. {
  25. $input = $request->all();
  26. $page_size = $input['page_size'];
  27. $page_index = $input['page_index'];
  28. $num = $page_size * ($page_index - 1);
  29. // $array=TravelUser::pluck('travel_id')->toArray();//报名的情况
  30. $array = [1];
  31. $image = TravelImageClass::where('is_deleted', 0)->whereIn('travel_id', $array)->with(['get_image' => function ($q) use ($array, $type) {
  32. $q->where('is_cover', 1)->orderByDesc('created_at')->whereIn('travel_id', $array); //只展示用户报名的赛季
  33. }, 'get_folder' => function ($q) use ($array, $type) {
  34. $q->orderByDesc('created_at')->whereIn('travel_id', $array); //只展示用户报名的赛季
  35. }])->orderByDesc('travel_id')->orderBy('img_type');
  36. $count = $image->get()->count();
  37. $image = $image->orderByDesc('created_at')->take($page_size)->skip($num)->get();
  38. $re = [];
  39. foreach ($image as $k => $v) {
  40. if ($v->img_type == 1) {
  41. $re[$k]['imgclass_name'] = $v->imgclass_name;
  42. $re[$k]['created_at'] = $v->created_at->toDateTimeString();
  43. $re[$k]['count'] = $v->count;
  44. $re[$k]['id'] = $v->id;
  45. $re[$k]['op_name'] = $v->op_name;
  46. $re[$k]['images'] = $v->get_image;
  47. $re[$k]['img_type'] = $v->img_type;
  48. $re[$k]['travel_id'] = $v->travel_id;
  49. $re[$k]['is_old'] = true;
  50. } else {
  51. $re[$k]['imgclass_name'] = $v->imgclass_name;
  52. $re[$k]['created_at'] = $v->created_at->toDateTimeString();
  53. $re[$k]['count'] = $v->count;
  54. $re[$k]['id'] = $v->id;
  55. $re[$k]['op_name'] = $v->op_name;
  56. $re[$k]['images'] = $v->get_image;
  57. $re[$k]['img_type'] = $v->img_type;
  58. $re[$k]['travel_id'] = $v->travel_id;
  59. $re[$k]['is_old'] = true;
  60. }
  61. }
  62. return $this->success_list($re, '', $count);
  63. }
  64. /**查看所有*/
  65. public function GetTravelImgDetail(Request $request)
  66. {
  67. $input = $request->all();
  68. $page_size = $input['page_size'];
  69. $page_index = $input['page_index'];
  70. $num = $page_size * ($page_index - 1);
  71. // $user_id=Auth::user()->id;
  72. // $res=TravelUser::where('dwbs_id',$user_id)->first();
  73. // if (!$res){
  74. // return $this->error('500214','','你没有权限查看');
  75. // }
  76. // $ids=Like::where('user_id',Auth::user()->id)->where('activity_id',$request->activity_id)->pluck('image_id')->toArray();
  77. $images = TravelImage::where('class_id', $request->id);
  78. $count = $images->get()->count();
  79. $images = $images->orderByDesc('id')->take($page_size)->skip($num)->get();
  80. return $this->success_list($images, '', $count);
  81. }
  82. public function get_folder(Request $request)
  83. {
  84. $id = $request->input('id', 1);
  85. $type = $request->input('type') ?? 0;
  86. if ($type == 0) {
  87. $re = ImageFolder::orderByDesc('total')
  88. // ->where('total','>',0)
  89. ->where('activity_id', $id)->OrderBy('id', 'desc')->get();
  90. foreach ($re as $k => $v) {
  91. $re[$k]['cover'] = Image::where('img_folder_id', $v->id)->whereNotNull('thumbnail')->limit(1)->value('thumbnail');
  92. }
  93. } else {
  94. $re = Image::where('img_type', 1)->where('activity_id', $id)->get();
  95. }
  96. return $this->success_list($re);
  97. }
  98. public function get_img_detail(Request $request)
  99. {
  100. $input = $request->all();
  101. $page_size = $input['page_size'];
  102. $page_index = $input['page_index'];
  103. $num = $page_size * ($page_index - 1);
  104. $id = $request->input('id');
  105. $images = Image::where('img_folder_id', $id);
  106. $count = $images->get()->count();
  107. $re = $images->select('img_url', 'file_name', 'url', 'id')->orderByDesc('id')->take($page_size)->skip($num)->get();
  108. foreach ($re as $k => $v) {
  109. // $re[$k]->img_url=$re[$k]->img_url.'?imageView2/0/q/20';
  110. $re[$k]->url = $re[$k]->img_url . '?imageMogr2/thumbnail/!10p';
  111. // $re[$k]->img_url=$re[$k]->img_url.'?imageMogr2/thumbnail/999999@';
  112. // $re[$k]->img_url=$re[$k]->img_url.'?imageMogr2/thumbnail/!40p';
  113. }
  114. return $this->success_list($re, '', $count);
  115. }
  116. /*
  117. * 获取openid
  118. */
  119. public function getUserOpenid($Code)
  120. {
  121. $client = new Client();
  122. $AppId = config('wechat.official_account.default.app_id');
  123. $Secret = config('wechat.official_account.default.secret');
  124. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $AppId . '&secret=' . $Secret . '&code=' . $Code . '&grant_type=authorization_code';
  125. $data = $client->request('get', $url);
  126. $da = $data->getBody()->getContents();
  127. $da = json_decode($da, true);
  128. $da = (array)$da;
  129. return $da;
  130. }
  131. public function wx_Login(Request $request)
  132. {
  133. $Code = $request->code;
  134. $data = $this->getUserOpenid($Code);
  135. if (isset($data['openid'])) {
  136. $user = User::where('openid', $data['openid'])->first();
  137. if ($user) {
  138. $id = TravelUser::where('id', '>', 0)->pluck('dwbs_id')->toArray();
  139. if (!in_array($user->id, $id)) {
  140. return $this->error(500214, '', '你没有权限访问!');
  141. }
  142. $token = Auth::guard('api')->fromUser($user);
  143. $da['token'] = $token;
  144. $da['openid'] = $data['openid'];
  145. return $this->success($da);
  146. }
  147. return $this->error(450001, '', '请先登录个人中心');
  148. } else {
  149. return $this->error(450001, $data, '你没有权限访问');
  150. }
  151. }
  152. /***账号密码登录**/
  153. public function Login(Request $request)
  154. {
  155. $mobile = $request->input('mobile');
  156. $password = $request->input('password');
  157. $user = User::where('mobile', $mobile)->first();
  158. if (!$user) {
  159. return $this->error(50014, '', '账号不存在');
  160. }
  161. if (!$password) {
  162. return $this->error(500014, '', '账号密码不正确');
  163. }
  164. if ($user->password != md5($password)) {
  165. return $this->error(500014, '', '账号密码不正确');
  166. }
  167. $token = Auth::guard('api')->fromUser($user);
  168. return $this->success(['token' => $token]);
  169. }
  170. public function szyImg()
  171. {
  172. return view('szyImg');
  173. }
  174. /**获取每次活动的相册**/
  175. public function GetTravelList(Request $request)
  176. {
  177. $input = $request->all();
  178. $page_size = $input['page_size'];
  179. $page_index = $input['page_index'];
  180. $num = $page_size * ($page_index - 1);
  181. $id = Auth::user()->id;
  182. $re = \DB::table('image_user')->where('dwbs_id', $id)->first();
  183. if ($re) {
  184. $res = DB::table('image_user')->where('dwbs_id', $id)->orderByDesc('id')->groupBy('travel_id')->get();
  185. $data = [];
  186. $time = [
  187. '77' => ['time_start' => '2025-02-18 08:00:00', 'end_time' => '2025-02-19 22:00:00','name'=>'大卫博士实战营'],
  188. '76' => ['time_start' => '2024-11-19 08:00:00', 'end_time' => '2024-11-20 22:00:00','name'=>'大卫博士实战营'],
  189. '75' => ['time_start' => '2024-10-23 08:00:00', 'end_time' => '2024-10-24 22:00:00','name'=>'大卫博士实战营'],
  190. '74' => ['time_start' => '2024-10-09 08:00:00', 'end_time' => '2024-10-10 22:00:00','name'=>'大卫博士实战营'],
  191. '73' => ['time_start' => '2024-09-25 08:00:00', 'end_time' => '2024-09-26 22:00:00','name'=>'爱丫丫团队领袖密训营'],
  192. '72' => ['time_start' => '2024-08-13 08:00:00', 'end_time' => '2024-08-14 22:00:00','name'=>'大卫博士实战营'],
  193. '71' => ['time_start' => '2024-07-10 08:00:00', 'end_time' => '2024-07-11 22:00:00','name'=>'大卫博士实战营'],
  194. '70' => ['time_start' => '2024-05-15 08:00:00', 'end_time' => '2024-05-16 22:00:00','name'=>'大卫博士实战营'],
  195. '69' => ['time_start' => '2024-03-05 08:00:00', 'end_time' => '2024-03-06 22:00:00','name'=>'大卫博士实战营'],
  196. '68' => ['time_start' => '2023-12-27 08:00:00', 'end_time' => '2023-12-28 22:00:00','name'=>'大卫博士实战营'],
  197. '67' => ['time_start' => '2023-10-24 08:00:00', 'end_time' => '2023-10-25 22:00:00','name'=>'大卫博士实战营'],
  198. ];
  199. foreach ($res as $kk => $v) {
  200. $get_img = Image::where('img_type', 0)->where('activity_id', $v->travel_id)->whereNotNull('thumbnail')->limit(8)->select('thumbnail')->get();
  201. $season = ImageFolder::where('activity_id', $v->travel_id)->value('season');
  202. $data[] = [
  203. 'cover_url' => null,
  204. 'time_start' => $time[$v->travel_id]['time_start'],
  205. 'time_end' => $time[$v->travel_id]['end_time'],
  206. 'travel_name' => '第' . $season . '届'. $time[$v->travel_id]['name'],
  207. // 'travel_name' => '第' . $season . '届大卫博士实战营',
  208. 'id' => $v->travel_id,
  209. 'get_img' => $get_img,
  210. ];
  211. }
  212. } else {
  213. $data[] = [];
  214. }
  215. return $this->success_list($data, '', 1);
  216. }
  217. /**上传相片**/
  218. public function UploadImg(Request $request, ImageUploadHandler $handler)
  219. {
  220. return $this->error(50214, '', '暂停使用');
  221. $travel_id = $request->input('travel_id');
  222. if (!$request->file('file')) {
  223. return $this->error(50214, '', '请使用正确的参数');
  224. }
  225. $re = $handler->save($request->file('file'), 'headImg', Auth::user()->id);
  226. if (isset($re['path'])) {
  227. $res = $this->GetFaceInfo($travel_id, $re['path']);
  228. if (isset($res['group_id'])) {
  229. $face = TravelUser::where('dwbs_id', Auth::user()->id)->first();
  230. $face->img_url = $re['path'];
  231. $face->group_id = $res['group_id'];
  232. $face->save();
  233. return $this->success(['group_id' => $res['group_id']]);
  234. }
  235. return $this->error($res['code'], '', $res['msg']);
  236. }
  237. }
  238. public function GetImg(Request $request)
  239. {
  240. return $this->success_list([], '', 0);
  241. $travel_id = $request->input('travel_id');
  242. $re = TravelUser::where('travel_id', $travel_id)->where('dwbs_id', Auth::user()->id)->first();
  243. $avatar = '';
  244. $group_id = '';
  245. $user = ['nickname' => Auth::user()->remark_name, 'avatar' => Auth::user()->headimgurl, 'mobile' => Auth::user()->mobile];
  246. if ($re) {
  247. if (is_numeric($re->group_id)) {
  248. return $this->success(['url' => $re->img_url, 'group_id' => $re->group_id, 'user' => $user]);
  249. }
  250. }
  251. return $this->success(['url' => $avatar, 'group_id' => $group_id, 'user' => $user]);
  252. }
  253. public function GetUserImage(Request $request)
  254. {
  255. return $this->success_list([], '', 0);
  256. $page_index = $request->input('page_index');
  257. $page_size = $request->input('page_size');
  258. $group_id = $request->input('group_id');
  259. $travel_id = $request->input('travel_id');
  260. $ids = TravelFaceInfo::where('groupid', $group_id)->where('travel_id', $travel_id)->groupBy('img_id')->pluck('img_id');
  261. $count = count($ids);
  262. $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();
  263. return $this->success_list($re, '', $count);
  264. }
  265. public function GetFaceInfo($travel_id, $url_img)
  266. {
  267. try {
  268. $re = TravelFaceName::where('travel_id', $travel_id)->first();
  269. $msg = '成功';
  270. $code = 200;
  271. if (!$re) {
  272. $msg = '暂未开启相册聚类';
  273. $code = 3000;
  274. return ['msg' => $msg, 'code' => $code];
  275. }
  276. $facename = $re->face_name;
  277. $appkey = config('faceplus.api_key');
  278. $appsecret = config('faceplus.api_secret');
  279. $url = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/addimage';
  280. $client = new Client();
  281. $array = [
  282. 'form_params' => [
  283. 'api_secret' => $appsecret,
  284. 'api_key' => $appkey,
  285. 'facealbum_token' => $facename,
  286. 'image_url' => $url_img
  287. ]
  288. ];
  289. try {
  290. $re = $client->post($url, $array);
  291. $res = $re->getBody()->getContents();
  292. $ress = json_decode($res, true);
  293. } catch (\Exception $exception) {
  294. $error = $exception->getMessage();
  295. $msg = '识别失败,请重新上传';
  296. $code = 30001;
  297. return ['msg' => $msg, 'code' => $code];
  298. }
  299. $face_token = $ress['faces'][0]['face_token'];
  300. $url1 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupface';
  301. $client1 = new Client();
  302. $array1 = [
  303. 'form_params' => [
  304. 'api_secret' => $appsecret,
  305. 'api_key' => $appkey,
  306. 'facealbum_token' => $facename,
  307. ]
  308. ];
  309. $re1 = $client1->post($url1, $array1);
  310. $res1 = $re1->getBody()->getContents();
  311. $ress1 = json_decode($res1, true);
  312. $task_id = $ress1['task_id'];
  313. sleep(5);
  314. $url2 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupfacetaskquery';
  315. $client2 = new Client();
  316. $array2 = [
  317. 'form_params' => [
  318. 'api_secret' => $appsecret,
  319. 'api_key' => $appkey,
  320. 'task_id' => $task_id,
  321. ]
  322. ];
  323. $facess = $client2->post($url2, $array2);
  324. $face = $facess->getBody()->getContents();
  325. $faces = json_decode($face, true);
  326. if ($faces['status'] == 0) {
  327. sleep(4);
  328. }
  329. if ($faces['status'] == 1) {
  330. $url2 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupfacetaskquery';
  331. $client2 = new Client();
  332. $array2 = [
  333. 'form_params' => [
  334. 'api_secret' => $appsecret,
  335. 'api_key' => $appkey,
  336. 'task_id' => $task_id,
  337. ]
  338. ];
  339. $facess_1 = $client2->post($url2, $array2);
  340. $face_1 = $facess_1->getBody()->getContents();
  341. Log::error($face_1);
  342. $faces_1 = json_decode($face_1, true);
  343. }
  344. if (!isset($faces_1['group_result'])) {
  345. $msg = '分组失败,请上传新的照片';
  346. $code = 30002;
  347. }
  348. $arr = $faces_1['group_result'];
  349. foreach ($arr as $k => $v) {
  350. $s = TravelFaceInfo::where('face_token', $v['face_token'])->first();
  351. if ($s) {
  352. $s->groupid = $v['group_id'];
  353. }
  354. if ($v['face_token'] == $face_token) {
  355. $group_id = $v['group_id'];
  356. }
  357. }
  358. return ['group_id' => $group_id, 'msg' => $msg, 'code' => 200];
  359. } catch (\Exception $exception) {
  360. $msg = '识别失败,请上传新的照片';
  361. $code = 30002;
  362. return ['msg' => $msg, 'code' => $code];
  363. }
  364. }
  365. }