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

优化用户下单方法-进行中

sunxbiao 1 год назад
Родитель
Сommit
c797913556

+ 18 - 0
app/common/dao/store/order/StoreCartDao.php

@@ -14,6 +14,8 @@ namespace app\common\dao\store\order;
14 14
 use app\common\dao\BaseDao;
15 15
 use app\common\model\BaseModel;
16 16
 use app\common\model\store\order\StoreCart;
17
+use app\entity\CommonEntity;
18
+use app\entity\data\store\CartEntity;
17 19
 use think\Collection;
18 20
 use think\db\exception\DataNotFoundException;
19 21
 use think\db\exception\DbException;
@@ -155,4 +157,20 @@ class StoreCartDao extends BaseDao
155 157
         })->leftJoin('StoreOrderProduct B', 'A.cart_id = B.cart_id')
156 158
             ->field('sum(B.product_num) as pay_num,sum(B.product_price) as pay_price,A.source_id')->group('A.source_id')->select();
157 159
     }
160
+
161
+    /**
162
+     * @param int $cartId
163
+     * @return CommonEntity
164
+     */
165
+    public function getCartEntityByCartId(int $cartId): CommonEntity
166
+    {
167
+        $storeCartData = [];
168
+        try {
169
+            $storeCartData = Db::name('store_cart')
170
+                ->where('cart_id', $cartId)
171
+                ->find();
172
+        } catch (\Exception $e) {
173
+        }
174
+        return CartEntity::newInstance($storeCartData);
175
+    }
158 176
 }

+ 21 - 0
app/common/dao/user/UserDao.php

@@ -12,9 +12,12 @@ namespace app\common\dao\user;
12 12
 
13 13
 
14 14
 use app\common\dao\BaseDao;
15
+use app\common\enum\user\UserEnum;
15 16
 use app\common\model\BaseModel;
16 17
 use app\common\model\user\User;
17 18
 use app\common\repositories\user\UserRepository;
19
+use app\entity\CommonEntity;
20
+use app\entity\data\user\UserEntity;
18 21
 use think\db\BaseQuery;
19 22
 use think\db\exception\DataNotFoundException;
20 23
 use think\db\exception\DbException;
@@ -376,4 +379,22 @@ class UserDao extends BaseDao
376 379
     {
377 380
         return User::getDB()->where('phone', $phone)->field('uid,nickname,avatar,user_type')->select();
378 381
     }
382
+
383
+    /**
384
+     * @param $uid
385
+     * @return CommonEntity
386
+     */
387
+    public function getUserEntityByUid($uid): CommonEntity
388
+    {
389
+        $userData = [];
390
+        try {
391
+            $userData = User::getDB()
392
+                ->where('uid', $uid)
393
+                ->where('is_del', '=', UserEnum::IS_DEL['No']['code'])
394
+                ->where('status', '=', UserEnum::STATUS['Normal']['code'])
395
+                ->find();
396
+        } catch (\Exception $e) {
397
+        }
398
+        return UserEntity::newInstance($userData);
399
+    }
379 400
 }

+ 13 - 0
app/common/repositories/store/order/StoreCartRepository.php

@@ -16,6 +16,7 @@ use app\common\repositories\BaseRepository;
16 16
 use app\common\repositories\store\coupon\StoreCouponProductRepository;
17 17
 use app\common\repositories\store\coupon\StoreCouponRepository;
18 18
 use app\common\repositories\store\product\ProductRepository;
19
+use app\entity\data\store\CartEntity;
19 20
 use think\facade\Db;
20 21
 
21 22
 /**
@@ -120,4 +121,16 @@ class StoreCartRepository extends BaseRepository
120 121
         ];
121 122
         return $this->dao->getWhereCount($where);
122 123
     }
124
+
125
+    /**
126
+     * 根据 购物车 ID 获取购物车实体
127
+     * @param int $cartId
128
+     * @return CartEntity
129
+     */
130
+    public function getCartEntityByCartId(int $cartId): CartEntity
131
+    {
132
+        /** @var CartEntity $cartEntity */
133
+        $cartEntity = $this->dao->getCartEntityByCartId($cartId);
134
+        return $cartEntity;
135
+    }
123 136
 }

