| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- <?php
- namespace addons\Store\client\controllers;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCate;
- use addons\Store\common\models\StoreFreightRules;
- use addons\Store\common\models\StoreGoodsModel;
- use addons\Store\common\models\StoreModel;
- use addons\Store\common\models\StoreScoreStoreApply;
- use addons\Store\common\models\StoreSettings;
- use addons\Store\common\models\StoreServiceRatioLog;
- use addons\Store\common\services\StoreSettingService;
- use common\enums\AddonsEnum;
- use common\enums\GoodsTypeEnum;
- use common\enums\StatusEnum;
- use common\globals\GlobalInvoke;
- use common\logic\common\AddonsLimit;
- use common\models\common\Region;
- use common\models\goods\FreightRules;
- use Yii;
- use addons\Store\common\models\StoreCommunity;
- use common\models\user\Town;
- class SettingController extends BaseController
- {
- public $enableCsrfValidation = false;
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $detail = Store::getOne([['id' => $this->store_id], ['mall_id' => $this->mall_id]], true);
- $detail['cIds'] = [];
- $detail['province'] = '';
- $detail['city'] = '';
- $detail['district'] = '';
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
- $detail['community_status'] = $setting['community_status']??0;
- if ($detail) {
- if (!empty($detail['c_id'])) {
- $cate = StoreCate::findOne(['mall_id' => $this->mall_id, 'id' => $detail['c_id']]);
- if (!empty($cate['parent_id'])) {
- $parent = StoreCate::findOne(['mall_id' => $this->mall_id, 'id' => $cate['parent_id']]);
- if (!empty($parent['parent_id'])) {
- $detail['cIds'][] = $parent['parent_id'];
- }
- $detail['cIds'][] = $cate['parent_id'];
- }
- $detail['cIds'][] = $detail['c_id'];
- }
- $region_id_arr = [$detail['province_id'], $detail['city_id'], $detail['district_id']];
- $region_list = Region::lists(['where' => [['id' => $region_id_arr]], 'index' => 'id', 'select' => 'id,name']);
- if (!empty($detail['province_id'])) {
- $detail['province'] = $region_list[$detail['province_id']]['name'];
- $detail['city'] = $region_list[$detail['city_id']]['name'];
- $detail['district'] = $region_list[$detail['district_id']]['name'];
- $streetInfo = Town::findOne(['id' => $detail['street_id']]);
- $detail['street'] = $streetInfo['name']??'';
- if($detail['community_id'] == '-1'){
- $detail['community'] = '其它社区';
- }else{
- $communityInfo = StoreCommunity::findOne(['street_id' => $detail['street_id'],'status'=>0]);
- $detail['community'] = $communityInfo['name']??'';
- }
- }
- }
- $detail['address_detail'] = $detail['address'] ?? '';
- $detail['ll'] = $detail['latitude'] . ',' . $detail['longitude'];
- if (!empty($detail['freight_type'])) {
- $detail['freight_type'] = json_decode($detail['freight_type'], true);
- } else {
- $detail['freight_type'] = [
- 'express_delivery' => 0,
- 'merchant_distribution' => 0,
- 'self_mention' => 0,
- ];
- }
- if (!empty($detail['intra_city_distribution'])) {
- $detail['intra_city_distribution'] = json_decode($detail['intra_city_distribution'], true);
- } else {
- $detail['intra_city_distribution'] = [];
- }
- if (!empty($detail['introduce'])) {
- $detail['introduce'] = json_decode($detail['introduce'], true);
- } else {
- $detail['introduce'] = [];
- }
- if (!empty($detail['bannar_pic'])) {
- $detail['bannar_pic'] = json_decode($detail['bannar_pic'], true);
- } else {
- $detail['bannar_pic'] = [];
- }
- if (!isset($detail['show_style'])) {
- $detail['show_style'] = "1";
- }
- if (isset($detail['pickup'])) {
- $detail['pickup'] = json_decode($detail['pickup'], true);
- } else {
- $detail['pickup'] = ['status' => 0];
- }
- $platform_address = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic.platform_address');
- $detail['platform_address'] = $platform_address ?? '';
- $decoration = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'decoration');
- $custom_store_style = $decoration['custom_store_style']??0;
- return $this->success('操作成功', ['detail' => $detail,'custom_store_style'=>$custom_store_style]);
- }
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $params['form']['id'] = $this->store_id;
- $params['form']['mall_id'] = $this->mall_id;
- // $params['form']['check_status'] = 0;
- $params['form']['c_id'] = array_pop($params['form']['cIds']);
- $businessTimeStart = strtotime($params['form']['business_time_start']);
- $businessTimeEnd = strtotime($params['form']['business_time_end']);
- $res = Store::setData($params['form']);
- if ($res == false) return $this->success(Store::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->render($this->action->id);
- }
- public function actionGoodsModel()
- {
- // 获取组件配置
- $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
- $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
- $addons_arr = [];
- if (!empty($basic['store_agent_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_AGENT;
- if (!empty($basic['store_area_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_AREA;
- if (!empty($basic['store_distribution_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_DISTRIBUTION;
- if (!empty($basic['store_rebate_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_REBATE;
- if ($addons_setting) {
- foreach ($addons_setting as $key => $val) {
- if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
- }
- } else {
- $addons_setting = [];
- }
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- // 获取商品详情
- $goods_model = StoreModel::getOne([['mall_id' => $this->mall_id, 'store_id' => $this->store_id]], true);
- $content = [];
- if (!empty($goods_model['content'])) {
- $content = json_decode($goods_model['content'], true);
- }
- // 获取配置
- foreach ($content as $key => &$item) {
- if (isset($content[$key])) {
- $setting[$key] = $content[$key];
- }
- }
- // 获取等级
- $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
- $level_params['mall_id'] = $this->mall_id;
- $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
- $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
- unset($commission_type_map['digital']);
- $status = $goods_model['status'];
- return $this->success('操作成功', compact('status', 'level', 'setting', 'commission_type_map'));
- } else {
- $store_setting = StoreSettings::findOne(['store_id'=>$this->store_id,'mall_id'=>$this->mall_id, 'status' => StatusEnum::ENABLED]);
- if(!empty($store_setting)){
- if($store_setting['store_settle_type']!=3 && $store_setting['store_settle_type']!=4){
- if(empty($store_setting['is_allow_editing'])) {
- return $this->error('分佣由平台承担,门店无权编辑');
- }
- }
- }else{
- if($basic['settle_type']!=3 && $basic['settle_type']!=4){
- return $this->error('分佣由平台承担,门店无权编辑');
- }
- }
- $params = Yii::$app->request->post('form');
- $status = 0;
- if (!empty($params['status'])) {
- $status = $params['status'];
- unset($params['status']);
- }
- $res = Yii::$app->services->validate->validate($params, [
- [array_keys($addons_setting), 'string']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- if(isset($params['Agent'])) $params['Agent'] = json_decode($params['Agent'] ,true);
- if(isset($params['Distribution'])) $params['Distribution'] = json_decode($params['Distribution'] ,true);
- if(isset($params['Area'])) $params['Area'] = json_decode($params['Area'] ,true);
- if(isset($params['Rebate'])) $params['Rebate'] = json_decode($params['Rebate'] ,true);
- $param = [
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'status' => $status,
- 'content' => json_encode($params),
- ];
- $res = StoreModel::setData($param);
- if ($res === false) return $this->error(StoreGoodsModel::getStaticError());
- return $this->success("保存成功");
- } catch (\Exception $e) {
- return $this->error("保存失败,msg=" . $e->getMessage());
- }
- }
- }
- return $this->render($this->action->id, ['addons_setting' => $addons_setting]);
- }
- public function actionFreightRules()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $keyword = Yii::$app->request->get('keyword', '');
- $where = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'status' => StatusEnum::ENABLED]];
- $keyword && $where[] = ['like', 'name', $keyword . '%', false];
- $order = 'id desc';
- $params = compact('where', 'order');
- $page_data = StoreFreightRules::listPage($params);
- if ($page_data === false) return $this->error(StoreFreightRules::getStaticError());
- return $this->success('操作成功', $page_data);
- } else {
- $form = Yii::$app->request->post();
- $rules = [[['id'], 'required'], [['id'], 'integer']];
- switch ($form['type']) {
- case 'destroy':
- $form['status'] = StatusEnum::DELETE;
- $rules[] = [['status'], 'required'];
- break;
- case 'default':
- $rules[] = [['is_default'], 'required'];
- break;
- }
- $res = Yii::$app->services->validate->validate($form, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $form['mall_id'] = $this->mall_id;
- $form['store_id'] = $this->store_id;
- $res = StoreFreightRules::setData($form);
- if ($res === false) return $this->error(FreightRules::getStaticError());
- return $this->success('操作成功', []);
- }
- }
- return $this->render($this->action->id);
- }
- public function actionFreightEdit()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [[['id'], 'required'], [['id'], 'integer']]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $detail = StoreFreightRules::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id, 'store_id' => $this->store_id]], true);
- $detail['rule'] = json_decode($detail['rule'], true);
- return $this->success('操作成功', compact('detail'));
- } else {
- $id = Yii::$app->request->get('id');
- $form = Yii::$app->request->post('form');
- $res = Yii::$app->services->validate->validate($form, [
- [['id','select_com'], 'integer'],
- [['name', 'type', 'rule'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $form['id'] = $id;
- $form['mall_id'] = $this->mall_id;
- $form['store_id'] = $this->store_id;
- $res = StoreFreightRules::setData($form);
- if ($res === false) return $this->error(StoreFreightRules::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->render($this->action->id);
- }
- public function actionConfig()
- {
- if (\Yii::$app->request->isGet) {
- $decoration = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'decoration');
- $settings = [
- 'decoration' => $decoration,
- ];
- return $this->success('success', $settings);
- }
- }
- //todo 不用验证权限
- public function actionGetAddressList()
- {
- $get = Yii::$app->request->get();
- $level = $get['level'] ?? 1;
- if ($level > 3) {
- if ($level == 4) {
- $where[] = ['district_id' => $get['id']];
- $params['where'] = $where;
- $params['select'] = 'id,name';
- $list = Town::lists($params);
- } else {
- $where[] = ['mall_id' => $this->mall_id, 'street_id' => $get['id'],'status'=>0];
- $params['where'] = $where;
- $params['select'] = 'id,community_name as name';
- $list = StoreCommunity::lists($params);
- $data = ['id'=>'-1','name'=>'其它社区'];
- $list[] = $data;
- }
- } else {
- if (!empty($get['id'])) $where[] = ['parent_id' => $get['id']];
- $where[] = ['level' => $level];
- $params['where'] = $where;
- $params['select'] = 'id,name,parent_id,level,lng,lat';
- $list = Region::lists($params);
- }
- return $this->success('操作成功', $list);
- }
- public function actionStoreCateTree()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $cond = [['mall_id' => $this->mall_id]];
- $list = StoreCate::getCates($cond, [], true, 'id,name,parent_id');
- $list = StoreCate::getTreeCate($list);
- return $this->success('操作成功', compact('list'));
- }
- }
- }
- /**
- * 客服管理
- * @return string
- */
- public function actionCustomer()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $setting = StoreSettings::find()
- ->select(['is_customer', 'customer_types'])->where(['mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'status' => StatusEnum::ENABLED]
- )->asArray()->one();
- if (!empty($setting['customer_types'])) $setting['customer_types'] = json_decode($setting['customer_types'], true);
- return $this->success("请求成功", ['setting' => $setting]);
- }
- if (Yii::$app->request->isPost) {// post提交
- $is_customer = Yii::$app->request->post('is_customer', 0);
- $customer_types = Yii::$app->request->post('customer_types', '');
- if (!empty($customer_types)) $customer_types = json_encode($customer_types);
- $setting = StoreSettings::findOne(['mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'status' => StatusEnum::ENABLED]
- );
- if (!$setting) {
- $setting = new StoreSettings();
- }
- $setting->mall_id = $this->mall_id;
- $setting->store_id = $this->store_id;
- $setting->is_customer = $is_customer;
- $setting->customer_types = $customer_types;
- if (!$setting->save()) {
- return $this->error("保存失败");
- }
- return $this->success("保存成功");
- }
- }
- return $this->render($this->action->id);
- }
- public function actionScoreDeduction()
- {
- try {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $score_deduction = (new StoreSettingService(['mall_id'=>$this->mall_id,'store_id'=>$this->store_id]))->getScoreDeduction();
- return $this->success("请求成功", $score_deduction);
- }
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- if(empty($params['score_give']['give_num'])){
- $params['score_give']['give_num'] = 0;
- }
- if(!empty($params['score_deduction']['max_deduction_percent'])) {
- $platform_score_deduction = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'payment.score_deduction');
- $platform_score_deduction = json_decode($platform_score_deduction, true);
- $platform_score_deduction['max_deduction_percent'] =
- !empty($platform_score_deduction['store_limit_type']) && isset($platform_score_deduction['store_limit_stores_deduction'][$this->store_id]) && is_numeric($platform_score_deduction['store_limit_stores_deduction'][$this->store_id]) ?
- $platform_score_deduction['store_limit_stores_deduction'][$this->store_id] : $platform_score_deduction['max_deduction_percent'];
- if (isset($platform_score_deduction['max_deduction_percent']) && $params['score_deduction']['max_deduction_percent'] > $platform_score_deduction['max_deduction_percent']) {
- return $this->error("独立设置的最高抵扣比例不能大于平台最高抵扣比例");
- }
- }
- (new StoreSettingService(['mall_id'=>$this->mall_id,'store_id'=>$this->store_id]))->updateScoreDeduction($params);
- return $this->success("保存成功");
- }
- }
- return $this->render($this->action->id);
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionScoreGiveApply()
- {
- try {
- if (!Yii::$app->request->isPost) throw new \Exception("非法请求");
- $store = Store::findOne(['mall_id' => $this->mall_id, 'id' => $this->store_id, 'status' => StatusEnum::ENABLED]);
- if (empty($store)) throw new \Exception("门店信息有误");
- StoreScoreStoreApply::saveData([
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'store_user_id' => $store->user_id,
- 'store_c_id' => $store->c_id,
- 'score_method' => 1
- ]);
- return $this->success("提交申请成功");
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionScoreDeductionApply()
- {
- try {
- if (!Yii::$app->request->isPost) throw new \Exception("非法请求");
- $store = Store::findOne(['mall_id' => $this->mall_id, 'id' => $this->store_id, 'status' => StatusEnum::ENABLED]);
- if (empty($store)) throw new \Exception("门店信息有误");
- StoreScoreStoreApply::saveData([
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'store_user_id' => $store->user_id,
- 'store_c_id' => $store->c_id,
- 'score_method' => 2
- ]);
- return $this->success("提交申请成功");
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionServiceFee()
- {
- try {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $settings = StoreSettings::find()
- ->where([
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'status' => StatusEnum::ENABLED,
- ])
- ->select('id,mall_id,store_id,store_service_fee,store_service_ratio')
- ->asArray()
- ->one();
- $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
- if (empty($settings)) {
- $settings['store_service_fee'] = $basic['service_fee_type'];
- $settings['store_service_ratio'] = 0;
- }
- $settings['platform_service_fee_setting'] = [
- 'service_fee_type' => $basic['service_fee_type'],
- 'max_platform_ratio' => $basic['max_platform_ratio'],
- 'min_platform_ratio' => $basic['min_platform_ratio']
- ];
- return $this->success('success', $settings);
- }
- if (Yii::$app->request->isPost) {
- $data = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($data, [
- [['store_service_ratio'], 'required'],
- [['store_service_ratio'], 'number'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- // $settings = StoreSettings::find()
- // ->where([
- // 'mall_id' => $this->mall_id,
- // 'store_id' => $this->store_id,
- // ])
- // ->select('id,mall_id,store_id,store_service_fee,store_service_ratio')
- // ->one();
- $settings = StoreSettings::setData($this->mall_id, [
- 'store_id' => $this->store_id,
- ]);
- if (!$settings) {
- return $this->error('数据不存在');
- }
- $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
- if ($basic['service_fee_type'] == 1 && ($data['store_service_ratio'] > $basic['max_platform_ratio'] || $data['store_service_ratio'] < $basic['min_platform_ratio'])) {
- return $this->error('只能设置在平台服务费比例区间内的比例当前平台服务费比例:' . $basic['min_platform_ratio'] . '%~' . $basic['max_platform_ratio'] . '%');
- }
- $settings->store_service_fee = 1;
- $settings->store_service_ratio = $data['store_service_ratio'];
- if (!$settings->save()) {
- return $this->error('修改失败');
- }
- StoreServiceRatioLog::setData([
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'before_store_service_ratio' => $settings['store_service_ratio'],
- 'after_store_service_ratio' => $data['store_service_ratio'],
- ]);
- return $this->success("操作成功");
- }
- }
- return $this->render($this->action->id);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|