作者 jinglong
1 个管道 的构建 通过 耗费 8 秒

增加订单接口

... ... @@ -333,4 +333,141 @@ class Cars extends Api
}
}
/**
* @ApiTitle (结算可使用优惠券)
* @ApiSummary (结算可使用优惠券)
* @ApiMethod (GET)
* @ApiRoute (/api/cars/settleUseCoupon)
*
* @ApiParams (name="goods_id", type="string", required=true, description="商品id(多个以逗号隔开,如:1,3,4)")
* @ApiParams (name="total_goods_price", type="inter", required=true, description="商品总金额")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575372162",
"data": [
{
"id": 2,//优惠券id
"coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
"coupon_price": "¥300",//(折扣或减少金额)
"coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
"coupon_name": "全场优惠券",//优惠券名称
"end_time": "2020.1.31",//优惠券有效期
"type": "全场通用"//优惠券用途
},
{
"id": 10,
"coupon_tag": "折扣券",
"coupon_price": "9.5折",
"coupon_tag1": "满2000可用",
"coupon_name": "商品优惠券",
"end_time": "2020.1.31",
"type": "商品可用"
}
]
})
*/
public function settleUseCoupon(){
if($this->request->isGet()){
$goods_id = $this->request->get('goods_id');
$total_goods_price = $this->request->get('total_goods_price');
$rule = config('verify.settle_use_coupon');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['goods_id'=>$goods_id,'total_goods_price'=>$total_goods_price])) {
$this->error($validate->getError());
}
//查询已经领取过
$flag = config('verify.flag');
$type = config('verify.type');
$receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid,'is_use'=>$flag[0]],'id,c_id');
$receive_s = array_column($receive,'c_id');
$goods_id_s = explode(',',$goods_id);
//查询商品所属品牌id
$res1 = Common::selectSoftWhereData('goods',['id'=>['in',$goods_id_s],'type'=>$flag[1],],'id,t_id');
$b_id_s = array_unique(array_values(array_column($res1,'t_id')));
//包含品牌中的商品
if(!empty($b_id_s)){
$where2['bg_id'] = ['in',$b_id_s];
$where2['type'] = $type[1];
}else{
$where2 = [];
}
//普通商品
$where1['bg_id'] = ['in',$goods_id_s];
$where1['type'] = $type[2];
$time = time();
$where['id'] = ['in',$receive_s];
$where['is_use'] = $flag[0];
$where['end_time'] = ['>',$time];
$data = Common::selectWhereOrData('coupon',$where,$where1,$where2,'id,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number,end_time','c_type asc,coupon_type desc');
$res = [];
foreach($data as $key=>$value){
$res[$key]['id'] = $value['id'];
//无门槛,有门槛
if($value['c_type'] == 0){
//无门槛
if($value['coupon_type'] == 0){
//减少券,去掉满减金额,折扣字段,
unset($value['full_reduce']);
unset($value['discount']);
$res[$key]['coupon_tag'] = '无门槛';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
$res[$key]['coupon_tag1'] = '无门槛';
}else{
//折扣券,去掉满减金额,减少金额字段,
unset($value['full_reduce']);
unset($value['reduce']);
$res[$key]['coupon_tag'] = '折扣券';
$res[$key]['coupon_price'] = $value['discount'].'折';
$res[$key]['coupon_tag1'] = '无门槛';
}
}else{
//有门槛
if(($total_goods_price <=> $value['full_reduce']) >= 0){
//商品总金额大于等于满减金额
if($value['coupon_type'] == 0){
//减少券,去掉折扣字段,
unset($value['discount']);
$res[$key]['coupon_tag'] = '满减券';
$res[$key]['coupon_price'] = '¥'.$value['reduce'];
$res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
}else{
//折扣券,去掉减少金额字段,
unset($value['reduce']);
$res[$key]['coupon_tag'] = '折扣券';
$res[$key]['coupon_price'] = $value['discount'].'折';
$res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
}
}
}
$res[$key]['coupon_name'] = $value['coupon_name'];//优惠券名称
$res[$key]['end_time'] = date('Y.n.j',$value['end_time']);//优惠券有效期
//全场,品牌,商品
if($value['type'] == 0){
$res[$key]['type'] = '全场通用';
}else if($value['type'] == 1){
$res[$key]['type'] = '部分品牌可用';
}else{
$res[$key]['type'] = '部分商品可用';
}
}
$this->success('成功',$res);
}else{
$this->error('请求方式错误');
}
}
}
... ...
... ... @@ -12,6 +12,7 @@ namespace app\api\controller;
use app\admin\model\Ogoods;
use app\admin\model\Order;
use app\admin\model\Rcoupon;
use think\Db;
use think\db\Query;
/**
... ... @@ -438,6 +439,9 @@ class Common
}
if(isset($data['coupon_id']) && !empty($data['coupon_id'])){
$data1['coupon_id'] = $data['coupon_id'];//优惠券id
//更新优惠券已使用
$rCouponModel = new Rcoupon();
$rCouponModel->where(['uid'=>$uid,'c_id'=>$data['coupon_id']])->update(['is_use'=>1]);
}
$data1['total_goods_price'] = $data['total_goods_price'];//商品金额
$data1['discount_price'] = $data['discount_price'];//优惠金额
... ...
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
* 订单接口
*/
class Orders extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $uid = '';//token存贮user_id
public function _initialize()
{
parent::_initialize();
$this->uid = $this->auth->getUserId();
}
/**
* @ApiTitle (我的订单)
* @ApiSummary (我的订单)
* @ApiMethod (GET)
* @ApiRoute (/api/orders/myOrderList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="status", type="inter", required=true, description="订单状态(-1:全部,0:待付款,1:待发货,2:待收货,3:已完成 )")
* @ApiParams (name="page", type="inter", required=true, description="分页页码")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575443560",
"data": {
"data": [
{
"id": 2,//订单id
"order_sn": 1000002,//订单号
"total_price": 390,//支付金额
"status": 0,//订单状态(0:待付款,1:待发货,2:待收货,3:已完成)
"goods_list": [
{
"id": 3,//附加表id
"o_id": 2,//订单id
"g_id": 1,//商品id
"style": "款式1",//商品款式
"goods_number": 1,//商品数量
"price": 200,//商品价格
"name": "MONENT 动感系列动感系列动感系列1",//商品名称
"introduce": "轻波款,为客厅缀上霞光淡雾"//商品简介
},
{
"id": 4,
"o_id": 2,
"g_id": 2,
"style": "款式2",
"goods_number": 1,
"price": 200,
"name": "MONENT 动感系列动感系列2",
"introduce": "轻波款,为客厅缀上霞光淡雾"
}
]
},
{
"id": 1,
"order_sn": 1000001,
"total_price": 200,
"goods_list": [
{
"id": 1,
"o_id": 1,
"g_id": 1,
"style": "款式1",
"goods_number": 2,
"price": 100,
"name": "MONENT 动感系列动感系列动感系列1",
"introduce": "轻波款,为客厅缀上霞光淡雾"
},
{
"id": 2,
"o_id": 1,
"g_id": 2,
"style": "款式2",
"goods_number": 3,
"price": 200,
"name": "MONENT 动感系列动感系列2",
"introduce": "轻波款,为客厅缀上霞光淡雾"
}
]
}
],
"total_page": 1//总页码
}
})
*/
public function myOrderList(){
if($this->request->isGet()){
$status = $this->request->get('status');
$page = $this->request->get('page');
$rule = config('verify.my_order');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['status'=>$status,'page'=>$page])) {
$this->error($validate->getError());
}
$arr = [];
if($status == -1){
$where = ['uid'=>$this->uid];
}else{
$where = ['uid'=>$this->uid,'status'=>$status];
}
$res = Common::selectSoftWherePageData('order',$where,'id,order_sn,status,total_price',$page);
$id_s = array_column($res,'id');
//订单附加表
$res_o_goods = Db::name('ogoods')
->alias('og')
->join('goods g','og.g_id = g.id','LEFT')
->where(['og.o_id'=>['in',$id_s]])
->field('og.id,og.o_id,og.g_id,og.style,og.goods_number,og.price,g.name,g.introduce')
->useSoftDelete('og.deletetime')
->select();
foreach ($res as &$value){
$k=0;
foreach ($res_o_goods as $o_value){
$k+=0;
if($value['id'] == $o_value['o_id']){
$value['goods_list'][$k] = $o_value;
$k++;
}
}
}
$arr['data'] = $res;
$arr['total_page'] = Common::countSoft('order',$where);
$this->success('成功',$arr);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (订单详情)
* @ApiSummary (订单详情)
* @ApiMethod (GET)
* @ApiRoute (/api/orders/myOrderDetail)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="inter", required=true, description="订单id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575444520",
"data": {
"id": 1,//订单id
"uid": 2,
"status": 0,//订单状态(0:待付款,1:待发货,2:待收货,3:已完成)
"order_sn": 1000001,//订单号
"pay_order_sn": "pay1575432163433287009591",
"receive_name": "景龙",//收货人
"receive_mobile": "13752011725",//收货电话
"receive_address": "天津市南开区",//收货地址
"leave_message": "",//留言
"discount_price": 0,//优惠价格
"total_goods_price": 200,//商品总价格
"total_expense_price": 0,//运费
"total_price": 200,//支付总金额
"coupon_id": 0,//(0:未选择优惠券)
"coupon_price": "9.5折"//为空则未选择优惠券
"createtime": "2019-12-04 14:06:52",//下单时间
"goods_list": [
{
"id": 3,//附加表id
"o_id": 2,//订单id
"g_id": 1,//商品id
"style": "款式1",//商品款式
"goods_number": 1,//商品数量
"price": 200,//商品价格
"name": "MONENT 动感系列动感系列动感系列1",//商品名称
"introduce": "轻波款,为客厅缀上霞光淡雾"//商品简介
},
{
"id": 2,
"o_id": 1,
"g_id": 2,
"style": "款式2",
"goods_number": 3,
"price": 200,
"name": "MONENT 动感系列动感系列2",
"introduce": "轻波款,为客厅缀上霞光淡雾"
}
]
}
})
*/
public function myOrderDetail(){
if($this->request->isGet()){
$order_id = $this->request->get('order_id');
$rule = config('verify.order_detail');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['order_id'=>$order_id])) {
$this->error($validate->getError());
}
$res = Common::findSoftWhereData('order',['id'=>$order_id,'uid'=>$this->uid],'updatetime,deletetime',true);
if($res){
$res['createtime'] = date('Y-m-d H:i:s',$res['createtime']);
//订单附加表
$res_o_goods = Db::name('ogoods')
->alias('og')
->join('goods g','og.g_id = g.id','LEFT')
->where(['og.o_id'=>$res['id']])
->field('og.id,og.o_id,og.g_id,og.style,og.goods_number,og.price,g.name,g.introduce')
->useSoftDelete('og.deletetime')
->select();
$res['goods_list'] = $res_o_goods;
//查询优惠券
$res['coupon_price'] = '';
if($res['coupon_id']){
$res_coupon = Common::findWhereData('coupon',['id'=>$res['coupon_id']],'*');
if($res_coupon['c_type'] == 0){
//无门槛
if($res_coupon['coupon_type'] == 0){
//减少券,去掉满减金额,折扣字段,
$res['coupon_price'] = '减:¥'.$res_coupon['reduce'];
}else{
//折扣券,去掉满减金额,减少金额字段,
$res['coupon_price'] = $res_coupon['discount'].'折';
}
}else{
//有门槛
if($res_coupon['coupon_type'] == 0){
//减少券,去掉折扣字段,
$res['coupon_price'] = '减:¥'.$res_coupon['reduce'];
}else{
//折扣券,去掉减少金额字段,
$res['coupon_price'] = $res_coupon['discount'].'折';
}
}
}
}
$this->success('成功',$res);
}else{
$this->error('请求方式错误');
}
}
}
... ...
... ... @@ -2,6 +2,7 @@
namespace app\api\controller;
use app\admin\model\Order;
use app\common\controller\Api;
use EasyWeChat\Factory;
use think\Validate;
... ... @@ -31,29 +32,30 @@ class Pay extends Api
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
* @ApiParams (name="pay_order_sn", type="inter", required=true, description="支付订单号")
* @ApiParams (name="order_sn", type="inter", required=true, description="订单号")
*
*/
public function pay(){
if($this->request->isPost()){
$openid = $this->request->post('openid');
$pay_order_sn = $this->request->post('pay_order_sn');
$order_sn = $this->request->post('order_sn');
$rule = config('verify.pay');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['openid'=>$openid,'pay_order_sn'=>$pay_order_sn])) {
if (!$validate->check(['openid'=>$openid,'order_sn'=>$order_sn])) {
$this->error($validate->getError());
}
$ffg_host = config('verify.ffg_host');
$status = $this->order_status;
//支付订单
$res = Common::findSoftWhereData('order',['uid'=>$this->uid,'pay_order_sn'=>$pay_order_sn,'status'=>$status[0]],'id,total_price');
$res = Common::findSoftWhereData('order',['uid'=>$this->uid,'order_sn'=>$order_sn,'status'=>$status[0]],'id,total_price');
if(!$res){
$this->error('无效的订单');
}
//创建支付对象
$config = config('verify.wx_pay');
$pay_order_sn = Common::genPayOrderSn('pay');//支付订单号
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => '佛山市理想中网络科技有限公司',
... ... @@ -73,7 +75,15 @@ class Pay extends Api
];
// 注意这里用的是商户平台的Key进行二次签名
$params['paySign'] = generate_sign($params, $config['key']);
$this->success('成功',$params);
//更新订单的支付订单号
$orderModel = new Order();
$res1 = $orderModel->where(['order_sn'=>$order_sn,'uid'=>$this->uid])->update(['pay_order_sn'=>$pay_order_sn]);
if($res1){
$this->success('成功',$params);
}else{
$this->error('失败');
}
}
$this->error($result['err_code_des']);
}else{
... ... @@ -94,7 +104,9 @@ class Pay extends Api
if ($message['result_code'] === 'SUCCESS') {
//支付成功,更新订单状态
$status = $this->order_status;
$orderModel = new Order();
$orderModel->where(['pay_order_sn'=>$message['out_trade_no']])->update(['status'=>$status[1]]);
}elseif($message['result_code'] === 'FAIL') {
//支付失败
}
... ... @@ -143,42 +155,35 @@ class Pay extends Api
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$pay_order_sn = Common::genPayOrderSn('pay');//支付订单号
//写入订单
$res = Common::createOrder($data,$pay_order_sn,$this->uid);
if($res){
$this->success('成功',['order_id'=>$res['order_id']]);
}
$this->error('创建订单失败');
$ffg_host = config('verify.ffg_host');
$result = $app->order->unify([
'body' => '佛山市理想中网络科技有限公司',
'out_trade_no' => $pay_order_sn,//支付订单号
'total_fee' => $data['security_price']*100,//单位分
'notify_url' => $ffg_host.'/api/pay/purchaseNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $data['openid'],
]);
// $ffg_host = config('verify.ffg_host');
// $result = $app->order->unify([
// 'body' => '佛山市理想中网络科技有限公司',
// 'out_trade_no' => $pay_order_sn,//支付订单号
// 'total_fee' => $data['security_price']*100,//单位分
// 'notify_url' => $ffg_host.'/api/pay/purchaseNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
// 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
// 'openid' => $data['openid'],
// ]);
//
// if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
// $params = [
// 'appId' => $config['app_id'],
// 'timeStamp' => time(),
// 'nonceStr' => $result['nonce_str'], // 统一下单返回的随机字符串
// 'package' => 'prepay_id='.$result['prepay_id'], // 统一下单Id
// 'signType' => 'MD5', // 签名方法
// ];
// // 注意这里用的是商户平台的Key进行二次签名
// $params['paySign'] = generate_sign($params, $config['key']);
// //写入订单
// $res = Common::createOrder($data,$pay_order_sn,$this->uid);
// if($res){
// $this->success('成功',['data'=>$params,'pay_order_sn'=>$pay_order_sn,'order_id'=>$res['order_id']]);
// }
// $this->error('创建订单失败');
// }
// $this->error($result['err_code_des']);
if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
$params = [
'appId' => $config['app_id'],
'timeStamp' => time(),
'nonceStr' => $result['nonce_str'], // 统一下单返回的随机字符串
'package' => 'prepay_id='.$result['prepay_id'], // 统一下单Id
'signType' => 'MD5', // 签名方法
];
// 注意这里用的是商户平台的Key进行二次签名
$params['paySign'] = generate_sign($params, $config['key']);
//写入订单
$res = Common::createOrder($data,$pay_order_sn,$this->uid);
if($res){
$this->success('成功',$params);
}
$this->error('创建订单失败');
}
$this->error($result['err_code_des']);
}else{
$this->error('请求方式错误');
}
... ... @@ -196,7 +201,9 @@ class Pay extends Api
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code'] === 'SUCCESS') {
//支付成功
$status = $this->order_status;
$orderModel = new Order();
$orderModel->where(['pay_order_sn'=>$message['out_trade_no']])->update(['status'=>$status[1]]);
}elseif($message['result_code'] === 'FAIL') {
//支付失败,好像有问题
}
... ... @@ -208,45 +215,4 @@ class Pay extends Api
$response->send();
}
/**
* @ApiTitle (更新订单号(支付订单失败后调用))
* @ApiSummary (更新订单号(支付订单失败后调用))
* @ApiMethod (POST)
* @ApiRoute (/api/pay/updateOrder)
* @ApiInternal
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_sn", type="inter", required=false, description="支付订单号")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571812329",
"data": null
})
*/
public function updateOrder(){
if($this->request->isPost()){
$order_sn = $this->request->post('order_sn');
$rule = config('verify.order_sn');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['order_sn'=>$order_sn])) {
$this->error($validate->getError());
}
$orderModel = new \app\admin\model\Order();
$pay_order = Common::genPayOrderSn('pay');//支付订单号
$res = $orderModel->where(['order_sn'=>$order_sn,'uid'=>$this->uid])->update(['order_sn'=>$pay_order]);
if($res){
$this->success('成功');
}else{
$this->error('失败');
}
}else{
$this->error('请求方式错误');
}
}
}
... ...
... ... @@ -325,8 +325,6 @@ class User extends Api
* @ApiRoute (/api/user/myCouponList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="goods_id", type="string", required=false, description="商品id(多个以逗号隔开,如:1,3,4)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
... ... @@ -355,40 +353,13 @@ class User extends Api
*/
public function myCouponList(){
if($this->request->isGet()){
$goods_id = $this->request->get('goods_id');
//查询已经领取过
$flag = config('verify.flag');
$type = config('verify.type');
$receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid,'is_use'=>$flag[0]],'id,c_id');
$receive_s = array_column($receive,'c_id');
if($goods_id){
$goods_id_s = explode(',',$goods_id);
//查询商品所属品牌id
$res1 = Common::selectSoftWhereData('goods',['id'=>['in',$goods_id_s],'type'=>$flag[1],],'id,t_id');
$b_id_s = array_unique(array_values(array_column($res1,'t_id')));
//包含品牌中的商品
if(!empty($b_id_s)){
$where2['bg_id'] = ['in',$b_id_s];
$where2['type'] = $type[1];
}else{
$where2 = [];
}
//普通商品
$where1['bg_id'] = ['in',$goods_id_s];
$where1['type'] = $type[2];
$time = time();
$where['id'] = ['in',$receive_s];
$where['is_use'] = $flag[0];
$where['end_time'] = ['>',$time];
$data = Common::selectWhereOrData('coupon',$where,$where1,$where2,'id,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number,end_time','c_type asc,coupon_type desc');
}else{
$where['id'] = ['in',$receive_s];
$data = Common::selectWhereData('coupon',$where,'id,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number,end_time','c_type asc,coupon_type desc');
}
$where['id'] = ['in',$receive_s];
$data = Common::selectWhereData('coupon',$where,'id,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number,end_time','c_type asc,coupon_type desc');
$res = [];
foreach($data as $key=>$value){
... ...
... ... @@ -201,11 +201,11 @@ return [
'pay' => [
'rule' => [
'openid' => 'require',
'pay_order_sn' => 'require',
'order_sn' => 'require',
],
'msg' => [
'openid.require' => 'openid不能为空',
'pay_order_sn.require' => '支付订单号不能为空',
'order_sn.require' => '订单号不能为空',
]
],
//一键领取优惠券
... ... @@ -230,4 +230,40 @@ return [
'is_flag.number' => '优惠券标识必须为数字',
]
],
//结算商品可使用优惠券
'settle_use_coupon' => [
'rule' => [
'goods_id' => 'require|number',
'total_goods_price' => 'require|number',
],
'msg' => [
'goods_id.require' => '商品id不能为空',
'goods_id.number' => '商品id必须为数字',
'total_goods_price.require' => '商品总金额不能为空',
'total_goods_price.number' => '商品总金额必须为数字',
]
],
//我的订单列表
'my_order' => [
'rule' => [
'status' => 'require|number',
'page' => 'require|number',
],
'msg' => [
'status.require' => '订单状态不能为空',
'status.number' => '订单状态必须为数字',
'page.require' => '分页页码不能为空',
'page.number' => '分页页码必须为数字',
]
],
//我的订单详情
'order_detail' => [
'rule' => [
'order_id' => 'require|number',
],
'msg' => [
'order_id.require' => '订单id不能为空',
'order_id.number' => '订单id必须为数字',
]
],
];
\ No newline at end of file
... ...
此 diff 太大无法显示。