PreOrder.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. class PreOrder extends Api
  6. {
  7. protected $noNeedLogin = [];
  8. protected $noNeedRight = '*';
  9. protected $model = null;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->model = Db::name('pre_order');
  14. }
  15. /**
  16. * 保存
  17. * @return void
  18. */
  19. public function save()
  20. {
  21. try {
  22. //验证参数
  23. $id = $this->request->param('id',0);
  24. $carId = $this->request->param('car_id',0);
  25. $preTime = $this->request->param('pre_time','');
  26. $userId = $this->auth->id;
  27. $scene = !empty($id) ? 'edit' : 'add';
  28. $validate = validate('PreOrder');
  29. if(!$validate->check($this->request->param(),[],$scene)){
  30. throw new Exception($validate->getError());
  31. }
  32. $time = time();
  33. $data = [
  34. 'company_id' => $this->request->param('company_id', 0),
  35. 'name' => $this->request->param('name', ''),
  36. 'mobile' => $this->request->param('mobile', ''),
  37. 'remark' => $this->request->param('remark', ''),
  38. 'car_id' => $carId,
  39. 'car_number' => $this->request->param('car_model', ''),
  40. 'servicetype_id' => $this->request->param('servicetype_id', ''),
  41. 'pre_time' => $this->request->param('pre_time', ''),
  42. ];
  43. if (empty($id)) {
  44. $data['user_id'] = $userId;
  45. $data['create'] = $time;
  46. $res = $this->model->insertGetId($data);
  47. } else {
  48. $data['updatetime'] = $time;
  49. $where['id'] = $id;
  50. $where['user_id'] = $userId;
  51. $res = $this->model->where($where)->update($data);
  52. }
  53. if (!$res) {
  54. throw new Exception('操作失败');
  55. }
  56. $this->success('操作成功');
  57. } catch (Exception $e) {
  58. $this->error($e->getMessage());
  59. }
  60. }
  61. }