TextreplaceController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\TextReplace;
  5. class TextreplaceController extends Base
  6. {
  7. function index()
  8. {
  9. $weid = weid();
  10. $keyword = input('post.keyword', '', 'serach_in');
  11. $query = TextReplace::where(['weid' => $weid]);
  12. if (!empty($keyword)) {
  13. $query->where('title', 'like', '%' . $keyword . '%');
  14. }
  15. $res = $query->order('sort asc,id asc')
  16. ->paginate(getpage())
  17. ->toArray();
  18. $data['data'] = $res;
  19. $data['no_replace'] = 1;
  20. return $this->json($data);
  21. }
  22. function listUpdate()
  23. {
  24. $data = only('id,status,sort');
  25. if (!$data['id']) throw new ValidateException('参数错误');
  26. TextReplace::update($data);
  27. return $this->json(['msg' => '操作成功']);
  28. }
  29. public function update()
  30. {
  31. $id = $this->request->post('id');
  32. $data = input('post.');
  33. if (empty($id)) {
  34. $data['weid'] = weid();
  35. try {
  36. $res = TextReplace::create($data);
  37. if ($res->id && empty($data['sort'])) {
  38. TextReplace::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. TextReplace::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 = TextReplace::find($id);
  58. if ($data) {
  59. $data = $data->toArray();
  60. }
  61. return $this->json(['data' => $data, 'no_replace' => 1]);
  62. }
  63. function delete()
  64. {
  65. return $this->del(new TextReplace());
  66. }
  67. }