Order.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 用户订单
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Order extends Backend
  11. {
  12. /**
  13. * Order模型对象
  14. * @var \app\admin\model\Order
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Order;
  21. $this->view->assign("ordertypeList", $this->model->getOrdertypeList());
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. $this->view->assign("paytypeList", $this->model->getPaytypeList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. //只能看自己的
  46. $where_op = [];
  47. if($this->auth->company_id){
  48. $where_op['order.company_id'] = $this->auth->company_id;
  49. }
  50. $list = $this->model
  51. ->with(['company','staff','user','preorder','servicetype'])
  52. ->where($where)
  53. ->where($where_op)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $row) {
  57. $row->getRelation('company')->visible(['name']);
  58. $row->getRelation('staff')->visible(['truename','mobile']);
  59. $row->getRelation('user')->visible(['nickname','mobile']);
  60. $row->getRelation('preorder')->visible(['pre_order_no']);
  61. $row->getRelation('servicetype')->visible(['title']);
  62. }
  63. $result = array("total" => $list->total(), "rows" => $list->items());
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 在线开单
  70. */
  71. public function submitordernew(){
  72. if(!$this->request->post()){
  73. $this->assign('staff_id',$this->auth->staff_id);
  74. return $this->view->fetch();
  75. }
  76. $data = request_post_hub([
  77. 'user_name','user_car_number','user_mobile','user_address',
  78. 'servicetype_id','server_time','server_info','server_images','pay_fee'
  79. ]);
  80. Db::startTrans();
  81. $data['pre_order_id'] = 0;
  82. //检查用户
  83. $user_info = Db::name('user')->where('mobile',$data['user_mobile'])->find();
  84. if(empty($user_info)){
  85. Db::rollback();
  86. $this->error('不存在的用户,请先让客户扫店铺码注册');
  87. }
  88. $data['user_id'] = $user_info['id'];
  89. //检索car_id,没必要了
  90. //准备数据
  91. $data['orderno'] = createUniqueNo('O',$user_info['id']);
  92. $data['ordertype'] = 2; //类型:1=预约下单,2=在线下单,3=套餐订单
  93. $data['company_id'] = $this->auth->company_id;
  94. $data['staff_id'] = $this->auth->staff_id;
  95. $data['total_fee'] = $data['pay_fee'];
  96. $data['status'] = 2; //2=已支付,待处理
  97. $data['createtime'] = time();
  98. $order_id = Db::name('order')->insertGetId($data);
  99. if(!$order_id){
  100. Db::rollback();
  101. $this->error('下单失败');
  102. }
  103. Db::commit();
  104. $this->success('下单完成',$order_id);
  105. }
  106. }