Match.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Request;
  5. use voice\IgrWsDemo;
  6. use WebSocket\Client;
  7. use app\common\library\Upload;
  8. /**
  9. * 划卡接口
  10. */
  11. class Match extends Common
  12. {
  13. protected $noNeedLogin = ['getVoiceTypeList','getVoiceText','getUserVoiceList'];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 首页
  17. *
  18. */
  19. public function index() {
  20. $this->success('请求成功');
  21. }
  22. /**
  23. * 声音识别
  24. * @date 2020-10-27
  25. * @createby 虎嗅网络科技
  26. */
  27. public function recognizingSounds() {
  28. $audio = $this->request->file('audio'); // 声音文件,base64
  29. $voice_time = $this->request->request('voice_time'); // 声音时长
  30. if(!$audio) {
  31. $this->error(__('Invalid parameters'));
  32. }
  33. $audiourl = $this->uploads($audio);
  34. $url = ROOT_PATH."public".$audiourl;
  35. exec("node /www/wwwroot/ggyuyin.node.api/igr-ws-node.js ".$this->auth->id." ".$url,$result);
  36. // $interval=2; // 每隔两秒运行一次
  37. // $i = 0;$res = true;
  38. // do{
  39. // $voiceInfo = $this->getVoiceInfo($audiourl,$voice_time);
  40. // sleep($interval);
  41. // $i++;
  42. // if($i >= 4 || $voiceInfo) {
  43. // $res = false;
  44. // }
  45. // }while($res);
  46. // 获取鉴定结果
  47. $analysisModel = new \app\common\model\MatchAnalysis();
  48. $res = $analysisModel->update(["voice"=>$audiourl,"voice_time"=>$voice_time],["user_id"=>$this->auth->id]);
  49. if($res !== false) {
  50. $this->success(__('识别完成!'));
  51. } else {
  52. $this->error("识别失败!");
  53. }
  54. }
  55. /**
  56. * 分析声音信息并生成描述
  57. */
  58. public function getVoiceInfo() {
  59. // 获取鉴定结果
  60. $analysisModel = new \app\common\model\MatchAnalysis();
  61. $analysisInfo = $analysisModel->where(["user_id"=>$this->auth->id])->find();
  62. if(!$analysisInfo) {
  63. $this->error("鉴定信息获取失败,请重新录制!");
  64. }
  65. // // 获取分析结果
  66. $voiceModel = new \app\common\model\MatchVoice();
  67. $voiceInfo = $voiceModel->alias("a")
  68. ->field("a.*,u.avatar,a.type_id,mt.name as type_name")
  69. ->join("hx_match_type mt","mt.id = a.type_id")
  70. ->join("hx_user u","u.id = a.user_id")
  71. ->where(["user_id"=>$this->auth->id])->find();
  72. if($voiceInfo && $voiceInfo["updatecode"] == $analysisInfo["updatecode"]) {// 获取的是老数据
  73. // 返回数据
  74. $voiceInfo = json_decode(json_encode($voiceInfo),true);
  75. // 做数据处理
  76. $voiceInfo["voice"] = $this->httpurlLocal($voiceInfo["voice"]);
  77. $voice_detail = explode(",",$voiceInfo["voice_detail"]);
  78. foreach($voice_detail as $m => $n) {
  79. $voi = explode("-",$n);
  80. $voiceInfo["voice_detail_arr"][] = ["key"=>$voi[0],"value"=>$voi[1]];
  81. }
  82. $voiceInfo["best_partner"] = explode(",",$voiceInfo["best_partner"]);
  83. $this->success("获取成功!",$voiceInfo);
  84. }
  85. // 年龄段数组
  86. $ageArr = ["age_middle","age_child","age_old"];
  87. // 分析结果
  88. $timbreModel = new \app\common\model\MatchTimbre();
  89. $where = [];
  90. $where["gender_type"] = $analysisInfo["gender_type"];
  91. $where["age_type"] = $analysisInfo["age_type"];
  92. $timbreList = $timbreModel->where($where)->select(); // 第一步过滤
  93. $ageRate = $analysisInfo[$ageArr[$analysisInfo["age_type"]]];
  94. $id = 0;
  95. if($timbreList) {
  96. $ageRate = intval($ageRate*100);
  97. foreach($timbreList as $k => $v) {
  98. $startRate = explode("-",$v["age_rate"])[0];
  99. $endRate = explode("-",$v["age_rate"])[1];
  100. if($ageRate > $startRate && $ageRate <= $endRate) {
  101. $id = $v["id"];
  102. break;
  103. }
  104. // for($i=1;$i<=51;$i++) {
  105. // if($ageRate == $startRate) {
  106. // $id = $v["id"];
  107. // } else {
  108. // $ageRate = $ageRate - 1;
  109. // }
  110. // }
  111. }
  112. }
  113. $timbreInfo = $timbreModel->where(["id"=>$id])->find(); // 第二步过滤
  114. // 写入数据库
  115. if(!$timbreInfo) $this->error("识别失败,请重新录制!");
  116. $nameRate1Min = explode("-",$timbreInfo["name_rate1"])[0]*100;
  117. $nameRate1max = explode("-",$timbreInfo["name_rate1"])[1]*100;
  118. $nameRate1 = rand($nameRate1Min,$nameRate1max)/100;
  119. $nameRate2Min = explode("-",$timbreInfo["name_rate2"])[0]*100;
  120. $nameRate2max = explode("-",$timbreInfo["name_rate2"])[1]*100;
  121. $nameRate2 = rand($nameRate2Min,$nameRate2max)/100;
  122. $nameRate3Min = explode("-",$timbreInfo["name_rate3"])[0]*100;
  123. $nameRate3max = explode("-",$timbreInfo["name_rate3"])[1]*100;
  124. $nameRate3 = rand($nameRate3Min,$nameRate3max)/100;
  125. $data = [];
  126. $data["type_id"] = $timbreInfo["type_id"];
  127. $data["voice"] = $analysisInfo['voice'];
  128. $data["voice_detail"] = $timbreInfo["name_type1"]."-".$nameRate1.",".$timbreInfo["name_type2"]."-".$nameRate2.",".$timbreInfo["name_type3"]."-".$nameRate3;
  129. $data["best_partner"] = $timbreInfo["bestlove"];
  130. $data["reliability"] = rand(8000,10000)/100;
  131. $data["commont"] = $timbreInfo["character"];
  132. $data["voice_time"] = $analysisInfo['voice_time'];
  133. $data["updatecode"] = $analysisInfo["updatecode"];
  134. if($voiceInfo) { // 更新
  135. $where = [];
  136. $where["user_id"] = $this->auth->id;
  137. $voiceModel->update($data,$where);
  138. } else { // 新增
  139. $data["user_id"] = $this->auth->id;
  140. $data["createtime"] = time();
  141. $voiceModel->insert($data);
  142. }
  143. $voiceInfo = $voiceModel->alias("a")
  144. ->field("a.*,u.avatar,a.type_id,mt.name as type_name")
  145. ->join("hx_match_type mt","mt.id = a.type_id")
  146. ->join("hx_user u","u.id = a.user_id")
  147. ->where(["user_id"=>$this->auth->id])->find();
  148. // 返回数据
  149. if($voiceInfo) {
  150. $voiceInfo = json_decode(json_encode($voiceInfo),true);
  151. // 做数据处理
  152. $voiceInfo["voice"] = $this->httpurl($voiceInfo["voice"]);
  153. $voice_detail = explode(",",$voiceInfo["voice_detail"]);
  154. foreach($voice_detail as $m => $n) {
  155. $voi = explode("-",$n);
  156. $voiceInfo["voice_detail_arr"][] = ["key"=>$voi[0],"value"=>$voi[1]];
  157. }
  158. $voiceInfo["best_partner"] = explode(",",$voiceInfo["best_partner"]);
  159. }
  160. if($voiceInfo) {
  161. $this->success("分析完成!",$voiceInfo);
  162. } else {
  163. $this->error("数据获取失败!");
  164. }
  165. }
  166. /**
  167. * 获取用户声音列表
  168. * @date 2020-10-27
  169. * @createby 虎嗅网络科技
  170. */
  171. public function getUserVoiceList() {
  172. $type_id = $this->request->request('type_id',0); // 声音类型
  173. $voiceModel = new \app\common\model\MatchVoice();
  174. $nolike_ids = [];
  175. $voicenolikeModel = new \app\common\model\MatchNolike();
  176. // 获取不喜欢的IDs
  177. // $nolike_ids = $voicenolikeModel->where(["user_id"=>$this->auth->id])->column("nolike_id");
  178. $where = [];
  179. $type_id && $where["a.type_id"] = $type_id;
  180. if($this->auth->isLogin()) {
  181. array_push($nolike_ids,$this->auth->id);// 过滤自己
  182. $where["a.user_id"] = ["notin",$nolike_ids];
  183. }
  184. $voiceList = $voiceModel->alias("a")
  185. ->field("a.id,a.user_id,u.avatar,u.nickname,a.type_id,mt.name as type_name,a.voice,a.voice_time")
  186. ->join("hx_match_type mt","mt.id = a.type_id")
  187. ->join("hx_user u","u.id = a.user_id")
  188. ->where($where)
  189. ->limit(100)
  190. ->select();
  191. if ($voiceList) {
  192. foreach($voiceList as $k => $v) {
  193. $voiceList[$k]["voice"] = $this->httpurlLocal($v["voice"]);
  194. }
  195. $this->success(__('获取成功!'), $voiceList);
  196. } else {
  197. $this->success(__('数据为空!'));
  198. }
  199. }
  200. /**
  201. * 是否已存在鉴定信息
  202. */
  203. public function isVoiceInfo() {
  204. $id = \app\common\model\MatchVoice::where(["user_id"=>$this->auth->id])->value("id");
  205. $data["isvoiceinfo"] = $id>0?1:0;
  206. $this->success("获取成功!",$data);
  207. }
  208. /**
  209. * 获取声音类型
  210. * @date 2020-10-27
  211. * @createby 虎嗅网络科技
  212. */
  213. public function getVoiceTypeList() {
  214. $voiceTypeModel = new \app\common\model\MatchType();
  215. $where = [];
  216. $where["is_show"] = 1;
  217. $voiceTypeList = $voiceTypeModel->field("id,name")->where($where)->select();
  218. if ($voiceTypeList) {
  219. $this->success(__('获取成功!'), $voiceTypeList);
  220. } else {
  221. $this->success(__('数据为空!'));
  222. }
  223. }
  224. /**
  225. * 添加喜欢声音列表
  226. * @date 2020-10-27
  227. * @createby 虎嗅网络科技
  228. */
  229. public function addVoiceLikeList() {
  230. $user_id = $this->request->request('user_id',0,"intval"); // 喜欢的用户ID
  231. $fans_id = $this->auth->id; // 添加到我喜欢,我就是粉丝
  232. if (!$user_id || !$fans_id) {
  233. $this->error(__('Invalid parameters'));
  234. }
  235. $matchLikeModel = new \app\common\model\MatchLike();
  236. $where = [];
  237. $where["user_id"] = $user_id;
  238. $where["fans_id"] = $fans_id;
  239. $matchLikeInfo = $matchLikeModel->where($where)->find();
  240. $data = [];
  241. if($matchLikeInfo) {
  242. $res = true;
  243. } else {
  244. $data["user_id"] = $user_id;
  245. $data["fans_id"] = $fans_id;
  246. $data["createtime"] = time();
  247. $res = $matchLikeModel->insert($data);
  248. }
  249. if ($res) {
  250. // +message
  251. // \app\common\model\Message::addMessage($user_id,"喜欢通知",$this->auth->nickname."(".$this->auth->u_id.") 喜欢了你,直接回复开始聊天吧");
  252. $message = "我喜欢了你哟,回复开始聊天吧";
  253. $tenim = new \app\api\controller\Tenim();
  254. $tenim->sendMessageToUser($fans_id,$user_id,$message);
  255. $this->success(__('添加成功!'), $data);
  256. } else {
  257. $this->success(__('添加失败!'));
  258. }
  259. }
  260. /**
  261. * 添加不喜欢声音列表
  262. * @date 2020-10-27
  263. * @createby 虎嗅网络科技
  264. */
  265. public function addVoiceNoLikeList() {
  266. $nolike_id = $this->request->request('nolike_id',0,"intval"); // 不喜欢的用户ID
  267. if (!$nolike_id) {
  268. $this->error(__('Invalid parameters'));
  269. }
  270. $matchNoLikeModel = new \app\common\model\MatchNolike();
  271. $where = [];
  272. $where["user_id"] = $this->auth->id;
  273. $where["nolike_id"] = $nolike_id;
  274. $matchLikeInfo = $matchNoLikeModel->where($where)->find();
  275. if($matchLikeInfo) $this->success("添加成功!");
  276. $data = [];
  277. $data["user_id"] = $this->auth->id;
  278. $data["nolike_id"] = $nolike_id;
  279. $data["createtime"] = time();
  280. $res = $matchNoLikeModel->insert($data);
  281. if ($res) {
  282. $this->success(__('添加成功!'));
  283. } else {
  284. $this->success(__('添加失败!'));
  285. }
  286. }
  287. /**
  288. * 获取喜欢我的
  289. * @date 2020-10-27
  290. * @createby 虎嗅网络科技
  291. */
  292. public function getVoiceFansList() {
  293. $page = $this->request->request('page',1); // 分页
  294. $pageNum = $this->request->request('pageNum',10); // 分页
  295. // 分页搜索构建
  296. $pageStart = ($page-1)*$pageNum;
  297. $matchLikeModel = new \app\common\model\MatchLike();
  298. $voiceFansList = $matchLikeModel->getVoiceFansList($this->auth->id,$pageStart,$pageNum);
  299. if ($voiceFansList) {
  300. $this->success(__('获取成功!'), $voiceFansList);
  301. } else {
  302. $this->success(__('数据为空!'));
  303. }
  304. }
  305. /**
  306. * 获取我喜欢的
  307. * @date 2020-10-27
  308. * @createby 虎嗅网络科技
  309. */
  310. public function getVoiceIdolList() {
  311. $page = $this->request->request('page',1); // 分页
  312. $pageNum = $this->request->request('pageNum',10); // 分页
  313. // 分页搜索构建
  314. $pageStart = ($page-1)*$pageNum;
  315. $matchLikeModel = new \app\common\model\MatchLike();
  316. $voiceIdolList = $matchLikeModel->getVoiceUserList($this->auth->id,$pageStart,$pageNum);
  317. if ($voiceIdolList) {
  318. $this->success(__('获取成功!'), $voiceIdolList);
  319. } else {
  320. $this->success(__('数据为空!'));
  321. }
  322. }
  323. /**
  324. * 获取我的声音鉴定信息
  325. * @date 2020-10-27
  326. * @createby 虎嗅网络科技
  327. */
  328. public function getMyVoiceInfo() {
  329. // 获取鉴定结果
  330. $analysisModel = new \app\common\model\MatchAnalysis();
  331. $analysisInfo = $analysisModel->where(["user_id"=>$this->auth->id])->find();
  332. if(!$analysisInfo) {
  333. $this->error("鉴定信息获取失败,请重新录制!");
  334. }
  335. // // 获取分析结果
  336. $voiceModel = new \app\common\model\MatchVoice();
  337. $voiceInfo = $voiceModel->alias("a")
  338. ->field("a.*,u.avatar,a.type_id,mt.name as type_name")
  339. ->join("hx_match_type mt","mt.id = a.type_id")
  340. ->join("hx_user u","u.id = a.user_id")
  341. ->where(["user_id"=>$this->auth->id])->find();
  342. if($voiceInfo) {
  343. // 返回数据
  344. $voiceInfo = json_decode(json_encode($voiceInfo),true);
  345. // 做数据处理
  346. $voiceInfo["voice"] = $this->httpurlLocal($voiceInfo["voice"]);
  347. $voice_detail = explode(",",$voiceInfo["voice_detail"]);
  348. foreach($voice_detail as $m => $n) {
  349. $voi = explode("-",$n);
  350. $voiceInfo["voice_detail_arr"][] = ["key"=>$voi[0],"value"=>$voi[1]];
  351. }
  352. $voiceInfo["best_partner"] = explode(",",$voiceInfo["best_partner"]);
  353. $this->success("获取成功!",$voiceInfo);
  354. } else {
  355. $this->success(__('数据为空!'));
  356. }
  357. }
  358. /**
  359. * 随机获取录制声音的一段文字
  360. */
  361. public function getVoiceText() {
  362. $voicetextModel = new \app\common\model\MatchVoicetext();
  363. $where = [];
  364. $list = $voicetextModel->where($where)->select();
  365. $keys = array_keys($list);
  366. shuffle($keys);
  367. $res = [];
  368. foreach($keys as $k) {
  369. $res[] = $list[$k];
  370. }
  371. $this->success(__('获取成功!'), $res);
  372. }
  373. }