Eyemargin.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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', 'getencoderesult'];
  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. //限制视频数量
  37. $eyemargin_count = Db::name('eyemargin')->where(['user_id' => $this->auth->id])->count('id');
  38. if ($eyemargin_count >= 10) {
  39. $this->error('眼缘最多发布10个');
  40. }
  41. $data = [];
  42. $data["user_id"] = $this->auth->id;
  43. $data["eye_type"] = $eye_type;
  44. $data["content"] = $content;
  45. if($eye_type == 2) {
  46. $cover = $this->request->request('cover'); // 影集封面图
  47. $album = $this->request->request('album'); // 影集图片
  48. $music = $this->request->request('music'); // 背景音乐
  49. if(!$cover || !$album || !$music) {
  50. $this->error(__('Invalid parameters'));
  51. }
  52. $data['cover'] = $cover;
  53. $data['album'] = $album;
  54. $data['music'] = $music;
  55. } elseif($eye_type == 1) {
  56. $video = $this->request->request('video'); // 视频文件
  57. if(!$video) {
  58. $this->error(__('视频图片缺失!'));
  59. }
  60. $cover = $this->request->request('cover'); // 影集封面图
  61. $data['cover'] = $cover;
  62. // $video_cover = str_replace("https://","http://",$video);
  63. // $cover = 'uploads/video_cover/'.date('Ymd').date('His').rand(1000,9999).'.png';
  64. // getVideoCover($video_cover,2,$cover);
  65. // $full_cover = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].'/'.$cover;
  66. // $data['cover'] = $full_cover;
  67. $data['video'] = $video;
  68. //视频转码加logo
  69. $oss_input_object = substr($data['video'], strpos($data['video'], 'uploads'));
  70. $oss_output_object = str_replace('uploads', 'output', $oss_input_object);
  71. $result = $this->mpsvideo($oss_input_object, $oss_output_object);
  72. if (!$result) {
  73. $this->error('网络错误,请稍后重试');
  74. }
  75. $data['jobid'] = $result;
  76. }
  77. $eyemarginModel = new \app\common\model\Eyemargin();
  78. $data["createtime"] = time();
  79. // 判断是否是第一个视频
  80. $info = $eyemarginModel->where(['user_id'=>$this->auth->id,'status'=>['in',[0,1]]])->find();
  81. if(!$info) {
  82. $data['is_main'] = 1;
  83. $msg = "恭喜,视频已发布审核中!温馨提示:长按可以取消或者修改'推荐'短视频,推荐的视频会展示给同城好友哦";
  84. } else {
  85. $msg = "发布成功!请耐心等待审核!";
  86. }
  87. $res = $eyemarginModel->insertGetId($data);
  88. if($res) {
  89. $this->success($msg);
  90. } else {
  91. $this->error("网络错误,请稍后重试");
  92. }
  93. }
  94. /**
  95. * 当数据为空时
  96. * @param $is_goddess
  97. * @param $is_new
  98. * @param $redis_ids
  99. * @param $field
  100. * @param $user_id
  101. * @return false|mixed|\PDOStatement|string|\think\Collection
  102. */
  103. private function getFateInfo($redis_ids,$field,$user_id,&$common_where) {
  104. $redis_ids && $common_where['a.id'] = ['not in',implode(',',$redis_ids)];
  105. // 按照距离排序
  106. $where = [];
  107. $where = array_merge($where,$common_where);
  108. $a = \app\common\model\Eyemargin::getDistanceList($this->auth->lng,$this->auth->lat,$field,$where,$user_id);
  109. return $a;
  110. }
  111. /**
  112. * 获取眼缘视频/影集
  113. */
  114. public function getFate() {
  115. $is_city = $this->request->request('is_city'); // 是否同城:1=是,0=否
  116. $is_goddess = $this->request->request('is_goddess'); // 是否女神:1=是,0=否
  117. $is_new = $this->request->request('is_new'); // 是否新人:1=是,0=否
  118. $is_neal = $this->request->request('is_neal'); // 是否附近:1=是,0=否
  119. if (!in_array($is_city,[0,1]) && !in_array($is_goddess,[0,1]) && !in_array($is_new,[0,1]) && !in_array($is_neal,[0,1])) {
  120. $this->error(__('请选择展示tab!'));
  121. }
  122. $pageNum = $this->request->request('pageNum',10); // 每页显示条数
  123. $user_id = $this->auth->id;
  124. $user_id_redis = 'u_'.$user_id;
  125. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  126. $time = time() - 30*86400;
  127. $common_where = [];
  128. // $common_where['u.avatar'] = ['like', 'https%'];
  129. if (!$is_city) {
  130. $common_where['a.is_main'] = 1;
  131. }
  132. $common_where['a.status'] = 1;
  133. $common_where['u.city'] = $this->auth->city;
  134. $is_goddess && $common_where['u.is_goddess'] = 1;
  135. $is_new && $common_where['u.createtime'] = ['gt',$time];
  136. if ($this->auth->gender == 1) {
  137. //男只能看女视频
  138. $common_where['u.gender'] = 0;
  139. // $common_where['u.wechat'] = ['neq', '']; //女生微信号必须审核通过
  140. } else {
  141. //女只能看男视频
  142. $common_where['u.gender'] = 1;
  143. }
  144. $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,
  145. u.age,u.constellation,u.hobby_ids,u.profession,u.declaration,u.lng,u.lat,u.mobile,u.copy_mobile,u.wechat,u.gender,u.profession";
  146. $list = [];
  147. $a = $this->getFateInfo($redis_ids,$field,$user_id,$common_where);
  148. if(!$a) {
  149. Cache::rm($user_id_redis);
  150. unset($common_where['a.id']);
  151. $redis_ids = [];
  152. $a = $this->getFateInfo($redis_ids,$field,$user_id,$common_where);
  153. }
  154. // 女神
  155. $where = [];
  156. $where['u.is_goddess'] = 1;
  157. $where = array_merge($where,$common_where);
  158. $b = \app\common\model\Eyemargin::getDistanceList($this->auth->lng,$this->auth->lat,$field,$where,$user_id);
  159. // 喜好 期望对象
  160. $expect = $this->auth->expect_ids;
  161. $user_ids =\app\common\model\Expect::getTagsByExpect($expect);
  162. if($user_ids) {
  163. $where = [];
  164. $where['u.id'] = ['in',implode(',',$user_ids)];
  165. $where = array_merge($where,$common_where);
  166. $c = \app\common\model\Eyemargin::getDistanceList($this->auth->lng,$this->auth->lat,$field,$where,$user_id);
  167. } else {
  168. $c = [];
  169. }
  170. // 新人
  171. $where = [];
  172. $where['u.createtime'] = ['gt',$time];
  173. $where = array_merge($where,$common_where);
  174. $d = \app\common\model\Eyemargin::getDistanceList($this->auth->lng,$this->auth->lat,$field,$where,$user_id);
  175. // 剩余
  176. // 三数组合并
  177. $bcd = \app\common\model\Eyemargin::arrayMerge($b,$c,$d);
  178. $e = array_diff($a,$bcd);
  179. // 附近
  180. $num_a = floor(config("site.nearbyTheOne") * 0.01 * $pageNum);
  181. $num_bcd = floor(config("site.nearbyTheTwo") * 0.01 * $pageNum);
  182. $num_e = $pageNum - $num_a - $num_bcd;
  183. // 先做好去重
  184. $a = array_slice($a,0,$pageNum);
  185. $bcd = array_diff($bcd,$a);
  186. $e = array_diff($e,array_merge($bcd,$a));
  187. $count_a = count($a);
  188. $count_e = count($e);
  189. $count_bcd = count($bcd);
  190. if($count_a <= ($num_a+$num_bcd+$num_e)) {
  191. $list = $a;
  192. }
  193. if($count_a >= $num_a && $count_bcd >= $num_bcd && $count_e >= $num_e) {
  194. $list_a = array_slice($a,0,$num_a);
  195. $list_bcd = array_slice($bcd,0,$num_bcd);
  196. $list_e = array_slice($e,0,$num_e);
  197. $list = \app\common\model\Eyemargin::arrayMerge($list_a,$list_bcd,$list_e);
  198. }
  199. // 10 = 10 =
  200. if($count_a >= $num_a && $count_bcd >= $num_bcd && $count_e < $num_e) {
  201. $list_a = array_slice($a,0,$pageNum-$num_bcd - $count_e);
  202. $list_bcd = array_slice($bcd,0,$num_bcd);
  203. $list_e = $e;
  204. $list = \app\common\model\Eyemargin::arrayMerge($list_a,$list_bcd,$list_e);
  205. }
  206. if($count_a >= $num_a && $count_bcd < $num_bcd && $count_e < $num_e) {
  207. $list_a = array_slice($a,0,$pageNum-$count_bcd-$count_e);
  208. $list_bcd = $bcd;
  209. $list_e = $e;
  210. $list = \app\common\model\Eyemargin::arrayMerge($list_a,$list_bcd,$list_e);
  211. }
  212. if($count_a >= $num_a && $count_bcd < $num_bcd && $count_e >= $num_e) {
  213. $list_a = array_slice($a,0,$pageNum-$count_bcd-$num_e);
  214. $list_bcd = $bcd;
  215. $list_e = array_slice($e,0,$num_e);
  216. $list = \app\common\model\Eyemargin::arrayMerge($list_a,$list_bcd,$list_e);
  217. }
  218. if($list) {
  219. // 收集眼缘ID
  220. $ids = array_column($list, 'id');
  221. if($redis_ids) {
  222. $redis_ids = array_merge($redis_ids,$ids);
  223. } else {
  224. $redis_ids = $ids;
  225. }
  226. Cache::set($user_id_redis,json_encode($redis_ids));
  227. } else {
  228. Cache::rm($user_id_redis);
  229. }
  230. $this->success("获取成功!",$list);
  231. }
  232. /**
  233. * 判断当前用户是否已经把我加到黑名单
  234. */
  235. public function isBlackUser() {
  236. $user_id = $this->request->request('user_id'); // 用户ID
  237. if(!$user_id) $this->error("参数缺失!");
  238. $where = [];
  239. $where['user_id'] = $user_id;
  240. $where['black_user_id'] = $this->auth->id;
  241. $blackInfo = \app\common\model\UserBlacklist::where($where)->find();
  242. if($blackInfo) {
  243. $this->success("获取成功!",['is_black'=>1]);
  244. } else {
  245. $this->success("获取成功!",['is_black'=>0]);
  246. }
  247. }
  248. /**
  249. * 阿里云媒体处理视频
  250. * @param string $oss_input_object 输入文件(相对路径)
  251. * @param string $oss_output_object 输出文件(相对路径)
  252. * @throws \ClientException
  253. * @throws \ServerException
  254. */
  255. public function mpsvideo($oss_input_object = '', $oss_output_object = '')
  256. {
  257. $access_key_id = config('oss.secretId'); //AccessKey ID
  258. $access_key_secret = config('oss.secretKey'); //AccessKey Secret
  259. $mps_region_id = config('oss.mps_region_id');
  260. $pipeline_id = config('oss.pipeline_id'); //管道id
  261. $watermark_template_id = config('oss.watermark_template_id'); //水印模板id
  262. $template_id = config('oss.template_id');//'S00000001-200030'; //转码模板id
  263. $oss_location = config('oss.region');
  264. $oss_bucket = config('oss.bucket'); //bucket
  265. // $oss_input_object = 'uploads/xuanzhuan.mp4'; //输入文件
  266. // $oss_output_object = 'output/xuanzhuan.mp4'; //输出文件
  267. $image_watermark_object = 'logo.png'; //水印logo地址
  268. // $video_watermark_object = 'logo.mov';
  269. # DefaultAcsClient
  270. $clientProfile = \DefaultProfile::getProfile(
  271. $mps_region_id, # Region ID
  272. $access_key_id, # AccessKey ID
  273. $access_key_secret # AccessKey Secret
  274. );
  275. $client = new \DefaultAcsClient($clientProfile);
  276. # request
  277. $request = new Mts\SubmitJobsRequest();
  278. $request->setAcceptFormat('JSON');
  279. # Input
  280. $input = array('Location' => $oss_location,
  281. 'Bucket' => $oss_bucket,
  282. 'Object' => urlencode($oss_input_object));
  283. $request->setInput(json_encode($input));
  284. # Output
  285. $output = array('OutputObject' => urlencode($oss_output_object));
  286. # Ouput->TemplateId
  287. $output['TemplateId'] = $template_id;
  288. ## Image Watermark
  289. $image_watermark_input = array(
  290. 'Location' => $oss_location,
  291. 'Bucket' => $oss_bucket,
  292. 'Object' => urlencode($image_watermark_object)
  293. );
  294. $image_watermark = array(
  295. 'WaterMarkTemplateId' => $watermark_template_id,
  296. 'Type' => 'Image',
  297. 'InputFile' => $image_watermark_input,
  298. // 'ReferPos' => 'TopRight',
  299. // 'Width' => 0.05,
  300. 'Dx' => 100,
  301. 'Dy'=> 50
  302. );
  303. ## Text Watermark
  304. /* $text_config = array(
  305. 'Content' => '5rWL6K+V5paH5a2X5rC05Y2w',
  306. 'FontName' => 'SimSun',
  307. 'FontSize' => 16,
  308. 'FontColor' => 'Red',
  309. 'FontAlpha' => 0.5,
  310. 'Top' => 10,
  311. 'Left' => 10
  312. );
  313. $text_watermark = array(
  314. 'WaterMarkTemplateId' => $watermark_template_id,
  315. 'Type' => 'Text',
  316. 'TextWaterMark' => $text_config
  317. );*/
  318. ## Video Watermark
  319. /*$video_watermark_input = array (
  320. 'Location' => $oss_location,
  321. 'Bucket' => $oss_bucket,
  322. 'Object' => urlencode($video_watermark_object)
  323. );
  324. $video_watermark = array(
  325. 'WaterMarkTemplateId' => $watermark_template_id,
  326. 'Type' => 'Image',
  327. 'InputFile'=> $video_watermark_input,
  328. 'ReferPos' => 'BottomLeft',
  329. 'Height' => 240,
  330. 'Dx' => 0,
  331. 'Dy' => 0
  332. );*/
  333. # Output->Watermarks
  334. // $watermarks = array($image_watermark, $text_watermark, $video_watermark);
  335. $watermarks = array($image_watermark);
  336. $output['WaterMarks'] = $watermarks;
  337. # Outputs
  338. $outputs = array($output);
  339. $request->setOUtputs(json_encode($outputs));
  340. $request->setOutputBucket($oss_bucket);
  341. $request->setOutputLocation($oss_location);
  342. # PipelineId
  343. $request->setPipelineId($pipeline_id);
  344. # call api
  345. try {
  346. $response = $client->getAcsResponse($request);
  347. // print 'RequestId is:' . $response->{'RequestId'} . "\n";;
  348. if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
  349. return $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'};
  350. // print 'JobId is:' .
  351. // $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
  352. } else {
  353. return false;
  354. // print 'SubmitJobs Failed code:' .
  355. // $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
  356. // ' message:' .
  357. // $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
  358. }
  359. } catch(ServerException $e) {
  360. return false;
  361. // print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
  362. } catch(ClientException $e) {
  363. return false;
  364. // print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
  365. }
  366. }
  367. //阿里云查询视频转码结果
  368. public function getencoderesult()
  369. {
  370. set_time_limit(0);
  371. $where = array(
  372. 'eye_type' => 1,
  373. 'jobid' => ['neq', ''],
  374. 'encode_status' => 0,
  375. );
  376. $eyemargin = Db::name('eyemargin');
  377. $list = $eyemargin->where($where)->limit(100)->select();
  378. if (!$list) {
  379. echo 'mei shu ju';
  380. die;
  381. }
  382. //构建转码查询数据
  383. $data['Format'] = 'JSON';
  384. $data['Version'] = '2014-06-18';
  385. $data['AccessKeyId'] = config('oss.secretId');
  386. $data['SignatureMethod'] = 'HMAC-SHA1';
  387. $data['SignatureVersion'] = '1.0';
  388. $data['Action'] = 'QueryJobList';
  389. foreach ($list as &$v) {
  390. $data['Timestamp'] = str_replace('+00:00', 'Z', gmdate('c', time()));
  391. $data['SignatureNonce'] = md5(uniqid(mt_rand(), true));
  392. $data['JobIds'] = $v['jobid'];
  393. $data['Signature'] = $this->computeSignature($data, config('oss.secretKey'));
  394. $url = 'http://mts.cn-hangzhou.aliyuncs.com/?';
  395. foreach ($data as $apiParamKey => $apiParamValue) {
  396. $url .= "$apiParamKey=" . urlencode($apiParamValue) . '&';
  397. }
  398. $url = substr($url, 0, -1);
  399. $result = file_get_contents($url);
  400. $result = json_decode($result, true);
  401. $_data = [];
  402. if ($result['JobList']['Job'][0]['Percent'] == 100 && $result['JobList']['Job'][0]['State'] == 'TranscodeSuccess') {
  403. $_data['video'] = str_replace('uploads', 'output', $v['video']);
  404. $_data['encode_status'] = 1;
  405. $eyemargin->where(['id' => $v['id'], 'encode_status' => 0])->setField($_data);
  406. } elseif ($result['JobList']['Job'][0]['State'] == 'TranscodeFail') {
  407. $_data['encode_status'] = 2;
  408. $eyemargin->where(['id' => $v['id'], 'encode_status' => 0])->setField($_data);
  409. }
  410. }
  411. echo 'wan bi';
  412. die;
  413. }
  414. /**
  415. * @param $parameters
  416. * @param $accessKeySecret
  417. * @param $iSigner
  418. *
  419. * @return mixed
  420. */
  421. protected function computeSignature($parameters, $accessKeySecret)
  422. {
  423. ksort($parameters);
  424. $canonicalizedQueryString = '';
  425. foreach ($parameters as $key => $value) {
  426. $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
  427. }
  428. $stringToBeSigned = 'GET&%2F&' . $this->percentEncode(substr($canonicalizedQueryString, 1));
  429. return $this->signString($stringToBeSigned, $accessKeySecret . '&');
  430. }
  431. /**
  432. * @param $str
  433. *
  434. * @return string|string[]|null
  435. */
  436. protected function percentEncode($str)
  437. {
  438. $res = urlencode($str);
  439. $res = str_replace(array('+', '*'), array('%20', '%2A'), $res);
  440. $res = preg_replace('/%7E/', '~', $res);
  441. return $res;
  442. }
  443. public function signString($source, $accessSecret)
  444. {
  445. return base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
  446. }
  447. }