Base.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dengjq
  5. * Date: 2019/1/24
  6. * Time: 14:59
  7. */
  8. namespace app\common\model;
  9. use think\Model;
  10. abstract class Base extends Model
  11. {
  12. protected $keywordColumn = "name";
  13. public function getList()
  14. {
  15. $where = [];
  16. $keyword = input("keyword");
  17. if (empty($keyword)) {
  18. $keyword = input("searchData.keyword");
  19. }
  20. if ($keyword) {
  21. $where[] = array($this->keywordColumn, "like", "%" . $keyword . "%");
  22. }
  23. $order_by = input("order_by", $this->getPk() . " desc");
  24. $page_size = input("page_size", 10);
  25. $list = $this->where($where)->order($order_by)->paginate($page_size);
  26. return returnFormat(0, '查询成功', $list);
  27. }
  28. public function add()
  29. {
  30. $param = input('param.');
  31. $param['update_time'] = $param['create_time'] = getNow();
  32. $res = self::insert($param);
  33. if ($res === false) {
  34. return returnFormat(999, '服务器错误,新增失败');
  35. }
  36. return returnFormat(0, '添加成功');
  37. }
  38. public function detail()
  39. {
  40. $id = input("id");
  41. if (!$id) {
  42. return returnFormat(999, '缺少参数');
  43. }
  44. $where['id'] = $id;
  45. $model = $this->where($where)->find();
  46. if (!$model) {
  47. return returnFormat(999, '未找到记录');
  48. }
  49. return returnFormat(0, '', $model->toArray());
  50. }
  51. public function del()
  52. {
  53. $id = input("id");
  54. $where[$this->getPk()] = $id;
  55. $model = $this->where($where)->find();
  56. if (empty($model)) {
  57. return returnFormat(999, '未找到记录');
  58. }
  59. $res = $model->delete();
  60. if ($res === false) {
  61. return returnFormat(999, '服务器错误');
  62. }
  63. return returnFormat(0, '删除成功');
  64. }
  65. public function edit()
  66. {
  67. $id = input("id");
  68. if (!$id) {
  69. return returnFormat(999, '缺少参数');
  70. }
  71. $where[$this->getPk()] = $id;
  72. $model = $this->where($where)->find();
  73. if (!$model) {
  74. return returnFormat(999, '未找到记录');
  75. }
  76. $param = input("param.");
  77. $param[$this->getPk()] = $id;
  78. $res = self::update($param);
  79. if ($res === false) {
  80. return returnFormat(999, '服务器错误');
  81. }
  82. return returnFormat(0, '修改成功');
  83. }
  84. public function appendMerge($arr)
  85. {
  86. $arr = (array)$arr;
  87. $this->append = array_merge($this->append, $arr);
  88. }
  89. /**
  90. * 构造标准输出
  91. * @param $code
  92. * @param string $msg
  93. * @return array
  94. */
  95. public static function standardOutput($code, $msg = "", $data = [])
  96. {
  97. $res = [
  98. "code" => $code,
  99. "msg" => $msg,
  100. "data" => $data
  101. ];
  102. return $res;
  103. }
  104. }