作者 景龙
1 个管道 的构建 失败 耗费 1 秒

修改订单成功后,处理库存,购物车

... ... @@ -2,6 +2,7 @@
namespace app\api\controller;
use app\admin\model\Gift;
use app\common\controller\Api;
use app\admin\model\Gorder;
use app\admin\model\User;
... ... @@ -155,6 +156,10 @@ class Gorders extends Api
->where(['id'=>$g_id,'status'=>$this->normal])
->field('score')
->find();
$stock = $gift_data['stock'] - $g_num;
if($stock < 0){
$this->error('抱歉,您的订单存在库存不足哦');
}
$total_score = $gift_data['score']*$g_num;
$data['order_sn'] = $order_sn;//订单编号
$data['uid'] = $this->user_id;//用户uid
... ... @@ -172,8 +177,16 @@ class Gorders extends Api
$score = intval($user['score']-$total_score);
$userModel = new User();
$result = $userModel->where(['id'=>$this->user_id])->update(['score'=>$score]);
if($res && $result){
$this->success('提交成功');
//减库存
$giftModel = new Gift();
$gift = $giftModel->where(['id'=>$g_id,'status'=>$this->normal])->update(['stock'=>$stock]);
if($gift){
$this->success('提交成功');
}else{
$this->error('提交失败');
}
}else{
$this->error('提交失败');
}
... ...
... ... @@ -243,17 +243,23 @@ class Person extends Api
}
/**
* 根据规则获取积分
* 根据规则增加积分
* @param $share_uid
* @param $type
* @return bool
*/
public function getScore($share_uid,$type){
$score = config('site.'.$type);
//给分享用户增加积分
$user = new \app\admin\model\User();
$user->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
$userModel = new \app\admin\model\User();
$user = $userModel->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
//成为分享用户的下线
$offlineModel = new Line();
$offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id]);
$off = $offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id]);
if($user && $off){
return true;
}
return false;
}
}
... ...
... ... @@ -145,7 +145,7 @@ class Porders extends Api
$data = Db::table('gc_product')
->where(['status'=>$this->normal])
->whereIn('id',$p_ids)
->field('uid,price')
->field('uid,price,stock')
->select();
$orderData = [];
$order = [];
... ... @@ -160,8 +160,15 @@ class Porders extends Api
$order['unit_price'] = $value['price'];//商品单价
$order['num'] = $p_nums[$key];//商品数量
$order['total_price'] = $value['price']*$p_nums[$key];//商品总价
//可用库存
$avaiNum = $value['stock'] - $p_nums[$key];
if($avaiNum < 0){
$this->error('抱歉,您的订单存在库存不足哦');
}
array_push($orderData,$order);
}
$p_order = new Porder();
$res = $p_order->saveAll($orderData);
if($res){
... ...
... ... @@ -50,7 +50,10 @@ class Sell extends Api
//如果携带分享uid,则按照积分增加
if(!empty($sell_data['share_uid'])){
$person = new Person();
$person->getScore($sell_data['share_uid'],'share_sell');
$res = $person->getScore($sell_data['share_uid'],'share_sell');
if(!$res){
$this->error('失败');
}
}
$sell = new \app\admin\model\Sell();
$data['uid'] = $this->user_id;
... ...
... ... @@ -10,6 +10,8 @@ use Yansongda\Pay\Pay;
use app\admin\model\Account;
use app\admin\model\User;
use app\admin\model\Message;
use app\admin\model\Car;
use app\admin\model\Product;
use think\Log;
use fast\Http;
use think\Validate;
... ... @@ -19,8 +21,8 @@ use Exception;
*/
class Wxpay extends Api
{
protected $noNeedLogin = ['notify','notifyCharge'];
protected $noNeedRight = ['notify','notifyCharge'];
protected $noNeedLogin = ['notify','notifyCharge','handle'];
protected $noNeedRight = ['notify','notifyCharge','handle'];
protected $user_id = '';//token存贮user_id
protected $order_status = [];//订单状态
public function _initialize()
... ... @@ -38,7 +40,6 @@ class Wxpay 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="string", required=true, description="支付订单号")
* @ApiParams (name="share_uid", type="integer", required=false, description="分享用户id")
* @ApiReturn ({code: 0, msg: "无效的订单", time: "1554176100", data: null})
*/
public function pay(){
... ... @@ -47,7 +48,6 @@ class Wxpay extends Api
$pay = Pay::wechat(Service::getConfig('wechat'));
$openid = $this->request->post('openid');//小程序传递openid
$pay_order_sn = $this->request->post('pay_order_sn');//支付订单号
$share_uid = $this->request->post('share_uid');//分享用户uid
if(empty($openid) && empty($pay_order_sn)){
$this->error('无效的参数');
}
... ... @@ -78,12 +78,9 @@ class Wxpay extends Api
// 'total_fee' => 1,
'total_fee' => floatval($total_price)*100,//单位:分
'openid' => $openid,
'notify_url' => url('api/Wxpay/notify','','',true),
'return_url' => url('api/Wxpay/notify','','',true),
];
if(!empty($share_uid)){
$order['notify_url'] = url('api/Wxpay/notify/share_uid/'.$share_uid,'','',true);
}else{
$order['notify_url'] = url('api/Wxpay/notify','','',true);
}
//跳转或输出
$this->success('成功',$pay->miniapp($order));
... ... @@ -134,18 +131,13 @@ class Wxpay extends Api
*/
public function notify(){
$pay = Pay::wechat(Service::getConfig('wechat'));
$share_uid = $this->request->param('share_uid');
try {
$data = $pay->verify();
if($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS'){
$porderModel = new Porder();
$porderModel->where(['pay_order_sn'=>$data['out_trade_no']])->update(['status'=>$this->order_status[1]]);
//如果携带分享uid,则按照积分增加
if(!empty($share_uid)){
$person = new Person();
$person->getScore($share_uid,'share_purchase');
}
//减库存,删相应购物车记录
$this->handle($data['out_trade_no']);
}
} catch (Exception $e) {
echo "验签失败";
... ... @@ -227,4 +219,66 @@ class Wxpay extends Api
return $pay->success()->send();
}
/**
* @ApiTitle (分享购买增加积分)
* @ApiSummary (分享购买增加积分)
* @ApiMethod (GET)
* @ApiRoute (/api/wxpay/addScore)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="share_uid", type="integer", required=true, description="分享uid")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1554184134",
"data": null
})
*/
public function addScore(){
if($this->request->isGet()){
$share_uid = $this->request->get('share_uid');
//如果携带分享uid,则按照积分增加
$person = new Person();
$res = $person->getScore($share_uid,'share_purchase');
if($res){
$this->error('成功');
}else{
$this->error('失败');
}
}else{
$this->error('请求方式错误');
}
}
public function handle($pay_order_sn){
//查询订单信息的商品id,数量num
$data = Db::table('gc_porder')
->where(['pay_order_sn'=>$pay_order_sn])
->field('p_id,num')
->select();
$pIds = array_column($data,'p_id');
//减库存
$product = Db::table('gc_product')
->whereIn('id',$pIds)
->field('id,stock')
->select();
foreach($product as &$p_value){
foreach($data as $value){
if($p_value['id'] == $value['p_id']){
$p_value['stock'] = $p_value['stock'] - $value['num'];
}
}
}
//更新库存
$productModel = new Product();
$productModel->saveAll($product);
$userModel = new User();
$user = $userModel->where(['openid' => $data['openid']])->find();
//清理购物车
$carModel = new Car();
$carModel->whereIn('p_id',$pIds)->where('uid',$user['id'])->delete();
}
}
... ...