| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- namespace app\api\controller\v1;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Models\CompareFacesResponseBody\data;
- use app\common\model\BoxHandleModel;
- use app\common\model\ShopGoodsModel;
- use app\common\model\ShopOrderModel;
- use app\common\model\UserAddressModel as UserAddress;
- use app\Request;
- use think\Exception;
- use think\facade\Db;
- use think\Response;
- class ShopOrder
- {
- /**
- * 订单列表
- */
- public function orderList (Request $request)
- {
- try {
- return api_succ_return(['msg'=>'获取成功', 'data'=>ShopOrderModel::list($request->only(['status', 'order_type']), $request->only(['page', 'limit']), $request->uid)]);
- } catch (\Exception $e) {
- return api_error_return('获取失败' . $e->getMessage());
- }
- }
- /**
- * 订单详情
- */
- public function orderDetail (Request $request): Response
- {
- try {
- return api_succ_return(['msg'=>'获取成功', 'data'=>ShopOrderModel::details($request->only(['order_sn']), $request->uid)]);
- } catch (\Exception $e) {
- return api_error_return('获取失败' . $e->getMessage());
- }
- }
- /**
- * 下单商品详情
- */
- public function buyDetail (Request $request)
- {
- $buy_goods = $request->post('buy_goods');
- $address = $request->post('address_id');
- if (empty($buy_goods)) {
- return api_error_return('参数错误11');
- }
- try {
- $buy_goods = json_decode(html_entity_decode($buy_goods), true);
- if (empty($buy_goods)) {
- return api_error_return('参数错误22');
- }
- // $hasMoney = false;
- // $hasScore = false;
- // $hasLockScore = false;
- // $hasMachine = false;
- // $hasXz = false;
- // $order_type = 1;
- foreach ($buy_goods as $k=>$v) {
- $goodsInfo = ShopGoodsModel::where(['goods_sn' => $v['goods_sn']])->field('goods_type')->find();
- if (!$goodsInfo) {
- return api_error_return('下单失败,请返回刷新列表1');
- }
- }
- // if ($goodsInfo['goods_type'] == 1){$hasMoney=true;}
- // if ($goodsInfo['goods_type'] == 2){$hasScore=true;$order_type=2;}
- // if ($goodsInfo['goods_type'] == 3){$hasMachine=true;$order_type=3;}
- // if ($goodsInfo['goods_type'] == 4){$hasLockScore=true;$order_type=4;}
- // if ($goodsInfo['goods_type'] == 5){$hasXz=true;$order_type=5;}
- // }
- // if ($hasMoney && ($hasScore || $hasLockScore || $hasMachine || $hasXz)){
- // return api_error_return('自营商品不能和其他商品下单');
- // }
- //
- // if ($hasScore && ($hasMoney || $hasLockScore || $hasMachine || $hasXz)){
- // return api_error_return('积分商品不能和其他商品下单');
- // }
- // if ($hasLockScore && ($hasScore || $hasMoney || $hasMachine || $hasXz)){
- // return api_error_return('返利积分不能和其他商品下单');
- // }
- // if ($hasMachine && ($hasScore || $hasLockScore || $hasMoney || $hasXz)){
- // return api_error_return('兑换券商品不能和其他商品下单');
- // }
- //
- // if ($hasXz && ($hasScore || $hasLockScore || $hasMoney || $hasMachine)){
- // return api_error_return('星钻商品不能和其他商品下单');
- // }
- if (empty($address)) {
- //用户默认收货地址
- $address = UserAddress::where(['uid' => $request->uid, 'is_default' => 1])->field('id,province,city,county,name,mobile,remark,is_default,mergename')->findOrEmpty()->toArray();
- } else {
- $address = UserAddress::where(['uid' => $request->uid, 'id' => $address])->field('id,province,city,county,name,mobile,remark,is_default,mergename')->findOrEmpty()->toArray();
- }
- // if (!$address){
- // return api_error_return('地址获取失败');
- // }
- $data = ShopOrderModel::buyDetails($buy_goods, $address);
- if (empty($data)) {
- return api_error_return('获取失败');
- }
- $cur_xzmoney = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
- foreach ($data['details'] as $key=>$val){
- foreach ($val['goods'] as $k1=>$v1){
- $goods_name = $v1['goods_name'];
- $goods_name = str_replace(' ','',$goods_name);
- if (strlen($goods_name) > 20){
- $goods_name = substr($goods_name, 0, 19);
- }
- // $data['details'][$key]['goods'][$k1]['goods_name'] = mb_convert_encoding($goods_name, 'UTF-8', 'UTF-8');
- // $data['details'][$key]['goods'][$k1]['goods_name'] = '云德商品购买';
- if ($v1['goods_type'] == 5){
- $data['details'][$key]['goods'][$k1]['price'] = getXzPirceWithPrice($cur_xzmoney, $data['details'][$key]['goods'][$k1]['price']);
- $data['details'][$key]['goods'][$k1]['total_fee'] = getXzPirceWithPrice($cur_xzmoney, $data['details'][$key]['goods'][$k1]['total_fee']);
- }
- }
- }
- // if ($order_type == 5){
- //
- // foreach ($data['details'] as $key=>$val){
- //// ['total_price'] = getXzPirceWithPrice($cur_xzmoney, $data['total_price']);
- // $data['details'][$key]['total_price']= getXzPirceWithPrice($cur_xzmoney, $val['total_price']);
- // $data['details'][$key]['payment']= getXzPirceWithPrice($cur_xzmoney, $val['payment']);
- //
- // }
- //
- // $data['total']['count_payment'] = getXzPirceWithPrice($cur_xzmoney, $data['total']['count_payment']);
- //
- // }
- return api_succ_return(['msg'=>'获取成功', 'data'=>['address' => (object)$address, 'buyDetail' => $data['details'], 'total' => $data['total']]]);
- } catch (\Exception $e) {
- return api_error_return($e->getMessage());
- }
- }
- /**
- * 创建订单
- */
- public function createOrder(Request $request)
- {
- // 这是测试 上线要干掉
- // return api_error_return('暂未开放');
- // $curandroid = env('app.CUR_ANDROID_VERSION', '');
- // $curios = env('app.CUR_IOS_VERSION', '');;
- //
- // $curAndroidVersion = str_replace('.', '', $curandroid);
- // $curAndroidVersion = dispRepair($curAndroidVersion, 4, '0', '0');
- // $curIosVersion = str_replace('.', '', $curios);
- // $curIosVersion = dispRepair($curIosVersion, 4, '0', '0');
- // $app_sources = $request->param('app_sources');
- // $app_version = $request->param('app_version');
- // $app_version = str_replace('.', '', $app_version);
- // $app_version = dispRepair($app_version, 4, '0', '0');
- //
- // if ($app_sources == 'ios'){
- // if ($curIosVersion > $app_version){
- // return api_error_return('app版本号过低,请更新');
- // }
- // }
- // if ($app_sources == 'android'){
- // if ($curAndroidVersion > $app_version){
- // return api_error_return('app版本号过低,请更新');
- // }
- // }
- //
- // if (empty($app_sources) || empty($app_version)){
- // if ($curAndroidVersion > $app_version){
- // return api_error_return('app版本号过低,请更新');
- // }
- // }
- $address_id = $request->post('address', '');
- $pay_type = $request->post('pay_type', 0);
- // 后面加了收银台 pay_type 去掉
- if ($pay_type != 3){
- $pay_type = 99;
- }
- $remark = $request->post('order_remark', '');
- $buy_goods = json_decode(html_entity_decode($request->post('buy_goods', '')), true);
- $hasMoney = false;
- $hasScore = false;
- $hasLockScore = false;
- $hasMachine = false;
- $hasXz = false;
- $order_type = 1; // 1 买商品送积分 2积分兑换 3任务包兑换券 4提货券 5 星钻兑换
- foreach ($buy_goods as $k=>$v){
- if ($v['num'] < 1){
- return api_error_return("哈哈,你不能买负数哇");
- }
- $goodsInfo = ShopGoodsModel::where('goods_sn', $v['goods_sn'])->field('goods_type,restrictions_num')->find();
- if (!$goodsInfo){
- return api_error_return('下单失败,请返回刷新列表');
- }
- if ($goodsInfo['goods_type'] == 2){
- return api_error_return('福袋商品不能购买');
- }
- if ($goodsInfo['restrictions_num']>0){
- $count = Db::name('shop_order_goods')
- ->alias('og')
- ->leftJoin('shop_order o', 'o.order_id = og.order_id')
- ->where('o.status', 'in', [1,2,4])
- ->where('og.uid', $request->uid)
- ->count('og.og_id');
- if ($count > $goodsInfo['restrictions_num']){
- return api_error_return('超过限购');
- }
- if ($v['num'] + $count > $goodsInfo['restrictions_num']){
- return api_error_return('超过限购数量,还可购买:'.($goodsInfo['restrictions_num']-$count).'件');
- }
- }
- }
- if ($request->user['status'] == 0){
- return api_error_return('用户被禁用');
- }
- if ($request->user['is_reward'] == 0) {
- return api_error_return('用户已被禁止交易');
- }
- try {
- if (!$userAddress = UserAddress::getAddressIdDetails((int)$address_id)) {
- return api_error_return('获取收货地址失败');
- }
- if ($userAddress['uid'] != $request->user['id']) {
- return api_error_return('收货地址信息异常');
- }
- if (empty($buy_goods)) {
- return api_error_return('参数错误2');
- }
- foreach ($buy_goods as $key => $value) {
- $result[$value['goods_sn'] . '_' . $value['spec_id']][] = $value;
- }
- if (!ShopOrderModel::checkStock($result)) {
- return api_error_return('下单失败,所选商品含有库存不足的商品');
- }
- try {
- $data = ShopOrderModel::buyDetails($buy_goods, $userAddress);
- $supplier_name = $data['details'][0]['supplier_name'];
- $paymoney = $data['total']['count_payment'];
- // // 如果是余额支付 先判断余额
- // if (in_array($order_type, [1,3]) && $pay_type == 3){
- //// return json_encode($request->user);
- // if ($request->user['money'] < $paymoney){
- // return api_error_return('下单失败,用户余额不足');
- // }
- // }
- //
- // if ($order_type == 2){
- // $paymoney = $data['total']['count_payment'];
- // if ($request->user['score'] < $paymoney){
- // return api_error_return('下单失败,用户积分不足');
- // }
- // }
- //
- // if ($order_type == 4){
- // $paymoney = $data['total']['count_payment'];
- // if ($request->user['lock_score'] < $paymoney){
- // return api_error_return('下单失败,用户提货券不足');
- // }
- // }
- // $cur_xzmoney = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
- // if ($order_type == 5){
- // $paymoney = getXzPirceWithPrice($cur_xzmoney, $data['total']['count_payment']);
- // $shouxu_scale = get_user_shouxufei($request->uid);
- // $shouxu = number_format($paymoney*$shouxu_scale/100, '2', '.', '');
- // $coupon_num = $data['total']['total_coupon_num'];
- // if ($request->user['xz_num'] < $paymoney+$shouxu){
- // return api_error_return('下单失败,用户星钻不足(总共需支付:'.($paymoney+$shouxu).'其中手续费:'.$shouxu_scale.'%');
- // }
- //
- // if ($request->user['xz_num'] - $paymoney - $shouxu < 10){
- // throw new Exception('账户必须预留10个星钻');
- // }
- //
- // if ($request->user['coupon_num'] < $coupon_num){
- // return api_error_return('下单失败,用户抢购值不足');
- // }
- //
- // }
- $count = Db::name('shop_order')->where('user_id', $request->uid)->where('order_type', 1)->where('status', 0)->whereTime('created_time', '-2 hours')->count();
- if ($count > 20){
- return api_error_return('未支付的订单太多,创建失败');
- }
- $flag = ShopOrderModel::createOrder(['order'=>$result, 'address' => $userAddress, 'cls'=>0, 'cart_ids'=>0, 'pay_type'=>$pay_type, 'order_remark'=>$remark, 'order_type'=>$order_type, 'supplier_name'=>$supplier_name], $request->uid, $request->user, $data, $request->post('cart_id', ''));
- // return $flag;
- // return json_encode($flag);
- if (!$flag) {
- return api_error_return('下单失败');
- }
- return api_succ_return(['msg'=>'下单成功', 'data'=>['order_ids'=>$flag['data']]]);
- } catch (\Exception $e) {
- return api_error_return('下单失败 ' . $e->getMessage());
- }
- } catch (\Exception $e){
- return api_error_return($e->getMessage());
- }
- }
- /**
- * 取消订单
- */
- public function cancelOrder (Request $request): Response
- {
- if (empty($request->only(['order_sn']))) {
- return api_error_return('订单号不能为空');
- }
- Db::startTrans();
- try {
- ShopOrderModel::cancelOrder($request->only(['order_sn']), $request->uid);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- return api_error_return('取消失败'.$e->getMessage());
- }
- return api_succ_return('取消成功');
- }
- /**
- * 确认订单
- */
- public function receiveOrder (Request $request): Response
- {
- if (empty($request->only(['order_sn']))) {
- return api_error_return('订单号不能为空');
- }
- Db::startTrans();
- try {
- ShopOrderModel::receiveOrder($request->only(['order_sn']), $request->uid);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- return api_error_return('确认失败'.$e->getMessage());
- }
- return api_succ_return('确认成功');
- }
- }
|