| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace addons\Happiness\agent\controllers;
- use addons\Happiness\common\enums\HappinessEnum;
- use addons\Happiness\common\models\HappinessStore;
- use addons\Happiness\common\models\HappinessStoreAdmin;
- use addons\Happiness\common\models\HappinessStoreGoods;
- use addons\Store\common\models\StoreCate;
- use addons\Store\common\models\StoreCommunity;
- use common\enums\StatusEnum;
- use common\models\common\Region;
- use common\models\mall\Mall;
- use common\models\mall\MallHost;
- use common\models\user\Town;
- use common\models\user\User;
- use Yii;
- class StoreController extends BaseController
- {
- /**
- * 门店首页(概况)
- * @Author bing
- * @DateTime 2022-04-26 18:06:27
- * @return void
- * @copyright: Copyright (c) 2022 广东七件事集团
- */
- 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];
- //or条件
- $wheres[] = ['or', ['in', 'city_id', $this->city_ids], ['in', 'district_id', $this->district_ids], ['in', 'province_id', $this->province_ids]];
- 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) {
- $user = $users[$list['user_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;
- }
- }
- }
- return $this->success('success', $store_list);
- }
- }
- 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 {
- }
- }
- //修改密码
- public function actionUpdatePassword()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['old_pass', 'new_pass', 'check_pass'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if ($params['new_pass'] != $params['check_pass']) return $this->error('新密码和确认密码不一致');
- if (!$this->admin->validatePassword($params['old_pass'])) {
- return $this->error('旧密码不正确');
- }
- $this->admin->password = Yii::$app->security->generatePasswordHash($params['new_pass']);
- $res = $this->admin->save();
- if ($res === false) return $this->error('修改密码失败');
- return $this->success('操作成功');
- }
- }
|