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

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

@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace app\api\controller; 3 namespace app\api\controller;
4 4
  5 +use app\admin\model\Gift;
5 use app\common\controller\Api; 6 use app\common\controller\Api;
6 use app\admin\model\Gorder; 7 use app\admin\model\Gorder;
7 use app\admin\model\User; 8 use app\admin\model\User;
@@ -155,6 +156,10 @@ class Gorders extends Api @@ -155,6 +156,10 @@ class Gorders extends Api
155 ->where(['id'=>$g_id,'status'=>$this->normal]) 156 ->where(['id'=>$g_id,'status'=>$this->normal])
156 ->field('score') 157 ->field('score')
157 ->find(); 158 ->find();
  159 + $stock = $gift_data['stock'] - $g_num;
  160 + if($stock < 0){
  161 + $this->error('抱歉,您的订单存在库存不足哦');
  162 + }
158 $total_score = $gift_data['score']*$g_num; 163 $total_score = $gift_data['score']*$g_num;
159 $data['order_sn'] = $order_sn;//订单编号 164 $data['order_sn'] = $order_sn;//订单编号
160 $data['uid'] = $this->user_id;//用户uid 165 $data['uid'] = $this->user_id;//用户uid
@@ -172,8 +177,16 @@ class Gorders extends Api @@ -172,8 +177,16 @@ class Gorders extends Api
172 $score = intval($user['score']-$total_score); 177 $score = intval($user['score']-$total_score);
173 $userModel = new User(); 178 $userModel = new User();
174 $result = $userModel->where(['id'=>$this->user_id])->update(['score'=>$score]); 179 $result = $userModel->where(['id'=>$this->user_id])->update(['score'=>$score]);
  180 +
175 if($res && $result){ 181 if($res && $result){
176 - $this->success('提交成功'); 182 + //减库存
  183 + $giftModel = new Gift();
  184 + $gift = $giftModel->where(['id'=>$g_id,'status'=>$this->normal])->update(['stock'=>$stock]);
  185 + if($gift){
  186 + $this->success('提交成功');
  187 + }else{
  188 + $this->error('提交失败');
  189 + }
177 }else{ 190 }else{
178 $this->error('提交失败'); 191 $this->error('提交失败');
179 } 192 }
@@ -243,17 +243,23 @@ class Person extends Api @@ -243,17 +243,23 @@ class Person extends Api
243 } 243 }
244 244
245 /** 245 /**
246 - * 根据规则获取积分 246 + * 根据规则增加积分
247 * @param $share_uid 247 * @param $share_uid
248 * @param $type 248 * @param $type
  249 + * @return bool
249 */ 250 */
250 public function getScore($share_uid,$type){ 251 public function getScore($share_uid,$type){
251 $score = config('site.'.$type); 252 $score = config('site.'.$type);
252 //给分享用户增加积分 253 //给分享用户增加积分
253 - $user = new \app\admin\model\User();  
254 - $user->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score); 254 + $userModel = new \app\admin\model\User();
  255 + $user = $userModel->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
255 //成为分享用户的下线 256 //成为分享用户的下线
256 $offlineModel = new Line(); 257 $offlineModel = new Line();
257 - $offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id]); 258 + $off = $offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id]);
  259 + if($user && $off){
  260 + return true;
  261 + }
  262 + return false;
258 } 263 }
  264 +
259 } 265 }
@@ -145,7 +145,7 @@ class Porders extends Api @@ -145,7 +145,7 @@ class Porders extends Api
145 $data = Db::table('gc_product') 145 $data = Db::table('gc_product')
146 ->where(['status'=>$this->normal]) 146 ->where(['status'=>$this->normal])
147 ->whereIn('id',$p_ids) 147 ->whereIn('id',$p_ids)
148 - ->field('uid,price') 148 + ->field('uid,price,stock')
149 ->select(); 149 ->select();
150 $orderData = []; 150 $orderData = [];
151 $order = []; 151 $order = [];
@@ -160,8 +160,15 @@ class Porders extends Api @@ -160,8 +160,15 @@ class Porders extends Api
160 $order['unit_price'] = $value['price'];//商品单价 160 $order['unit_price'] = $value['price'];//商品单价
161 $order['num'] = $p_nums[$key];//商品数量 161 $order['num'] = $p_nums[$key];//商品数量
162 $order['total_price'] = $value['price']*$p_nums[$key];//商品总价 162 $order['total_price'] = $value['price']*$p_nums[$key];//商品总价
  163 +
  164 + //可用库存
  165 + $avaiNum = $value['stock'] - $p_nums[$key];
  166 + if($avaiNum < 0){
  167 + $this->error('抱歉,您的订单存在库存不足哦');
  168 + }
