diff --git a/app/api/controller/order/OrderController.php b/app/api/controller/order/OrderController.php index 2e2c2d97..40da7fa4 100644 --- a/app/api/controller/order/OrderController.php +++ b/app/api/controller/order/OrderController.php @@ -8,6 +8,7 @@ use app\api\logic\order\OrderLogic; use app\api\controller\BaseApiController; use app\api\lists\order\OrderList; use app\api\service\WechatUserService; +use app\api\validate\OrderValidate; use app\common\enum\PayEnum; use app\common\logic\PaymentLogic; use app\common\logic\PayNotifyLogic; @@ -113,7 +114,6 @@ class OrderController extends BaseApiController */ public function createOrder() { - $cartId = (array)$this->request->post('cart_id', []); $store_id = (array)$this->request->post('store_id', 0); $pay_type = (int)$this->request->post('pay_type'); @@ -368,4 +368,18 @@ class OrderController extends BaseApiController return $this->success('添加成功'); } } + + //核销 + public function writeoff_order() + { + $params = (new OrderValidate())->post()->goCheck('check'); + $userId = $this->request->userId; + $res = OrderLogic::writeOff($params,$userId); + if ($res) { + return $this->success('核销成功'); + } + return $this->fail('核销失败'); + } + + } diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php index 1c4ce05f..058290c4 100644 --- a/app/api/logic/order/OrderLogic.php +++ b/app/api/logic/order/OrderLogic.php @@ -2,16 +2,12 @@ namespace app\api\logic\order; +use app\common\enum\OrderEnum; use app\common\enum\PayEnum; +use app\common\enum\YesNoEnum; use app\common\logic\BaseLogic; -use app\common\model\goods\Goods; -use app\common\model\goods\Unit; use app\common\model\merchant\Merchant; -use app\common\model\opurchase\Opurchaseclass; -use app\common\model\opurchase\Opurchaseinfo; use app\common\model\order\Cart; -use app\common\model\retail\Cashierclass; -use app\common\model\retail\Cashierinfo; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_order\StoreOrder; use app\common\model\store_order_cart_info\StoreOrderCartInfo; @@ -23,6 +19,7 @@ use app\common\model\user\UserAddress; use app\Request; use support\Log; use taoser\exception\ValidateException; +use think\Exception; use think\facade\Db; use Yansongda\Pay\Event\PayEnd; @@ -61,6 +58,7 @@ class OrderLogic extends BaseLogic $cart_select[$k]['product_id'] = $find['goods']; $cart_select[$k]['old_cart_id'] = implode(',',$cartId); $cart_select[$k]['cart_num'] = $v['cart_num']; + $cart_select[$k]['verify_code'] = $params['verify_code']; //理论上每笔都是拆分了 // $cart_select[$k]['name'] = $find['store_name']; // $cart_select[$k]['imgs'] = $find['image']; @@ -94,6 +92,8 @@ class OrderLogic extends BaseLogic */ static public function createOrder($cartId, $addressId, $user = null, $params = []) { + $verify_code = generateUniqueVerificationCode(); + $params['verify_code'] = $verify_code; $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params); if(!$orderInfo){ return false; @@ -113,6 +113,7 @@ class OrderLogic extends BaseLogic $_order['real_name'] = $user['real_name']; $_order['mobile'] = $user['mobile']; $_order['pay_type'] = $user['pay_type']; + $_order['verify_code'] = $verify_code; if($addressId>0){ $address=UserAddress::where(['address_id'=>$addressId,'uid'=>Request()->userId])->find(); @@ -396,4 +397,58 @@ class OrderLogic extends BaseLogic } return $find; } + + + //核销 + + /** + * @param $params + * @param $uid + * @return bool + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author: codeliu + * @Time: 2024/6/3 22:42 + */ + public static function writeOff($params,$uid): bool + { + $data = StoreOrderCartInfo::where([ + 'oid'=>$params['order_id'], + 'verify_code'=>$params['verify_code'], + 'uid'=>$uid + ])->select()->toArray(); + if (empty($data)){ + return false; + } + Db::startTrans(); + try { + $newArr = []; + $oid = []; + foreach ($data as $k =>$value){ + $oid [] = $value['oid']; + $newArr[$k]['writeoff_time'] = time(); + $newArr[$k]['is_writeoff'] = YesNoEnum::YES; + $newArr[$k]['update_time'] = time(); + } + (new StoreOrderCartInfo())->saveAll($newArr); + $oidArr = array_values(array_unique($oid)); + StoreOrder::whereIn('id',$oidArr) + ->update([ + 'status' => OrderEnum::RECEIVED_GOODS, + 'update_time' => time(), + ]); + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + + } + + + + } diff --git a/app/api/validate/OrderValidate.php b/app/api/validate/OrderValidate.php new file mode 100644 index 00000000..ded301cf --- /dev/null +++ b/app/api/validate/OrderValidate.php @@ -0,0 +1,48 @@ + 'require|number', + 'verify_code' => 'require', + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $field = [ + 'order_id' => '订单', + 'verify_code' => '验证码', + ]; + + + /** + * @notes 添加场景 + * @return OrderValidate + * @author likeadmin + * @date 2024/04/24 10:37 + */ + public function sceneCheck() + { + return $this->only(['order_id','verify_code']); + } + + + +} \ No newline at end of file diff --git a/app/common/enum/OrderEnum.php b/app/common/enum/OrderEnum.php index 0753b46e..c10fef0c 100644 --- a/app/common/enum/OrderEnum.php +++ b/app/common/enum/OrderEnum.php @@ -39,6 +39,14 @@ class OrderEnum const EXPENDITURE =0; const INCOME =1; + + /** + * 状态 + * @RECEIVED_GOODS 已收货 + */ + const RECEIVED_GOODS = 2; + + /** * 账户类型 * @USER 用户 diff --git a/app/functions.php b/app/functions.php index 5fddb93e..90b3db2b 100644 --- a/app/functions.php +++ b/app/functions.php @@ -342,3 +342,21 @@ if (!function_exists('setUnique')) { } } + + +if (!function_exists('generateUniqueVerificationCode')) { + function generateUniqueVerificationCode() { + // 获取当前时间的毫秒部分 + list($msec, $sec) = explode(' ', microtime()); + $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', ''); + + // 生成一个随机数作为核销码的后缀 + $randomNumber = mt_rand(10000000, 99999999); // 假设核销码是8位数 + + // 将前缀、毫秒时间戳和随机数连接起来 + $type = rand(1, 10); // 生成一个1-10之间的随机数作为前缀 + return $type . $msectime . $randomNumber; + } + +} +