+ 9 - 0
app/common/repositories/store/order/StoreOrderRepository.php

@@ -38,6 +38,9 @@ use app\common\repositories\user\UserThirdRepository;
38 38
 use app\common\repositories\wechat\RoutineQrcodeRepository;
39 39
 use app\common\repositories\wechat\WechatUserRepository;
40 40
 use app\controller\api\user\Volunteer;
41
+use app\entity\data\store\CartEntity;
42
+use app\entity\data\user\UserEntity;
43
+use app\entity\request\api\store\order\StoreOrderCreateRequestEntity;
41 44
 use app\model\common\JdOrderModel;
42 45
 use app\model\common\TbOrderModel;
43 46
 use app\traits\GongApiRequest;
@@ -1414,6 +1417,12 @@ class StoreOrderRepository extends BaseRepository
1414 1417
         return $group;
1415 1418
     }
1416 1419
 
1420
+
1421
+    public function createOrderNew(StoreOrderCreateRequestEntity $storeOrderCreateRequestEntity, UserEntity $userEntity, CartEntity $cartEntity)
1422
+    {
1423
+
1424
+    }
1425
+
1417 1426
     /**
1418 1427
      * @remarks 年费订单创建
1419 1428
      * @author Tang

+ 12 - 0
app/common/repositories/user/UserRepository.php

@@ -26,6 +26,7 @@ use app\common\repositories\wechat\RoutineQrcodeRepository;
26 26
 use app\controller\api\store\product\Jd;
27 27
 use app\controller\api\store\product\Vip;
28 28
 use app\controller\api\user\Im;
29
+use app\entity\data\user\UserEntity;
29 30
 use app\traits\GzcApiRequest;
30 31
 use crmeb\exceptions\AuthException;
31 32
 use crmeb\jobs\SendNewPeopleCouponJob;
@@ -4369,4 +4370,15 @@ class UserRepository extends BaseRepository
4369 4370
             Elm::number('pack','份数',$stock['pack'])->required()
4370 4371
         ])->setTitle('编辑');
4371 4372
     }
4373
+
4374
+    /**
4375
+     * @param int $uid
4376
+     * @return UserEntity
4377
+     */
4378
+    public function getUserEntityByUid(int $uid): UserEntity
4379
+    {
4380
+        /** @var UserEntity $userEntity */
4381
+        $userEntity = $this->dao->getUserEntityByUid($uid);
4382
+        return $userEntity;
4383
+    }
4372 4384
 }

+ 65 - 40
app/controller/api/store/order/StoreOrder.php

@@ -18,8 +18,10 @@ use app\common\repositories\merchant\MerchantRepository;
18 18
 use app\common\repositories\store\product\ProductAttrValueRepository;
19 19
 use app\common\repositories\user\UserAddressRepository;
20 20
 use app\common\repositories\user\UserRepository;
21
+use app\entity\data\user\UserEntity;
22
+use app\entity\request\api\store\order\StoreOrderCreateRequestEntity;
21 23
 use app\traits\ShouxinyiApiRequest;
22
-use app\validate\api\store\order\CreateValidate;
24
+use app\validate\api\store\order\StoreOrderCreateValidate;
23 25
 use crmeb\basic\BaseController;
24 26
 use app\common\repositories\store\order\StoreCartRepository;
25 27
 use app\common\repositories\store\order\StoreGroupOrderRepository;
@@ -694,72 +696,95 @@ class StoreOrder extends BaseController
694 696
     }
695 697
 
696 698
     /**
697
-     * @param CreateValidate $createValidate
699
+     * @param StoreOrderCreateValidate $storeOrderCreateValidate
698 700
      * @return false|string|\think\response\Json
699 701
      * @throws DataNotFoundException
700 702
      * @throws DbException
701 703
      * @throws ModelNotFoundException
702 704
      * @throws \Throwable
703 705
      */
