0], ['store_list', function ($attr, $params) { if (!is_array($this->store_list)) { $data = Json::decode($this->store_list); if (!$data) { $this->addError($attr, "{$attr}数据格式错误。"); } $this->store_list = $data; } }], ['store_list', function ($attr, $params) { foreach ($this->store_list as $i => &$store) { if (!is_array($store['goods_list'])) { $this->addError($attr, "{$attr}[{$i}]['goods_list']必须是一个数组。"); return; } } }], [['longitude', 'latitude', '_from'], 'trim'], [['_from'], 'in', 'range' => ['app', 'mini']], [['pt_activity_id', 'pt_number'], 'integer'] ]; return $rules; } public function afterValidate() { //$this->user = User::findOne($this->user_id); $saas_user = get_saas_user(); $this->user = User::findOne(['binding'=>$saas_user->mobile,'store_id'=>get_store_id(),'is_delete'=>0]); //$this->level = $this->getLevelData(); $this->address = $this->getAddressData(); $this->integral = [ 'forehead' => 0, 'forehead_integral' => 0, 'integration' => Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'store')['value'])['value'] ]; parent::afterValidate(); } /** * @throws \Exception */ protected function getStoreListData($submit = false) { foreach ($this->store_list as $i => &$mch) { $mch['pt_number'] = $mch['pt_number'] ?? $this->pt_number; $mch['goods_list'] = $this->getGoodsList($this->store_list[0]['goods_list'], $submit); if (empty($mch['goods_list'])) { throw new \Exception($mch['goods_list']['msg'], 1); } if ($mch['goods_list']['code'] === 1) { throw new \Exception($mch['goods_list']['msg'], 1); } //拼团信息检测 $pt_activity = PtActivity::findOne($this->pt_activity_id); if (!$pt_activity) { throw new \Exception("活动不存在或已结束"); } if ($this->store_list[0]['store_id']) { $storeInfo = Store::find()->where(['id'=>$this->store_list[0]['store_id'],'is_delete'=>0])->asArray()->one(); if(!$storeInfo){ throw new \Exception('商城信息有误', 1); } } else { $storeInfo['name'] = '平台'; } $mch['name'] = $storeInfo['name']; $this->head_integral = 0; if (!empty($mch['pt_number'])) { $pt_order = PtActivityOrder::findOne($mch['pt_number']); if ($pt_order) { if ((int)$pt_order->is_pay === 0) { throw new \Exception("当前团暂未开放"); } //error $check_pt_order = PtActivityOrder::find()->where(['trade_status' => 0, 'is_pay' => 1]) ->andWhere(['OR', ['id' => $mch['pt_number']], ['pt_number' => $mch['pt_number']]]) ->select('id, is_winner, user_id')->asArray()->all(); //判断是否到解散时间 $end_time = (($pt_activity->split_time * 60 * 60) + ($pt_order->pay_time)); if ($end_time <= time()) { throw new \Exception("当前团已解散"); } //判断是否开奖 $open = false; foreach ($check_pt_order as $item) { if ((int)$item['is_winner'] > 0) { $open = true; } if($pt_activity->party_type == 0){ if ((int)$item['user_id'] === get_user_id()) { throw new \Exception("已加入,不可重复操作"); } } } if ($open) { throw new \Exception("当前团已开奖"); } if($pt_activity->party_type == 0){ if ($pt_activity->party_size <= (count($check_pt_order))) { throw new \Exception("当前团已达上限"); } } if($pt_activity->party_type == 1){ $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod') ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id') ->where(['po.is_pay' => 1, 'pod.is_delete' => 0]) ->andWhere(['OR' , ['po.id' => $mch['pt_number']], ['po.pt_number' => $mch['pt_number']]]); $goodsCount = $goodsCountQuery->sum('pod.num'); if ($pt_activity->party_goods_count <= (int)$goodsCount) { throw new \Exception("当前团商品数量已达上限"); } $buyCount = 0; foreach ($mch['goods_list'] as $_goods) { $buyCount += (int)$_goods['num']; if ($pt_activity->party_goods_count < ((int)$goodsCount + $buyCount)) { throw new \Exception("当前购买商品数量已超活动限制"); } } } } else { //判断团长是否积分充足 $head_integral = $pt_activity->head_integral; if ($head_integral > 0) { if ($head_integral > get_user()->integral) { throw new \Exception("开团积分不足"); } $this->head_integral = $head_integral; } } } else { //判断团长是否积分充足 $head_integral = $pt_activity->head_integral; if ($head_integral > 0) { if ($head_integral > get_user()->integral) { throw new \Exception("开团积分不足"); } $this->head_integral = $head_integral; } } if($pt_activity->party_type == 1){ $buyCount = 0; foreach ($mch['goods_list'] as $_goods) { $buyCount += (int)$_goods['num']; if ($pt_activity->party_goods_count < $buyCount) { throw new \Exception("当前购买商品数量已超活动限制" . $pt_activity->party_goods_count); } } } else { $buyCount = 0; foreach ($mch['goods_list'] as $_goods) { $buyCount += (int)$_goods['num']; if ($pt_activity->order_goods_limit < $buyCount) { throw new \Exception("当前购买商品数量已超活动限制" . $pt_activity->order_goods_limit); } } } // if ($submit == false) { // $mch['form'] = $this->getFormData(); // } else { // $mch['form'] = $this->getForm($mch['form']); // } $send_type_arr = Order::getSendTypeArr($this->store_id, $submit, $mch['goods_list'], ['mini_id' => get_mini_id()]); $mch['send_type'] = $send_type_arr; if (in_array('shop', $send_type_arr)) { $shopArr = $this->getShopList(); $mch['is_shop'] = $shopArr['shop']; $mch['shop_list'] = $shopArr['list']; } else { $mch['shop_list'] = []; $mch['is_shop'] = ''; } $total_price = 0; $level_price = 0; $next_level_price = 0; $integral = [ 'forehead' => 0, 'forehead_integral' => 0 ]; $mch['plugin_type'] = isset($mch['plugin_type']) ? $mch['plugin_type'] : 0; $open = false; foreach ($mch['goods_list'] as &$_goods) { if ($submit == false) { $_goods = array_merge($_goods, [ 'form' => [] ]); $_goods['form'] = $this->getNewFormData($_goods); } else { $_goods['form'] = $this->getForm($_goods['form']); } $total_price += doubleval($_goods['price']); if ((int)$_goods['product_type'] === 2) { $_goods['level_price'] = sprintf('%.2f', $mch['list']['price']); $_goods['price'] = $mch['list']['price']; } $level_price += doubleval($_goods['level_price']) > 0 ? doubleval($_goods['level_price']) : doubleval($_goods['price']); $_goods['integral'] = [ 'forehead' => 0, 'forehead_integral' => 0 ]; $integral['forehead'] = 0; $integral['forehead_integral'] = 0; unset($_goods['is_form']); unset($_goods['form_name']); } // $integral['forehead'] = round($integral['forehead'], 2); $mch['total_price'] = sprintf('%.2f', $total_price); $mch['level_price'] = sprintf('%.2f', $level_price); $mch['diy_send_type'] = [ [ 'key' => 'express', 'name' => Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'store', '快递配送')['value'] ?: '快递配送')['value'] ], [ 'key' => 'shop', 'name' => Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'store', '自提配送')['value'] ?: '自提配送')['value'] ], [ 'key' => 'delivery', 'name' => Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'store', '同城配送')['value'] ?: '同城配送')['value'] ] ]; $mch['integral'] = [ "forehead" => 0, "forehead_integral" => 0 ]; $mch['coupon_list'] = []; $mch['express_price'] = $this->getExpressPrice($mch); $mch['offer_rule'] = null; $mch['is_area'] = 0; } return $this->store_list; } protected function getGoodsList($goods_list) { $goodsIds = []; // $cards = []; foreach ($goods_list as $i => &$item) { $food_ext_goods = []; if ($item['goods_id']) { $attr_id_list = []; foreach ($item['attr'] as $_a) { array_push($attr_id_list, $_a['attr_id']); } $goods = Goods::findOne([ 'id' => $item['goods_id'], ]); } else { unset($goods_list[$i]); continue; } if (!$goods) { unset($goods_list[$i]); continue; } foreach ($attr_id_list as $index => $value) { if ($value < 0) { unset($attr_id_list[$index]); } } $cat_id_arr = GoodsCat::find()->where(['goods_id' => $goods->id, 'is_delete' => 0])->select('cat_id')->column(); $shop_count = Cat::find()->where(['id' => $cat_id_arr])->andWhere(['>', 'shop_count', 0])->select('shop_count')->min('shop_count') ?: 0; if ($shop_count > 0 && $shop_count > $item['num']) { return [ 'code' => 1, 'msg' => $goods->name . "起购数量为" . $shop_count, ]; } $attr = json_decode($goods->attr, true); $attr_info = $goods->getAttrInfo($attr_id_list); //获取拼团价格 $pt_activity_goods = PtActivityGoods::find()->alias('pag')->where(['pag.goods_id' => $goods->id]) ->leftJoin(['pa' => PtActivity::tableName()], 'pag.activity_id = pa.id') ->andWhere(['AND', ['pa.is_delete' => 0], ['>', 'pa.end_time', time()], ['<', 'start_time', time()], ['pag.is_delete' => 0]]) ->select('pa.id, pa.party_size, pa.party_type, pa.party_goods_count, pa.join_num, pa.start_time, pa.end_time, pag.attr, pag.pt_price, pag.use_attr, pag.sale_num, pag.virtual_sales, pag.activity_id, pa.store_id')->asArray()->one(); if ($pt_activity_goods) { $this->store_id = $pt_activity_goods['store_id']; $pt_goods_attr = json_decode($pt_activity_goods['attr'], true); foreach ($pt_goods_attr as $pt_attr) { $pt_attr_id = array_column($pt_attr['attr_list'], 'attr_id'); sort($pt_attr_id); sort($attr_id_list); if (empty(array_diff($pt_attr_id, $attr_id_list))) { $attr_info['price'] = $pt_attr['pt_price']; $goods->price = $pt_activity_goods['pt_price'] = $pt_attr['pt_price']; } foreach ($attr as &$g_attr) { $g_attr_id = array_column($g_attr['attr_list'], 'attr_id'); sort($g_attr_id); if (empty(array_diff($pt_attr_id, $g_attr_id))) { $g_attr['price'] = $pt_attr['pt_price']; } } } $attr = json_encode($attr); $this->pt_activity_id = $pt_activity_goods['activity_id']; $join_num = $pt_activity_goods['join_num']; if($pt_activity_goods['party_type'] == 0){ $user_id = 0; $saas_user = get_saas_user(); $user = User::findOne(['binding' => $saas_user->mobile, 'is_delete' => 0, 'store_id' => $pt_activity_goods['store_id']]); if ($user) { $user_id = $user->id; } $s_today_time = strtotime(date('Y-m-d')); $e_today_time = (strtotime(date('Y-m-d')) + 3600 * 24); $join_count = PtActivityOrderDetail::find()->alias('od') ->leftJoin(['o' => PtActivityOrder::tableName()], 'od.order_id = o.id') ->where(['od.goods_id' => $goods->id, 'o.user_id' => $user_id, 'is_pay' => 1]) ->andWhere(['AND', ['>', 'od.created_at', $s_today_time], ['<', 'od.created_at', $e_today_time]])->select('od.id')->count(); if ($join_num > 0 && $join_count >= $join_num) { return [ 'code' => 1, 'msg' => "今日参加此商品的拼团次数已达上限" ]; } } } else { return [ 'code' => 1, 'msg' => "拼团活动不存在" ]; } $attr_list = Attr::find()->alias('a') ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name') ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id') ->where(['a.id' => $attr_id_list, 'ag.store_id' => $this->store_id])->asArray()->all(); $item['user_id'] = !empty($user_id) ? $user_id : 0; $item['attr_list'] = $attr_list; $item['goods_id'] = $goods->id; $item['mch_id'] = $goods->mch_id; $item['rate'] = $goods->rate; $item['rate_type'] = $goods->rate_type; $item['goods_name'] = $goods->name; $item['goods_pic'] = $attr_info['pic'] ? $attr_info['pic'] : $goods->cover_pic; //如果没有使用规格,则使用商品售价 否则使用规格售价 $item['price'] = sprintf('%.2f', (($goods->use_attr ? $attr_info['price'] : $goods->price) * $item['num'])); $item['single_price'] = sprintf('%.2f', $attr_info['price']); $item['weight'] = $goods->weight; $item['integral'] = 0; $item['integral'] = $goods->integral ? $goods->integral : 0; $item['freight'] = 0; $item['full_cut'] = ""; $item['goods_cat_id'] = $goods->cat_id; $item['id'] = $goods->id; $item['verify_card_id'] = 0; $item['type'] = $goods->type; $item['delivery_type'] = Json::decode($goods->delivery_type); // 当前选择的规格 $attrIdArr = []; foreach ($item['attr_list'] as $attrListItem) { $attrIdArr[] = $attrListItem['attr_id']; } // $GoodsFullMinus = GoodsFullMinus::find() // ->select('*') // ->where(['store_id' => get_store_id(), 'goods_id' => $goods->id, 'is_delete' => 0]) // ->orderBy('full_minus_num ASC')->asArray()->all(); // 门店逻辑 $price = $goods->price; if ($pt_activity_goods) { $price = $pt_activity_goods['pt_price']; } // var_dump($price);die; // if (get_md_id() && empty(input_params('flag_id'))) { // $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]); // if ($md_goods) { // $attr = $md_goods->attr; // $price = $md_goods->price; // } // } $res = \app\modules\client\models\v1\common\CommonGoods::currentGoodsAttr([ 'attr' => $attr, 'price' => $price, 'is_level' => 0, 'mch_id' => $goods->mch_id, 'unit' => $goods->unit, 'use_attr' => $goods->use_attr, 'food_ext_goods' => $food_ext_goods, ], $attrIdArr, [], $item['num']); $item['batch_price_tips'] = $res['batch_price_tips']; $item['current_batch_price_tips'] = $res['current_batch_price_tips']; $item['price'] = sprintf('%.2f', ($res['price'] * $item['num'])); $item['level_price'] = sprintf('%.2f', ($res['level_price'] * $item['num'])); $item['next_level_price'] = sprintf('%.2f', ($res['next_level_price'] * $item['num'])); $item['is_level'] = $res['is_level']; $item['give'] = 0; $item['resIntegral'] = 0; $goodsIds[] = $goods->id; $item['goods_card_list'] = null; $item['is_form'] = $goods->is_form; $item['form_name'] = $goods->form_name; $item['product_type'] = $goods->product_type; // $res_acnu = $this->getActivityNewUserGoodsPrice($res, $goods, $attr_id_list, $submit, $item['num']); // if ($res_acnu['code'] === 0) { // $item['is_level'] = 0; // if ($res_acnu['price'] > 0) { // $item['price'] = $item['level_price'] = sprintf('%.2f', ($res_acnu['price'])); // $item['price_str'] = $res_acnu['price_str']; // } // } else { // return $res_acnu; // } // $res = $this->getSceKillGoodsPrice($goods, $attr_id_list, $submit, $item['num']); // if ($res['code'] === 0) { // $item['is_level'] = 0; // if ($res['data'] > 0) { // $item['price'] = $item['level_price'] = sprintf('%.2f', ($res['data'] * $item['num'])); // } // } else { // return $res; // } // if ($goods->product_type == Goods::GOODS_TYPE_ADOPT) { // $adopt_goods = AdoptGoods::find()->where(['goods_id' => $goods->id,'is_delete' => 0])->one(); // $item['desc'] = $adopt_goods->desc; // } } // 和空数组合并重建索引,避免出现因索引key间断导致客户端显示问题 return array_merge($goods_list, []); } //新人专享商品处理 // public function getActivityNewUserGoodsPrice($res, $goods, $attr, $submit, $num) // { // try { // $price = 0; // $price_str = []; // $activity = ActivityNewUser::activityAt($goods->store_id); // if($activity){ // $goods_ext = ActivityNewUserGoods::findOne(['activity_id' => $activity->id, 'goods_id' => $goods->id, 'is_delete' => 0]); // if($goods_ext){ // $ext_attrs = json_decode($goods_ext['attr'], true); // foreach ($ext_attrs as $ext_attr) { // $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id'); // //与购物车添加的规格id数组比较 // if (empty(array_diff($ext_attr_id, $attr))) { // $ext_price = $ext_attr['price']; // $id = $goods_ext['id']; // } // } // $buy_limit = $activity->buy_limit; // if (($buy_limit && $buy_limit > 0)) { // //部分享受优惠 // $goodsNum = intval(Goods::getBuyNum($this->user, $goods->id)); // $goodsNum = 0; // for($i=$goodsNum+$this->goods_count_activity_new_user[$goods->id]; $i<$num; $i++){ // if($i < $buy_limit){ // $price += $ext_price; // $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price); // $this->goods_count_activity_new_user[$goods->id]++; // }else{ // $price += sprintf('%.2f', $res['level_price']); // $price_str[] = sprintf('%.2f', $res['level_price']); // } // } // }else{ // //全部享受优惠 // $price = $ext_price * $num; // $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $num; // } // } // } // return [ // 'code' => 0, // 'msg' => '获取成功', // 'price' => $price, // 'price_str' => implode('+', $price_str), // 'id' => $id // ]; // } catch (\Exception $e) { // return [ // 'code' => 1, // 'msg' => $e->getMessage() // ]; // } // } //自定义表单 protected function getFormData() { $new_list = []; $new_list['is_form'] = Option::get('is_form', $this->store_id, 'store', 0)['value']; $form_list = []; if ($new_list['is_form'] == 1) { $new_list['name'] = Option::get('form_name', $this->store_id, 'store', '表单信息')['value']; $form_list = Form::find()->where([ 'store_id' => $this->store_id, 'is_delete' => 0, ])->orderBy(['sort' => SORT_ASC])->asArray()->all(); foreach ($form_list as $index => $value) { if (in_array($value['type'], ['radio', 'checkbox'])) { $default = str_replace(",", ",", $value['default']); $list = explode(',', $default); $default_list = []; foreach ($list as $k => $v) { $default_list[$k]['name'] = $v; if ($k == 0) { if ($value['type'] == 'radio') { $form_list[$index]['default'] = $v; } else { $form_list[$index]['default'] = [$v]; } $default_list[$k]['checked'] = true; } else { $default_list[$k]['checked'] = false; } } $form_list[$index]['default_list'] = $default_list; } } } $new_list['list'] = $form_list; return $new_list; } //自定义表单 protected function getNewFormData($goods) { $new_list = []; $new_list['is_form'] = $goods['is_form']; $form_list = []; if ($new_list['is_form'] == 1) { $new_list['name'] = $goods['form_name']; $form_list = Form::find()->where([ 'store_id' => $this->store_id, 'is_delete' => 0, 'goods_id' => $goods['id'] ])->orderBy(['sort' => SORT_ASC])->asArray()->all(); foreach ($form_list as $index => $value) { if (in_array($value['type'], ['radio', 'checkbox'])) { $default = str_replace(",", ",", $value['default']); $list = explode(',', $default); $default_list = []; foreach ($list as $k => $v) { $default_list[$k]['name'] = $v; if ($k == 0) { if ($value['type'] == 'radio') { $form_list[$index]['default'] = $v; } else { $form_list[$index]['default'] = [$v]; } $default_list[$k]['checked'] = true; } else { $default_list[$k]['checked'] = false; } } $form_list[$index]['default_list'] = $default_list; } } } $new_list['list'] = $form_list; return $new_list; } protected function getAddress() { if (!$this->address) { if ($this->address_id) { $this->address = Address::findOne(['id' => $this->address_id, 'user_id' => get_saas_user_id(), 'is_delete' => 0]); } else { $this->address = Address::find()->where([ 'user_id' => get_saas_user_id(), 'is_default' => 1, 'is_delete' => 0, ])->limit(1)->one(); } } return (object)$this->address; } //获取收货地址,有address_id优先获取,没有则获取默认地址 protected function getAddressData() { $address = $this->getAddress(); if (isset($address->id)) { return [ 'id' => $address->id, 'name' => $address->name, 'mobile' => $address->mobile, 'province_id' => $address->province_id, 'province' => $address->province, 'city_id' => $address->city_id, 'city' => $address->city, 'district_id' => $address->district_id, 'district' => $address->district, 'detail' => $address->detail, 'is_default' => $address->is_default, 'latitude' => $address->latitude, 'longitude' => $address->longitude, ]; } else { return null; } } //获取支付方式 public function getPayTypeList() { $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', '{"wechat":{"value":1}}')['value']); $pay_type_list = Json::decode($pay_type_list_json['value']); $new_list = []; if (is_wechat_platform() || $this->_from == 'app') { $new_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' ]; } if (is_alipay_platform() || $this->_from == 'app') { $new_list[] = [ 'name' => '支付宝支付', 'payment' => 4, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png' ]; } //如果是微信且商城已配置通联支付进件数据 if (is_wechat_platform()) { $store = Store::findOne(get_store_id()); if (!empty($store->cusid)) { $new_list[] = [ 'name' => '通联支付', 'payment' => Order::PAY_TYPE_YUNST_WECHAT_PAY, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' ]; } } if (is_toutiao_platform()) { $new_list[] = [ 'name' => '线上支付', 'payment' => 5, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' ]; } foreach ($new_list as $index => $item) { foreach ($pay_type_list as $pay_type_index => $pay_type_item) { if ($item['payment'] === 1 && $pay_type_index === 'wechat') { if (intval($pay_type_item['value']) === 0) { unset($new_list[$index]); } } if ($item['payment'] === 4 && $pay_type_index === 'alipay') { if (intval($pay_type_item['value']) === 0) { unset($new_list[$index]); } } } } return array_values($new_list); } /** * 支付方式 * @param array $is_payment //支付方式 * @param array $ignore //忽略的支付方式 * @return array */ public static function getPayType($is_payment = array(), $ignore = array()) { if (!$is_payment || empty($is_payment)) { $default = '{"wechat":{"value":1}}'; if (is_alipay_platform()) { $default = '{"alipay":{"value":1}}'; } if (is_toutiao_platform()) { $default = '{"toutiao":{"value":1}}'; } $pay_str = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', $default)['value']); $is_payment = Json::decode($pay_str['value']); } $pay_type_list = []; foreach ($is_payment as $index => $value) { if (in_array($index, $ignore)) { continue; } if ($index == 'wechat' && $value['value'] == 1 && is_wechat_platform()) { $pay_type_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png' ]; } if ($index == 'toutiao' && $value['value'] == 1 && is_toutiao_platform()) { $pay_type_list[] = [ 'name' => '线上支付', 'payment' => 5, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png' ]; } // if ($index == 'huodao' && $value['value'] == 1) { // $pay_type_list[] = [ // 'name' => '货到付款', // 'payment' => 2, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-huodao.png' // ]; // } if (get_params('_from') == OrderPayDataForm::PAY_FROM_APP || is_alipay_platform()) { if ($index == 'alipay' && $value['value'] == 1) { $pay_type_list[] = [ 'name' => '支付宝支付', 'payment' => 4, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/recharge/icon-alipay.png' ]; } } if ($index == 'balance' && $value['value'] == 1) { $pay_type_list[] = [ 'name' => '账户余额支付', 'payment' => 3, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-balance.png' ]; } } if (!$pay_type_list) { if (is_wechat_platform()) { $pay_type_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png' ]; } if (is_toutiao_platform()) { $pay_type_list[] = [ 'name' => '线上支付', 'payment' => 5, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png' ]; } } return $pay_type_list; } protected function getLevelData() { $level = Level::find()->select([ 'name', 'level', 'discount', ])->where(['level' => $this->user->level, 'store_id' => $this->store_id, 'is_delete' => 0]) ->asArray()->one(); return $level; } //积分计算 protected function getExpressPrice($mch) { $expressPrice = 0; if ($this->address) { $address = $this->address; //先计算单品满件包邮和满额包邮 $resGoodsList = Goods::cutFull($mch['goods_list']); //再通过运费规则计算运费 $expressPrice = PostageRules::getExpressPriceMore($this->store_id, $address['city_id'], $resGoodsList, $address['province_id']); } $expressPrice = $this->getFreeDeliveryRules($mch, $expressPrice); return $expressPrice >= 0 ? $expressPrice : 0; } // 获取门店列表 protected function getShopList() { $start = 0; $shop_table_name = Shop::tableName(); $latitude = 0; $longitude = 0; if ($this->address && isset($this->address['id'])) { $address = Address::findOne($this->address['id']); if ($address) { $latitude = $address->latitude ? $address->latitude : 0; $longitude = $address->longitude ? $address->longitude : 0; } } $sql = "SELECT *, acos(cos({$latitude}*pi()/180 )*cos(latitude*pi()/180)*cos({$longitude}*pi()/180 -longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as distance FROM {$shop_table_name} WHERE ((`store_id`={$this->store_id}) AND (`is_delete`=0)) "; if ($this->keyword) { $sql .= " AND (`name` like '%{$this->keyword}%') "; } $sql .= "ORDER BY `distance` LIMIT {$start},30"; $list = \Yii::$app->db->createCommand($sql)->queryAll(); $shop = null; foreach ($list as $index => $item) { if ($item['is_default'] == 1) { $shop = $item; } $list[$index]['distance'] = round($item['distance']/1000, 2).'km'; } return [ 'list' => $list, 'shop' => $shop ]; } protected function getTerritorialLimitation($mch) { $isArea = 0; if ($mch['mch_id'] > 0) { return $isArea; } if ($this->address) { $area = TerritorialLimitation::findOne([ 'store_id' => $this->store_id, 'is_delete' => 0, 'is_enable' => 1, ]); if ($area) { $city_id = []; //限制的地区ID $detail = json_decode($area->detail); if (!is_array($detail)) { $detail = []; } foreach ($detail as $key => $value) { foreach ($value->province_list as $key2 => $value2) { $city_id[] = $value2->id; } } $addressArr = [ $this->address['province_id'], $this->address['city_id'], $this->address['district_id'] ]; $addressArray = array_intersect($addressArr, $city_id); if (empty($addressArray)) { $isArea = 1; } } } return $isArea; } // 包邮规则 protected function getFreeDeliveryRules($mch, $expressPrice) { if ($expressPrice == 0) { return $expressPrice; } if ($mch['mch_id'] == 0) { $free = FreeDeliveryRules::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'mch_id' => 0])->asArray()->all(); foreach ($free as $k => $v) { $city = json_decode($v['city'], true); foreach ($city as $v1) { if ($this->address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) { $expressPrice = 0; break; } } } } else { $free = FreeDeliveryRules::find()->where(['store_id' => $this->store_id, 'mch_id' => $mch['mch_id'], 'is_delete' => 0])->asArray()->all(); foreach ($free as $k => $v) { $city = json_decode($v['city'], true); foreach ($city as $v1) { if ($this->address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) { $expressPrice = 0; break; } } } } return $expressPrice; } // 获取用户填写的自定义表单 protected function getForm(&$form) { if ($form['is_form'] == 1) { $formList = &$form['list']; foreach ($formList as $index => $value) { if ($value['required'] == 1) { if (in_array($value['type'], ['radio', 'checkbox'])) { $is_true = false; foreach ($value['default_list'] as $k => $v) { if ($v['checked'] == true) { $is_true = true; } } if (!$is_true) { return [ 'code' => 1, 'msg' => '请填写' . $form['name'] . ',加“*”为必填项', 'name' => $value['name'] ]; } } else { if (empty($value['default']) && strlen($value['default']) == 0) { return [ 'code' => 1, 'msg' => '请填写' . $form['name'] . ',加“*”为必填项', 'name' => $value['name'] ]; } } } if ($value['type'] == 'checkbox') { $formList[$index]['default'] = is_array($value['default']) ? implode(',', $value['default']) : $value['default']; } } } return $form; } protected function goodsCardList() { $list = []; foreach ($this->mch_list as $mch) { if($mch['mch_id'] == 0) { foreach ($mch['goods_list'] as $goods) { if (!empty($goods['goods_list'])) { foreach ($goods['goods_list'] as $val) { if(!$val['goods_card_list']) { $val['goods_card_list'] = []; } $list = array_merge($list, $val['goods_card_list']); } } } } } return $list; } /** * @param $goods * @param $type */ public function bookCheckGoodsNum($goods, $type) { // 酒店预约 if ($type == 1) { // $date_book = GoodsBook::findOne(['goods_id' => $goods['id']])->date_book; // $date_book = Json::decode($date_book); // if (!$date_book) { // return [ // 'code' => 1, // 'msg' => '数据异常' // ]; // } // $data_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'num')); // // foreach ($goods['book'] as $value) { // if ($data_config[$value['date']] < $goods['num']) { // return [ // 'code' => 1, // 'msg' => '选中日期' . $value['date'] . '内暂无房源' // ]; // } // } } // 服务预约 if ($type == 2) { $service_book = GoodsBook::findOne(['goods_id' => $goods['id']])->service_book; $service_book = Json::decode($service_book); if (!$service_book) { return [ 'code' => 1, 'msg' => '数据异常' ]; } $date_data = $this->getDateByInterval(7); $count = 0; $new_arr = [ 'data' => [] ]; $order = Order::find()->alias('o') ->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id') ->where(['o.trade_status' => [0, 2, 3], 'o.is_delete' => 0, 'o.order_type' => 2, 'goods_id' => $goods['id']]) ->andWhere(['>', 'o.created_at', strtotime(date("Y-m-d"))]) ->select('od.attr, od.num')->asArray()->all(); foreach ($date_data as $index => $datum) { $new_arr['data'][$index]['date'] = $datum; $new_arr['data'][$index]['time'] = $service_book['data'][0]['time']; } if (!$new_arr) { return [ 'code' => 1, 'msg' => '数据异常' ]; } $service_book = $new_arr['data']; $service_book_date = array_column($service_book, NULL, 'date'); $service_book_time = $service_book_date[$goods['service']['date']]['time']; $num = 0; $price = 0; foreach ($order as $item) { $item['attr'] = json_decode($item['attr'], true); if ($item['attr']['date'] === $goods['service']['date']) { if ($goods['service']['time'] === $item['attr']['time']) { $num += $item['num']; } } } foreach ($service_book_time as $m) { foreach ($m['times'] as $n) { if ($n['time'] == $goods['service']['time']) { if (($goods['num'] + $num) > $n['num']) { return [ 'code' => 1, 'msg' => $n['time'] . '时间段内暂不可预约' ]; } } } } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'price' => $price ] ]; } /** * 计算初始价格 * @param $order_id * @return array */ public static function findPrice($order_id) { $order_detail = OrderDetail::findAll(['order_id' => $order_id]); $original_price = 0; $md_price = 0; foreach ($order_detail as $detail) { $goods = Goods::findOne($detail['goods_id']); if ($goods->use_attr > 0) { $attrs = Json::decode($goods->attr); // 当前选择的规格 $attrIdArr = []; foreach (Json::decode($detail['attr']) as $attrListItem) { $attrIdArr[] = $attrListItem['attr_id']; } foreach ($attrs as $attr) { $attrIds = []; foreach ($attr['attr_list'] as $item) { $attrIds[] = $item['attr_id']; } sort($attrIds); sort($attrIdArr); // 找出当前规格信息 if (implode($attrIds) === implode($attrIdArr)) { $original_price += $attr['price']; } } } else { $original_price += $goods->price; } } foreach ($order_detail as $detail) { $md_goods = MdGoods::findOne(['goods_id' => $detail['goods_id'], 'md_id' => get_md_id()]); $goods = Goods::findOne($detail['goods_id']); $goods_attr = $goods->attr; if ($md_goods) { $goods_attr = $md_goods->attr; } if ($goods->use_attr == 1) { $attrs = Json::decode($goods_attr); // 当前选择的规格 $attrIdArr = []; foreach (Json::decode($detail['attr']) as $attrListItem) { $attrIdArr[] = $attrListItem['attr_id']; } foreach ($attrs as $attr) { $attrIds = []; foreach ($attr['attr_list'] as $item) { $attrIds[] = $item['attr_id']; } sort($attrIds); sort($attrIdArr); // 找出当前规格信息 if (implode($attrIds) === implode($attrIdArr)) { $md_price += $attr['price']; } } } else { if ($md_goods) { $md_price += $md_goods->price; } else { $md_price += $goods->price; } } } return [ 'original_price' => $original_price > 0 ? $original_price : $md_price, 'md_price' => $md_price ]; } public function getDateByInterval(int $num) :array { //var_dump($st, $et);die; $returnData = []; $i = 0; do { $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d')))); $returnData[] = $temp; $i++; } while ($i < $num); return $returnData; } //判断是否需为代理配送且要上门安装 public function goodsIsInstall($goods_id) { try { $goods = Goods::findOne($goods_id); $goods_url = "/goods/getAttrParams"; $param = [ 'id' => $goods->cloud_goods_id ]; //请求接口 $domain = (new OptionSetting)->getCloudDomainName(); $goodsInfo = cloud_post($domain . $goods_url, $param); $goodsInfo = json_decode($goodsInfo, true); if ((int)$goodsInfo['code'] === 0) { $goodsInfo = $goodsInfo['data']; //需要安装 if ((int)$goodsInfo['send_type'] === 3 && (int)$goodsInfo['is_need_install'] === 1) { return 1; } //不需要安装 if ((int)$goodsInfo['send_type'] === 3) { return 3; } } return 2; } catch (\Exception $e) { return 2; } } //判断是否存在代理 public function goodsIsGoodsAgent($address, $goods_id) { try { if (empty($address) || !is_array($address)) { throw new \Exception('缺少地址信息'); } $goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0]); if ((!empty($goods) && empty($goods->cloud_goods_id) || empty($goods) )) { return [ 'code' => 0, 'msg' => '非云仓商品' ]; } $goods_agent_admin = AgentGoodsBindGoods::find()->where(['cloud_goods_id' => $goods->cloud_goods_id, 'status' => 1, 'is_delete' => 0]) ->select('goods_agent_admin_id')->column(); $where = [ 'province_id' => $address['province_id'], 'type' => 'goods_agent', 'is_delete' => 0, 'id' => $goods_agent_admin ]; $admin = Admin::findOne(array_merge( [ 'area_level' => 1, 'city_id' => $address['city_id'], 'district_id' => $address['district_id'], ], $where)); if (!empty($admin)) { return [ 'code' => 0, 'msg' => 'success', 'data' => $admin ]; } $admin = Admin::findOne(array_merge( [ 'area_level' => 2, 'city_id' => $address['city_id'], ], $where)); if (!empty($admin)) { return [ 'code' => 0, 'msg' => 'success', 'data' => $admin ]; } $admin = Admin::findOne(array_merge( [ 'area_level' => 3, ], $where)); if (!empty($admin)) { return [ 'code' => 0, 'msg' => 'success', 'data' => $admin ]; } return [ 'code' => 1, 'msg' => '该区域未存在产品代理,无法下单' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } // 检测拼团信息 public function checkPtActivity($pt_number) { $pt_order = PtActivityOrder::findOne(['id' => $pt_number]); if ($pt_order) { if ((int)$pt_order->is_pay === 0) { return [ 'code' => 1, 'msg' => "当前团暂未开放" ]; } //error $check_pt_order = PtActivityOrder::find()->where(['trade_status' => 0, 'is_pay' => 1]) ->andWhere(['OR', ['id' => $pt_number], ['pt_number' => $pt_number]]) ->select('id')->column(); $pt_activity = PtActivity::findOne($this->pt_activity_id); if (!$this->pt_activity_id) { return [ 'code' => 1, 'msg' => "活动信息不存在或已结束" ]; } if($pt_activity->party_type == 0){ if ($pt_activity->party_size <= (count($check_pt_order))) { return [ 'code' => 1, 'msg' => "当前团已达上限" ]; } } if($pt_activity->party_type == 1){ $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod') ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id') ->where(['po.is_pay' => 1, 'pod.is_delete' => 0]) ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]); $goodsCount = $goodsCountQuery->sum('pod.num'); if($pt_activity->party_goods_count <= (int)$goodsCount){ return [ 'code' => 1, 'msg' => "当前团商品数量已达上限" ]; } } } return ['code' => 0, 'msg' => '']; } }