Просмотр исходного кода

wesmiler 报恩寺项目提交

wesmiler 4 лет назад
Родитель
Сommit
0687fba333

+ 13 - 5
app/Http/Controllers/Api/v1/OrderController.php

@@ -31,9 +31,17 @@ class OrderController extends BaseController
     public function __construct()
     {
         parent::__construct();
-        $this->service = new GongdengOrderService();
-        $this->rechargeService = new RechargeService();
-        $this->shopService = new OrdersService();
+        $this->service = new OrdersService();
+    }
+
+    /**
+     * 列表
+     * @return array|mixed
+     */
+    public function index(){
+        $params = request()->all();
+        $params['user_id'] = $this->userId;
+        return $this->service->getDataList($params);
     }
 
     /**
@@ -43,11 +51,11 @@ class OrderController extends BaseController
      * @return array
      */
     public function exchange(Request $request, OrderValidator $validator){
-        $params = $validator->check($request->all(),'books');
+        $params = $validator->check($request->all(),'exchange');
         if(!is_array($params)){
             return message($params, false);
         }
 
-        return $this->shopService->exchange($this->userId);
+        return $this->service->exchange($this->userId);
     }
 }

+ 6 - 2
app/Http/Validator/OrderValidator.php

@@ -5,7 +5,9 @@ class OrderValidator extends BaseValidator
     // 当前模型所有验证规则
     public static $rules = [
         'id' => 'required',
-        'num' => 'required|string|min:1|max:100',
+        'num' => 'required|min:1|max:100',
+        'realname' => 'required|string|min:1|max:30',
+        'mobile' => 'required|string|min:1|max:20',
         'remark' => 'nullable|string|min:2|max:150',
     ];
 
@@ -23,13 +25,15 @@ class OrderValidator extends BaseValidator
     public static $fields = [
         'id' => '商品ID',
         'num' => '购买数量',
+        'realname' => '收货人姓名',
+        'mobile' => '收货人联系方式',
         'remark' => '备注内容',
     ];
 
     // 当前模型所有验证场景
     public static $scenes = [
         'info'=> ['id'],
-        'buy'=> ['id','num','remark'],
+        'exchange'=> ['id','num','remark','realname','mobile'],
     ];
 
     /**

+ 5 - 0
app/Services/GoodsService.php

@@ -12,6 +12,7 @@
 namespace App\Services;
 
 use App\Models\GoodsModel;
+use App\Models\OrdersModel;
 
 /**
  * 商品管理-服务类
@@ -137,6 +138,10 @@ class GoodsService extends BaseService
             foreach ($dataList['data'] as &$item) {
                 $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
                 $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
+
+                // 销量
+                $sale = OrdersModel::where(['goods_id'=> $item['id'],'mark'=> 1])->whereIn('status',[2,3,4])->count('id');
+                $item['sale'] = $sale? $sale : 0;
             }
             unset($item);
         }

+ 124 - 67
app/Services/OrdersService.php

@@ -12,7 +12,10 @@
 namespace App\Services;
 
 use App\Models\GoodsModel;
+use App\Models\MemberModel;
 use App\Models\OrdersModel;
+use App\Models\TradeModel;
+use Illuminate\Support\Facades\DB;
 
 /**
  * 商城订单管理-服务类
@@ -90,6 +93,46 @@ class OrdersService extends BaseService
     }
 
     /**
+     * 获取列表
+     * @return array
+     * @since 2020/11/11
+     * @author wesmiler
+     */
+    public function getDataList($params)
+    {
+        $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
+        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
+        $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
+
+        $dataList = $this->model::from('orders as o')
+            ->leftJoin('goods as g', 'g.id', '=', 'o.goods_id')
+            ->leftJoin('member as m', 'm.id', '=', 'o.user_id')
+            ->leftJoin('express as es', 'es.express_code', '=', 'o.express_code')
+            ->where(['o.user_id'=> $userId,'o.mark'=> 1])
+            ->where('o.status','>', 0)
+            ->select(['o.*','g.title as goods_name','g.thumb','m.nickname','m.mobile','es.name as express_company'])
+            ->orderBy('o.create_time', 'desc')
+            ->paginate($pageSize);
+
+        $dataList = $dataList ? $dataList->toArray() : [];
+        if ($dataList) {
+            foreach ($dataList['data'] as &$item) {
+                $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
+                $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
+            }
+            unset($item);
+        }
+
+        return [
+            'code' => 0,
+            'success'=> true,
+            'msg' => '操作成功',
+            'count' => isset($dataList['total']) ? $dataList['total'] : 0,
+            'data' => isset($dataList['data']) ? $dataList['data'] : 0,
+        ];
+    }
+
+    /**
      * 添加或编辑
      * @return array
      * @since 2020/11/11
@@ -150,110 +193,124 @@ class OrdersService extends BaseService
     /**
      * 积分商品兑换
      */
