Eyemargin.php 18 KB

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