| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <?php
- namespace addons\Happiness\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\services\StoreSettingService;
- use addons\Happiness\common\models\HappinessStore;
- 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 common\models\user\User;
- use Yii;
- use addons\Store\common\models\StoreCommunity;
- use addons\Happiness\common\enums\HappinessEnum;
- 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 = HappinessStore::getOne([['id' => $this->store_id], ['mall_id' => $this->mall_id]], true);
- $detail['province'] = '';
- $detail['city'] = '';
- $detail['district'] = '';
- $detail['nickname'] = '';
- $detail['avatar_url'] = '';
- if ($detail) {
- $user = User::findOne(['id' => $detail['user_id']]);
- $detail['nickname'] = $user['nickname'];
- $detail['avatar_url'] = $user['avatar_url'];
- $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'];
- }
- }
- $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];
- }
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
- $configs = [
- 'delivery_amount' => $setting['delivery_amount'], //起送金额
- 'delivery_fee' => $setting['delivery_fee'], //配送费设置
- 'start_limit' => $setting['start_limit'], //初始公里数
- 'start_amount' => $setting['start_amount'], //初始配送费
- 'add_limit' => $setting['add_limit'], //每次增加配送费公里数
- 'add_amount' => $setting['add_amount'], //每次增加配送费数量
- ];
- if ($setting['is_location_limit'] == 1) {
- $configs['localtion_limit'] = $setting['localtion_limit'];
- } else {
- $configs['localtion_limit'] = '';
- }
- return $this->success('操作成功', ['detail' => $detail, 'configs' => $configs]);
- }
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $params['form']['id'] = $this->store_id;
- $params['form']['mall_id'] = $this->mall_id;
- $businessTimeStart = strtotime($params['form']['business_time_start']);
- $businessTimeEnd = strtotime($params['form']['business_time_end']);
- if ($businessTimeStart >= $businessTimeEnd) {
- return $this->error('营业时间,结束时间不能小于开始时间');
- }
- $res = HappinessStore::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]);
- 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 {
- $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['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());
- }
- }
- }
|