www.wdadmin.cn // +---------------------------------------------------------------------- namespace app\api\controller\wdsxh\points; use app\admin\model\wdsxh\points\Logistics; use app\api\model\wdsxh\UserWechat; use app\common\controller\Api; use think\Exception; use think\exception\PDOException; use think\exception\ValidateException; class Order extends Api { protected $noNeedLogin = ['']; protected $noNeedRight = ['*']; protected $model = null; protected $wechat_id = null; public function _initialize() { parent::_initialize(); $this->model = new \app\api\model\wdsxh\points\Order(); $this->wechat_id = (new UserWechat())->where('user_id',$this->auth->id)->value('id'); } /** * Desc 提交结算 * Create on 2025/11/13 上午10:08 * Create by wangyafang */ public function submitSettlement() { if (!$this->request->isPost()) { $this->error('请求类型错误'); } $data = $this->request->post(); try { $validate = new \app\api\validate\wdsxh\points\Order(); if (!$validate->scene('submitSettlement')->check($data)) { $this->error($validate->getError()); } } catch (ValidateException|PDOException|Exception $e) { $this->error($e->getMessage()); } try { $user_points = (new UserWechat())->where('id',$this->wechat_id)->value('points'); $data['points'] = (new \app\api\model\wdsxh\points\Goods())->where('id',$data['goods_id'])->value('points'); $data['total_points'] = bcmul($data['points'],$data['number']); if ($data['total_points'] > $user_points) { $this->error('积分不足,无法结算'); } } catch (ValidateException|PDOException|Exception $e) { $this->error($e->getMessage()); } $data['wechat_id'] = $this->wechat_id; try { $result = (new \app\api\model\wdsxh\points\Order())->submitSettlement($data); } catch (ValidateException|PDOException|Exception $e) { $this->error($e->getMessage()); } $this->success('兑换成功', $result); } /** * Desc 兑换订单列表 * Create on 2025/11/13 上午11:52 * Create by wangyafang */ public function index() { if(!$this->request->isGet()) { $this->error('请求类型错误'); } $param = $this->request->get(); $page = isset($param['page']) ? $param['page'] : ''; $limit = isset($param['limit']) ? $param['limit'] : 10; $where = []; $where['wechat_id'] = array('eq',$this->wechat_id); if (isset($param['state']) && $param['state'] !== '') { $where['state'] = array('eq',$param['state']); } $total = $this->model->where($where)->count(); $data = $this->model ->where($where) ->field('id,order_no,number,goods_id,state,points') ->page($page,$limit) ->order('id desc') ->select(); foreach ($data as $k=>&$v){ $v->hidden(['goods_id','points']); } $this->success('请求成功',['total' => $total,'data' => $data]); } /** * Desc 订单详情 * Create on 2025/11/13 下午1:59 * Create by wangyafang */ public function detail() { if(!$this->request->isGet()) { $this->error('请求类型错误'); } $id = $this->request->get('id',''); if(!$id) { $this->error('参数错误'); } try { $where = []; $where['id'] = array('eq',$id); $where['wechat_id'] = array('eq',$this->wechat_id); $fields = 'id,order_no,number,goods_id,state,real_name,user_phone,user_address,redemption_time,total_points,points'; $data = $this->model ->where($where) ->field($fields) ->find(); if(!$data) { $this->error('订单不存在'); } if ($data->state == '3' || $data->state == '4') { $logistics = $this->model->logistics($data->id); $data->logistics = $logistics; } $data->hidden(['goods_id','points']); } catch (ValidateException|PDOException|Exception $e) { $this->error($e->getMessage()); } $this->success('请求成功',$data); } /** * Desc 确认收货 * Create on 2025/11/13 下午2:18 * Create by wangyafang */ public function confirmReceipt(){ if(!$this->request->isPost()) { $this->error('请求类型错误'); } $id = $this->request->post('id',''); if(!$id) { $this->error('参数错误'); } $where = []; $where['id'] = array('eq',$id); $where['wechat_id'] = array('eq',$this->wechat_id); $where['state'] = array('eq','3');//待收货状态 $order = $this->model->where($where)->find(); if(!$order) { $this->error('订单不存在或状态异常'); } $logisticsObj = (new Logistics())->where('order_id',$id)->find(); try { $order->state = '4';//已完成 $order->complete_time = time(); $order->save(); $logisticsObj->receive_time = time(); $logisticsObj->save(); } catch (ValidateException|PDOException|Exception $e) { $this->error($e->getMessage()); } $this->success('确认收货成功'); } }