SImageController.php 17 KB

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