123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- namespace app\controller\api;
- use app\model\AudioList;
- use app\model\BackConfig;
- use app\model\BackList;
- use app\model\DigitUser;
- use app\model\RoleList;
- use app\model\TaskList;
- use app\validate\DigitValidate;
- use laytp\controller\Api;
- use think\App;
- use think\Exception;
- use think\Request;
- /**
- * AI相关
- * @ApiWeigh (90)
- */
- class Digit extends Api
- {
- public $user;
- public function __construct(Request $request, App $app)
- {
- parent::__construct($app);
- $token = $request->header('Authorization');
- $this->user = DigitUser::where('token', $token)->where('expire_time', '>', time())->find();
- }
- public $noNeedLogin = [
- "list",
- "addDigit",
- 'deleteDigit',
- 'draftList',
- 'getRoleList',
- 'getBackList',
- 'getAudioList',
- 'saveVideoPath',
- 'register',
- 'login',
- 'getUserInfo',
- 'userUpdate',
- 'getBackImageByRole'
- ];
- /**
- * 验证登陆
- *
- * @return string|\think\response\Json
- */
- // private function checkLogin(){
- // if (!$this->user){
- // return $this->error('登陆信息已过期');
- // }
- // return '';
- // }
- /**
- * 获取用户信息
- *
- * @return false|string|\think\response\Json
- */
- public function getUserInfo()
- {
- if (!$this->user) {
- abort(401, '登陆信息已过期');
- }
- return $this->success($this->user);
- }
- /**
- * 更新用户信息
- *
- * @return false|string|\think\response\Json
- */
- public function userUpdate()
- {
- $params = $this->request->param();
- $this->user->save($params);
- return $this->success();
- }
- // 数字人列表
- public function list()
- {
- if (!$this->user) {
- abort(401, '登陆信息已过期');
- }
- $digit = TaskList::field('id,state,url,cover,name')
- ->where('is_draft', 0)
- ->where('user_id', $this->user->id ?? 0)
- ->order('id desc')
- ->select();
- return $this->success('数据获取成功', $digit);
- }
- /**
- *
- * 获取数字人草稿
- * @return false|string|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function draftList()
- {
- if (!$this->user) {
- abort(401, '登陆信息已过期');
- }
- $digit = TaskList::with(['backs', 'roles', 'audios'])->field('id,state,url,cover,name,stage,content,(audio + 1) as audio,(back + 1) as back,(role + 1) as role,audio_url')
- ->where('is_draft', 1)
- ->where('user_id', $this->user->id ?? 0)
- ->select();
- return $this->success('数据获取成功', $digit);
- }
- /**
- * 新增任务
- *
- * @return false|string|\think\response\Json
- */
- public function addDigit($name, $content = '', $role, $back, $audio = 0, $stage = 1.1, $is_draft = 0, $audio_url = '')
- {
- if (!$this->user) {
- abort(401, '登陆信息已过期');
- }
- $audio_file_name = explode('/', $audio_url);
- $type = 0;
- $validate = new DigitValidate();
- $data = [
- 'name' => $name,
- 'type' => empty($audio_url) ? $type : 2,
- 'content' => $content,
- 'role' => $role,
- 'back' => $back,
- 'audio' => $audio,
- 'stage' => $stage,
- 'user_id' => $this->user->id ?? 0,
- 'audio_file_name' => empty($audio_url) ? null : end($audio_file_name),
- 'audio_url' => empty($audio_url) ? null : $audio_url
- ];
- if (!$validate->check($data)) {
- return $this->error($validate->getError());
- }
- if (mb_strlen($content) < 10) {
- return $this->error('内容过短,请输入至少10个文字;');
- }
- $data = array_merge($data, ['created_at' => date('Y-m-d H:i:s'), 'is_draft' => $is_draft]);
- $digit = TaskList::create($data);
- return $this->success('创建成功');
- }
- /**
- *
- * 删除数字人
- * @param $id
- * @return false|string|\think\response\Json
- */
- public function deleteDigit($id)
- {
- if (!$this->user) {
- abort(401, '登陆信息已过期');
- }
- $delete = TaskList::where('id', $id)->where('user_id', $this->user->id ?? 0)->delete();
- if ($delete) {
- return $this->success('删除成功');
- } else {
- return $this->error('删除失败');
- }
- }
- /**
- * 获取角色列表
- *
- * @return false|string|\think\response\Json
- */
- public function getRoleList()
- {
- return $this->success(RoleList::field('id,url,name,proportion')->select());
- }
- /**
- * 获取背景列表
- *
- * @return false|string|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getBackList()
- {
- return $this->success(BackList::field('id,url,scale')->select());
- }
- /**
- * 获取声音列表
- *
- * @return false|string|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getAudioList()
- {
- return $this->success(AudioList::field('id,url,name,title')->select());
- }
- protected function getSslPage($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_REFERER, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- public function saveVideoPath($videoUrl, $id = 0, $cover = '')
- {
- try {
- $videoContent = file_get_contents($videoUrl);
- $coverContent = file_get_contents($cover);
- $videoDir = $_SERVER['DOCUMENT_ROOT'] . '/static/video/' . date('Ymd') . '/';
- $imageDir = $_SERVER['DOCUMENT_ROOT'] . '/static/images/' . date('Ymd') . '/';
- $path = uniqid() . time() . '_video.mp4';
- $pathCover = uniqid() . time() . '_images.jpg';
- $host = $_SERVER['HTTP_HOST'] . '/static/video/' . date('Ymd') . '/';
- $hostCover = $_SERVER['HTTP_HOST'] . '/static/images/' . date('Ymd') . '/';
- $localFilePath = $videoDir . $path;
- $localFilePathCover = $imageDir . $pathCover;
- if (!is_dir($videoDir)) {
- mkdir($videoDir, 0777, true);
- }
- if (!is_dir($imageDir)) {
- mkdir($imageDir, 0777, true);
- }
- if ($videoContent !== false) {
- $saved = file_put_contents($localFilePath, $videoContent);
- $savedImages = file_put_contents($localFilePathCover, $coverContent);
- if ($saved !== false && $savedImages !== false) {
- $url = 'https://' . $host . $path;
- $cover = 'https://' . $hostCover . $pathCover;
- TaskList::where('id', $id)->update([
- 'url' => $url,
- 'state' => 1,
- 'cover' => $cover,
- 'build_at' => time()
- ]);
- return $this->success('保存成功', ['url' => $url]);
- } else {
- return $this->error('保存失败');
- }
- } else {
- return $this->error('保存失败#');
- }
- } catch (\Exception$exception) {
- var_dump($exception->getMessage());
- }
- return 'xxxx';
- }
- /**
- * 帐号登陆
- *
- * @param Request $request
- * @return false|string|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function login(Request $request)
- {
- $account = $request->post('account', '');
- $password = $request->post('password', '');
- try {
- $user = DigitUser::where('account', $account)->find();
- } catch (\Exception $e) {
- var_dump($e->getMessage());
- }
- if (!$user) {
- return $this->error('帐号不存在');
- }
- if ($user->password != md5($password)) {
- return $this->error('密码错误');
- }
- $token = md5($user->account . uniqid());
- $user->token = $token;
- $user->updated_at = date('Y-m-d H:i:s');
- $user->save();
- return $this->success('登陆成功', ['token' => $token]);
- }
- /**
- * 注册成功
- *
- * @param Request $request
- * @return false|string|\think\response\Json
- */
- public function register(Request $request)
- {
- $account = $request->post('account', '');
- if (empty($account)) {
- return $this->error('请输入帐号');
- }
- $password = $request->post('password', '');
- $isUser = DigitUser::where('account', $account)->find();
- if ($isUser) {
- return $this->error('帐号已注册');
- }
- $user = DigitUser::create([
- 'account' => $account,
- 'password' => md5($password)
- ]);
- if (!$user) {
- return $this->error('注册失败');
- }
- return $this->success('注册成功');
- }
- /**
- * 获取合成背景
- *
- * @param $roleId
- * @param $backId
- * @return false|string|\think\response\Json
- */
- public function getBackImageByRole($roleId = 1,$backId = 1){
- $backImage = BackConfig::where(['role' => $roleId,'back' => $backId])->value('url');
- return $this->success('获取成功',$backImage);
- }
- }
|