Eyemargin.php 18 KB

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