Browse Source

增加运费区域列表,运费区域创建功能

antshop
linyaostalker 5 years ago
parent
commit
fc72e148d1
  1. 79
      backend/modules/shop/controllers/ExpressTemplateController.php
  2. 50
      backend/modules/shop/views/express-template/express_area_create.php
  3. 145
      backend/modules/shop/views/express-template/express_area_form.php
  4. 51
      backend/modules/shop/views/express-template/express_area_update.php

79
backend/modules/shop/controllers/ExpressTemplateController.php

@ -108,7 +108,7 @@ class ExpressTemplateController extends Controller
$provinces = Province::find()->cache(0)->all();
foreach ($provinces as $k => $v) {
$data[$k]['province'] = $v->name;
$cities = City::find()->cache(0)
$cities = City::find()
->where(['province_id' => $v->province_id])
->all();
foreach ($cities as $city) {
@ -153,7 +153,7 @@ class ExpressTemplateController extends Controller
$provinces = Province::find()->cache(0)->all();
foreach ($provinces as $k => $v) {
$data[$k]['province'] = $v->name;
$cities = City::find()->cache(0)
$cities = City::find()
->where(['province_id' => $v->province_id])
->all();
foreach ($cities as $city) {
@ -233,4 +233,79 @@ class ExpressTemplateController extends Controller
'expressTemplate' => $expressTemplate
]);
}
/**
* @return array|mixed|string|Response
* 运费区域模板区域创建方法
*/
public function actionExpressAreaCreate()
{
$expressTemplateId = Yii::$app->request->get('expressTemplateId');
$expressTemplateModel = ExpressTemplate::findOne($expressTemplateId);
$model = new ExpressArea();
$model->basic_count = 1;
$model->basic_price = '0.00';
$model->express_template = $expressTemplateModel->id;
if (Yii::$app->request->isPost) {
$data = Yii::$app->request->post('ExpressArea');
if (Yii::$app->request->isAjax) {
$model->load($data, '');
Yii::$app->response->format = Response::FORMAT_JSON;
$data = ActiveForm::validate($model);
$data['status'] = 2;
return $data;
}
if (Yii::$app->request->post('area') == null) {
return $this->redirect(Yii::$app->request->referrer . '?status=1');
}
$cityIds = array_keys(Yii::$app->request->post('area'));
$data['city'] = implode(',', $cityIds);
$model->load($data, '');
$model->basic_price *= 100;
$model->extra_price *= 100;
if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
$model->basic_count *= 10;
$model->extra_count *= 10;
} else {
$model->basic_count *= 1;
$model->extra_count *= 1;
}
$model->save();
return $this->redirect('express-area-list?id='.$model->express_template);
}
$data = [];
$expressAreas = ExpressArea::find()->select(['city'])->where(['express_template' => $expressTemplateModel->id])->all();
$expressAresCityIdArr = [];
if ($expressAreas) {
foreach ($expressAreas as $expressAreaCity) {
$cityIdArr = explode(',', $expressAreaCity->city);
$expressAresCityIdArr = array_unique(array_merge($cityIdArr, $expressAresCityIdArr));
}
}
$provinces = Province::find()->cache(0)->all();
$j = 0;
foreach ($provinces as $k => $v) {
$cities = City::find()
->where(['province_id' => $v->province_id])
->andWhere(['not in', 'city_id', $expressAresCityIdArr])
->all();
if ($cities) {
$data[$j]['province'] = $v->name;
foreach ($cities as $city) {
$data[$j]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
}
$j++;
}
}
if (empty($data)) {
Yii::$app->session->setFlash('error', '已无地区选择');
return $this->redirect('express-area-list?id='.$expressTemplateModel->id);
}
return $this->render('express_area_create', [
'model' => $model,
'data' => $data,
'expressTemplateModel' => $expressTemplateModel
]);
}
}

50
backend/modules/shop/views/express-template/express_area_create.php