704
-    public function createOrderNew(CreateValidate $createValidate)
706
+    public function createOrderNew(StoreOrderCreateValidate $storeOrderCreateValidate)
705 707
     {
706 708
         // 1、接收参数
707
-        $validationResult = $createValidate->validateRequest();
709
+        $storeOrderCreateValidateResult = $storeOrderCreateValidate->validateRequest();
708 710
         // 如果验证失败,返回错误
709
-        if ($validationResult['status'] == 'error') {
710
-            return json($validationResult);
711
+        if ($storeOrderCreateValidateResult['status'] == 'error') {
712
+            return app('json')->fail($storeOrderCreateValidateResult['message']);
711 713
         }
712 714
         // 验证通过,继续处理数据
713
-        $allParams = $validationResult['data'];
715
+        /** @var StoreOrderCreateRequestEntity $storeOrderCreateRequestEntity */
716
+        $storeOrderCreateRequestEntity = $storeOrderCreateValidateResult['data'];
717
+        // 根据用户ID 获取用户实体信息
718
+        $uid = $this->request->uid();
714 719
 
720
+        // 2、加锁,他之前加的锁好像在锁狗屎,毛线都没锁住, 而且为什么要锁用户
721
+        // 增加锁,防止同一用户重读点击
722
+        // $redisHandler = Cache::store('redis')->handler();
723
+        // $isSet = $redisHandler->set('createOrder' . $uid . '-' . $storeOrderCreateRequestEntity->getCartId(), 1, ['nx', 'ex' => 30]);
724
+        // if ($isSet) {
725
+        //     return app('json')->fail('请勿重复下单');
726
+        // }
715 727
 
716
-        $allParams = input();
728
+        /** @var UserRepository $userRepository */
729
+        $userRepository = app()->make(UserRepository::class);
730
+        $userEntity = $userRepository->getUserEntityByUid($uid);
731
+        if (empty($userEntity->getUid())) {
732
+            return app('json')->fail('获取当前用户信息失败');
733
+        }
717 734
 
718
-        $cartId = $allParams['cart_id'] ?? null;
719
-        if (is_null($cartId)) {
720
-            return app('json')->fail("缺少购物车ID参数!");
735
+        /** @var StoreCartRepository $storeCartRepository */
736
+        $storeCartRepository = app()->make(StoreCartRepository::class);
737
+        $cartEntity = $storeCartRepository->getCartEntityByCartId($storeOrderCreateRequestEntity->getCartId());
738
+        if (empty($cartEntity->getCartId())) {
739
+            return app('json')->fail('获取购物车内容失败');
721 740
         }
722
-        $fzonePayType = $allParams['fzone_paytype'] ?? '[]';
723
-        if (is_null($fzonePayType) || strstr($fzonePayType, "type") === false) {
724
-            return app('json')->fail("请选择支付方式!");
741
+        // 确认购物车内 微唯宝 的商品 是否满足发货
742
+        /** @var ProductAttrValueRepository $productAttrValueRepository */
743
+        $productAttrValueRepository = app()->make(ProductAttrValueRepository::class);
744
+        // 检查库存 查看 此商品已下架 此商品规格无货 此商品不支持该地区
745
+        // (按照代码中所写的逻辑来推理:从微唯宝拉取过来,从而上架到商品表的商品,都有SPUId,所以去走微唯宝提供的接口验证,而会员专区和商贸区的商品可能是商户自己加的,没有SPUId,所以也就不用走微唯宝的验证)
746
+        $checkGong = $productAttrValueRepository->checkGong($cartEntity->getProductId(), $cartEntity->getProductAttrUnique(), $cartEntity->getCartNum(), $uid, $storeOrderCreateRequestEntity->getAddressId());
747
+        if (isset($checkGong['code']) && $checkGong['code'] == -1) {
748
+            return app('json')->fail($checkGong['message']);
725 749
         }
726
-        $fzonePayTypeMap = json_decode($fzonePayType, true);
750
+
751
+
752
+        $this->repository->createOrderNew($storeOrderCreateRequestEntity, $userEntity, $cartEntity);
753
+        
754
+        
755
+        return app('json')->success('aaa', $storeOrderCreateRequestEntity);
756
+        $uid = $this->request->uid();
757
+
758
+        $this->repository->createOrderNew($uid, $allParams);
759
+
760
+
761
+        $allParams = input();
762
+
763
+        $cartId = $allParams['cart_id'];
764
+
765
+        $fzonePayType = $allParams['fzone_paytype'];
766
+        $fzonePayTypeMap = $allParams['fzone_paytype'];
727 767
         $addressId = (int)($allParams['address_id'] ?? 0); // 地址ID
728 768
 
729 769
         $payType = $this->request->param('pay_type');// 5积分 6京豆
730 770
         $share_id = $this->request->param('share_id', '');//分享id
731 771
         $mer_id = $this->request->param('mer_id', '');//分享id
732
-        $uid = $this->request->uid();
772
+        // 声明 引用
773
+
733 774
 
734
-        // 验证用户信息
735
-        try {
736
-            $userData = Db::name('user')
737
-                ->where('uid', $uid)
738
-                ->where('is_del', '=', UserEnum::IS_DEL['No']['code'])
739
-                ->find();
740
-        } catch (\Exception $e) {
741
-            return app('json')->fail('获取当前用户信息失败');
742
-        }
743 775
 
744
-        // 2、加锁,他之前加的锁好像在锁狗屎,毛线都没锁住, 而且为什么要锁用户
745
-        // 增加锁,防止同一用户重读点击
746
-        $redisHandler = Cache::store('redis')->handler();
747
-        $isSet = $redisHandler->set('createOrder' . $uid . '-' . $cartId, 1, ['nx', 'ex' => 30]);
748
-        if ($isSet) {
749
-            return app('json')->fail('请勿重复下单');
750
-        }
751 776
 
752 777
         // 3、验证购物车内商品 是否已下架,是否无货,是否支持发货 是否满足数量
753 778
         // 处理购物车 按照之前需求的意思,应该 每次下单 只能购买 一个SPU 的 一个SKU,而且只能买一件
754 779
         //在提交订单之前会产出购物车cartid,其中会产生购买的数量,现在默认会员专区是只让购买一个商品,需要修改值为1
755
-        try {
756
-            $storeCartDataList = Db::name('store_cart')
757
-                ->where('cart_id', $cartId)
758
-                ->select()
759
-                ->toArray();
760
-        } catch (\Exception $e) {
761
-            return app('json')->fail('获取购物车内容失败');
762
-        }
780
+        // try {
781
+        //     $storeCartDataList = Db::name('store_cart')
782
+        //         ->where('cart_id', $cartId)
783
+        //         ->select()
784
+        //         ->toArray();
785
+        // } catch (\Exception $e) {
786
+        //     return app('json')->fail('获取购物车内容失败');
787
+        // }
763 788
         // 这块 限制还 欠完善考虑
764 789
         if (count($storeCartDataList) > 1 || $storeCartDataList[0]['cart_num'] > 1) {
765 790
             return app('json')->fail('购买数量默认为一件,请修改购买数量!');

+ 49 - 0
app/entity/CommonEntity.php

@@ -0,0 +1,49 @@
1
+<?php
2
+
3
+namespace app\entity;
4
+
5
+use ReflectionClass;
6
+
7
+class CommonEntity
8
+{
9
+    /**
10
+     * 将 参数 实例化到 实体
11
+     * @param array $data
12
+     * @return CommonEntity
13
+     */
14
+    public static function newInstance(array $data = []): CommonEntity
15
+    {
16
+        if (empty($data)) {
17
+            return new static();
18
+        } else {
19
+            $instance = new static();
20
+            foreach ($instance as $ik => $iv) {
21
+                if (isset($data[$ik])) {
22
+                    $setFunctionName = 'set' . ucfirst($ik);
23
+                    $instance->$setFunctionName($data[$ik]);
24
+                }
25
+            }
26
+            return $instance;
27
+        }
28
+    }
29
+
30
+    /**
31
+     * 声明公共方法 将实体 转成数组
32
+     * @return array
33
+     */
34
+    public function toArray(): array
35
+    {
36
+        $data = [];
37
+        try {
38
+            $ref = new ReflectionClass(static::class);
39
+            foreach ($ref->getProperties() as $property) {
40
+                $p = $ref->getProperty($property->name);
41
+                $p->setAccessible(true);
42
+                $data[$property->name] = $p->getValue($this);
43
+            }
44
+        } catch (\Exception $e) {
45
+
46
+        }
47
+        return $data;
48
+    }
49
+}

+ 10 - 0
app/entity/data/DataEntity.php

@@ -0,0 +1,10 @@
1
+<?php
2
+
3
+namespace app\entity\data;
4
+
5
+use app\entity\CommonEntity;
6
+
7
+class DataEntity extends CommonEntity
8
+{
9
+
10
+}

+ 184 - 0
app/entity/data/store/CartEntity.php

@@ -0,0 +1,184 @@
1
+<?php
2
+
3
+namespace app\entity\data\store;
4
+
5
+use app\entity\data\DataEntity;
6
+
7
+class CartEntity extends DataEntity
8
+{
9
+    public $cartId = 0;  // 购物车表ID
10
+    public $uid = 0;  // 用户ID
11
+    public $merId = 0;  // 商户ID
12
+    public $productType = 0;  // 类型 0=普通产品
13
+    public $productId = 0;  // 商品ID
14
+    public $productAttrUnique = '';  // 商品属性
15
+    public $cartNum = 0;  // 商品数量
16
+    public $createTime = null;  // 添加时间
17
+    public $source = 0;  // 来源 1=直播间
18
+    public $sourceId = 0;  // 来源关联ID
19
+    public $isPay = 0;  // 0 = 未购买 1 = 已购买
20
+    public $isDel = 0;  // 是否删除
21
+    public $isNew = 0;  // 是否为立即购买
22
+    public $isFail = 0;  // 是否失效
23
+
24
+    public function getCartId(): int
25
+    {
26
+        return $this->cartId;
27
+    }
28
+
29
+    public function setCartId(int $cartId): CartEntity
30
+    {
31
+        $this->cartId = $cartId;
32
+        return $this;
33
+    }
34
+
35
+    public function getUid(): int
36
+    {
37
+        return $this->uid;
38
+    }
39
+
40
+    public function setUid(int $uid): CartEntity
41
+    {
42
+        $this->uid = $uid;
43
+        return $this;
44
+    }
45
+
46
+    public function getMerId(): int
47
+    {
48
+        return $this->merId;
49
+    }
50
+
51
+    public function setMerId(int $merId): CartEntity
52
+    {
53
+        $this->merId = $merId;
54
+        return $this;
55
+    }
56
+
57
+    public function getProductType(): int
58
+    {
59
+        return $this->productType;
60
+    }
61
+
62
+    public function setProductType(int $productType): CartEntity
63
+    {
64
+        $this->productType = $productType;
65
+        return $this;
66
+    }
67
+
68
+    public function getProductId(): int
69
+    {
70
+        return $this->productId;
71
+    }
72
+
73
+    public function setProductId(int $productId): CartEntity
74
+    {
75
+        $this->productId = $productId;
76
+        return $this;
77
+    }
78
+
79
+    public function getProductAttrUnique(): string
80
+    {
81
+        return $this->productAttrUnique;
82
+    }
83
+
84
+    public function setProductAttrUnique(string $productAttrUnique): CartEntity
85
+    {
86
+        $this->productAttrUnique = $productAttrUnique;
87
+        return $this;
88
+    }
89
+
90
+    public function getCartNum(): int
91
+    {
92
+        return $this->cartNum;
93
+    }
94
+
95
+    public function setCartNum(int $cartNum): CartEntity
96
+    {
97
+        $this->cartNum = $cartNum;
98
+        return $this;
99
+    }
100
+
101
+    /**
102
+     * @return null
103
+     */
104
+    public function getCreateTime()
105
+    {
106
+        return $this->createTime;
107
+    }
108
+
109
+    /**
110
+     * @param null $createTime
111
+     * @return CartEntity
112
+     */
113
+    public function setCreateTime($createTime)
114
+    {
115
+        $this->createTime = $createTime;
116
+        return $this;
117
+    }
118
+
119
+    public function getSource(): int
120
+    {
121
+        return $this->source;
122
+    }
123
+
124
+    public function setSource(int $source): CartEntity
125
+    {
126
+        $this->source = $source;
127
+        return $this;
128
+    }
129
+
130
+    public function getSourceId(): int
131
+    {
132
+        return $this->sourceId;
133
+    }
134
+
135
+    public function setSourceId(int $sourceId): CartEntity
136
+    {
137
+        $this->sourceId = $sourceId;
138
+        return $this;
139
+    }
140
+
141
+    public function getIsPay(): int
142
+    {
143
+        return $this->isPay;
144
+    }
145
+
146
+    public function setIsPay(int $isPay): CartEntity
147
+    {
148
+        $this->isPay = $isPay;
149
+        return $this;
150
+    }
151
+
152
+    public function getIsDel(): int
153
+    {
154
+        return $this->isDel;
155
+    }
156
+
157
+    public function setIsDel(int $isDel): CartEntity
158
+    {
159
+        $this->isDel = $isDel;
160
+        return $this;
161
+    }
162
+
163
+    public function getIsNew(): int
164
+    {
165
+        return $this->isNew;
166
+    }
167
+
168
+    public function setIsNew(int $isNew): CartEntity
169
+    {
170
+        $this->isNew = $isNew;
171
+        return $this;
172
+    }
173
+
174
+    public function getIsFail(): int
175
+    {
176
+        return $this->isFail;
177
+    }
178
+
179
+    public function setIsFail(int $isFail): CartEntity
180
+    {
181
+        $this->isFail = $isFail;
182
+        return $this;
183
+    }
184
+}

Разница между файлами не показана из-за своего большого размера
+ 1420 - 0
app/entity/data/user/UserEntity.php


+ 10 - 0
app/entity/request/RequestCommonEntity.php

@@ -0,0 +1,10 @@
1
+<?php
2
+
3
+namespace app\entity\request;
4
+
5
+use app\entity\CommonEntity;
6
+
7
+class RequestCommonEntity extends CommonEntity
8
+{
9
+
10
+}

+ 93 - 0
app/entity/request/api/store/order/StoreOrderCreateRequestEntity.php

@@ -0,0 +1,93 @@
1
+<?php
2
+
3
+namespace app\entity\request\api\store\order;
4
+
5
+use app\entity\request\RequestCommonEntity;
6
+
7
+class StoreOrderCreateRequestEntity extends RequestCommonEntity
8
+{
9
+    public $cartId = 0;
10
+    public $addressId = 0;
11
+    public $payType = 0;
12
+    public $mark = '';
13
+    public $distributionMode = 0;
14
+    public $fzonePayType = [];
15
+    public $shareId = 0;
16
+
17
+    public function getCartId(): int
18
+    {
19
+        return $this->cartId;
20
+    }
21
+
22
+    public function setCartId(int $cartId): StoreOrderCreateRequestEntity
23
+    {
24
+        $this->cartId = $cartId;
25
+        return $this;
26
+    }
27
+
28
+    public function getAddressId(): int
29
+    {
30
+        return $this->addressId;
31
+    }
32
+
33
+    public function setAddressId(int $addressId): StoreOrderCreateRequestEntity
34
+    {
35
+        $this->addressId = $addressId;
36
+        return $this;
37
+    }
38
+
39
+    public function getPayType(): int
40
+    {
41
+        return $this->payType;
42
+    }
43
+
44
+    public function setPayType(int $payType): StoreOrderCreateRequestEntity
45
+    {
46
+        $this->payType = $payType;
47
+        return $this;
48
+    }
49
+
50
+    public function getMark(): string
51
+    {
52
+        return $this->mark;
53
+    }
54
+
55
+    public function setMark(string $mark): StoreOrderCreateRequestEntity
56
+    {
57
+        $this->mark = $mark;
58
+        return $this;
59
+    }
60
+
61
+    public function getDistributionMode(): int
62
+    {
63
+        return $this->distributionMode;
64
+    }
65
+
66
+    public function setDistributionMode(int $distributionMode): StoreOrderCreateRequestEntity
67
+    {
68
+        $this->distributionMode = $distributionMode;
69
+        return $this;
70
+    }
71
+
72
+    public function getFzonePayType(): array
73
+    {
74
+        return $this->fzonePayType;
75
+    }
76
+
77
+    public function setFzonePayType(array $fzonePayType): StoreOrderCreateRequestEntity
78
+    {
79
+        $this->fzonePayType = $fzonePayType;
80
+        return $this;
81
+    }
82
+
83
+    public function getShareId(): int
84
+    {
85
+        return $this->shareId;
86
+    }
87
+
88
+    public function setShareId(int $shareId): StoreOrderCreateRequestEntity
89
+    {
90
+        $this->shareId = $shareId;
91
+        return $this;
92
+    }
93
+}

+ 30 - 0
app/utils/ArrayUtil.php

@@ -0,0 +1,30 @@
1
+<?php
2
+
3
+namespace app\utils;
4
+
5
+class ArrayUtil
6
+{
7
+    /**
8
+     * 将数组内的 keys下划线转驼峰
9
+     *
10
+     * @param array $arr
11
+     * @param string $separator
12
+     *
13
+     * @return array
14
+     */
15
+    public static function underline2CamelArr(array $arr, string $separator = '_'): array
16
+    {
17
+        $result = [];
18
+        if (!empty($arr)) {
19
+            foreach ($arr as $words => $value) {
20
+                if (strpos($words, $separator) !== false) {
21
+                    $words = $separator . str_replace($separator, " ", strtolower($words));
22
+                    $result[ltrim(str_replace(" ", "", ucwords($words)), $separator)] = $value;
23
+                } else {
24
+                    $result[$words] = $value;
25
+                }
26
+            }
27
+        }
28
+        return $result;
29
+    }
30
+}

+ 37 - 0
app/utils/StringUtil.php

@@ -0,0 +1,37 @@
1
+<?php
2
+
3
+namespace app\utils;
4
+class StringUtil
5
+{
6
+
7
+    /**
8
+     * 下划线转驼峰
9
+     *
10
+     * @param $words
11
+     * @param string $separator
12
+     *
13
+     * @return string
14
+     */
15
+    public static function underline2Camel($words, string $separator = '_'): string
16
+    {
17
+        if (strpos($words, $separator) !== false) {
18
+            $words = $separator . str_replace($separator, " ", strtolower($words));
19
+            return ltrim(str_replace(" ", "", ucwords($words)), $separator);
20
+        } else {
21
+            return $words;
22
+        }
23
+    }
24
+
25
+    /**
26
+     * 驼峰命名转下划线命名
27
+     *
28
+     * @param $camelCaps
29
+     * @param string $separator
30
+     *
31
+     * @return string
32
+     */
33
+    public static function camel2Underline($camelCaps, string $separator = '_'): string
34
+    {
35
+        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
36
+    }
37
+}

+ 24 - 0
app/validate/CommonValidate.php

@@ -0,0 +1,24 @@
1
+<?php
2
+
3
+namespace app\validate;
4
+
5
+use app\Request;
6
+use think\Validate;
7
+
8
+class CommonValidate extends Validate
9
+{
10
+    protected $request;
11
+
12
+    public function __construct(Request $request)
13
+    {
14
+        $this->request = $request;
15
+        parent::__construct();
16
+    }
17
+
18
+    // 自定义验证规则:验证是否为有效的JSON格式
19
+    protected function isJson($value): bool
20
+    {
21
+        json_decode($value);
22
+        return (json_last_error() == JSON_ERROR_NONE);
23
+    }
24
+}

+ 19 - 17
app/validate/api/store/order/CreateValidate.php

@@ -2,42 +2,37 @@
2 2
 
3 3
 namespace app\validate\api\store\order;
4 4
 
5
-use app\Request;
5
+use app\entity\request\api\store\order\StoreOrderCreateRequestEntity;
6
+use app\utils\ArrayUtil;
7
+use app\validate\CommonValidate;
6 8
 use think\facade\Log;
7
-use think\Validate;
8 9
 
9
-class CreateValidate extends Validate
10
+class StoreOrderCreateValidate extends CommonValidate
10 11
 {
11
-    protected $request;
12
-
13
-    public function __construct(Request $request)
14
-    {
15
-        $this->request = $request;
16
-        parent::__construct();
17
-    }
18 12
 
19 13
     protected $rule = [
20 14
         'cart_id' => 'require|integer',
21
-        'fzone_paytype' => 'require|json',
15
+        'fzone_paytype' => 'require',
22 16
         'address_id' => 'require|integer|gt:0',
23 17
         'pay_type' => 'require|integer',
24
-        'share_id' => 'integer',  // share_id 变为非必填项
18
+        'share_id' => 'integer',
19
+        'mark' => 'string'
25 20
     ];
26 21
 
27 22
     protected $message = [
28
-        'cart_id.require' => '购物车ID不能为空!',
23
+        'cart_id.require' => '缺少购物车ID参数!',
29 24
         'cart_id.integer' => '购物车ID必须是整数!',
30 25
         'fzone_paytype.require' => '支付方式不能为空!',
31
-        'fzone_paytype.json' => '支付方式必须是有效的JSON格式!',
26
+        // 'fzone_paytype.string' => '支付方式必须是字符串!',
32 27
         'address_id.require' => '地址ID不能为空!',
33 28
         'address_id.integer' => '地址ID必须是整数!',
34 29
         'address_id.gt' => '地址ID必须大于0!',
35 30
         'pay_type.require' => '支付方式不能为空!',
36 31
         'pay_type.integer' => '支付方式类型错误!',
37
-        'share_id.integer' => 'share_id 必须是整数!',  // 错误信息,适用于share_id
32
+        'share_id.integer' => 'share_id 必须是整数!',
33
+        'mark.string' => '备注类型错误'
38 34
     ];
39 35
 
40
-
41 36
     /**
42 37
      * 将Request参数获取与验证封装到验证类中
43 38
      *
@@ -50,6 +45,13 @@ class CreateValidate extends Validate
50 45
         if (!$this->check($data)) {
51 46
             return ['status' => 'error', 'message' => $this->getError()];
52 47
         }
53
-        return ['status' => 'success', 'data' => $data];
48
+        var_dump($data);
49
+
50
+        $data['fzonePayType'] = json_decode($data['fzone_paytype'], true);
51
+        if (empty($data['fzonePayType']['type'] ?? null)) {
52
+            return ['status' => 'error', 'message' => '请选择支付方式!'];
53
+        }
54
+        $data = ArrayUtil::underline2CamelArr($data);
55
+        return ['status' => 'success', 'data' => StoreOrderCreateRequestEntity::newInstance($data)];
54 56
     }
55 57
 }

+ 4 - 3
route/api.php

@@ -82,6 +82,7 @@ Route::group('api/', function () {
82 82
             Route::post('check', '/checkOrder');
83 83
             Route::post('tests/:order_sn', '/tests');
84 84
             Route::post('create', '/createOrder');//创建订单
85
+            Route::post('createNew', '/createOrderNew');//创建订单
85 86
             Route::post('delete', '/delete');//删除订单
86 87
             Route::post('create_annual_cost', '/create_annual_costorder');
87 88
             Route::post('create_activation_code_order', '/create_activation_code_order');
@@ -1304,9 +1305,9 @@ Route::group('api/', function () {
1304 1305
     ->middleware(\app\common\middleware\InstallMiddleware::class)
1305 1306
     ->middleware(\app\common\middleware\CheckSiteOpenMiddleware::class);
1306 1307
 Route::get('/test/assignment', 'api.store.merchant.Taskorderguquan/assignment');
1307
-//Route::get('test_hehuoren', 'api.store.merchant.TaskPartnerProfits/partnerProfits');
1308
-//Route::get('test_guquan', 'api.store.merchant.Taskorderguquan/lst6');
1309
-//Route::get('test_lianchuang', 'api.store.merchant.Taskorderlianc/lst2');
1308
+Route::get('test_hehuoren', 'api.store.merchant.TaskPartnerProfits/partnerProfits');
1309
+Route::get('test_guquan', 'api.store.merchant.Taskorderguquan/lst6');
1310
+Route::get('test_lianchuang', 'api.store.merchant.Taskorderlianc/lst2');
1310 1311
 Route::any('/share', function () {
1311 1312
     return view(app()->getRootPath() . 'public/share/register.html');
1312 1313
 });