VideoService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MemberModel;
  13. use App\Models\VideoCollectModel;
  14. use App\Models\VideoModel;
  15. use App\Services\BaseService;
  16. use App\Services\ConfigService;
  17. use App\Services\RedisService;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 短视频管理-服务类
  21. * @author laravel开发员
  22. * @since 2020/11/11
  23. * @package App\Services\Api
  24. */
  25. class VideoService extends BaseService
  26. {
  27. // 静态对象
  28. protected static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * GoodsService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new VideoModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return static|null
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = (new static());
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * 列表数据
  52. * @param $params
  53. * @param int $pageSize
  54. * @return array
  55. */
  56. public function getDataList($params, $pageSize = 18, $field = '', $userId=0)
  57. {
  58. $where = ['a.mark' => 1,'b.mark'=>1];
  59. $field = $field? $field : 'lev_a.*';
  60. $order = 'lev_a.id desc';
  61. $list = $this->model->with(['member'])->from('videos as a')
  62. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  63. ->where($where)
  64. ->where(function ($query) use ($params) {
  65. $type = isset($params['type']) ? $params['type'] : 0;
  66. if ($type > 0) {
  67. $query->where('a.type', $type);
  68. }
  69. $uid = isset($params['uid']) ? $params['uid'] : 0;
  70. if ($uid > 0) {
  71. $query->where('a.user_id', $uid);
  72. }else{
  73. $query->where('a.status', 2);
  74. }
  75. $id = isset($params['id']) ? $params['id'] : 0;
  76. if ($id > 0) {
  77. $query->whereNotIn('a.id', [$id]);
  78. }
  79. })
  80. ->where(function ($query) use ($params) {
  81. $keyword = isset($params['kw']) ? $params['kw'] : '';
  82. if ($keyword) {
  83. $query->where('a.title', 'like', "%{$keyword}%")
  84. ->orWhere('a.tags', 'like', "%{$keyword}%")
  85. ->orWhere('a.description', 'like', "%{$keyword}%")
  86. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  87. }
  88. })
  89. ->where(function ($query) use ($params, $userId) {
  90. $query->where(function($query) use($userId){
  91. // 所有人可见
  92. $query->where('a.visible_type', 1);
  93. })->orWhere(function($query) use($userId){
  94. // 关注视频发布用户可见
  95. $uids = $uids = MemberCollectService::make()->getCollectUsers(['user_id'=> $userId,'status'=>1,'mark'=>1],'collect_uid');
  96. $uids = $uids? $uids : [0];
  97. $query->where('a.visible_type', 2)->where('a.user_id', $uids); // 仅自己可见
  98. })->orWhere(function($query) use($userId){
  99. // 仅自己可见
  100. $query->where('a.visible_type', 3)->where('a.user_id', $userId); // 仅自己可见
  101. });
  102. })
  103. ->selectRaw($field)
  104. ->orderByRaw($order)
  105. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  106. $list = $list ? $list->toArray() : [];
  107. if ($list && $list['data']) {
  108. foreach ($list['data'] as &$item) {
  109. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  110. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  111. if(isset($item['albums'])){
  112. $albums = $item['albums']? json_decode($item['albums'], true):[];
  113. $item['albums'] = $albums? get_images_preview($albums) : [];
  114. }
  115. if(isset($item['file_url'])){
  116. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  117. }
  118. if(isset($item['music_url'])){
  119. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  120. }
  121. $member = isset($item['member'])? $item['member'] : [];
  122. if($member){
  123. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  124. }
  125. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  126. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  127. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? intval($item['collect_num']) : 0;
  128. $item['views'] = isset($item['views']) && $item['views']? intval($item['views']) : 0;
  129. $item['comment_num'] = isset($item['comment_num']) && $item['comment_num']? intval($item['comment_num']) : 0;
  130. $item['is_like'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 3);
  131. $item['is_collect'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 2);
  132. $item['is_follow'] = MemberCollectService::make()->checkCollect($userId, $item['user_id'], 1);
  133. $item['is_fans'] = MemberCollectService::make()->checkCollect($item['user_id'], $userId, 1);
  134. $item['is_publisher'] = $userId == $item['user_id']? 1 : 0;
  135. $item['member'] = $member;
  136. }
  137. }
  138. return [
  139. 'pageSize' => $pageSize,
  140. 'total' => isset($list['total']) ? $list['total'] : 0,
  141. 'list' => isset($list['data']) ? $list['data'] : []
  142. ];
  143. }
  144. /**
  145. * 列表数据
  146. * @param $params
  147. * @param int $pageSize
  148. * @return array
  149. */
  150. public function getIndexList($params, $pageSize = 6, $field='', $userId=0)
  151. {
  152. $cacheKey = "caches:videos:index_{$userId}_{$pageSize}";
  153. $datas = RedisService::get($cacheKey);
  154. if($datas){
  155. return $datas;
  156. }
  157. $where = ['a.mark' => 1,'a.status'=>2,'b.mark'=>1];
  158. $field = $field? $field : 'lev_a.*';
  159. $order = 'rand()';
  160. $model = $this->model->with(['member'])->from('videos as a')
  161. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  162. ->where($where)
  163. ->where(function ($query) use ($params) {
  164. $type = isset($params['type']) ? $params['type'] : 0;
  165. if ($type > 0) {
  166. $query->where('a.type', $type);
  167. }
  168. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  169. if ($uid > 0) {
  170. $query->where('a.user_id', $uid);
  171. }
  172. })
  173. ->where(function ($query) use ($params) {
  174. $keyword = isset($params['kw']) ? $params['kw'] : '';
  175. if ($keyword) {
  176. $query->where('a.title', 'like', "%{$keyword}%")
  177. ->orWhere('a.tags', 'like', "%{$keyword}%")
  178. ->orWhere('a.description', 'like', "%{$keyword}%")
  179. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  180. }
  181. })
  182. ->where(function ($query) use ($params, $userId) {
  183. $query->where(function($query) use($userId){
  184. // 所有人可见
  185. $query->where('a.visible_type', 1);
  186. })->orWhere(function($query) use($userId){
  187. // 关注视频发布用户可见
  188. $uids = $uids = MemberCollectService::make()->getCollectUsers(['user_id'=> $userId,'status'=>1,'mark'=>1],'collect_uid');
  189. $uids = $uids? $uids : [0];
  190. $query->where('a.visible_type', 2)->where('a.user_id', $uids); // 仅自己可见
  191. })->orWhere(function($query) use($userId){
  192. // 仅自己可见
  193. $query->where('a.visible_type', 3)->where('a.user_id', $userId); // 仅自己可见
  194. });
  195. });
  196. // 推荐的数据
  197. $uids = [];
  198. $countModel = clone $model;
  199. $countModel = $countModel->whereNotIn('user_id',[$userId])
  200. ->where(function($query) use($params, $userId, &$uids){
  201. // 推荐视频数据
  202. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  203. if ($isRecommend > 0) {
  204. $recommendData = VideoCollectService::make()->getRecommendData($userId);
  205. $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
  206. $tags = isset($recommendData['tags'])? $recommendData['tags'] : []; // 按标签推荐
  207. $uids = array_unique($uids);
  208. if($uids){
  209. $query->orWhere(function($query) use($uids){
  210. $query->whereIn('a.user_id', $uids);
  211. });
  212. }
  213. if($tags){
  214. $query->orWhere(function($query) use($tags){
  215. foreach($tags as $tag){
  216. $query->where('a.tags', 'like',"%{$tag}%")
  217. ->orWhere('a.description','like',"%{$tag}%");
  218. }
  219. });
  220. }
  221. }
  222. });
  223. $total = $countModel->count('a.id');
  224. // 随机优先匹配推送
  225. $match = false;
  226. $list = [];
  227. if($total > 3 ){
  228. // 关联推荐数据
  229. $list = $countModel->selectRaw($field)
  230. ->orderByRaw($order)
  231. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  232. $list = $list ? $list->toArray() : [];
  233. $list = isset($list['data']) && $list['data']? $list['data'] :[];
  234. }
  235. // 数据不够补充默认匹配数据
  236. if($total<$pageSize){
  237. // 默认推荐数据
  238. $list1 = $model->selectRaw($field)
  239. ->orderByRaw($order)
  240. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  241. $list1 = $list1 ? $list1->toArray() : [];
  242. $list1 = isset($list1['data']) && $list1['data']? $list1['data'] :[];
  243. $list = array_merge($list, $list1);
  244. }
  245. if ($list) {
  246. foreach ($list as &$item) {
  247. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  248. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  249. if(isset($item['albums'])){
  250. $albums = $item['albums']? json_decode($item['albums'], true):[];
  251. $item['albums'] = $albums? get_images_preview($albums) : [];
  252. }
  253. if(isset($item['file_url'])){
  254. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  255. }
  256. if(isset($item['music_url'])){
  257. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  258. }
  259. $member = isset($item['member'])? $item['member'] : [];
  260. if($member){
  261. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  262. }
  263. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  264. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  265. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? intval($item['collect_num']) : 0;
  266. $item['views'] = isset($item['views']) && $item['views']? intval($item['views']) : 0;
  267. $item['comment_num'] = isset($item['comment_num']) && $item['comment_num']? intval($item['comment_num']) : 0;
  268. $item['is_like'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 3);
  269. $item['is_collect'] = VideoCollectService::make()->checkCollect($userId, $item['id'], 2);
  270. $item['is_follow'] = MemberCollectService::make()->checkCollect($userId, $item['user_id'], 1);
  271. $item['is_fans'] = MemberCollectService::make()->checkCollect($item['user_id'], $userId, 1);
  272. $item['is_publisher'] = $userId == $item['user_id']? 1 : 0;
  273. $item['member'] = $member;
  274. }
  275. }
  276. $datas = [
  277. 'pageSize' => $pageSize,
  278. "match"=> $match,
  279. 'total' => count($list),
  280. 'list' => $list ? $list : []
  281. ];
  282. RedisService::set($cacheKey, $datas, rand(2,3));
  283. return $datas;
  284. }
  285. /**
  286. * 详情
  287. * @param $id
  288. * @return array
  289. */
  290. public function getInfo($id, $userId=0, $field=[])
  291. {
  292. $field = $field? $field : ['a.*'];
  293. $info = $this->model->from('videos as a')->with(['member'])
  294. ->leftJoin('member as b','b.id','=','a.user_id')
  295. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  296. ->select($field)
  297. ->first();
  298. $info = $info? $info->toArray() : [];
  299. if($info){
  300. if(isset($info['thumb'])){
  301. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  302. }
  303. if(isset($info['file_url'])){
  304. $info['file_url'] = $info['file_url']? get_image_url($info['file_url']) : '';
  305. }
  306. if(isset($item['music_url'])){
  307. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  308. }
  309. if(isset($info['albums'])){
  310. $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
  311. $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
  312. }
  313. $member = isset($info['member'])? $info['member'] : [];
  314. if($member){
  315. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  316. }
  317. $info['member'] = $member;
  318. $info['tags'] = isset($info['tags']) && $info['tags']? explode(',', $info['tags']) : [];
  319. $info['like_num'] = isset($info['like_num']) && $info['like_num']? intval($info['like_num']) : 0;
  320. $info['collect_num'] = isset($info['collect_num']) && $info['collect_num']? intval($info['collect_num']) : 0;
  321. $info['views'] = isset($info['views']) && $info['views']? intval($info['views']) : 0;
  322. $info['comment_num'] = isset($info['comment_num']) && $info['comment_num']? intval($info['comment_num']) : 0;
  323. $info['is_like'] = VideoCollectService::make()->checkCollect($userId, $info['id'], 3);
  324. $info['is_collect'] = VideoCollectService::make()->checkCollect($userId, $info['id'], 2);
  325. $info['is_follow'] = MemberCollectService::make()->checkCollect($userId, $info['user_id'], 1);
  326. $info['is_fans'] = MemberCollectService::make()->checkCollect($info['user_id'], $userId, 1);
  327. $info['is_publisher'] = $userId == $info['user_id']? 1 : 0;
  328. $info['time_text'] = isset($info['create_time']) ? dateFormat($info['create_time'], 'Y-m-d H:i') : '';
  329. // 浏览历史
  330. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  331. $data = [
  332. 'user_id'=> $userId,
  333. 'type'=> 1,
  334. 'collect_id'=> $id,
  335. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  336. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  337. 'create_time'=> time(),
  338. 'status'=> 1,
  339. ];
  340. VideoCollectModel::insert($data);
  341. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_1", $data, rand(10,30));
  342. RedisService::clear("caches:videos:recommend:{$userId}_1");
  343. RedisService::clear("caches:member:fans:{$userId}_{$id}_1");
  344. }
  345. // 浏览量
  346. $this->updateView($userId, $id);
  347. }
  348. return $info;
  349. }
  350. /**
  351. * 更新播放浏览历史
  352. * @param $userId 用户ID
  353. * @param $id 视频ID
  354. * @return false
  355. */
  356. public function updatePlay($userId, $id)
  357. {
  358. // 浏览历史
  359. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  360. $info = $this->model->from('videos as a')
  361. ->where(['a.id'=> $id,'a.mark'=>1])
  362. ->select(['a.id','a.tags','a.user_id'])
  363. ->first();
  364. if(empty($info)){
  365. return false;
  366. }
  367. $data = [
  368. 'user_id'=> $userId,
  369. 'type'=> 1,
  370. 'collect_id'=> $id,
  371. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  372. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  373. 'create_time'=> time(),
  374. 'status'=> 1,
  375. ];
  376. VideoCollectModel::insert($data);
  377. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_1", $data, rand(10,30));
  378. RedisService::clear("caches:videos:recommend:{$userId}_1");
  379. RedisService::clear("caches:member:fans:{$userId}_{$id}_1");
  380. // 清除数据只保留7天历史数据
  381. VideoCollectModel::where(['user_id'=> $userId,'type'=>1])->where('create_time', '<=', time() - 7 * 86400)->delete();
  382. }
  383. // 浏览量
  384. $this->updateView($userId, $id);
  385. $this->error = 1010;
  386. return true;
  387. }
  388. /**
  389. * 更新浏览量
  390. * @param $userId
  391. * @param $dynamicId
  392. * @return array|mixed
  393. */
  394. public function updateView($userId, $id)
  395. {
  396. $cacheKey = "caches:videos:views:u{$userId}_d{$id}";
  397. $data = RedisService::get($cacheKey);
  398. if($data){
  399. return false;
  400. }
  401. // 浏览视频任务处理
  402. TaskService::make()->updateTask($userId,2, $id);
  403. $data = $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  404. RedisService::set($cacheKey, $id, rand(1,3)*3600);
  405. return $data;
  406. }
  407. /**
  408. * 发布
  409. * @param $goodsId
  410. * @return bool
  411. */
  412. public function publish($userId, $params, $request)
  413. {
  414. $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
  415. $type = isset($params['type'])? intval($params['type']) : 2;
  416. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  417. ->select(['id','nickname','status'])
  418. ->first();
  419. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  420. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  421. if(empty($userInfo) || $status != 1){
  422. $this->error = 2017;
  423. return false;
  424. }
  425. $thumb = isset($params['thumb'])? trim($params['thumb']) : '';
  426. if($thumb){
  427. $result = upload_base64($thumb);
  428. $thumb = isset($result['file_path'])? $result['file_path'] : '';
  429. if(empty($thumb)){
  430. $this->error = 2208;
  431. return false;
  432. }
  433. }
  434. // 音乐
  435. $musicUrl = isset($params['music_url'])? trim($params['music_url']) : '';
  436. if($musicUrl){
  437. $result = upload_remote_image($musicUrl,'mp3','music');
  438. $musicUrl = isset($result['file_path'])? $result['file_path'] : '';
  439. if(empty($musicUrl)){
  440. $this->error = 2209;
  441. return false;
  442. }
  443. }
  444. // 视频
  445. $fileUrl = isset($params['file_url'])? trim($params['file_url']) : '';
  446. $albums = isset($params['album_urls'])?json_decode( $params['album_urls'],true) : [];
  447. if($type == 1) {
  448. if($fileUrl){
  449. $result = upload_video($request,'video');
  450. $data = isset($result['data'])? $result['data'] : [];
  451. $fileUrl = isset($data['file_path'])? $data['file_path'] : '';
  452. if(empty($fileUrl)){
  453. $this->error = 2212;
  454. return false;
  455. }
  456. }else{
  457. $this->error = 2213;
  458. return false;
  459. }
  460. }
  461. // 相册
  462. else if($type == 2){
  463. if(empty($albums) || !is_array($albums)){
  464. $this->error = 2211;
  465. return false;
  466. }
  467. $albums = get_format_images($albums);
  468. }
  469. $title = isset($params['title']) && $params['title']? trim($params['title']) : "{$nickname} 发布的短视频";
  470. $description = isset($params['description']) && $params['description']? trim($params['description']) : "{$nickname} 发布的短视频";
  471. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check',0);
  472. $publishCheck = $publishCheck>0? $publishCheck : 0;
  473. $tags = isset($params['tags']) && $params['tags']? $params['tags'] : '';
  474. $data = [
  475. 'user_id' => $userId,
  476. 'title' => $title,
  477. 'type' => $type,
  478. 'description' => $description,
  479. 'tags' => $tags && is_array($tags)? implode(',', $tags) : $tags,
  480. 'file_url' => $fileUrl,
  481. 'thumb' => $thumb,
  482. 'albums' => $albums? $albums : '',
  483. 'music_hash' => isset($params['music_hash'])? trim($params['music_hash']) : '',
  484. 'music_name' => isset($params['music_name'])? trim($params['music_name']) : '',
  485. 'music_url' => $musicUrl,
  486. 'goods_id' => $goodsId,
  487. 'visible_type' => isset($params['visible_type'])? intval($params['visible_type']) : 1,
  488. 'is_comment' => isset($params['is_comment'])? intval($params['is_comment']) : 1,
  489. 'status' => $publishCheck? 1 : 2,
  490. 'mark' => 1,
  491. 'publish_at' => date('Y-m-d H:i:s'),
  492. 'create_time' => time(),
  493. ];
  494. RedisService::set('caches:videos:publish', ['params'=> $params,'data'=> $data], 600);
  495. if($id = $this->model->insertGetId($data)){
  496. $this->error = 1023;
  497. // 发布视频任务处理
  498. TaskService::make()->updateTask($userId,5, $id);
  499. return ['id'=> $id];
  500. }
  501. $this->error = 1024;
  502. return false;
  503. }
  504. /**
  505. * 状态设置
  506. * @return bool
  507. */
  508. public function status()
  509. {
  510. $id = request()->post('id', 0);
  511. $status = request()->post('status', 1);
  512. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  513. $this->error = 2981;
  514. return false;
  515. }
  516. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  517. $this->error = 1002;
  518. return true;
  519. }
  520. $this->error = 1003;
  521. return true;
  522. }
  523. /**
  524. * 删除
  525. * @return bool
  526. */
  527. public function delete()
  528. {
  529. // 参数
  530. $param = request()->all();
  531. $id = getter($param, "id");
  532. if (empty($id)) {
  533. $this->error = 2014;
  534. return false;
  535. }
  536. if(!$this->model->where(['id'=> $id])->value('id')){
  537. $this->error = 1039;
  538. return false;
  539. }
  540. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  541. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  542. $this->error = 1025;
  543. return true;
  544. }else{
  545. $this->error = 1026;
  546. return false;
  547. }
  548. }
  549. }