-    public function buy($userId)
+    public function exchange($userId)
     {
         $params = request()->all();
         // 验证佛像信息
-        $fid = isset($params['fid']) ? $params['fid'] : 0;
-        $info = GoodsModel::where(['id' => $fid, 'status' => 1, 'mark' => 1])
-            ->select(['id', 'title'])
+        $id = isset($params['id']) ? $params['id'] : 0;
+        $info = GoodsModel::where(['id' => $id, 'status' => 1, 'mark' => 1])
+            ->select(['id', 'title','score','price','stock'])
             ->first();
-        if (!$info || $fid <= 0) {
+        if (!$info || $id <= 0) {
             return message('商品信息不存在', false);
         }
 
         $payType = isset($params['payType']) ? $params['payType'] : 0;
-        if (!in_array($payType, [1])) {
+        if (!in_array($payType, [1,4])) {
             return message('支付方式暂不支持', false);
         }
 
-        // 套餐信息
-        $mid = isset($params['mid']) ? $params['mid'] : 0;
-        $mealInfo = LampMealsModel::where(['id' => $mid, 'status' => 1, 'mark' => 1])
-            ->select(['id', 'name', 'price', 'num'])
-            ->first();
-        if (!$mealInfo) {
-            return message('供灯套餐不存在或已下架', false);
+        // 库存
+        $num = isset($params['num'])? $params['num'] : 0;
+        if($info->stock < $num){
+            return message('商品库存不足,仅剩'.$info->stock.'个', false);
         }
 
         // 验证用户是否已授权
         $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1, 'status' => 1])
-            ->select('id', 'openid', 'nickname')
+            ->select('id', 'openid', 'nickname','coupon','score')
             ->first();
-        $openid = isset($memberInfo['openid']) ? trim($memberInfo['openid']) : '';
         if (!$memberInfo) {
             return message('账号已被冻结,请联系客服', false);
         }
 
-        if (empty($openid)) {
-            return message('账号获取授权参数失败,请退出重试', false);
-        }
+        // 创建订单
 
-        // 验证是否供灯过
-        $buyType = 1;
-        $gdCheckData = GongdengOrderModel::where(['source_id' => $fid, 'user_id' => $userId,'lamp_status'=> 2, 'status' => 2, 'mark' => 1])
-            ->where('expire_time', '>=', time())
-            ->select(['id', 'source_id', 'user_id','device_num', 'expire_time', 'params'])
-            ->first();
+        $price = 0;
+        if($payType == 1){
+            $field = 'coupon';
+            $payTypeName = '花灯券';
+            $price = $info->price;
+            $total = moneyFormat($num * $info->price);
+
+            // 验证账户
+            if($memberInfo->coupon < $total){
+                return message( "账号{$payTypeName}不足,请先充值", false);
+            }
 
-        // 续费
-        if ($gdCheckData && $gdCheckData->id) {
-            $buyType = 2;
+        }else if($payType == 4){
+            $field = 'score';
+            $payTypeName = '积分';
+            $price = $info->score;
+            $total = moneyFormat($num * $info->score);
+            // 验证账户
+            if($memberInfo->score < $total){
+                return message( "账号{$payTypeName}不足,请更换其他支付方式", false);
+            }
         }
 
-        // 创建订单
-        $expireTime = ($mealInfo->num * 24 * 3600) + time();
+        // 扣款和处理
+        \DB::beginTransaction();
+
+        // 处理
         $data = [
-            'source_id' => $fid,
+            'type' => 1,
             'user_id' => $userId,
             'pay_type' => $payType,
-            'buy_type' => $buyType,
-            'order_sn' => get_order_num('G'),
-            'num' => 1,
-            'price' => $mealInfo->price,
-            'total' => $mealInfo->price,
-            'sf_name' => isset($params['sf_name']) ? trim($params['sf_name']) : '',
-            'qf_content' => isset($params['qf_content']) ? trim($params['qf_content']) : '',
-            'is_hide' => isset($params['is_hide']) ? intval($params['is_hide']) : 0,
-            'expire_time' => $expireTime,
+            'goods_id' => $id,
+            'order_sn' => get_order_num('S'),
+            'num' => $num,
+            'price' => $price,
+            'total' => $total,
+            'pay_money'=> $total,
+            'realname' => isset($params['realname']) ? trim($params['realname']) : '',
+            'mobile' => isset($params['mobile']) ? trim($params['mobile']) : '',
+            'address' => isset($params['address']) ? intval($params['address']) : '',
+            'pay_at' => date('Y-m-d H:i:s'),
+            'update_time' => time(),
             'create_time' => time(),
-            'status' => 1,
+            'status' => 2,
         ];
 
-        if($gdCheckData){
-            $data['device_num'] = $gdCheckData->device_num;
-            $data['params'] = $gdCheckData->params;
+        if(!$this->model::insertGetId($data)){
+            \DB::rollBack();
+            return message( "订单创建失败,请刷新重试", false);
         }
 
-        // 订单
-        if (!$oid = GongdengOrderModel::insertGetId($data)) {
-            return message('供灯订单创建失败', false);
+        // 扣除账户
+        if(!MemberModel::where(['id'=> $userId,'mark'=> 1])->decrement($field, $total)){
+            \DB::rollBack();
+            return message( "订单支付失败,请刷新重试", false);
         }
 
+        // 明细
+        $data = [
+            'user_id'=> $userId,
+            'type'=> 1,
+            'coin_type'=> $payType==4? 3 : $payType,
+            'pay_type'=> 1,
+            'money'=> $total,
+            'change_type'=> 2,
+            'balance'=> $payType==1? $memberInfo->coupon : $memberInfo->score,
+            'create_time'=> time(),
+            'update_time'=> time(),
+            'remark'=> '商品兑换订单支付',
+            'status'=> 1
+        ];
+
+        if(!TradeModel::insertGetId($data)){
+            \DB::rollBack();
+            return message( "交易处理失败,请刷新重试", false);
+        }
 
-        // 支付参数
-        switch ($payType) {
-            case 1: // 微信支付
-                $order = [
-                    'openid' => $openid,
-                    'orderNo' => $data['order_sn'],
-                    'amount' => $data['total'],
-                    'body' => '供灯订单支付',
-                ];
-
-                $jsapiParams = WechatService::jsapiUnifiedorder($order);
-                $code = isset($jsapiParams['code']) ? $jsapiParams['code'] : '';
-                if ($code == 'error' || empty($jsapiParams)) {
-                    $message = isset($jsapiParams['message']) && $jsapiParams['message'] ? $jsapiParams['message'] : '订单支付处理失败';
-                    return message($message, false);
-                }
-                return message('订单创建成功', true, ['id' => $oid, 'params' => $jsapiParams]);
-                break;
-            default:
-                break;
+        // 扣除库存
+        if(!GoodsModel::where(['id'=> $id])->decrement('stock', $num)){
+            \DB::rollBack();
+            return message( "库存处理失败,请刷新重试", false);
         }
 
-        return message('支付处理失败', false);
+        \DB::commit();
+        return message('兑换订单支付成功', true);
     }
 
 }

BIN
public/uploads/img/qrcode/U_11770EE25BB6370D6FB7581C7F683E33.png


+ 2 - 2
routes/api.php

@@ -137,5 +137,5 @@ Route::post('/goods/info', [\App\Http\Controllers\Api\v1\GoodsController::class,
 
 // 商城订单
 Route::post('/orders/list', [\App\Http\Controllers\Api\v1\OrderController::class, 'index']);
-Route::post('/orders/buy', [\App\Http\Controllers\Api\v1\OrderController::class, 'buy']);
-Route::post('/orders/exchange', [\App\Http\Controllers\Api\v1\OrderController::class, 'buy']);
+Route::post('/orders/buy', [\App\Http\Controllers\Api\v1\GongdengController::class, 'buy']);
+Route::post('/orders/exchange', [\App\Http\Controllers\Api\v1\OrderController::class, 'exchange']);