Eyemargin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. namespace app\api\controller;
  3. use Limen\Redisun\Examples\HashModel;
  4. use Limen\Redisun\Examples\StringModel;
  5. use think\Db;
  6. use think\Cache;
  7. include_once '../vendor/aliyun/aliyun-php-sdk-core/Config.php';
  8. use Mts\Request\V20140618 as Mts;
  9. /**
  10. * 眼缘接口
  11. */
  12. class Eyemargin extends Common
  13. {
  14. protected $noNeedLogin = ['mpsvideo'];
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. /**
  21. * 发布眼缘
  22. */
  23. public function addFate() {
  24. $eye_type = $this->request->request('eye_type'); // 动态类型
  25. $content = $this->request->request('content'); // 内容
  26. if (!in_array($eye_type,[1,2]) || !$content) {
  27. $this->error(__('Invalid parameters'));
  28. }
  29. $str_arr = sensitive($content);
  30. if($str_arr['count'] > 0) {
  31. $this->error(__('发布内容存在敏感词汇:'.$str_arr['sensitiveWord'].' 请重新编辑后发布!'));
  32. }
  33. // 判断当前用户是否实名认证
  34. $userAuthInfo = \app\common\model\UserAuth::userIsAuth($this->auth->id);
  35. if($userAuthInfo['status'] == 0) $this->error($userAuthInfo['msg']);
  36. $data = [];
  37. $data["user_id"] = $this->auth->id;
  38. $data["eye_type"] = $eye_type;
  39. $data["content"] = $content;
  40. if($eye_type == 2) {
  41. $cover = $this->request->request('cover'); // 影集封面图
  42. $album = $this->request->request('album'); // 影集图片
  43. $music = $this->request->request('music'); // 背景音乐
  44. if(!$cover || !$album || !$music) {
  45. $this->error(__('Invalid parameters'));
  46. }
  47. $data['cover'] = $cover;
  48. $data['album'] = $album;
  49. $data['music'] = $music;
  50. } elseif($eye_type == 1) {
  51. $video = $this->request->request('video'); // 视频文件
  52. if(!$video) {
  53. $this->error(__('视频图片缺失!'));
  54. }
  55. $cover = $this->request->request('cover'); // 影集封面图
  56. $data['cover'] = $cover;
  57. // $video_cover = str_replace("https://","http://",$video);
  58. // $cover = 'uploads/video_cover/'.date('Ymd').date('His').rand(1000,9999).'.png';
  59. // getVideoCover($video_cover,2,$cover);
  60. // $full_cover = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].'/'.$cover;
  61. // $data['cover'] = $full_cover;
  62. $data['video'] = $video;
  63. //视频转码加logo
  64. $oss_input_object = substr($data['video'], strpos($data['video'], 'uploads'));
  65. $oss_output_object = str_replace('uploads', 'output', $oss_input_object);
  66. $result = $this->mpsvideo($oss_input_object, $oss_output_object);
  67. if (!$result) {
  68. $this->error('网络错误,请稍后重试');
  69. }
  70. $data['jobid'] = $result;
  71. }
  72. $eyemarginModel = new \app\common\model\Eyemargin();
  73. $data["createtime"] = time();
  74. // 判断是否是第一个视频
  75. $info = $eyemarginModel->where(['user_id'=>$this->auth->id,'status'=>['in',[0,1]]])->find();
  76. if(!$info) {
  77. $data['is_main'] = 1;
  78. $msg = "恭喜,视频已发布审核中!温馨提示:长按可以取消或者修改'推荐'短视频,推荐的视频会展示给同城好友哦";
  79. } else {
  80. $msg = "发布成功!请耐心等待审核!";
  81. }
  82. $res = $eyemarginModel->insertGetId($data);
  83. if($res) {
  84. $this->success($msg);
  85. } else {
  86. $this->error("网络错误,请稍后重试");
  87. }
  88. }
  89. /**
  90. * 当数据为空时
  91. * @param $is_goddess
  92. * @param $is_new
  93. * @param $redis_ids
  94. * @param $field
  95. * @param $user_id
  96. * @return false|mixed|\PDOStatement|string|\think\Collection
  97. */
  98. private function getFateInfo($redis_ids,$field,$user_id,&$common_where) {
  99. $redis_ids && $common_where['a.id'] = ['notin',implode(',',$redis_ids)];
  100. // 按照距离排序
  101. $where = [];
  102. $where = array_merge($where,$common_where);
  103. $a = \app\common\model\Eyemargin::getDistanceList($this->auth->lng,$this->auth->lat,$field,$where,$user_id);
  104. return $a;
  105. }
  106. /**
  107. * 获取眼缘视频/影集
  108. */
  109. public function getFate() {
  110. $is_goddess = $this->request->request('is_goddess'); // 是否女神:1=是,0=否
  111. $is_new = $this->request->request('is_new'); // 是否新人:1=是,0=否
  112. $is_neal = $this->request->request('is_neal'); // 是否附近:1=是,0=否
  113. if (!in_array($is_goddess,[0,1]) && !in_array($is_new,[0,1]) && !in_array($is_neal,[0,1])) {
  114. $this->error(__('请选择展示tab!'));
  115. }
  116. $pageNum = $this->request->request('pageNum',10); // 每页显示条数
  117. $user_id = $this->auth->id;
  118. $user_id_redis = 'u_'.$user_id;
  119. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  120. $time = time() - 30*86400;
  121. $common_where = [];
  122. $common_where['a.is_main'] = 1;
  123. $common_where['a.status'] = 1;
  124. $common_where['u.city'] = $this->auth->city;
  125. $is_goddess && $common_where['u.is_goddess'] = 1;
  126. $is_new && $common_where['u.createtime'] = ['gt',$time];
  127. $common_where['a.status'] = 1;
  128. $field = "a.*,u.avatar,u.city_name,u.district_name,u.nickname,u.is_goddess,u.is_auth,vipStatus(u.vip_duetime) as is_vip,
  129. u.age,u.constellation,u.hobby_ids,u.profession,u.declaration,u.lng,u.lat";
  130. $list = [];
  131. $a = $this->getFateInfo($redis_ids,$field,$user_id,$common_where);
  132. if(!$a) {
  133. Cache::rm($user_id_redis);
  134. unset($common_where['a.id']);
  135. $redis_ids = [];
  136. $a = $this->getFateInfo($redis_ids,$field,$user_id,$common_where);
  137. }
  138. // 女神
  139. $where = [];
  140. $where['u.is_goddess'] = 1;
  141. $where = array_merge($where,$common_where);
  142. $b = \app\common\model\Eyemargin::getDistanceList($this->auth->lng,$this->auth->lat,$field,$where,$user_id);
  143. // 喜好 期望对象
  144. $expect = $this->auth->expect_ids;
  145. $user_ids =\app\common\model\Expect::getTagsByExpect($expect);
  146. if($user_ids) {
  147. $where = [];
  148. $where['u.id'] = ['in',implode(',',$user_ids)];
  149. $where = array_merge($where,$common_where);
  150. $c = \app\common\model\Eyemargin::getDistanceList($this->auth->lng,$this->auth->lat,$field,$where,$user_id);
  151. } else {
  152. $c = [];
  153. }
  154. // 新人
  155. $where = [];
  156. $where['u.createtime'] = ['gt',$time];
  157. $where = array_merge($where,$common_where);
  158. $d = \app\common\model\Eyemargin::getDistanceList($this->auth->lng,$this->auth->lat,$field,$where,$user_id);
  159. // 剩余
  160. // 三数组合并
  161. $bcd = \app\common\model\Eyemargin::arrayMerge($b,$c,$d);
  162. $e = array_diff($a,$bcd);
  163. // 附近
  164. $num_a = floor(config("site.nearbyTheOne") * 0.01 * $pageNum);
  165. $num_bcd = floor(config("site.nearbyTheTwo") * 0.01 * $pageNum);
  166. $num_e = $pageNum - $num_a - $num_bcd;
  167. // 先做好去重
  168. $a = array_slice($a,0,$pageNum);
  169. $bcd = array_diff($bcd,$a);
  170. $e = array_diff($e,array_merge($bcd,$a));
  171. $count_a = count($a);
  172. $count_e = count($e);
  173. $count_bcd = count($bcd);
  174. if($count_a <= ($num_a+$num_bcd+$num_e)) {
  175. $list = $a;
  176. }
  177. if($count_a >= $num_a && $count_bcd >= $num_bcd && $count_e >= $num_e) {
  178. $list_a = array_slice($a,0,$num_a);
  179. $list_bcd = array_slice($bcd,0,$num_bcd);
  180. $list_e = array_slice($e,0,$num_e);
  181. $list = \app\common\model\Eyemargin::arrayMerge($list_a,$list_bcd,$list_e);
  182. }
  183. // 10 = 10 =
  184. if($count_a >= $num_a && $count_bcd >= $num_bcd && $count_e < $num_e) {
  185. $list_a = array_slice($a,0,$pageNum-$num_bcd - $count_e);
  186. $list_bcd = array_slice($bcd,0,$num_bcd);
  187. $list_e = $e;
  188. $list = \app\common\model\Eyemargin::arrayMerge($list_a,$list_bcd,$list_e);
  189. }
  190. if($count_a >= $num_a && $count_bcd < $num_bcd && $count_e < $num_e) {
  191. $list_a = array_slice($a,0,$pageNum-$count_bcd-$count_e);
  192. $list_bcd = $bcd;
  193. $list_e = $e;
  194. $list = \app\common\model\Eyemargin::arrayMerge($list_a,$list_bcd,$list_e);
  195. }
  196. if($count_a >= $num_a && $count_bcd < $num_bcd && $count_e >= $num_e) {
  197. $list_a = array_slice($a,0,$pageNum-$count_bcd-$num_e);
  198. $list_bcd = $bcd;
  199. $list_e = array_slice($e,0,$num_e);
  200. $list = \app\common\model\Eyemargin::arrayMerge($list_a,$list_bcd,$list_e);
  201. }
  202. if($list) {
  203. // 收集眼缘ID
  204. $ids = array_column($list, 'id');
  205. if($redis_ids) {
  206. $redis_ids = array_merge($redis_ids,$ids);
  207. } else {
  208. $redis_ids = $ids;
  209. }
  210. Cache::set($user_id_redis,json_encode($redis_ids));
  211. } else {
  212. Cache::rm($user_id_redis);
  213. }
  214. $this->success("获取成功!",$list);
  215. }
  216. /**
  217. * 判断当前用户是否已经把我加到黑名单
  218. */
  219. public function isBlackUser() {
  220. $user_id = $this->request->request('user_id'); // 用户ID
  221. if(!$user_id) $this->error("参数缺失!");
  222. $where = [];
  223. $where['user_id'] = $user_id;
  224. $where['black_user_id'] = $this->auth->id;
  225. $blackInfo = \app\common\model\UserBlacklist::where($where)->find();
  226. if($blackInfo) {
  227. $this->success("获取成功!",['is_black'=>1]);
  228. } else {
  229. $this->success("获取成功!",['is_black'=>0]);
  230. }
  231. }
  232. /**
  233. * 阿里云媒体处理视频
  234. * @param string $oss_input_object 输入文件(相对路径)
  235. * @param string $oss_output_object 输出文件(相对路径)
  236. * @throws \ClientException
  237. * @throws \ServerException
  238. */
  239. public function mpsvideo($oss_input_object = '', $oss_output_object = '')
  240. {
  241. $access_key_id = config('oss.secretId'); //AccessKey ID
  242. $access_key_secret = config('oss.secretKey'); //AccessKey Secret
  243. $mps_region_id = 'cn-hangzhou';
  244. $pipeline_id = '0c4f9b648be54da09e29d8819f60a0df'; //管道id
  245. $watermark_template_id = '0c09ed4ef64e424b89a3449bcc0de522'; //水印模板id
  246. $template_id = '4c43c0ff9436425dbb6eaf2ce74e0ff0';//'S00000001-200030'; //转码模板id
  247. $oss_location = 'oss-cn-hangzhou';
  248. $oss_bucket = config('oss.bucket'); //bucket
  249. // $oss_input_object = 'uploads/xuanzhuan.mp4'; //输入文件
  250. // $oss_output_object = 'output/xuanzhuan.mp4'; //输出文件
  251. $image_watermark_object = 'logo.png'; //水印logo地址
  252. // $video_watermark_object = 'logo.mov';
  253. # DefaultAcsClient
  254. $clientProfile = \DefaultProfile::getProfile(
  255. $mps_region_id, # Region ID
  256. $access_key_id, # AccessKey ID
  257. $access_key_secret # AccessKey Secret
  258. );
  259. $client = new \DefaultAcsClient($clientProfile);
  260. # request
  261. $request = new Mts\SubmitJobsRequest();
  262. $request->setAcceptFormat('JSON');
  263. # Input
  264. $input = array('Location' => $oss_location,
  265. 'Bucket' => $oss_bucket,
  266. 'Object' => urlencode($oss_input_object));
  267. $request->setInput(json_encode($input));
  268. # Output
  269. $output = array('OutputObject' => urlencode($oss_output_object));
  270. # Ouput->TemplateId
  271. $output['TemplateId'] = $template_id;
  272. ## Image Watermark
  273. $image_watermark_input = array(
  274. 'Location' => $oss_location,
  275. 'Bucket' => $oss_bucket,
  276. 'Object' => urlencode($image_watermark_object)
  277. );
  278. $image_watermark = array(
  279. 'WaterMarkTemplateId' => $watermark_template_id,
  280. 'Type' => 'Image',
  281. 'InputFile' => $image_watermark_input,
  282. // 'ReferPos' => 'TopRight',
  283. // 'Width' => 0.05,
  284. 'Dx' => 18,
  285. 'Dy'=> 18
  286. );
  287. ## Text Watermark
  288. /* $text_config = array(
  289. 'Content' => '5rWL6K+V5paH5a2X5rC05Y2w',
  290. 'FontName' => 'SimSun',
  291. 'FontSize' => 16,
  292. 'FontColor' => 'Red',
  293. 'FontAlpha' => 0.5,
  294. 'Top' => 10,
  295. 'Left' => 10
  296. );
  297. $text_watermark = array(
  298. 'WaterMarkTemplateId' => $watermark_template_id,
  299. 'Type' => 'Text',
  300. 'TextWaterMark' => $text_config
  301. );*/
  302. ## Video Watermark
  303. /*$video_watermark_input = array (
  304. 'Location' => $oss_location,
  305. 'Bucket' => $oss_bucket,
  306. 'Object' => urlencode($video_watermark_object)
  307. );
  308. $video_watermark = array(
  309. 'WaterMarkTemplateId' => $watermark_template_id,
  310. 'Type' => 'Image',
  311. 'InputFile'=> $video_watermark_input,
  312. 'ReferPos' => 'BottomLeft',
  313. 'Height' => 240,
  314. 'Dx' => 0,
  315. 'Dy' => 0
  316. );*/
  317. # Output->Watermarks
  318. // $watermarks = array($image_watermark, $text_watermark, $video_watermark);
  319. $watermarks = array($image_watermark);
  320. $output['WaterMarks'] = $watermarks;
  321. # Outputs
  322. $outputs = array($output);
  323. $request->setOUtputs(json_encode($outputs));
  324. $request->setOutputBucket($oss_bucket);
  325. $request->setOutputLocation($oss_location);
  326. # PipelineId
  327. $request->setPipelineId($pipeline_id);
  328. # call api
  329. try {
  330. $response = $client->getAcsResponse($request);
  331. // print 'RequestId is:' . $response->{'RequestId'} . "\n";;
  332. if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
  333. return $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'};
  334. // print 'JobId is:' .
  335. // $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
  336. } else {
  337. return false;
  338. // print 'SubmitJobs Failed code:' .
  339. // $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
  340. // ' message:' .
  341. // $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
  342. }
  343. } catch(ServerException $e) {
  344. return false;
  345. // print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
  346. } catch(ClientException $e) {
  347. return false;
  348. // print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
  349. }
  350. }
  351. }