| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783 |
- <?php
- /*
- * @Descripttion:
- * @version:
- * @Author: ewgof
- * @Date: 2022-04-29 10:31:21
- * @LastEditors: Please set LastEditors
- * @LastEditTime: 2024-07-15 15:26:41
- */
- namespace addons\Happiness\backend\controllers;
- use addons\Happiness\common\enums\HappinessVoucherEnum;
- use addons\Happiness\common\logic\voucher\WalletLogic;
- use addons\Happiness\common\models\HappinessStore;
- use addons\Happiness\common\models\HappinessStoreAdmin;
- use addons\Happiness\common\enums\HappinessEnum;
- use addons\Happiness\common\models\HappinessClerk;
- use addons\Happiness\common\models\HappinessStoreGoods;
- use addons\Happiness\common\models\HappinessVoucherWallet;
- use addons\Store\common\models\StoreCate;
- use addons\Store\common\models\StoreCommunity;
- use addons\WeightBonus\common\enums\WeightBonusEnum;
- use common\enums\StatusEnum;
- use common\helpers\RegularHelper;
- use common\models\common\CapitalLog;
- use common\models\common\Region;
- use common\models\mall\MallHost;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use Yii;
- use common\models\user\Town;
- class StoreController extends BaseController
- {
- public function actionIndex()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/store/index');
- }
- if (\Yii::$app->request->isGet) {
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $store_name = \Yii::$app->request->get('store_name');
- $storeowner = \Yii::$app->request->get('storeowner');
- $nickname = \Yii::$app->request->get('nickname');
- // 门店状态,1:正常;2:过期;3:禁用
- $status = \Yii::$app->request->get('status');
- $time = time();
- $wheres[] = ['mall_id' => $this->mall_id];
- $wheres[] = ['check_status' => StatusEnum::ENABLED];
- if (empty($status)) {
- $wheres[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- } else {
- if (1 == $status) {
- $wheres[] = ['status' => StatusEnum::ENABLED];
- $wheres[] = ['or', ['>=', 'expire_time', $time], ['expire_time' => 0]];
- } elseif (2 == $status) {
- $wheres[] = ['status' => StatusEnum::ENABLED];
- $wheres[] = ['<=', 'expire_time', $time];
- $wheres[] = ['!=', 'expire_time', 0];
- } elseif (3 == $status) {
- $wheres[] = ['status' => StatusEnum::DISABLED];
- } else {
- return $this->error('参数错误');
- }
- }
- if (!empty($store_name)) {
- $store_name = trim($store_name);
- $wheres[] = ['or', ['like', 'store_name', $store_name], ['id' => $store_name]];
- }
- if (!empty($storeowner)) {
- $wheres[] = ['like', 'shop_owner', $storeowner];
- }
- if (!empty($nickname)) {
- $users = User::find()
- ->select('id,nickname,mobile,avatar_url')
- ->where(['like', 'nickname', $nickname])
- ->andWhere(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->indexBy('id')
- ->all();
- $user_ids = array_column($users, 'id');
- $wheres[] = ['user_id' => $user_ids];
- }
- $params = [
- 'select' => 'id,mall_id,user_id,shop_owner,business_id,shop_mobile,store_name,province_id,city_id,district_id,address,logo_img,total_money,balance,expire_time,user_nums,status,created_at,is_freeze,total_order_money,total_settled_amount,unsettled_amount,total_num',
- 'where' => $wheres,
- 'order' => 'created_at desc',
- 'page' => $page,
- 'limit' => $limit,
- 'with' => ['business' => function ($query) {
- $query->select('id,nickname,mobile');
- }],
- ];
- $store_list = HappinessStore::listPage($params);
- if (false === $store_list) {
- return $this->error(HappinessStore::getStaticError());
- }
- if (!empty($store_list['list'])) {
- if (empty($users)) {
- $user_ids = array_unique(array_column($store_list['list'], 'user_id'));
- $users = User::find()
- ->select('id,nickname,mobile,avatar_url')
- ->where(['id' => $user_ids, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->indexBy('id')
- ->all();
- }
- $region_ids = [];
- $province_ids = array_unique(array_column($store_list['list'], 'province_id'));
- $city_ids = array_unique(array_column($store_list['list'], 'city_id'));
- $district_ids = array_unique(array_column($store_list['list'], 'district_id'));
- $street_ids = array_unique(array_column($store_list['list'], 'street_id'));
- $community_ids = array_unique(array_column($store_list['list'], 'community_id'));
- $region_ids = array_merge($province_ids, $city_ids, $district_ids);
- $regions = Region::find()
- ->select('id,name')
- ->where(['id' => $region_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- $streets = town::find()
- ->select('id,name')
- ->where(['id' => $street_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- $communitys = StoreCommunity::find()
- ->select('id,community_name')
- ->where(['mall_id' => $this->mall_id, 'id' => $community_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- $H5Url = MallHost::getHost($this->mall_id, 'h5_url');
- foreach ($store_list['list'] as &$list) {
- $h5_domain = $H5Url . '/#/plugins2/Store/pay/cashRegister?sign=' . $this->mall['mall_sign'];
- $user = $users[$list['user_id']] ?? [];
- $category = $categorys[$list['c_id']] ?? [];
- $province = $regions[$list['province_id']] ?? [];
- $city = $regions[$list['city_id']] ?? [];
- $district = $regions[$list['district_id']] ?? [];
- $streetInfo = $streets[$list['street_id']] ?? [];
- if ($list['community_id'] == '-1') {
- $CommunityInfo = '其它社区';
- } else {
- if (isset($communitys[$list['community_id']])) {
- $CommunityInfo = $communitys[$list['community_id']]['community_name'];
- } else {
- $CommunityInfo = '';
- }
- }
- $list['nickname'] = $user['nickname'] ?? '';
- $list['user_mobile'] = $user['mobile'] ?? '';
- $list['avatar_url'] = $user['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- $list['business']['avatar_url'] = $list['business']['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- //判断address字段是否只是详细
- preg_match('/(.*?(?:省|自治区|北京市|天津市|上海市|重庆市))+(.*?(?:市|自治州|地区|区划|县))+(.*?(?:市|区|县|镇|乡|街道))/', $list['address'], $matches);
- if (count($matches) > 1) {
- $list['store_address'] = $list['address'];
- } else {
- $list['store_address'] = ($province['name'] ?? '') . ($city['name'] ?? '') . ($district['name'] ?? '') . ($streetInfo['name'] ?? '') . ($CommunityInfo ?? '') . $list['address'];
- }
- // 门店状态,1:正常;2:过期;3:禁用
- if ($list['status'] == StatusEnum::ENABLED) {
- $list['store_status'] = 1;
- } elseif ($list['status'] == StatusEnum::DISABLED) {
- $list['store_status'] = 3;
- } else {
- $list['store_status'] = 1;
- }
- $h5_domain .= '&store_id=' . $list['id'];
- $list['store_collection_url'] = $h5_domain;
- $list['promotion_code'] = $H5Url . '/#/plugins2/Store/store/index?store_id=' . $list['id'] . "&sign=" . $this->mall['mall_sign'];
- }
- }
- $store_list['store_login_url'] = \Yii::$app->request->hostInfo . '?r=happiness/client/auth/login&sign=' . $this->mall['mall_sign'];
- $cate_list = StoreCate::find()
- ->select('id,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->all();
- $store_list['cate_list'] = $cate_list;
- $recharge_list = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.recharge_list');
- $recharge_list = isset($recharge_list) ? json_decode($recharge_list, true) : [];
- $store_list['recharge_list'] = $recharge_list;
- return $this->success('success', $store_list);
- }
- }
- /**
- * @name:
- * @msg: 切换门店状态
- * @param {*}
- * @return {*}
- */
- public function actionSwitchStatus()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $post = \Yii::$app->request->post();
- $rules = [
- [['store_id', 'status'], 'required'],
- [['store_id', 'status'], 'integer'],
- ['status', 'in', 'range' => [1, 3]],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- if (1 == $post['status']) {
- $status = StatusEnum::ENABLED;
- } else {
- $status = StatusEnum::DISABLED;
- }
- $params = [
- 'id' => $post['store_id'],
- 'status' => $status,
- 'mall_id' => $this->mall_id,
- ];
- $res = HappinessStore::setData($params);
- if (false === $res) {
- return $this->error(HappinessStore::getStaticError());
- }
- return $this->success('操作成功');
- }
- /**
- * @name:
- * @msg: 根据地区id获取第一层子级地区信息列表
- * @param {*}
- * @return {*}
- */
- public function actionGetRegions()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $region_id = \Yii::$app->request->get('region_id');
- $level = \Yii::$app->request->get('level');
- if (empty($region_id)) {
- return $this->error('参数错误');
- }
- $region = Region::findOne($region_id);
- if (empty($region) && $level <= 3) {
- return $this->error('地区信息未找到');
- }
- if (isset($level) && $level >= 3) {
- if ($level == 3) {
- $region_list = Town::find()
- ->select('id,name')
- ->where(['district_id' => $region->id])
- ->asArray()
- ->indexBy('id')
- ->all();
- } else {
- $region_list = StoreCommunity::find()
- ->select('id,community_name')
- ->where(['street_id' => $region_id, 'mall_id' => $this->mall_id, 'status' => 0])
- ->asArray()
- ->indexBy('id')
- ->all();
- $data = ['id' => '-1', 'community_name' => '其它社区'];
- $region_list[] = $data;
- }
- } else {
- $region_list = Region::find()
- ->select('id,name,lng,lat')
- ->where(['parent_id' => $region->id])
- ->asArray()
- ->indexBy('id')
- ->all();
- }
- return $this->success('success', $region_list);
- }
- /**
- * @name:
- * @msg: 获取用户列表
- * @param {*}
- * @return {*}
- */
- public function actionGetUsers()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- $keyword = \Yii::$app->request->get('keyword');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $store_user_ids = HappinessStore::find()->select('user_id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->column();
- $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
- $wheres[] = ['not in', 'id', $store_user_ids];
- if (!empty($user_id)) {
- $wheres[] = ['id' => $user_id];
- }
- if (!empty($keyword)) {
- $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
- }
- $params = [
- 'select' => 'id,nickname,avatar_url,mobile,level,parent_id',
- 'where' => $wheres,
- 'order' => 'created_at desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $user_list = User::listPage($params);
- if (empty($user_list['list'])) {
- if (false === $user_list['list']) {
- return $this->error(User::getStaticError());
- }
- } else {
- $levels = UserLevel::find()
- ->select('id,level,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->indexBy('level')
- ->all();
- $parent_ids = array_column($user_list['list'], 'parent_id');
- $parent_users = User::find()
- ->select('id,nickname,avatar_url,mobile,level,parent_id')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($user_list['list'] as &$user) {
- if (empty($user['avatar_url'])) {
- $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- }
- $level = $levels[$user['level']] ?? [];
- $parent_user = $parent_users[$user['parent_id']] ?? [];
- $user['level_name'] = $level['name'] ?? '';
- $user['parent_nickname'] = $parent_user['nickname'] ?? '';
- }
- }
- return $this->success('success', $user_list);
- }
- /**
- * @name:
- * @msg: 门店添加/编辑
- * @param {*}
- * @return {*}
- */
- public function actionStorage()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/store/add-edit');
- }
- if (\Yii::$app->request->isGet) {
- $regions = Region::find()
- ->select('id,name,lng,lat')
- ->where(['level' => 1])
- ->asArray()
- ->indexBy('id')
- ->all();
- $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('success', ['provinces' => $regions, 'configs' => $configs]);
- } else {
- $post = \Yii::$app->request->post();
- $rules = [
- [['user_id', 'shop_mobile', 'shop_owner', 'store_name', 'logo_img', 'license_img', 'province_id', 'city_id', 'district_id', 'address', 'longitude', 'latitude'], 'required'],
- [['user_id', 'shop_mobile', 'province_id', 'city_id', 'district_id','street_id', 'merchant_distribution', 'self_mention', 'shop_status','business_id'], 'integer'],
- [['longitude', 'latitude'], 'number'],
- [['shop_owner', 'store_name', 'store_desc', 'logo_img', 'license_img', 'address', 'business_time_start', 'business_time_end'], 'string', 'max' => 255],
- [['logo_img', 'license_img'], 'url'],
- ['shop_mobile', function ($attribute, $params) {
- if (!RegularHelper::verify('mobile', $this->$attribute)) {
- $this->addError($attribute, '手机号格式错误');
- }
- }],
- [['freight_type', 'intra_city_distribution'], 'safe']
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $post['mall_id'] = $this->mall_id;
- // $post['expire_time'] = strtotime("+1 year");
- if (empty($post['store_desc'])) $post['store_desc'] = '';
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
- $post['is_alone'] = $post['intra_city_distribution']['is_alone'];
- if ($post['is_alone'] == 1) {
- $post['distribution_scope'] = $post['intra_city_distribution']['distribution_scope'];
- } else if ($post['is_alone'] == 0) {
- $post['distribution_scope'] = $setting['localtion_limit'] ? $setting['localtion_limit'] : 100000;
- if ($setting['is_location_limit'] == 0) $post['distribution_scope'] = 100000;
- }
- $post['check_status']=HappinessEnum::CHECK_ADOPT;
- $streets = Town::find()
- ->where(['district_id'=>$post['district_id']])
- ->select('id,name')
- ->asArray()
- ->indexBy('id')
- ->all();
- $street_name = $streets[$post['street_id']]['name']??'';
- $region_ids = [$post['province_id'], $post['city_id'], $post['district_id']];
- $regions = Region::find()
- ->select('id,name')
- ->where(['id' => $region_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- $name_rule=!empty($setting['name_rule'])?json_decode($setting['name_rule'],true):[];
- $store_name='';
- if(!empty($name_rule))
- {
- if(in_array('province',$name_rule))
- {
- $store_name.=$regions[$post['province_id']]['name'].'-';
- }
- if(in_array('city',$name_rule))
- {
- $store_name.=$regions[$post['city_id']]['name'].'-';
- }
- if(in_array('district',$name_rule))
- {
- $store_name.=$regions[$post['district_id']]['name'].'-';
- }
- if(in_array('street',$name_rule))
- {
- $store_name.=$street_name.'-';
- }
- if(in_array('no',$name_rule))
- {
- //取最新的编号
- $no=$setting['name_rule_no'];
- $has=HappinessStore::find()
- ->where(['mall_id'=>$this->mall_id])
- ->orderBy('id desc')
- ->asArray()
- ->one();
- if(!empty($has))
- {
- $no=$has['no'];
- }
- $number = intval($no) + 1;
- $post['no'] = str_pad($number, strlen($no), '0', STR_PAD_LEFT);
- $store_name.=$post['no'].'-';
- }
- $post['store_name'] = $store_name.$post['store_name'];
- }
- else
- {
- return $this->error('请设置小店命名规则');
- }
- $store = HappinessStore::addStore($post);
- if (false === $store) {
- return $this->error(HappinessStore::getStaticError());
- }
- if (!empty($store)) {
- $params = [
- 'mall_id' => $store['mall_id'],
- 'user_id' => $store['user_id'],
- 'store_id' => $store['id'],
- 'remark' => '后台新增门店添加',
- ];
- $res = HappinessClerk::setData($params);
- }
- // }
- return $this->success('添加成功');
- }
- }
- /**
- * @name:
- * @msg: 门店详情
- * @param {*}
- * @return {*}
- */
- public function actionDetail()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/store/detail');
- }
- if (\Yii::$app->request->isGet) {
- $store_id = \Yii::$app->request->get('store_id');
- if (empty($store_id)) {
- return $this->error('参数错误');
- }
- $store = HappinessStore::find()
- ->where(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'id' => $store_id])
- ->asArray()
- ->one();
- if (empty($store)) {
- return $this->error('门店不存在');
- }
- $streets = Town::find()
- ->where(['district_id'=>$store['district_id']])
- ->select('id,name')
- ->asArray()
- ->indexBy('id')
- ->all();
- $street_name = $streets[$store['street_id']]['name']??'';
- $region_ids = [$store['province_id'], $store['city_id'], $store['district_id']];
- $regions = Region::find()
- ->select('id,name')
- ->where(['id' => $region_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- $store['addr'] = $regions[$store['province_id']]['name'] . '/' . $regions[$store['city_id']]['name'] . '/' . $regions[$store['district_id']]['name'].'/' . $street_name;
- $good_counts = HappinessStoreGoods::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id])
- ->count();
- $store['good_counts'] = $good_counts;
- $store['total_order_number'] = $store['total_num'];
- $store['unsettled_amount'] = $store['total_expenses'];
- $store['show_address'] = $regions[$store['province_id']]['name'] . $regions[$store['city_id']]['name'] .
- $regions[$store['district_id']]['name'] .$street_name.$store['address'];
- $basic_setting = $store;
- $store_admin = HappinessStoreAdmin::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id]);
- $store_setting = [
- 'shop_status' => $store['shop_status'] ?? 0,
- 'business_time_start' => $store['business_time_start'] ?? '',
- 'business_time_end' => $store['business_time_end'] ?? '',
- 'username' => $store_admin->username,
- 'merchant_distribution' => $store['merchant_distribution'] ?? 1,
- 'self_mention' => $store['self_mention'] ?? 1,
- ];
- // if (!empty($basic_setting['freight_type'])) $store_setting['freight_type'] = json_decode($basic_setting['freight_type'], true);
- if (!empty($basic_setting['intra_city_distribution'])) $store_setting['intra_city_distribution'] = json_decode($basic_setting['intra_city_distribution'], true);
- $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'] = '';
- }
- //赋值省市区街道等详情信息
- $regions_provinces = Region::find()
- ->select('id,name')
- ->where(['level' => 1])
- ->asArray()
- ->indexBy('id')
- ->all();
- return $this->success('success', ['basic_setting' => $basic_setting, 'store_setting' => $store_setting, 'provinces' => $regions_provinces, 'configs' => $configs]);
- } else {
- $post = \Yii::$app->request->post();
- $group = $post['group'];
- $store_id = $post['store_id'];
- if (empty($group) || !in_array($group, ['store_setting', 'cashier_setting', 'basic_setting']) || empty($store_id)) {
- return $this->error('参数错误');
- }
- // dd($post[$group]);
- $rules = [
- 'basic_setting' => [
- [['store_name', 'logo_img', 'license_img', 'longitude', 'latitude', 'address'], 'string', 'max' => 255],
- [['shop_mobile', 'expire_time', 'province_id', 'city_id', 'district_id','street_id'], 'integer'],
- ],
- 'store_setting' => [
- [['shop_status', 'business_time_start', 'business_time_end', 'username'], 'required'],
- [['shop_status', 'merchant_distribution', 'self_mention'], 'integer'],
- [['business_time_start', 'business_time_end', 'username'], 'string'],
- [['password', 'repassword', 'freight_type', 'intra_city_distribution'], 'safe'],
- ],
- ];
- $res = \Yii::$app->services->validate->validate($post[$group], $rules[$group]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $post[$group]['store_id'] = $store_id;
- if ('store_setting' == $group) {
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
- $post[$group]['is_alone'] = $post[$group]['intra_city_distribution']['is_alone'];
- if ($post[$group]['is_alone'] == 1) {
- $post[$group]['distribution_scope'] = $post[$group]['intra_city_distribution']['distribution_scope'];
- } else if ($post[$group]['is_alone'] == 0) {
- $post[$group]['distribution_scope'] = $setting['localtion_limit'] ? $setting['localtion_limit'] : 100000;
- if ($setting['is_location_limit'] == 0) $post[$group]['distribution_scope'] = 100000;
- }
- $res = HappinessStore::saveStoreSetting($this->mall_id, $post[$group]);
- if (false === $res) {
- return $this->error(HappinessStore::getStaticError());
- }
- return $this->success('保存成功');
- } elseif ('basic_setting' == $group) {
- $post[$group]['mall_id'] = $this->mall_id;
- $post[$group]['id'] = $store_id;
- unset($post[$group]['store_id']);
- // dd($post);
- // $addr = $post[$group]['address'] ?? '';
- // if (!empty($addr)) {
- // $addrs = explode('/', $addr);
- // if(!empty($addrs[0])&&!empty( $addrs[1])&&!empty( $addrs[2])){
- // $addrs[0] = str_replace('中国','',$addrs[0]);
- // $regions = Region::find()
- // ->select('id,name,level')
- // ->where(['or', ['name' => $addrs[0]], ['name' => $addrs[1]], ['name' => $addrs[2]]])
- // ->asArray()
- // ->indexBy('level')
- // ->all();
- // $post[$group]['province_id'] = $regions[1]['id'];
- // $post[$group]['city_id'] = $regions[2]['id'];
- // $post[$group]['district_id'] = $regions[3]['id'];
- // }
- // }
- $res = HappinessStore::setData($post[$group]);
- if (false === $res) {
- return $this->error(HappinessStore::getStaticError());
- }
- return $this->success('保存成功');
- }
- }
- }
- /**
- * 组件获取店铺列表
- * @Author: lun
- * @DateTime: 2022/8/3 0003 18:40
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionStoreList()
- {
- $retuest = \Yii::$app->request;
- if ($retuest->isAjax) {
- if ($retuest->isGet) {
- $get = \Yii::$app->request->get();
- // 检查提交的参数
- $res = \Yii::$app->services->validate->validate($get, [
- [['id'], 'integer'],
- [['store_name'], 'string'],
- [['store_ids', 'exist_ids'], 'safe'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $where = $list = [];
- !empty($get['id']) && $where[] = ['=', 'id', $get['id']];
- $where[] = ['=', 'mall_id', $this->mall_id];
- !empty($get['store_name']) && $where[] = ['like', 'store_name', $get['store_name']];
- !empty($get['store_ids']) && $where[] = ['id' => $get['store_ids']];
- !empty($get['exist_ids']) && $where[] = ['not in', 'id', $get['exist_ids']];
- $where[] = ['=', 'check_status', StatusEnum::ENABLED];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $select = 'id,c_id,mall_id,logo_img,store_name,shop_mobile,created_at';
- $params = array('where' => $where, 'select' => $select, 'order' => $order, 'limit' => $get['limit']);
- $params['with'] = ['cate' => function ($query) {
- $query->select('id, name');
- }];
- $list = HappinessStore::listPage($params, false, true);
- return $this->success('获取成功', $list);
- }
- }
- }
- /**
- * 选择门店接口,外部调用
- * @return mixed|void
- */
- public function actionSelectStore()
- {
- if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
- $params = Yii::$app->request->get();
- $params['where'][] = ['mall_id' => $this->mall_id];
- $params['where'][] = ['status' => [StatusEnum::ENABLED]];
- if (!empty($params['keyword'])) $params['where'][] = ['or', ['like', 'store_name', $params['keyword']], ['id' => $params['keyword']]];
- $params['order'] = 'id desc';
- $params['select'] = 'id,store_name,shop_owner,shop_mobile,logo_img,store_desc,shop_status,status';
- $list = HappinessStore::listPage($params);
- return $this->success('操作成功', $list);
- }
- }
- //门店删除
- public function actionDel()
- {
- try {
- $params = Yii::$app->request->post();
- HappinessStore::delStore($this->mall_id, $params['id']);
- return $this->success();
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- //发放提货券
- public function actionRecharge()
- {
- try {
- $params = Yii::$app->request->post();
- $number = $params['number'];//充值的金额
- $store_id = $params['store_id'];//门店id
- $recharge_list = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.recharge_list');
- $recharge_list = !empty($recharge_list) ? json_decode($recharge_list, true) : [];
- if (empty($recharge_list)) {
- throw new \Exception('请先配置充值金额');
- }
- $itemSetting = [];
- foreach ($recharge_list as $item) {
- if ($item['number'] == $number) {
- $itemSetting = $item;
- break;
- }
- }
- if (empty($itemSetting) || empty($itemSetting['items'])) {
- throw new \Exception('请先配置充值金额');
- }
- $wallet_log = [];
- foreach ($itemSetting['items'] as $item) {
- $wallet_log[] = [
- 'mall_id' => $this->mall_id,
- 'store_id' => $store_id,
- 'level' => $item['level'],
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $item['price'],
- 'desc' => '管理员后台充值',
- 'source_table' => HappinessVoucherWallet::tableName(),
- 'source_table_id' => $store_id,
- 'from_type' => HappinessVoucherEnum::SYSTEM_ADD,
- 'wallet_type' => HappinessVoucherEnum::WALLET_VALUE,
- 'status' => 1,
- 'order_no' => ''
- ];
- }
- if(!empty($wallet_log))
- {
- foreach ($wallet_log as $item)
- {
- try {
- $res=WalletLogic::handle($item);
- }
- catch (\Exception $e)
- {
- \Yii::error($e->getMessage());
- }
- if($res===false)
- {
- throw new \Exception(WalletLogic::getStaticError());
- }
- }
- }
- return $this->success('操作成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|