UserCar.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class UserCar extends Api
  7. {
  8. protected $noNeedLogin = [];
  9. protected $noNeedRight = '*';
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = Db::name('user_car');
  15. }
  16. /**
  17. * 车辆列表
  18. * @return void
  19. */
  20. public function getList()
  21. {
  22. try {
  23. $userId = $this->auth->id;
  24. $field = 'id,car_number,car_model';
  25. $where['user_id'] = $userId;
  26. $result = $this->model->field($field)->where($where)->order('createtime desc')->autopage()->select();
  27. $this->success('获取成功',$result);
  28. } catch (Exception $e) {
  29. $this->error($e->getMessage());
  30. }
  31. }
  32. /**
  33. * 车辆详情
  34. * @return void
  35. */
  36. public function getInfo()
  37. {
  38. try {
  39. $id = $this->request->param('id',0);
  40. $userId = $this->auth->id;
  41. $field = 'id,car_number,car_model';
  42. $where['user_id'] = $userId;
  43. $where['id'] = $id;
  44. $result = $this->model->field($field)->where($where)->find();
  45. $this->success('获取成功',$result);
  46. } catch (Exception $e) {
  47. $this->error($e->getMessage());
  48. }
  49. }
  50. /**
  51. * 保存
  52. * @return void
  53. */
  54. public function save()
  55. {
  56. try {
  57. //验证参数
  58. $id = $this->request->param('id',0);
  59. $userId = $this->auth->id;
  60. $scene = !empty($id) ? 'edit' : 'add';
  61. $validate = validate('UserCar');
  62. if(!$validate->check($this->request->param(),[],$scene)){
  63. throw new Exception($validate->getError());
  64. }
  65. $time = time();
  66. $data = [
  67. 'car_number' => $this->request->param('car_number', ''),
  68. 'car_model' => $this->request->param('car_model', ''),
  69. ];
  70. if (empty($id)) {
  71. $data['user_id'] = $userId;
  72. $data['createtime'] = $time;
  73. $res = $this->model->insertGetId($data);
  74. } else {
  75. $data['updatetime'] = $time;
  76. $where['id'] = $id;
  77. $where['user_id'] = $userId;
  78. $res = $this->model->where($where)->update($data);
  79. }
  80. if (!$res) {
  81. throw new Exception('操作失败');
  82. }
  83. $this->success('操作成功');
  84. } catch (Exception $e) {
  85. $this->error($e->getMessage());
  86. }
  87. }
  88. /**
  89. * 删除
  90. * @return void
  91. */
  92. public function del()
  93. {
  94. try {
  95. //验证参数
  96. $id = $this->request->param('id',0);
  97. $userId = $this->auth->id;
  98. $where['id'] = $id;
  99. $where['user_id'] = $userId;
  100. $userCar = $this->model->where($where)->find();
  101. if (empty($userCar)) {
  102. throw new Exception('未找到车辆信息');
  103. }
  104. $res = $this->model->where($where)->delete();
  105. if (!$res) {
  106. throw new Exception('操作失败');
  107. }
  108. $this->success('操作成功');
  109. } catch (Exception $e) {
  110. $this->error($e->getMessage());
  111. }
  112. }
  113. }