123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Request;
- use voice\IgrWsDemo;
- use WebSocket\Client;
- use app\common\library\Upload;
- /**
- * 划卡接口
- */
- class Match extends Common
- {
- protected $noNeedLogin = ['getVoiceTypeList','getVoiceText','getUserVoiceList'];
- protected $noNeedRight = ['*'];
- /**
- * 首页
- *
- */
- public function index() {
- $this->success('请求成功');
- }
- /**
- * 声音识别
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function recognizingSounds() {
- $audio = $this->request->file('audio'); // 声音文件,base64
- $voice_time = $this->request->request('voice_time'); // 声音时长
- if(!$audio) {
- $this->error(__('Invalid parameters'));
- }
- $audiourl = $this->uploads($audio);
- $url = ROOT_PATH."public".$audiourl;
- exec("node /www/wwwroot/gganli.node.api/igr-ws-node.js ".$this->auth->id." ".$url,$result);
- // $interval=2; // 每隔两秒运行一次
- // $i = 0;$res = true;
- // do{
- // $voiceInfo = $this->getVoiceInfo($audiourl,$voice_time);
- // sleep($interval);
- // $i++;
- // if($i >= 4 || $voiceInfo) {
- // $res = false;
- // }
- // }while($res);
- // 获取鉴定结果
- $analysisModel = new \app\common\model\MatchAnalysis();
- $res = $analysisModel->update(["voice"=>$audiourl,"voice_time"=>$voice_time],["user_id"=>$this->auth->id]);
- if($res !== false) {
- $this->success(__('识别完成!'));
- } else {
- $this->error("识别失败!");
- }
- }
- /**
- * 分析声音信息并生成描述
- */
- public function getVoiceInfo() {
- // 获取鉴定结果
- $analysisModel = new \app\common\model\MatchAnalysis();
- $analysisInfo = $analysisModel->where(["user_id"=>$this->auth->id])->find();
- if(!$analysisInfo) {
- $this->error("鉴定信息获取失败,请重新录制!");
- }
- // // 获取分析结果
- $voiceModel = new \app\common\model\MatchVoice();
- $voiceInfo = $voiceModel->alias("a")
- ->field("a.*,u.avatar,a.type_id,mt.name as type_name")
- ->join("hx_match_type mt","mt.id = a.type_id")
- ->join("hx_user u","u.id = a.user_id")
- ->where(["user_id"=>$this->auth->id])->find();
- if($voiceInfo && $voiceInfo["updatecode"] == $analysisInfo["updatecode"]) {// 获取的是老数据
- // 返回数据
- $voiceInfo = json_decode(json_encode($voiceInfo),true);
- // 做数据处理
- $voiceInfo["voice"] = $this->httpurlLocal($voiceInfo["voice"]);
- $voice_detail = explode(",",$voiceInfo["voice_detail"]);
- foreach($voice_detail as $m => $n) {
- $voi = explode("-",$n);
- $voiceInfo["voice_detail_arr"][] = ["key"=>$voi[0],"value"=>$voi[1]];
- }
- $voiceInfo["best_partner"] = explode(",",$voiceInfo["best_partner"]);
- $this->success("获取成功!",$voiceInfo);
- }
- // 年龄段数组
- $ageArr = ["age_middle","age_child","age_old"];
- // 分析结果
- $timbreModel = new \app\common\model\MatchTimbre();
- $where = [];
- $where["gender_type"] = $analysisInfo["gender_type"];
- $where["age_type"] = $analysisInfo["age_type"];
- $timbreList = $timbreModel->where($where)->select(); // 第一步过滤
- $ageRate = $analysisInfo[$ageArr[$analysisInfo["age_type"]]];
- $id = 0;
- if($timbreList) {
- $ageRate = intval($ageRate*100);
- foreach($timbreList as $k => $v) {
- $startRate = explode("-",$v["age_rate"])[0];
- $endRate = explode("-",$v["age_rate"])[1];
- if($ageRate > $startRate && $ageRate <= $endRate) {
- $id = $v["id"];
- break;
- }
- // for($i=1;$i<=51;$i++) {
- // if($ageRate == $startRate) {
- // $id = $v["id"];
- // } else {
- // $ageRate = $ageRate - 1;
- // }
- // }
- }
- }
- $timbreInfo = $timbreModel->where(["id"=>$id])->find(); // 第二步过滤
- // 写入数据库
- if(!$timbreInfo) $this->error("识别失败,请重新录制!");
- $nameRate1Min = explode("-",$timbreInfo["name_rate1"])[0]*100;
- $nameRate1max = explode("-",$timbreInfo["name_rate1"])[1]*100;
- $nameRate1 = rand($nameRate1Min,$nameRate1max)/100;
- $nameRate2Min = explode("-",$timbreInfo["name_rate2"])[0]*100;
- $nameRate2max = explode("-",$timbreInfo["name_rate2"])[1]*100;
- $nameRate2 = rand($nameRate2Min,$nameRate2max)/100;
- $nameRate3Min = explode("-",$timbreInfo["name_rate3"])[0]*100;
- $nameRate3max = explode("-",$timbreInfo["name_rate3"])[1]*100;
- $nameRate3 = rand($nameRate3Min,$nameRate3max)/100;
- $data = [];
- $data["type_id"] = $timbreInfo["type_id"];
- $data["voice"] = $analysisInfo['voice'];
- $data["voice_detail"] = $timbreInfo["name_type1"]."-".$nameRate1.",".$timbreInfo["name_type2"]."-".$nameRate2.",".$timbreInfo["name_type3"]."-".$nameRate3;
- $data["best_partner"] = $timbreInfo["bestlove"];
- $data["reliability"] = rand(8000,10000)/100;
- $data["commont"] = $timbreInfo["character"];
- $data["voice_time"] = $analysisInfo['voice_time'];
- $data["updatecode"] = $analysisInfo["updatecode"];
- if($voiceInfo) { // 更新
- $where = [];
- $where["user_id"] = $this->auth->id;
- $voiceModel->update($data,$where);
- } else { // 新增
- $data["user_id"] = $this->auth->id;
- $data["createtime"] = time();
- $voiceModel->insert($data);
- }
- $voiceInfo = $voiceModel->alias("a")
- ->field("a.*,u.avatar,a.type_id,mt.name as type_name")
- ->join("hx_match_type mt","mt.id = a.type_id")
- ->join("hx_user u","u.id = a.user_id")
- ->where(["user_id"=>$this->auth->id])->find();
- // 返回数据
- if($voiceInfo) {
- $voiceInfo = json_decode(json_encode($voiceInfo),true);
- // 做数据处理
- $voiceInfo["voice"] = $this->httpurl($voiceInfo["voice"]);
- $voice_detail = explode(",",$voiceInfo["voice_detail"]);
- foreach($voice_detail as $m => $n) {
- $voi = explode("-",$n);
- $voiceInfo["voice_detail_arr"][] = ["key"=>$voi[0],"value"=>$voi[1]];
- }
- $voiceInfo["best_partner"] = explode(",",$voiceInfo["best_partner"]);
- }
- if($voiceInfo) {
- $this->success("分析完成!",$voiceInfo);
- } else {
- $this->error("数据获取失败!");
- }
- }
- /**
- * 获取用户声音列表
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function getUserVoiceList() {
- $type_id = $this->request->request('type_id',0); // 声音类型
- $voiceModel = new \app\common\model\MatchVoice();
- $nolike_ids = [];
- $voicenolikeModel = new \app\common\model\MatchNolike();
- // 获取不喜欢的IDs
- // $nolike_ids = $voicenolikeModel->where(["user_id"=>$this->auth->id])->column("nolike_id");
- $where = [];
- $type_id && $where["a.type_id"] = $type_id;
- if($this->auth->isLogin()) {
- array_push($nolike_ids,$this->auth->id);// 过滤自己
- $where["a.user_id"] = ["notin",$nolike_ids];
- }
- $voiceList = $voiceModel->alias("a")
- ->field("a.id,a.user_id,u.avatar,u.nickname,a.type_id,mt.name as type_name,a.voice,a.voice_time")
- ->join("hx_match_type mt","mt.id = a.type_id")
- ->join("hx_user u","u.id = a.user_id")
- ->where($where)
- ->limit(100)
- ->select();
- if ($voiceList) {
- foreach($voiceList as $k => $v) {
- $voiceList[$k]["voice"] = $this->httpurlLocal($v["voice"]);
- }
- $this->success(__('获取成功!'), $voiceList);
- } else {
- $this->success(__('数据为空!'));
- }
- }
- /**
- * 是否已存在鉴定信息
- */
- public function isVoiceInfo() {
- $id = \app\common\model\MatchVoice::where(["user_id"=>$this->auth->id])->value("id");
- $data["isvoiceinfo"] = $id>0?1:0;
- $this->success("获取成功!",$data);
- }
- /**
- * 获取声音类型
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function getVoiceTypeList() {
- $voiceTypeModel = new \app\common\model\MatchType();
- $where = [];
- $where["is_show"] = 1;
- $voiceTypeList = $voiceTypeModel->field("id,name")->where($where)->select();
- if ($voiceTypeList) {
- $this->success(__('获取成功!'), $voiceTypeList);
- } else {
- $this->success(__('数据为空!'));
- }
- }
- /**
- * 添加喜欢声音列表
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function addVoiceLikeList() {
- $user_id = $this->request->request('user_id',0,"intval"); // 喜欢的用户ID
- $fans_id = $this->auth->id; // 添加到我喜欢,我就是粉丝
- if (!$user_id || !$fans_id) {
- $this->error(__('Invalid parameters'));
- }
- $matchLikeModel = new \app\common\model\MatchLike();
- $where = [];
- $where["user_id"] = $user_id;
- $where["fans_id"] = $fans_id;
- $matchLikeInfo = $matchLikeModel->where($where)->find();
- $data = [];
- if($matchLikeInfo) {
- $res = true;
- } else {
- $data["user_id"] = $user_id;
- $data["fans_id"] = $fans_id;
- $data["createtime"] = time();
- $res = $matchLikeModel->insert($data);
- }
- if ($res) {
- // +message
- // \app\common\model\Message::addMessage($user_id,"喜欢通知",$this->auth->nickname."(".$this->auth->u_id.") 喜欢了你,直接回复开始聊天吧");
- $message = "我喜欢了你哟,回复开始聊天吧";
- $tenim = new \app\api\controller\Tenim();
- $tenim->sendMessageToUser($fans_id,$user_id,$message);
- $this->success(__('添加成功!'), $data);
- } else {
- $this->success(__('添加失败!'));
- }
- }
- /**
- * 添加不喜欢声音列表
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function addVoiceNoLikeList() {
- $nolike_id = $this->request->request('nolike_id',0,"intval"); // 不喜欢的用户ID
- if (!$nolike_id) {
- $this->error(__('Invalid parameters'));
- }
- $matchNoLikeModel = new \app\common\model\MatchNolike();
- $where = [];
- $where["user_id"] = $this->auth->id;
- $where["nolike_id"] = $nolike_id;
- $matchLikeInfo = $matchNoLikeModel->where($where)->find();
- if($matchLikeInfo) $this->success("添加成功!");
- $data = [];
- $data["user_id"] = $this->auth->id;
- $data["nolike_id"] = $nolike_id;
- $data["createtime"] = time();
- $res = $matchNoLikeModel->insert($data);
- if ($res) {
- $this->success(__('添加成功!'));
- } else {
- $this->success(__('添加失败!'));
- }
- }
- /**
- * 获取喜欢我的
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function getVoiceFansList() {
- $page = $this->request->request('page',1); // 分页
- $pageNum = $this->request->request('pageNum',10); // 分页
- // 分页搜索构建
- $pageStart = ($page-1)*$pageNum;
- $matchLikeModel = new \app\common\model\MatchLike();
- $voiceFansList = $matchLikeModel->getVoiceFansList($this->auth->id,$pageStart,$pageNum);
- if ($voiceFansList) {
- $this->success(__('获取成功!'), $voiceFansList);
- } else {
- $this->success(__('数据为空!'));
- }
- }
- /**
- * 获取我喜欢的
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function getVoiceIdolList() {
- $page = $this->request->request('page',1); // 分页
- $pageNum = $this->request->request('pageNum',10); // 分页
- // 分页搜索构建
- $pageStart = ($page-1)*$pageNum;
- $matchLikeModel = new \app\common\model\MatchLike();
- $voiceIdolList = $matchLikeModel->getVoiceUserList($this->auth->id,$pageStart,$pageNum);
- if ($voiceIdolList) {
- $this->success(__('获取成功!'), $voiceIdolList);
- } else {
- $this->success(__('数据为空!'));
- }
- }
- /**
- * 获取我的声音鉴定信息
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function getMyVoiceInfo() {
- // 获取鉴定结果
- $analysisModel = new \app\common\model\MatchAnalysis();
- $analysisInfo = $analysisModel->where(["user_id"=>$this->auth->id])->find();
- if(!$analysisInfo) {
- $this->error("鉴定信息获取失败,请重新录制!");
- }
- // // 获取分析结果
- $voiceModel = new \app\common\model\MatchVoice();
- $voiceInfo = $voiceModel->alias("a")
- ->field("a.*,u.avatar,a.type_id,mt.name as type_name")
- ->join("hx_match_type mt","mt.id = a.type_id")
- ->join("hx_user u","u.id = a.user_id")
- ->where(["user_id"=>$this->auth->id])->find();
- if($voiceInfo) {
- // 返回数据
- $voiceInfo = json_decode(json_encode($voiceInfo),true);
- // 做数据处理
- $voiceInfo["voice"] = $this->httpurlLocal($voiceInfo["voice"]);
- $voice_detail = explode(",",$voiceInfo["voice_detail"]);
- foreach($voice_detail as $m => $n) {
- $voi = explode("-",$n);
- $voiceInfo["voice_detail_arr"][] = ["key"=>$voi[0],"value"=>$voi[1]];
- }
- $voiceInfo["best_partner"] = explode(",",$voiceInfo["best_partner"]);
- $this->success("获取成功!",$voiceInfo);
- } else {
- $this->success(__('数据为空!'));
- }
- }
- /**
- * 随机获取录制声音的一段文字
- */
- public function getVoiceText() {
- $voicetextModel = new \app\common\model\MatchVoicetext();
- $where = [];
- $list = $voicetextModel->where($where)->select();
- $keys = array_keys($list);
- shuffle($keys);
- $res = [];
- foreach($keys as $k) {
- $res[] = $list[$k];
- }
- $this->success(__('获取成功!'), $res);
- }
- }
|