131 lines
3.7 KiB
PHP
131 lines
3.7 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: Administrator
|
|
* Date: 2020/4/17
|
|
* Time: 13:26
|
|
*/
|
|
|
|
namespace app\api\service;
|
|
use app\api\model\Order as OrderModel;
|
|
use app\lib\exception\ParamException;
|
|
use think\Log;
|
|
|
|
class Order
|
|
{
|
|
protected $uid;
|
|
protected $totalMoney;
|
|
/**
|
|
* @param array $orderInfo 下单信息
|
|
* @return array 订单状态
|
|
* @throws Exception
|
|
*/
|
|
public function place($uid,$orderInfo)
|
|
{
|
|
$this->uid = $uid;
|
|
//判断是否有该权限商品
|
|
$this->getProductByType($orderInfo['vip_type']);
|
|
//判断订单是否已购买
|
|
$resOrder = $this->checkOrderStatus($uid,$orderInfo);
|
|
//有待支付的订单提交支付,没有则创建新订单
|
|
if($resOrder){
|
|
$status = [
|
|
'order_id' => $resOrder
|
|
];
|
|
}else{
|
|
//创建订单
|
|
$status = self::createOrderBy($orderInfo);
|
|
|
|
}
|
|
$status['pass'] = true;
|
|
return $status;
|
|
}
|
|
|
|
//创建订单
|
|
private function createOrderBy($orderInfo)
|
|
{
|
|
|
|
$orderNo = $this->makeOrderNo();
|
|
$order = new OrderModel();
|
|
$order->user_id = $this->uid;
|
|
$order->ord_no = $orderNo;
|
|
$order->money = $this->totalMoney;
|
|
$order->score = $orderInfo['score'];
|
|
$order->telnum = $orderInfo['telnum'];
|
|
$order->wenli = $orderInfo['subType'];
|
|
$order->mykemu = $orderInfo['mySub'];
|
|
$order->stu_yzy_province = $orderInfo['userPro'];
|
|
$order->stu_province = $orderInfo['province']*10000;
|
|
$order->province_title = config('province.province')[$order->stu_province]['title'];
|
|
if($order->mykemu){
|
|
$order->mykemu = sortStr($orderInfo['mySub']);
|
|
$order->mykemustr = changeSubString($order->mykemu);
|
|
}else{
|
|
$order->mykemustr = '';
|
|
}
|
|
$order->vip = $orderInfo['vip_type'];
|
|
$order->add_time = time();
|
|
|
|
$order->save();
|
|
if($order->id){
|
|
return [
|
|
'order_no' => $orderNo,
|
|
'order_id' => $order->id,
|
|
'create_time' => $order->add_time
|
|
];
|
|
}else{
|
|
return api_error("订单创建失败!");
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
* 判断价格是否匹配
|
|
* */
|
|
private function getProductByType($type){
|
|
$productArr = config('product.vipType');
|
|
|
|
if(array_key_exists($type,$productArr)){
|
|
$this->totalMoney = $productArr[$type];
|
|
}else{
|
|
throw new ParamException([
|
|
'msg' => '购买的权限不存在',
|
|
'erroCode' => 10000,
|
|
'code' => 404
|
|
]);
|
|
}
|
|
}
|
|
//生成订单号
|
|
public static function makeOrderNo()
|
|
{
|
|
$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
|
|
$orderSn =
|
|
$yCode[intval(date('Y')) - 2019] . strtoupper(dechex(date('m'))) . date(
|
|
'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
|
|
'%02d', rand(0, 99));
|
|
return $orderSn;
|
|
}
|
|
|
|
/*
|
|
* 判断用户是否购买该分数下的订单
|
|
* */
|
|
public function checkOrderStatus($uid,$orderInfo){
|
|
|
|
$result = OrderModel::checkOrder($uid,$orderInfo['score'],$orderInfo['userPro'],$orderInfo['vip_type'],$orderInfo['subType'],$orderInfo['mySub']);
|
|
|
|
if($result){
|
|
if($result['status'] == 1){
|
|
throw new ParamException([
|
|
'msg' => '订单已存在并支付过啦',
|
|
'erroCode' => 10000,
|
|
'code' => 400
|
|
]);
|
|
}else{
|
|
return $result['id'];
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |