/** * 对于30分钟内尚未结算的购物车经行清空 */ function crontab() { //清空30分钟内尚未结算的购物车商品 $cartModel = $this->model('cart'); //商品库存回归 $cart = $cartModel->where('time < ?', array($_SERVER['REQUEST_TIME'] - 1800))->select(); $productHelper = new product(); foreach ($cart as $product) { $productHelper->increaseNum($this->model('product'), $this->model('collection'), $product['pid'], $product['collection'], $product['num']); } $cartModel->where('time < ?', array($_SERVER['REQUEST_TIME'] - 1800))->delete(); }
/** * 计划任务 * 关闭超时的订单 */ function crontab() { $orderModel = $this->model('orderlist'); $weixinprepayModel = $this->model('weixinprepay'); $weixin = new weixin_gateway(config('weixin'), $this->model('system')); $timeout = (new time())->format($this->model('system')->get('timeout', 'payment'), false); $result = $orderModel->where('status=? and createtime < ?', array(0, $_SERVER['REQUEST_TIME'] - $timeout))->select(); foreach ($result as $order) { //设置订单状态为关闭 $orderModel->setStatus($order['orderno'], orderlistModel::STATUS_CLOSE, $_SERVER['REQUEST_TIME'], 0, ''); //将商品库存回退 $goods = $orderModel->getOrderDetail($order['id']); $productHelper = new product(); foreach ($goods as $product) { $productHelper->increaseNum($this->model('product'), $this->model('collection'), $product['pid'], $product['content'], $product['num']); } } }
/** * 直接购买一件商品 而不通过购物车 */ function order() { if (!login::user()) { return json_encode(array('code' => 2, 'result' => '尚未登陆')); } //是否为预订单 $preorder = $this->post->preorder; //商品id $pid = $this->post->pid; $uid = $this->session->id; if (empty($pid)) { return json_encode(array('code' => 3, 'result' => '参数错误')); } //购买数量 $num = filter::int($this->post->num); $num = empty($num) ? 1 : $num; //支付方式 $paytype = $this->post->paytype; if (empty($paytype)) { return json_encode(array('code' => 5, 'result' => '没有支付方式')); } //商品属性组合 $content = $this->post->content; $content = (new collection())->stringToArray($content); //:,转数组 $product = $this->model('product')->get($pid); //订单货款 $ordergoodsamount = $product['price'] * $num; //订单商品详情 $orderdetail = array(); //商品属性 可以看到的明白的额 $prototype = $this->model('prototype')->getByPid($pid); if (!empty($content)) { //存在多选属性的话价格以多选属性中的价格为准 $skupricestock = $this->model('collection')->find($pid, $content); if (empty($content)) { return json_encode(array('code' => 4, 'result' => '可选属性组合错误')); } $prototype = (new prototype())->format($prototype, $content); $ordergoodsamount = $skupricestock['price'] * $num; $orderdetail[] = array('sku' => $skupricestock['sku'], 'pid' => $product['id'], 'productname' => $product['name'], 'brand' => $this->model('brand')->get($product['bid'], 'name'), 'unitprice' => $skupricestock['price'], 'content' => serialize($content), 'prototype' => $prototype, 'origin' => $product['origin'], 'score' => $product['score'], 'num' => $num); } else { $prototype = (new prototype())->format($prototype, $content); $orderdetail[] = array('sku' => $product['sku'], 'pid' => $product['id'], 'productname' => $product['name'], 'brand' => $this->model('brand')->get($product['bid'], 'name'), 'unitprice' => $product['price'], 'content' => serialize($content), 'prototype' => $prototype, 'origin' => $product['origin'], 'score' => $product['score'], 'num' => $num); } //收货地址id $addressid = $this->post->addressid; //优惠代码 $coupon = $this->post->coupon; //优惠金额 $discount = 0; //判断商品是否参加了活动 switch ($product['activity']) { case 'fullcut': $fullcut = $this->model('fullcutdetail')->getPrice($pid, $ordergoodsamount); if ($fullcut !== NULL) { $discount = $fullcut['minus'] * $num; $ordergoodsamount -= $fullcut['minus']; } break; case 'sale': $sale = $this->model('sale')->getPrice($pid); if ($sale !== NULL) { $discount = $ordergoodsamount - $sale * $num; $ordergoodsamount = $sale * $num; } break; case 'seckill': $seckill = $this->model('seckill')->getPrice($pid); if ($seckill !== NULL) { $discount = $ordergoodsamount - $seckill * $num; $ordergoodsamount = $seckill * $num; } break; default: if (!empty($coupon)) { $couponModel = $this->model('coupon'); $used = $couponModel->check($coupon, $product); if (!empty($used)) { if ($ordergoodsamount >= $used['max']) { if (!$preorder) { $couponModel->increaseTimes($coupon, -1); } $ordergoodsamount = $used['type'] == 'fixed' ? $ordergoodsamount - $used['value'] : $ordergoodsamount * $used['value']; $discount = $used['type'] == 'fixed' ? $used['value'] : $ordergoodsamount * (1 - $used['value']); break; } } } break; } //计算运费 $shipid = filter::int($this->post->shipid); if (empty($shipid)) { return json_encode(array('code' => 7, 'result' => '没有配送方案')); } $shipModel = $this->model('ship'); $ship = $shipModel->get($shipid); if (empty($ship)) { return json_encode(array('code' => 5, 'result' => '错误的配送方案')); } //物流方式 $postmode = $ship['code']; //配送费用 $feeamount = $shipModel->getPrice($shipid, $ordergoodsamount); //订单税款 免税 $ordertaxamount = 0; //订单总金额 $ordertotalamount = $ordertaxamount + $ordergoodsamount + $feeamount; //支付单号 $paynumber = ''; //订单编号 $orderno = (new order())->swift($this->session->id); //成交时间 $tradetime = 0; //创建时间 $createtime = $_SERVER['REQUEST_TIME']; //成交总价 $totalamount = 0; //收件人 $address_parameter = 'city.name as city,province.name as province,address.county,address.address,address.zcode,address.name,address.telephone'; $address = $this->model('address')->get($this->post->addressid, $address_parameter); if (empty($address)) { if (empty($preorder)) { return json_encode(array('code' => 6, 'result' => '错误的配送地址')); } else { $address = array('name' => '', 'telephone' => '', 'address' => '', 'province' => '', 'city' => '', 'zcode' => '', 'county' => ''); } } $consignee = $address['name']; $consigneetel = $address['telephone']; $consigneeaddress = $address['address']; $consigneeprovince = $address['province']; $consigneecity = $address['city']; $consigneecounty = $address['county']; $zipcode = $address['zcode']; //运单号 $waybills = ''; //发件人 $sendername = $this->model('system')->get('sendername', 'system'); //公司名称 $companyname = $this->model('system')->get('companyname', 'system'); //备注信息 $note = ''; //订单状态 $status = 0; //订单来源 $client = $this->post->client; /** * 财付通专用,标注是否已经报过报过接口 1没有 2已经报过 */ $action_type = '1'; $money = 0; //余额支付 /* $extra_money = $this->model('user')->get($uid,'money'); if ($extra_money >= $ordertotalamount) { //余额足够支付订单 $extra_money -= $ordertotalamount; $ordertotalamount = 0; $money = $ordertotalamount; $status = orderlistModel::STATUS_PAYED; } else { $ordertotalamount -= $extra_money; $money = $extra_money; } if (!$preorder) { //更改用户余额 $this->model('user')->money($uid,-$money); } */ $order = array(NULL, $uid, $paytype, $paynumber, $ordertotalamount, $money, $orderno, $ordertaxamount, $ordergoodsamount, $feeamount, $tradetime, $createtime, $totalamount, $consignee, $consigneetel, $consigneeaddress, $consigneeprovince, $consigneecity, $consigneecounty, $postmode, $waybills, $sendername, $companyname, $zipcode, $note, $status, $discount, $client, $action_type); if ($preorder) { $order = array('id' => NULL, 'uid' => $uid, 'paytype' => $paytype, 'paynumber' => $paynumber, 'ordertotalamount' => $ordertotalamount, 'money' => $money, 'orderno' => $orderno, 'ordertaxamount' => $ordertaxamount, 'ordergoodsamount' => $ordergoodsamount, 'feeamount' => $feeamount, 'tradetime' => $tradetime, 'createtime' => $createtime, 'totalamount' => $totalamount, 'consignee' => $consignee, 'consigneetel' => $consigneetel, 'consigneeaddress' => $consigneeaddress, 'consigneeprovince' => $consigneeprovince, 'consigneecity' => $consigneecity, 'consigneecounty' => $consigneecounty, 'postmode' => $postmode, 'waybills' => $waybills, 'sendername' => $sendername, 'companyname' => $companyname, 'zipcode' => $zipcode, 'note' => $note, 'status' => $status, 'discount' => $discount, 'client' => $client, 'action_type' => $action_type); $order['orderdetail'] = $orderdetail; return json_encode(array('code' => 1, 'result' => 'ok', 'body' => $order)); } else { $orderModel = $this->model('orderlist'); $oid = $orderModel->create($order, $orderdetail); if ($oid) { //用户订单数量+1 $this->model('user')->where('id=?', array($uid))->increase('ordernum', 1); //商品订单数量+1 $this->model('product')->where('id=?', array($pid))->increase('ordernum', 1); //商品库存减少 $productHelper = new product(); $productHelper->increaseNum($this->model('product'), $this->model('collection'), $pid, $content, -$num); $order = $orderModel->get($oid); $order['orderdetail'] = $orderModel->getOrderDetail($oid); return json_encode(array('code' => 1, 'result' => 'ok', 'body' => $order)); } return json_encode(array('code' => 2, 'result' => '订单创建失败')); } }