163 array_push($orderData,$order); 169 array_push($orderData,$order);
164 } 170 }
  171 +
165 $p_order = new Porder(); 172 $p_order = new Porder();
166 $res = $p_order->saveAll($orderData); 173 $res = $p_order->saveAll($orderData);
167 if($res){ 174 if($res){
@@ -50,7 +50,10 @@ class Sell extends Api @@ -50,7 +50,10 @@ class Sell extends Api
50 //如果携带分享uid,则按照积分增加 50 //如果携带分享uid,则按照积分增加
51 if(!empty($sell_data['share_uid'])){ 51 if(!empty($sell_data['share_uid'])){
52 $person = new Person(); 52 $person = new Person();
53 - $person->getScore($sell_data['share_uid'],'share_sell'); 53 + $res = $person->getScore($sell_data['share_uid'],'share_sell');
  54 + if(!$res){
  55 + $this->error('失败');
  56 + }
54 } 57 }
55 $sell = new \app\admin\model\Sell(); 58 $sell = new \app\admin\model\Sell();
56 $data['uid'] = $this->user_id; 59 $data['uid'] = $this->user_id;
@@ -10,6 +10,8 @@ use Yansongda\Pay\Pay; @@ -10,6 +10,8 @@ use Yansongda\Pay\Pay;
10 use app\admin\model\Account; 10 use app\admin\model\Account;
11 use app\admin\model\User; 11 use app\admin\model\User;
12 use app\admin\model\Message; 12 use app\admin\model\Message;
  13 +use app\admin\model\Car;
  14 +use app\admin\model\Product;
13 use think\Log; 15 use think\Log;
14 use fast\Http; 16 use fast\Http;
15 use think\Validate; 17 use think\Validate;
@@ -19,8 +21,8 @@ use Exception; @@ -19,8 +21,8 @@ use Exception;
19 */ 21 */
20 class Wxpay extends Api 22 class Wxpay extends Api
21 { 23 {
22 - protected $noNeedLogin = ['notify','notifyCharge'];  
23 - protected $noNeedRight = ['notify','notifyCharge']; 24 + protected $noNeedLogin = ['notify','notifyCharge','handle'];
  25 + protected $noNeedRight = ['notify','notifyCharge','handle'];
24 protected $user_id = '';//token存贮user_id 26 protected $user_id = '';//token存贮user_id
25 protected $order_status = [];//订单状态 27 protected $order_status = [];//订单状态
26 public function _initialize() 28 public function _initialize()
@@ -38,7 +40,6 @@ class Wxpay extends Api @@ -38,7 +40,6 @@ class Wxpay extends Api
38 * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") 40 * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
39 * @ApiParams (name="openid", type="string", required=true, description="小程序openid") 41 * @ApiParams (name="openid", type="string", required=true, description="小程序openid")
40 * @ApiParams (name="pay_order_sn", type="string", required=true, description="支付订单号") 42 * @ApiParams (name="pay_order_sn", type="string", required=true, description="支付订单号")
41 - * @ApiParams (name="share_uid", type="integer", required=false, description="分享用户id")  
42 * @ApiReturn ({code: 0, msg: "无效的订单", time: "1554176100", data: null}) 43 * @ApiReturn ({code: 0, msg: "无效的订单", time: "1554176100", data: null})
43 */ 44 */
44 public function pay(){ 45 public function pay(){
@@ -47,7 +48,6 @@ class Wxpay extends Api @@ -47,7 +48,6 @@ class Wxpay extends Api
47 $pay = Pay::wechat(Service::getConfig('wechat')); 48 $pay = Pay::wechat(Service::getConfig('wechat'));
48 $openid = $this->request->post('openid');//小程序传递openid 49 $openid = $this->request->post('openid');//小程序传递openid
49 $pay_order_sn = $this->request->post('pay_order_sn');//支付订单号 50 $pay_order_sn = $this->request->post('pay_order_sn');//支付订单号
50 - $share_uid = $this->request->post('share_uid');//分享用户uid  
51 if(empty($openid) && empty($pay_order_sn)){ 51 if(empty($openid) && empty($pay_order_sn)){
52 $this->error('无效的参数'); 52 $this->error('无效的参数');
53 } 53 }
@@ -78,12 +78,9 @@ class Wxpay extends Api @@ -78,12 +78,9 @@ class Wxpay extends Api
78 // 'total_fee' => 1, 78 // 'total_fee' => 1,
79 'total_fee' => floatval($total_price)*100,//单位:分 79 'total_fee' => floatval($total_price)*100,//单位:分
80 'openid' => $openid, 80 'openid' => $openid,
  81 + 'notify_url' => url('api/Wxpay/notify','','',true),
  82 + 'return_url' => url('api/Wxpay/notify','','',true),
81 ]; 83 ];
82 - if(!empty($share_uid)){  
83 - $order['notify_url'] = url('api/Wxpay/notify/share_uid/'.$share_uid,'','',true);  
84 - }else{  
85 - $order['notify_url'] = url('api/Wxpay/notify','','',true);  
86 - }  
87 //跳转或输出 84 //跳转或输出
88 $this->success('成功',$pay->miniapp($order)); 85 $this->success('成功',$pay->miniapp($order));
89 86
@@ -134,18 +131,13 @@ class Wxpay extends Api @@ -134,18 +131,13 @@ class Wxpay extends Api
134 */ 131 */
135 public function notify(){ 132 public function notify(){
136 $pay = Pay::wechat(Service::getConfig('wechat')); 133 $pay = Pay::wechat(Service::getConfig('wechat'));
137 - $share_uid = $this->request->param('share_uid');  
138 try { 134 try {
139 $data = $pay->verify(); 135 $data = $pay->verify();
140 if($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS'){ 136 if($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS'){
141 $porderModel = new Porder(); 137 $porderModel = new Porder();
142 $porderModel->where(['pay_order_sn'=>$data['out_trade_no']])->update(['status'=>$this->order_status[1]]); 138 $porderModel->where(['pay_order_sn'=>$data['out_trade_no']])->update(['status'=>$this->order_status[1]]);
143 -  
144 - //如果携带分享uid,则按照积分增加  
145 - if(!empty($share_uid)){  
146 - $person = new Person();  
147 - $person->getScore($share_uid,'share_purchase');  
148 - } 139 + //减库存,删相应购物车记录
  140 + $this->handle($data['out_trade_no']);
149 } 141 }
150 } catch (Exception $e) { 142 } catch (Exception $e) {
151 echo "验签失败"; 143 echo "验签失败";
@@ -227,4 +219,66 @@ class Wxpay extends Api @@ -227,4 +219,66 @@ class Wxpay extends Api
227 return $pay->success()->send(); 219 return $pay->success()->send();
228 } 220 }
229 221
  222 + /**
  223 + * @ApiTitle (分享购买增加积分)
  224 + * @ApiSummary (分享购买增加积分)
  225 + * @ApiMethod (GET)
  226 + * @ApiRoute (/api/wxpay/addScore)
  227 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  228 + * @ApiParams (name="share_uid", type="integer", required=true, description="分享uid")
  229 + * @ApiReturn ({
  230 + "code": 1,
  231 + "msg": "成功",
  232 + "time": "1554184134",
  233 + "data": null
  234 + })
  235 + */
  236 + public function addScore(){
  237 + if($this->request->isGet()){
  238 + $share_uid = $this->request->get('share_uid');
  239 + //如果携带分享uid,则按照积分增加
  240 + $person = new Person();
  241 + $res = $person->getScore($share_uid,'share_purchase');
  242 + if($res){
  243 + $this->error('成功');
  244 + }else{
  245 + $this->error('失败');
  246 + }
  247 + }else{
  248 + $this->error('请求方式错误');
  249 + }
  250 + }
  251 +
  252 +
  253 + public function handle($pay_order_sn){
  254 + //查询订单信息的商品id,数量num
  255 + $data = Db::table('gc_porder')
  256 + ->where(['pay_order_sn'=>$pay_order_sn])
  257 + ->field('p_id,num')
  258 + ->select();
  259 + $pIds = array_column($data,'p_id');
  260 + //减库存
  261 + $product = Db::table('gc_product')
  262 + ->whereIn('id',$pIds)
  263 + ->field('id,stock')
  264 + ->select();
  265 + foreach($product as &$p_value){
  266 + foreach($data as $value){
  267 + if($p_value['id'] == $value['p_id']){
  268 + $p_value['stock'] = $p_value['stock'] - $value['num'];
  269 + }
  270 + }
  271 + }
  272 + //更新库存
  273 + $productModel = new Product();
  274 + $productModel->saveAll($product);
  275 +
  276 + $userModel = new User();
  277 + $user = $userModel->where(['openid' => $data['openid']])->find();
  278 +
  279 + //清理购物车
  280 + $carModel = new Car();
  281 + $carModel->whereIn('p_id',$pIds)->where('uid',$user['id'])->delete();
  282 + }
  283 +
230 } 284 }