Digit.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace app\controller\api;
  3. use app\model\AudioList;
  4. use app\model\BackConfig;
  5. use app\model\BackList;
  6. use app\model\DigitUser;
  7. use app\model\RoleList;
  8. use app\model\TaskList;
  9. use app\validate\DigitValidate;
  10. use laytp\controller\Api;
  11. use think\App;
  12. use think\Exception;
  13. use think\Request;
  14. /**
  15. * AI相关
  16. * @ApiWeigh (90)
  17. */
  18. class Digit extends Api
  19. {
  20. public $user;
  21. public function __construct(Request $request, App $app)
  22. {
  23. parent::__construct($app);
  24. $token = $request->header('Authorization');
  25. $this->user = DigitUser::where('token', $token)->where('expire_time', '>', time())->find();
  26. }
  27. public $noNeedLogin = [
  28. "list",
  29. "addDigit",
  30. 'deleteDigit',
  31. 'draftList',
  32. 'getRoleList',
  33. 'getBackList',
  34. 'getAudioList',
  35. 'saveVideoPath',
  36. 'register',
  37. 'login',
  38. 'getUserInfo',
  39. 'userUpdate',
  40. 'getBackImageByRole'
  41. ];
  42. /**
  43. * 验证登陆
  44. *
  45. * @return string|\think\response\Json
  46. */
  47. // private function checkLogin(){
  48. // if (!$this->user){
  49. // return $this->error('登陆信息已过期');
  50. // }
  51. // return '';
  52. // }
  53. /**
  54. * 获取用户信息
  55. *
  56. * @return false|string|\think\response\Json
  57. */
  58. public function getUserInfo()
  59. {
  60. if (!$this->user) {
  61. abort(401, '登陆信息已过期');
  62. }
  63. return $this->success($this->user);
  64. }
  65. /**
  66. * 更新用户信息
  67. *
  68. * @return false|string|\think\response\Json
  69. */
  70. public function userUpdate()
  71. {
  72. $params = $this->request->param();
  73. $this->user->save($params);
  74. return $this->success();
  75. }
  76. // 数字人列表
  77. public function list()
  78. {
  79. if (!$this->user) {
  80. abort(401, '登陆信息已过期');
  81. }
  82. $digit = TaskList::field('id,state,url,cover,name')
  83. ->where('is_draft', 0)
  84. ->where('user_id', $this->user->id ?? 0)
  85. ->order('id desc')
  86. ->select();
  87. return $this->success('数据获取成功', $digit);
  88. }
  89. /**
  90. *
  91. * 获取数字人草稿
  92. * @return false|string|\think\response\Json
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function draftList()
  98. {
  99. if (!$this->user) {
  100. abort(401, '登陆信息已过期');
  101. }
  102. $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')
  103. ->where('is_draft', 1)
  104. ->where('user_id', $this->user->id ?? 0)
  105. ->select();
  106. return $this->success('数据获取成功', $digit);
  107. }
  108. /**
  109. * 新增任务
  110. *
  111. * @return false|string|\think\response\Json
  112. */
  113. public function addDigit($name, $content = '', $role, $back, $audio = 0, $stage = 1.1, $is_draft = 0, $audio_url = '')
  114. {
  115. if (!$this->user) {
  116. abort(401, '登陆信息已过期');
  117. }
  118. $audio_file_name = explode('/', $audio_url);
  119. $type = 0;
  120. $validate = new DigitValidate();
  121. $data = [
  122. 'name' => $name,
  123. 'type' => empty($audio_url) ? $type : 2,
  124. 'content' => $content,
  125. 'role' => $role,
  126. 'back' => $back,
  127. 'audio' => $audio,
  128. 'stage' => $stage,
  129. 'user_id' => $this->user->id ?? 0,
  130. 'audio_file_name' => empty($audio_url) ? null : end($audio_file_name),
  131. 'audio_url' => empty($audio_url) ? null : $audio_url
  132. ];
  133. if (!$validate->check($data)) {
  134. return $this->error($validate->getError());
  135. }
  136. if (mb_strlen($content) < 10) {
  137. return $this->error('内容过短,请输入至少10个文字;');
  138. }
  139. $data = array_merge($data, ['created_at' => date('Y-m-d H:i:s'), 'is_draft' => $is_draft]);
  140. $digit = TaskList::create($data);
  141. return $this->success('创建成功');
  142. }
  143. /**
  144. *
  145. * 删除数字人
  146. * @param $id
  147. * @return false|string|\think\response\Json
  148. */
  149. public function deleteDigit($id)
  150. {
  151. if (!$this->user) {
  152. abort(401, '登陆信息已过期');
  153. }
  154. $delete = TaskList::where('id', $id)->where('user_id', $this->user->id ?? 0)->delete();
  155. if ($delete) {
  156. return $this->success('删除成功');
  157. } else {
  158. return $this->error('删除失败');
  159. }
  160. }
  161. /**
  162. * 获取角色列表
  163. *
  164. * @return false|string|\think\response\Json
  165. */
  166. public function getRoleList()
  167. {
  168. return $this->success(RoleList::field('id,url,name,proportion')->select());
  169. }
  170. /**
  171. * 获取背景列表
  172. *
  173. * @return false|string|\think\response\Json
  174. * @throws \think\db\exception\DataNotFoundException
  175. * @throws \think\db\exception\DbException
  176. * @throws \think\db\exception\ModelNotFoundException
  177. */
  178. public function getBackList()
  179. {
  180. return $this->success(BackList::field('id,url,scale')->select());
  181. }
  182. /**
  183. * 获取声音列表
  184. *
  185. * @return false|string|\think\response\Json
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\DbException
  188. * @throws \think\db\exception\ModelNotFoundException
  189. */
  190. public function getAudioList()
  191. {
  192. return $this->success(AudioList::field('id,url,name,title')->select());
  193. }
  194. protected function getSslPage($url)
  195. {
  196. $ch = curl_init();
  197. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  198. curl_setopt($ch, CURLOPT_HEADER, false);
  199. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  200. curl_setopt($ch, CURLOPT_URL, $url);
  201. curl_setopt($ch, CURLOPT_REFERER, $url);
  202. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  203. $result = curl_exec($ch);
  204. curl_close($ch);
  205. return $result;
  206. }
  207. public function saveVideoPath($videoUrl, $id = 0, $cover = '')
  208. {
  209. try {
  210. $videoContent = file_get_contents($videoUrl);
  211. $coverContent = file_get_contents($cover);
  212. $videoDir = $_SERVER['DOCUMENT_ROOT'] . '/static/video/' . date('Ymd') . '/';
  213. $imageDir = $_SERVER['DOCUMENT_ROOT'] . '/static/images/' . date('Ymd') . '/';
  214. $path = uniqid() . time() . '_video.mp4';
  215. $pathCover = uniqid() . time() . '_images.jpg';
  216. $host = $_SERVER['HTTP_HOST'] . '/static/video/' . date('Ymd') . '/';
  217. $hostCover = $_SERVER['HTTP_HOST'] . '/static/images/' . date('Ymd') . '/';
  218. $localFilePath = $videoDir . $path;
  219. $localFilePathCover = $imageDir . $pathCover;
  220. if (!is_dir($videoDir)) {
  221. mkdir($videoDir, 0777, true);
  222. }
  223. if (!is_dir($imageDir)) {
  224. mkdir($imageDir, 0777, true);
  225. }
  226. if ($videoContent !== false) {
  227. $saved = file_put_contents($localFilePath, $videoContent);
  228. $savedImages = file_put_contents($localFilePathCover, $coverContent);
  229. if ($saved !== false && $savedImages !== false) {
  230. $url = 'https://' . $host . $path;
  231. $cover = 'https://' . $hostCover . $pathCover;
  232. TaskList::where('id', $id)->update([
  233. 'url' => $url,
  234. 'state' => 1,
  235. 'cover' => $cover,
  236. 'build_at' => time()
  237. ]);
  238. return $this->success('保存成功', ['url' => $url]);
  239. } else {
  240. return $this->error('保存失败');
  241. }
  242. } else {
  243. return $this->error('保存失败#');
  244. }
  245. } catch (\Exception$exception) {
  246. var_dump($exception->getMessage());
  247. }
  248. return 'xxxx';
  249. }
  250. /**
  251. * 帐号登陆
  252. *
  253. * @param Request $request
  254. * @return false|string|\think\response\Json
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\DbException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. public function login(Request $request)
  260. {
  261. $account = $request->post('account', '');
  262. $password = $request->post('password', '');
  263. try {
  264. $user = DigitUser::where('account', $account)->find();
  265. } catch (\Exception $e) {
  266. var_dump($e->getMessage());
  267. }
  268. if (!$user) {
  269. return $this->error('帐号不存在');
  270. }
  271. if ($user->password != md5($password)) {
  272. return $this->error('密码错误');
  273. }
  274. $token = md5($user->account . uniqid());
  275. $user->token = $token;
  276. $user->updated_at = date('Y-m-d H:i:s');
  277. $user->save();
  278. return $this->success('登陆成功', ['token' => $token]);
  279. }
  280. /**
  281. * 注册成功
  282. *
  283. * @param Request $request
  284. * @return false|string|\think\response\Json
  285. */
  286. public function register(Request $request)
  287. {
  288. $account = $request->post('account', '');
  289. if (empty($account)) {
  290. return $this->error('请输入帐号');
  291. }
  292. $password = $request->post('password', '');
  293. $isUser = DigitUser::where('account', $account)->find();
  294. if ($isUser) {
  295. return $this->error('帐号已注册');
  296. }
  297. $user = DigitUser::create([
  298. 'account' => $account,
  299. 'password' => md5($password)
  300. ]);
  301. if (!$user) {
  302. return $this->error('注册失败');
  303. }
  304. return $this->success('注册成功');
  305. }
  306. /**
  307. * 获取合成背景
  308. *
  309. * @param $roleId
  310. * @param $backId
  311. * @return false|string|\think\response\Json
  312. */
  313. public function getBackImageByRole($roleId = 1,$backId = 1){
  314. $backImage = BackConfig::where(['role' => $roleId,'back' => $backId])->value('url');
  315. return $this->success('获取成功',$backImage);
  316. }
  317. }