Train.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\common\model\Config;
  5. use app\common\model\Train as TrainModel;
  6. class Train extends BaseController
  7. {
  8. public function index()
  9. {
  10. return view('train/index');
  11. }
  12. public function form()
  13. {
  14. $id = input('id/d, 0');
  15. $info = TrainModel::findOrEmpty($id);
  16. return view('train/form', [
  17. 'info' => $info,
  18. ]);
  19. }
  20. public function list()
  21. {
  22. $limit = input('limit');
  23. $page = input('page');
  24. $where = [];
  25. $title = input('title', '');
  26. if (!empty($title)) {
  27. $where[] = ['title', 'like', "%{$title}%"];
  28. }
  29. $contact = input('contact', '');
  30. if (!empty($contact)) {
  31. $where[] = ['contact|mobile', 'like', "%{$contact}%"];
  32. }
  33. $status = input('status', 0);
  34. if (!empty($status)) {
  35. $where[] = ['status', '=', $status];
  36. }
  37. $list = TrainModel::order(['priority' => 'desc', 'id' => 'desc'])->where($where)->limit($limit)->page($page)->select()->append(['status_text']);
  38. $count = TrainModel::where($where)->count();
  39. if ($count == 0) {
  40. exit(json_encode([
  41. 'code' => 1,
  42. 'msg' => "未查询到数据",
  43. ]));
  44. }
  45. exit(json_encode([
  46. 'code' => 0,
  47. 'msg' => "",
  48. 'count' => $count,
  49. 'data' => $list,
  50. ]));
  51. }
  52. public function field()
  53. {
  54. $id = input('id/d');
  55. $info = TrainModel::find($id);
  56. if ($info == null) {
  57. exit(json_encode([
  58. 'code' => 1,
  59. 'msg' => "数据不存在",
  60. ]));
  61. } else {
  62. $info->save([
  63. input('field/s') => input('value/s'),
  64. ]);
  65. }
  66. exit(json_encode([
  67. 'code' => 0,
  68. ]));
  69. }
  70. public function edit()
  71. {
  72. $id = input('id/d');
  73. if (empty($id)) {
  74. TrainModel::create([
  75. 'title' => input('title/s'),
  76. 'contact' => input('contact/s'),
  77. 'mobile' => input('mobile/s'),
  78. 'address' => input('address/s'),
  79. 'status' => input('status/d') == 1 ? 1 : 2,
  80. 'priority' => input('priority/d'),
  81. 'create_time' => time(),
  82. ]);
  83. } else {
  84. $info = TrainModel::find($id);
  85. $info->save([
  86. 'title' => input('title/s'),
  87. 'contact' => input('contact/s'),
  88. 'mobile' => input('mobile/s'),
  89. 'address' => input('address/s'),
  90. 'status' => input('status/d') == 1 ? 1 : 2,
  91. 'priority' => input('priority/d'),
  92. ]);
  93. }
  94. exit(json_encode([
  95. 'code' => 0,
  96. ]));
  97. }
  98. public function del()
  99. {
  100. $id = input('id/d');
  101. $info = TrainModel::where('id', $id)->select();
  102. $result = $info->delete();
  103. if ($result) {
  104. exit(json_encode([
  105. 'code' => 0,
  106. 'msg' => "",
  107. ]));
  108. }
  109. exit(json_encode([
  110. 'code' => 1,
  111. 'msg' => "删除失败,请稍后重试",
  112. ]));
  113. }
  114. public function post()
  115. {
  116. $post = Config::getConfigValue('train_post');
  117. return view('train/post', [
  118. 'post' => $post,
  119. ]);
  120. }
  121. public function editpost()
  122. {
  123. $post = input('post', 0);
  124. Config::setConfigValueSingle('train_post', $post);
  125. exit(json_encode([
  126. 'code' => 0,
  127. ]));
  128. }
  129. }