LiveanchorController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\LiveAnchor;
  5. class LiveanchorController extends Base
  6. {
  7. function index()
  8. {
  9. $weid = weid();
  10. $keyword = input('post.keyword', '', 'serach_in');
  11. $query = LiveAnchor::where(['weid' => $weid]);
  12. if (!empty($keyword)) {
  13. $query->where('name', 'like', '%' . $keyword . '%');
  14. }
  15. $res = $query->order('sort asc,id asc')
  16. ->paginate(getpage())
  17. ->toArray();
  18. $data['data'] = $res;
  19. return $this->json($data);
  20. }
  21. function listUpdate()
  22. {
  23. $data = only('id,status,sort');
  24. if (!$data['id']) throw new ValidateException('参数错误');
  25. LiveAnchor::update($data);
  26. return $this->json(['msg' => '操作成功']);
  27. }
  28. public function update()
  29. {
  30. $id = $this->request->post('id');
  31. $data = input('post.');
  32. unset($data['create_time']);
  33. if (empty($id)) {
  34. $data['weid'] = weid();
  35. try {
  36. $res = LiveAnchor::create($data);
  37. if ($res->id && empty($data['sort'])) {
  38. LiveAnchor::update(['sort' => $res->id, 'id' => $res->id]);
  39. }
  40. } catch (\Exception $e) {
  41. throw new ValidateException($e->getMessage());
  42. }
  43. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  44. } else {
  45. try {
  46. LiveAnchor::update($data);
  47. } catch (\Exception $e) {
  48. throw new ValidateException($e->getMessage());
  49. }
  50. return $this->json(['msg' => '修改成功']);
  51. }
  52. }
  53. function getInfo()
  54. {
  55. $id = $this->request->post('id', '', 'serach_in');
  56. if (!$id) throw new ValidateException('参数错误');
  57. $data = LiveAnchor::field('*')->find($id)->toArray();
  58. return $this->json(['data' => $data]);
  59. }
  60. function delete()
  61. {
  62. return $this->del(new LiveAnchor());
  63. }
  64. }