SImageController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. '76' => ['time_start' => '2024-11-19 08:00:00', 'end_time' => '2024-11-20 22:00:00','name'=>'大卫博士实战营'],
  186. '75' => ['time_start' => '2024-10-23 08:00:00', 'end_time' => '2024-10-24 22:00:00','name'=>'大卫博士实战营'],
  187. '74' => ['time_start' => '2024-10-09 08:00:00', 'end_time' => '2024-10-10 22:00:00','name'=>'大卫博士实战营'],
  188. '73' => ['time_start' => '2024-09-25 08:00:00', 'end_time' => '2024-09-26 22:00:00','name'=>'爱丫丫团队领袖密训营'],
  189. '72' => ['time_start' => '2024-08-13 08:00:00', 'end_time' => '2024-08-14 22:00:00','name'=>'大卫博士实战营'],
  190. '71' => ['time_start' => '2024-07-10 08:00:00', 'end_time' => '2024-07-11 22:00:00','name'=>'大卫博士实战营'],
  191. '70' => ['time_start' => '2024-05-15 08:00:00', 'end_time' => '2024-05-16 22:00:00','name'=>'大卫博士实战营'],
  192. '69' => ['time_start' => '2024-03-05 08:00:00', 'end_time' => '2024-03-06 22:00:00','name'=>'大卫博士实战营'],
  193. '68' => ['time_start' => '2023-12-27 08:00:00', 'end_time' => '2023-12-28 22:00:00','name'=>'大卫博士实战营'],
  194. '67' => ['time_start' => '2023-10-24 08:00:00', 'end_time' => '2023-10-25 22:00:00','name'=>'大卫博士实战营'],
  195. ];
  196. foreach ($res as $kk => $v) {
  197. $get_img = Image::where('img_type', 0)->where('activity_id', $v->travel_id)->whereNotNull('thumbnail')->limit(8)->select('thumbnail')->get();
  198. $season = ImageFolder::where('activity_id', $v->travel_id)->value('season');
  199. $data[] = [
  200. 'cover_url' => null,
  201. 'time_start' => $time[$v->travel_id]['time_start'],
  202. 'time_end' => $time[$v->travel_id]['end_time'],
  203. 'travel_name' => '第' . $season . '届'. $time[$v->travel_id]['name'],
  204. // 'travel_name' => '第' . $season . '届大卫博士实战营',
  205. 'id' => $v->travel_id,
  206. 'get_img' => $get_img,
  207. ];
  208. }
  209. } else {
  210. $data[] = [];
  211. }
  212. return $this->success_list($data, '', 1);
  213. }
  214. /**上传相片**/
  215. public function UploadImg(Request $request, ImageUploadHandler $handler)
  216. {
  217. return $this->error(50214, '', '暂停使用');
  218. $travel_id = $request->input('travel_id');
  219. if (!$request->file('file')) {
  220. return $this->error(50214, '', '请使用正确的参数');
  221. }
  222. $re = $handler->save($request->file('file'), 'headImg', Auth::user()->id);
  223. if (isset($re['path'])) {
  224. $res = $this->GetFaceInfo($travel_id, $re['path']);
  225. if (isset($res['group_id'])) {
  226. $face = TravelUser::where('dwbs_id', Auth::user()->id)->first();
  227. $face->img_url = $re['path'];
  228. $face->group_id = $res['group_id'];
  229. $face->save();
  230. return $this->success(['group_id' => $res['group_id']]);
  231. }
  232. return $this->error($res['code'], '', $res['msg']);
  233. }
  234. }
  235. public function GetImg(Request $request)
  236. {
  237. return $this->success_list([], '', 0);
  238. $travel_id = $request->input('travel_id');
  239. $re = TravelUser::where('travel_id', $travel_id)->where('dwbs_id', Auth::user()->id)->first();
  240. $avatar = '';
  241. $group_id = '';
  242. $user = ['nickname' => Auth::user()->remark_name, 'avatar' => Auth::user()->headimgurl, 'mobile' => Auth::user()->mobile];
  243. if ($re) {
  244. if (is_numeric($re->group_id)) {
  245. return $this->success(['url' => $re->img_url, 'group_id' => $re->group_id, 'user' => $user]);
  246. }
  247. }
  248. return $this->success(['url' => $avatar, 'group_id' => $group_id, 'user' => $user]);
  249. }
  250. public function GetUserImage(Request $request)
  251. {
  252. return $this->success_list([], '', 0);
  253. $page_index = $request->input('page_index');
  254. $page_size = $request->input('page_size');
  255. $group_id = $request->input('group_id');
  256. $travel_id = $request->input('travel_id');
  257. $ids = TravelFaceInfo::where('groupid', $group_id)->where('travel_id', $travel_id)->groupBy('img_id')->pluck('img_id');
  258. $count = count($ids);
  259. $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();
  260. return $this->success_list($re, '', $count);
  261. }
  262. public function GetFaceInfo($travel_id, $url_img)
  263. {
  264. try {
  265. $re = TravelFaceName::where('travel_id', $travel_id)->first();
  266. $msg = '成功';
  267. $code = 200;
  268. if (!$re) {
  269. $msg = '暂未开启相册聚类';
  270. $code = 3000;
  271. return ['msg' => $msg, 'code' => $code];
  272. }
  273. $facename = $re->face_name;
  274. $appkey = config('faceplus.api_key');
  275. $appsecret = config('faceplus.api_secret');
  276. $url = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/addimage';
  277. $client = new Client();
  278. $array = [
  279. 'form_params' => [
  280. 'api_secret' => $appsecret,
  281. 'api_key' => $appkey,
  282. 'facealbum_token' => $facename,
  283. 'image_url' => $url_img
  284. ]
  285. ];
  286. try {
  287. $re = $client->post($url, $array);
  288. $res = $re->getBody()->getContents();
  289. $ress = json_decode($res, true);
  290. } catch (\Exception $exception) {
  291. $error = $exception->getMessage();
  292. $msg = '识别失败,请重新上传';
  293. $code = 30001;
  294. return ['msg' => $msg, 'code' => $code];
  295. }
  296. $face_token = $ress['faces'][0]['face_token'];
  297. $url1 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupface';
  298. $client1 = new Client();
  299. $array1 = [
  300. 'form_params' => [
  301. 'api_secret' => $appsecret,
  302. 'api_key' => $appkey,
  303. 'facealbum_token' => $facename,
  304. ]
  305. ];
  306. $re1 = $client1->post($url1, $array1);
  307. $res1 = $re1->getBody()->getContents();
  308. $ress1 = json_decode($res1, true);
  309. $task_id = $ress1['task_id'];
  310. sleep(5);
  311. $url2 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupfacetaskquery';
  312. $client2 = new Client();
  313. $array2 = [
  314. 'form_params' => [
  315. 'api_secret' => $appsecret,
  316. 'api_key' => $appkey,
  317. 'task_id' => $task_id,
  318. ]
  319. ];
  320. $facess = $client2->post($url2, $array2);
  321. $face = $facess->getBody()->getContents();
  322. $faces = json_decode($face, true);
  323. if ($faces['status'] == 0) {
  324. sleep(4);
  325. }
  326. if ($faces['status'] == 1) {
  327. $url2 = 'https://api-cn.faceplusplus.com/imagepp/v1/facealbum/groupfacetaskquery';
  328. $client2 = new Client();
  329. $array2 = [
  330. 'form_params' => [
  331. 'api_secret' => $appsecret,
  332. 'api_key' => $appkey,
  333. 'task_id' => $task_id,
  334. ]
  335. ];
  336. $facess_1 = $client2->post($url2, $array2);
  337. $face_1 = $facess_1->getBody()->getContents();
  338. Log::error($face_1);
  339. $faces_1 = json_decode($face_1, true);
  340. }
  341. if (!isset($faces_1['group_result'])) {
  342. $msg = '分组失败,请上传新的照片';
  343. $code = 30002;
  344. }
  345. $arr = $faces_1['group_result'];
  346. foreach ($arr as $k => $v) {
  347. $s = TravelFaceInfo::where('face_token', $v['face_token'])->first();
  348. if ($s) {
  349. $s->groupid = $v['group_id'];
  350. }
  351. if ($v['face_token'] == $face_token) {
  352. $group_id = $v['group_id'];
  353. }
  354. }
  355. return ['group_id' => $group_id, 'msg' => $msg, 'code' => 200];
  356. } catch (\Exception $exception) {
  357. $msg = '识别失败,请上传新的照片';
  358. $code = 30002;
  359. return ['msg' => $msg, 'code' => $code];
  360. }
  361. }
  362. }