0 ? date('Y-m-d H:i:s', $data['have_paid']) : ''; } /** * 格式化时间 deliveredtime * @return false|int|string */ public function getDeliveredtimeAttr($value, $data) { return $data['have_delivered'] > 0 ? date('Y-m-d H:i:s', $data['have_delivered']) : ''; } /** * 格式化时间 receivedtime * @return false|int|string */ public function getReceivedtimeAttr($value, $data) { return $data['have_received'] > 0 ? date('Y-m-d H:i:s', $data['have_received']) : ''; } /** * 格式化时间 commentedtime * @return false|int|string */ public function getCommentedtimeAttr($value, $data) { return $data['have_commented'] > 0 ? date('Y-m-d H:i:s', $data['have_commented']) : ''; } /** * 支付类型 */ public function getPayTypeTextAttr($value, $data) { switch ($data['pay_type']) { case self::PAY_ONLINE: return __('Online'); break; case self::PAY_OFFLINE: return __('Offline'); break; case self::PAY_WXPAY: return __('wxPay'); break; case self::PAY_ALIPAY: return __('aliPay'); break; } } /** * 加密订单id * @param $value * @param $data * @return string */ public function getOrderIdAttr($value, $data) { return Hashids::encodeHex($data['id']); } /** * * 0=全部,1=待付款,3=待核销,41=已评价,40=未评价,20=已取消 * 获取当前的订单状态 */ public function getStateAttr($value, $data) { switch (true) { case $data['have_paid'] == self::PAID_NO && $data['status'] == self::STATUS_NORMAL: $state = self::TYPE_PAY; break; /*case $data['have_delivered'] == self::DELIVERED_NO && $data['status'] == self::STATUS_NORMAL: $state = self::TYPE_DELIVES; break;*/ case $data['have_paid'] != self::PAID_NO && $data['have_received'] == self::RECEIVED_NO && $data['status'] == self::STATUS_NORMAL: $state = self::TYPE_RECEIVE; break; case $data['have_paid'] != self::PAID_NO && $data['have_received'] != self::RECEIVED_NO && $data['have_commented'] == self::COMMENTED_NO && $data['status'] == self::STATUS_NORMAL: $state = self::TYPE_NOCOMMENT; break; case $data['have_paid'] != self::PAID_NO && $data['have_received'] != self::RECEIVED_NO && $data['have_commented'] != self::COMMENTED_NO && $data['status'] == self::STATUS_NORMAL: $state = self::TYPE_COMMENTED; break; case $data['status'] == self::STATUS_CANCEL: $state = self::TYPE_CANCEL; break; //case $data['status'] == self::STATUS_REFUND && $data['refund_status'] == self::REFUND_STATUS_AGREE: // TODO 申请退款并且已通过同意,则订单为关闭状态 /* case $data['status'] == self::STATUS_REFUND && $data['refund_status'] == self::REFUND_STATUS_REFUSE: $state = self::TYPE_REFUSE; break; case $data['status'] == self::STATUS_REFUND: $state = self::TYPE_REFUND; break;*/ default: $state = self::TYPE_ALL; break; } return $state; } //获取当前的订单状态,中文 public function getStatetextAttr($state,$refund_status){ $data = [ 1 => '待付款', 3 => '待核销', 41 => '已评价', 40 => '待评价', 20 => '已取消', ]; if($state == 20 && $refund_status == 3){ return '已退款'; } return isset($data[$state]) ? $data[$state] : $state; } /** * 退款状态 */ public function getRefundStatusTextAttr($value, $data) { $name = ''; if ($data['status'] == self::STATUS_REFUND) { switch ($data['refund_status']) { case self::REFUND_STATUS_APPLY: $name = '申请中'; break; case self::REFUND_STATUS_DELIVERY: $name = '通过申请/请发货'; break; case self::REFUND_STATUS_AGREE: $name = '退款成功'; break; case self::REFUND_STATUS_REFUSE: $name = '退款失败'; break; } } return $name; } /** * 创建订单 * @param $userId * @param $data * @return int * @throws \Exception */ public function createOrder($userId, $data) { $data['userId'] = $userId; Hook::listen('create_order_before', $params, $data); list($products, $delivery, $coupon, $baseProductInfos, $address, $orderPrice, $specs, $numbers) = $params; // 获取雪花算法分布式id,方便以后扩展 $snowflake = new Snowflake(); $id = $snowflake->id(); // 优惠费用 // $discountPrice = isset($coupon['value']) ? $coupon['value'] : 0; $discountPrice = 0; // 订单费用 //$orderPrice; // 运费 // $deliveryPrice = Delivery::algorithm($delivery, array_sum($numbers)); $deliveryPrice = '0'; // 总费用 $totalPrice = bcadd(bcsub($orderPrice, $discountPrice, 2), $deliveryPrice, 2); $out_trade_no = date('Ymd',time()).uniqid().$userId; $db_pay_type = [ 'wallet' => 2, 'wechat' => 3, 'alipay' => 4, ]; (new self)->save([ 'id' => $id, 'user_id' => $userId, 'out_trade_no' => $out_trade_no, 'order_price' => $orderPrice, 'discount_price' => $discountPrice, 'delivery_price' => $deliveryPrice, 'total_price' => $totalPrice, 'pay_type' => $db_pay_type[$data['pay_type']], 'ip' => $_SERVER['REMOTE_ADDR'] ?? '', 'remark' => $data['remark'] ?? '', 'status' => self::STATUS_NORMAL, ]); (new OrderExtend)->save([ 'user_id' => $userId, 'order_id' => $id, 'coupon_id' => $coupon ? $coupon['id'] : 0, 'coupon_json' => json_encode($coupon), 'delivery_id' => $delivery ? $delivery['id'] : 0, 'delivery_json' => json_encode($delivery), // 'address_id' => $address['id'], // 'address_json' => json_encode($address), ]); $orderProduct = $specNumber = []; foreach($products as $key => $product) { $orderProduct[] = [ 'user_id' => $userId, 'order_id' => $id, 'product_id' => $product['id'], 'title' => $product['title'], 'image' => $product['image'], 'number' => $numbers[$key], 'spec' => $specs[$key] ?? '', 'price' => $baseProductInfos[$key]['sales_price'], //'product_json' => json_encode($product), // Todo 耗内存,损速度 (考虑去掉) 'createtime' => time(), 'updatetime' => time(), 'flash_id' => $data['flash_id'] ?? 0, // 秒杀id ]; if (!empty($specs[$key])) { $specNumber[$specs[$key]] = $numbers[$key]; } else { $specNumber[$key] = $numbers[$key]; } } (new OrderProduct)->insertAll($orderProduct); $data['specNumber'] = $specNumber; Hook::listen('create_order_after', $products, $data); return [ 'order_id' => Hashids::encodeHex($id), 'out_trade_no' => $out_trade_no ]; } /** * 获取我的订单 * @param int $userId 用户id * @param int $state 0=全部,1=待付款,3=待核销,4=已完成,40=待评价 */ public function getOrdersByType($userId, $state = 0, $page = 1, $pageSize = 10) { $condition['user_id'] = ['=', $userId]; switch ($state) { case self::TYPE_PAY: //1=待付款 $condition['have_paid'] = ['=', self::PAID_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'createtime'; break; /*case self::TYPE_DELIVES: $condition['have_paid'] = ['>', self::PAID_NO]; $condition['have_delivered'] = ['=', self::DELIVERED_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'have_paid'; break;*/ case self::TYPE_RECEIVE: //3=待核销 $condition['have_paid'] = ['>', self::PAID_NO]; $condition['have_delivered'] = ['>', self::DELIVERED_NO]; $condition['have_received'] = ['=', self::RECEIVED_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'have_delivered'; break; case self::TYPE_FINISH: //4=已完成 $condition['have_paid'] = ['>', self::PAID_NO]; $condition['have_delivered'] = ['>', self::DELIVERED_NO]; $condition['have_received'] = ['>', self::RECEIVED_NO]; // $condition['have_commented'] = ['=', self::COMMENTED_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'have_received'; break; case self::TYPE_NOCOMMENT: //40=待评价 $condition['have_paid'] = ['>', self::PAID_NO]; $condition['have_delivered'] = ['>', self::DELIVERED_NO]; $condition['have_received'] = ['>', self::RECEIVED_NO]; $condition['have_commented'] = ['=', self::COMMENTED_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'have_received'; break; /*case self::TYPE_REFUND: $condition['have_paid'] = ['>', self::PAID_NO]; $condition['status'] = ['=', self::STATUS_REFUND]; $condition['refund_status'] = ['<>', self::REFUND_STATUS_AGREE]; $orderBy = 'createtime'; break;*/ default: //全部 $orderBy = 'createtime'; break; } $result = $this ->with([ 'products' => function($query) { $query->field('id,id as order_product_id,title,image,number,price,spec,order_id,product_id'); }, 'evaluate' => function($query) { $query->field('id,order_id,order_product_id,product_id'); }, ]) ->where($condition) ->order([$orderBy => 'desc']) ->limit(($page - 1) * $pageSize, $pageSize) ->select(); foreach ($result as &$item) { $item->append(['order_id','state']); $item['state_text'] = $this->getStatetextAttr($item['state'],$item['refund_status']);//状态中文 $item = $item->toArray(); // 是否已评论 $evaluate = array_column($item['evaluate'], 'order_product_id'); unset($item['evaluate']); foreach ($item['products'] as &$product) { $product['image'] = Config::getImagesFullUrl($product['image']); // 是否已评论 if (in_array($product['order_product_id'], $evaluate)) { $product['evaluate'] = true; } else { $product['evaluate'] = false; } } } return $result; } //订单各个状态的数量 public function getOrdersByType_num($userId, $state = 0) { $condition['user_id'] = ['=', $userId]; switch ($state) { case self::TYPE_PAY: //1=待付款 $condition['have_paid'] = ['=', self::PAID_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'createtime'; break; /*case self::TYPE_DELIVES: $condition['have_paid'] = ['>', self::PAID_NO]; $condition['have_delivered'] = ['=', self::DELIVERED_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'have_paid'; break;*/ case self::TYPE_RECEIVE: //3=待核销 $condition['have_paid'] = ['>', self::PAID_NO]; $condition['have_delivered'] = ['>', self::DELIVERED_NO]; $condition['have_received'] = ['=', self::RECEIVED_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'have_delivered'; break; case self::TYPE_FINISH: //4=已完成 $condition['have_paid'] = ['>', self::PAID_NO]; $condition['have_delivered'] = ['>', self::DELIVERED_NO]; $condition['have_received'] = ['>', self::RECEIVED_NO]; // $condition['have_commented'] = ['=', self::COMMENTED_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'have_received'; break; case self::TYPE_NOCOMMENT: //40=待评价 $condition['have_paid'] = ['>', self::PAID_NO]; $condition['have_delivered'] = ['>', self::DELIVERED_NO]; $condition['have_received'] = ['>', self::RECEIVED_NO]; $condition['have_commented'] = ['=', self::COMMENTED_NO]; $condition['status'] = ['=', self::STATUS_NORMAL]; $orderBy = 'have_received'; break; /*case self::TYPE_REFUND: $condition['have_paid'] = ['>', self::PAID_NO]; $condition['status'] = ['=', self::STATUS_REFUND]; $condition['refund_status'] = ['<>', self::REFUND_STATUS_AGREE]; $orderBy = 'createtime'; break;*/ default: //全部 $orderBy = 'createtime'; break; } $result = $this->where($condition)->count(); return $result; } /** * 关联订单的商品 */ public function products() { return $this->hasMany('orderProduct', 'order_id', 'id'); } /** * 关联扩展订单信息 * @return \think\model\relation\HasOne */ public function extend() { return $this->hasOne('orderExtend', 'order_id', 'id'); } /** * 关联评价 * @return \think\model\relation\HasOne */ public function evaluate() { return $this->hasMany('evaluate', 'order_id', 'id'); } /** * 关联退货信息 * @return \think\model\relation\HasMany */ public function refund() { return $this->hasOne('orderRefund', 'order_id', 'id'); } /** * 关联退货的商品 * @return \think\model\relation\HasMany */ public function refundProducts() { return $this->hasMany('orderRefundProduct', 'order_id', 'id'); } }