@ -0,0 +1,50 @@
<?php
use yii\bootstrap4\Html;
use yii\bootstrap4\ActiveForm;
use kartik\tabs\TabsX;
/* @var $this yii\web\View */
/* @var $model backend\modules\shop\models\ars\ExpressTemplate */
$this->title = '创建运费模板';
$this->params['breadcrumbs'][] = ['label' => '运费模板', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
Yii::$app->params['bsVersion'] = '4.x';
?>
<div class="express-template-create">
<div class="express-template-form">
<?php
$form = ActiveForm::begin(['options' => ['class' => 'container-fluid']]);
echo TabsX::widget([
'bordered' => true,
'items' => [
[
'label' => '<i class="fas fa-user"></i> 基本信息',
'content' => $this->render('_form', [
'model' => $model,
'form' => $form,
]),
],
[
'label' => '<i class="fas fa-globe"></i> 选择配送区域',
'content' => $this->render('area', ['data' => $data, 'form' => $form, 'cities' => []
]),
],
],
'position' => TabsX::POS_ABOVE,
'encodeLabels' => false
]);
?>
<div class="form-group">
<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
<?= Html::a('返回', ['index'], ['class' => 'btn btn-info']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>

145
backend/modules/shop/views/express-template/express_area_form.php

@ -0,0 +1,145 @@
<?php
use blobt\widgets\Icheck;
use backend\modules\shop\models\ars\ExpressTemplate;
/* @var $this yii\web\View */
/* @var $model backend\modules\shop\models\ars\ExpressTemplate */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
$status = Yii::$app->request->get('status');
if ($status == 1) {
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
aria-hidden="true">&times;</span></button>
<p style="color: #2e2e2e">必须至少选择一个城市为配送区域</p>
</div>
<?php } ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'calculation_type')->widget(Icheck::className(), ["items" => ExpressTemplate::$calculationType, 'type' => "radio"]) ?>
<?= $form->field($model, 'basic_count')->textInput() ?>
<?= $form->field($model, 'basic_price')->textInput() ?>
<?= $form->field($model, 'extra_count')->textInput() ?>
<?= $form->field($model, 'extra_price')->textInput() ?>
<?php
$js=<<<JS
const formList = [//切换时,class对应的标题
{
"field-expresstemplate-basic_count":"基本重量(KG)",
"field-expresstemplate-basic_price":"基本运费(元)",
"field-expresstemplate-extra_count":"续重重量(KG)",
"field-expresstemplate-extra_price":"续重运费(元)"
},
{
"field-expresstemplate-basic_count":"基本数量(件)",
"field-expresstemplate-basic_price":"基本运费(元)",
"field-expresstemplate-extra_count":"续重数量(件)",
"field-expresstemplate-extra_price":"续重运费(元)"
}
]
const udfVal = [//初始值
[0.1,"0.00"],
[1,"0.00"]
]
var calType = 1;//初始的计算方式0:计重 1:计件
function updateTypeChangeCalType(type){//当切换计算方式
$.each(formList[type],function(index,value){ //更改文字标题
$("." + index).children("label").html(value)
});
$("#expresstemplate-basic_count").val(udfVal[type][0])//重置初始值
$("#expresstemplate-basic_price").val(udfVal[type][1])
$("#expresstemplate-extra_count").val(0)
$("#expresstemplate-extra_price").val(udfVal[type][1])
calType = type;
}
function changeCalType(type){//当切换计算方式
$.each(formList[type],function(index,value){ //更改文字标题
$("." + index).children("label").html(value)
});
if(!$("#expresstemplate-basic_count").val()){
$("#expresstemplate-basic_count").val(udfVal[type][0])//重置初始值
}
if(!$("#expresstemplate-basic_price").val()){
$("#expresstemplate-basic_price").val(udfVal[type][1])
}
if(!$("#expresstemplate-extra_count").val()){
$("#expresstemplate-extra_count").val(0)
}
if(!$("#expresstemplate-extra_price").val()){
$("#expresstemplate-extra_price").val(udfVal[type][1])
}
calType = type;
}
$(document).ready(function(){
$("#expresstemplate-basic_count").blur(function(){
if (calType == 0) {
if($(this).val() < 0.1){
$(this).val(0.1)
}
var basiccount = $(this).val();
$(this).val(Math.floor(basiccount * 10) / 10);
} else{
if($(this).val() < 1){
$(this).val(1)
}
var basiccount = $(this).val();
$(this).val(Math.floor(basiccount * 1) / 1);
}
})
$("#expresstemplate-basic_price").blur(function(){
if($(this).val().indexOf('-') != -1){
$(this).val("0.00")
}
var basicPrice = $(this).val();
$(this).val(basicPrice.toString().match(/^\d+(?:\.\d{0,2})?/));
})
$("#expresstemplate-extra_count").blur(function(){
if (calType == 0) {
if($(this).val() < 0){
$(this).val(0)
}
var basiccount = $(this).val();
$(this).val(Math.floor(basiccount * 10) / 10);
} else{
if($(this).val() < 0){
$(this).val(0)
}
var basiccount = $(this).val();
$(this).val(Math.floor(basiccount * 1) / 1);
}
})
$("#expresstemplate-extra_price").blur(function(){
if($(this).val().indexOf('-') != -1){
$(this).val("0.00")
}
var basicPrice = $(this).val();
$(this).val(basicPrice.toString().match(/^\d+(?:\.\d{0,2})?/));
})
$("input:radio[name='ExpressTemplate[calculation_type]']").on('ifChecked', function(event){
updateTypeChangeCalType($(this).val()-1)
})
changeCalType(calType)
})
JS;
$this->registerJs($js)
?>

51
backend/modules/shop/views/express-template/express_area_update.php

@ -0,0 +1,51 @@
<?php
use yii\bootstrap4\Html;
use yii\bootstrap4\ActiveForm;
use kartik\tabs\TabsX;
/* @var $this yii\web\View */
/* @var $model backend\modules\shop\models\ars\ExpressTemplate */
$this->title = '编辑运费模板: ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => '运费模板', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update ';
Yii::$app->params['bsVersion'] = '4.x';
?>
<div class="express-template-update">
<div class="express-template-form">
<?php
$form = ActiveForm::begin(['options' => ['class' => 'container-fluid']]);
echo TabsX::widget([
'bordered' => true,
'items' => [
[
'label' => '<i class="fas fa-user"></i> 基本信息',
'content' => $this->render('_form', [
'model' => $model,
'form' => $form,
]),
],
[
'label' => '<i class="fas fa-globe"></i> 选择配送区域',
'content' => $this->render('area', ['data' => $data, 'form' => $form, 'cities' => $cities
]),
],
],
'position' => TabsX::POS_ABOVE,
'encodeLabels' => false
]);
?>
<div class="form-group">
<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
<?= Html::a('返回', ['index'], ['class' => 'btn btn-info']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
Loading…
Cancel
Save