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'); $cate_id = \Yii::$app->request->get('cate_id'); $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, 'check_status' => MchEnum::CHECK_ADOPT]; 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]; } elseif (3 == $status) { $wheres[] = ['status' => StatusEnum::DISABLED]; } else { return $this->error('参数错误'); } } if (!empty($cate_id)) { $wheres[] = ['c_id' => $cate_id]; } if (!empty($store_name)) { $wheres[] = ['like', 'store_name', $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,shop_mobile,store_name,c_id,province_id,city_id,district_id,check_status,address,logo_img,total_money,balance,expire_time,user_nums,status,created_at', 'where' => $wheres, 'order' => 'created_at desc', 'page' => $page, 'limit' => $limit, ]; $store_list = Mch::listPage($params); if (false === $store_list) { return $this->error(Mch::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(); } $H5Url = MallHost::getHost($this->mall_id,'h5_url'); foreach ($store_list['list'] as &$list) { $user = $users[$list['user_id']] ?? []; $list['nickname'] = $user['nickname'] ?? ''; $list['avatar_url'] = $user['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png'; // 门店状态,1:正常;2:过期;3:禁用 if ($list['status'] == StatusEnum::ENABLED && $list['check_status'] == MchEnum::CHECK_ADOPT && ($list['expire_time'] > $time || $list['expire_time'] == 0)) { $list['store_status'] = 1; } elseif ($list['status'] == StatusEnum::ENABLED && $list['check_status'] == MchEnum::CHECK_ADOPT && $list['expire_time'] <= $time) { $list['store_status'] = 2; } elseif ($list['status'] == StatusEnum::DISABLED) { $list['store_status'] = 3; } else { $list['store_status'] = 1; } $list['promotion_code'] = $H5Url . '/#/plugins/Mch/store/index?mch_id='.$list['id'] . '&sign=' . $this->mall['mall_sign']; } } $store_list['store_login_url'] = \Yii::$app->request->hostInfo . '?r=mch/client/auth/login&sign=' . $this->mall['mall_sign']; 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 = [ [['mch_id', 'status'], 'required'], [['mch_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['mch_id'], 'status' => $status, 'mall_id' => $this->mall_id, ]; $res = Mch::setData($params); if (false === $res) { return $this->error(Mch::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'); if (empty($region_id)) { return $this->error('参数错误'); } $region = Region::findOne($region_id); if (empty($region)) { return $this->error('地区信息未找到'); } $region_list = Region::find() ->select('id,name,lng,lat') ->where(['parent_id' => $region->id]) ->asArray() ->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); $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]; 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) { $cate_list = MchCate::find() ->select('id,name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->all(); return $this->success('success', ['list' => $cate_list]); } else { $post = \Yii::$app->request->post(); // dd($post); $rules = [ [['user_id', 'shop_mobile', 'shop_owner', 'store_name', 'c_id','logo_img', 'license_img'], 'required'], [['user_id', 'shop_mobile'], 'integer'], [['shop_owner', 'store_name', 'store_desc', 'logo_img', 'license_img'], 'string', 'max' => 255], [['logo_img', 'license_img'], 'url'], ['shop_mobile', function ($attribute, $params) { if (!RegularHelper::verify('mobile', $this->$attribute)) { $this->addError($attribute, '手机号格式错误'); } }], ['store_desc', 'safe'] ]; $res = \Yii::$app->services->validate->validate($post, $rules); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); if(empty($post['store_desc']))$post['store_desc'] = ''; $post['mall_id'] = $this->mall_id; $config_enter = \Yii::$app->services->setting->addons->get($this->mall_id, MchEnum::ADDONS_NAME, 'enter'); if (1 == $config_enter['time_status']) { $days = $config_enter['expired_days']; $post['expire_time'] = time() + $days * 24 * 60 * 60; } else { $post['expire_time'] = 0; } $post['check_status'] = MchEnum::CHECK_ADOPT; $rr = Mch::addStore($post); if (false === $rr) { return $this->error(Mch::getStaticError()); } 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) { $mch_id = \Yii::$app->request->get('mch_id'); if (empty($mch_id)) { return $this->error('参数错误'); } $store = Mch::find() ->where(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'id' => $mch_id]) ->asArray() ->one(); if (empty($store)) { return $this->error('门店不存在'); } $categorys = MchCate::find() ->select('id,name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->all(); $good_counts = Goods::find() ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'mch_id' => $mch_id]) ->count(); $cate_id_name_map = array_column($categorys, 'name', 'id'); $store['cate_name'] = $cate_id_name_map[$store['c_id']] ?? ''; $store['good_counts'] = $good_counts; $store['total_order_number'] = $store['total_num']; $store['unsettled_amount'] = $store['total_expenses']; $store['customer_service'] = empty($store['customer_service']) ? ['web_service_url' => ''] : json_decode($store['customer_service'], true); $basic_setting = $store; $store_admin = MchAdmin::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'mch_id' => $store['id']]); $settings = MchSettings::find() ->where(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'mch_id' => $mch_id]) ->asArray() ->one(); $store_setting = [ 'shop_status' => $store['shop_status'] ?? 0, 'business_time_start' => $store['business_time_start'] ?? '', 'business_time_end' => $store['business_time_end'] ?? '', 'store_background' => $store['store_background'] ?? '', 'username' => $store_admin->username, 'individual_fee_setting' => $settings['individual_fee_setting'] ?? 0, 'store_service_fee' => $settings['store_service_fee'] ?? 0, 'store_service_ratio' => $settings['store_service_ratio'] ?? 10, 'store_settle_type' => $settings['store_settle_type'] ?? 1, 'store_settle_scene' => $settings['store_settle_scene'] ?? 1, 'is_balance_cash_service_fee' => $settings['is_balance_cash_service_fee'] ?? 0, 'balance_cash_service_fee' => $settings['balance_cash_service_fee'] ?? 0, 'store_commission_authority' => json_decode($settings['store_commission_authority'], true) ?? ['agent'=> 0, 'area'=> 0, 'boss'=>0, 'distribution'=> 0], ]; /* ---------- 会员等级相关配置处理 start ------ */ $cashier_setting = [ 'cachier_service_ratio' => $settings['cachier_service_ratio'] ?? 10, 'cashier_member_status' => $settings['cashier_member_status'] ?? 0, 'cashier_member_settings' => $settings['cashier_member_settings'], 'cashier_distribution_is_enable' => $settings['cashier_distribution_is_enable'] ?? 0, 'cashier_distribution_settings' => $settings['cashier_distribution_settings'], 'cashier_agent_is_enable' => $settings['cashier_agent_is_enable'] ?? 0, 'cashier_agent_settings' => $settings['cashier_agent_settings'], 'deepNames' => ['一级分销', '二级分销'], ]; $member_levels = UserLevel::find() ->select('level,name as level_name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->all(); if (empty($member_levels)) { $member_levels = [ ['level' => 0, 'level_name' => '默认等级'], ]; } if (!empty($cashier_setting['cashier_member_settings'])) { $setting_members = json_decode($cashier_setting['cashier_member_settings'], true); $mem_levels = array_column($setting_members, 'level'); $mem_level_discount_map = array_column($setting_members, 'discount', 'level'); } else { $mem_levels = []; $mem_level_discount_map = []; } foreach ($member_levels as &$mlevel) { if (!in_array($mlevel['level'], $mem_levels)) { $mlevel['discount'] = 0; } else { $mlevel['discount'] = floatval($mem_level_discount_map[$mlevel['level']] ?? 0); } } // dd($member_levels); $cashier_setting['cashier_member_settings'] = $member_levels; /* ---------- 会员等级相关配置处理 end ------ */ /* ---------- 分销等级相关配置处理 start ------ */ $is_install_distribution = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION); if (!$is_install_distribution) { $distribution_settings = []; $is_distribution_enable = 0; } else { $is_distribution_enable = 1; $distribution_levels = DistributionLevel::find() ->select('level,name as level_name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->all(); if (empty($distribution_levels)) { $distribution_levels = [ ['level' => 0, 'level_name' => '默认等级'], ]; } if (!empty($cashier_setting['cashier_distribution_settings'])) { $distribution_settings = json_decode($cashier_setting['cashier_distribution_settings'], true); $distr_levels = array_column($distribution_settings, 'level'); $distr_level_discount_map = array_column($distribution_settings, 'commission_rate', 'level'); } else { $distr_levels = $distr_level_discount_map = []; } foreach ($distribution_levels as &$dlevel) { if (!in_array($dlevel['level'], $distr_levels)) { $dlevel['commission_rate'] = [0, 0]; } else { $dlevel['commission_rate'] = $distr_level_discount_map[$dlevel['level']] ?? [0, 0]; } } $cashier_setting['cashier_distribution_settings'] = $distribution_levels; $cashier_setting['is_distribution_enable'] = $is_distribution_enable; } /* ---------- 分销等级相关配置处理 end ------ */ /* ---------- 经销等级相关配置处理 start ------ */ $is_install_agent = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_AGENT); if (!$is_install_agent) { $agent_settings = [ 'team_rewards' => [ ['level' => 0, 'level_name' => '默认等级', 'commission_rate' => 0], ], 'equal_rewards' => [ ['level' => 0, 'level_name' => '默认等级', 'commission_rate' => 0], ], 'over_rewards' => [ ['level' => 0, 'level_name' => '默认等级', 'commission_rate' => 0], ], ]; $is_agent_enable = 0; } else { $is_agent_enable = 1; $agent_levels = AgentLevel::find() ->select('level,name as level_name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->all(); if (empty($agent_levels)) { $agent_levels = [ ['level' => 0, 'level_name' => '默认等级'], ]; } if (!empty($cashier_setting['cashier_agent_settings'])) { $agent_settings = json_decode($cashier_setting['cashier_agent_settings'], true); } else { $agent_settings = [ 'team_rewards' => [ ['level' => 0, 'level_name' => '默认等级', 'commission_rate' => 0], ], 'equal_rewards' => [ ['level' => 0, 'level_name' => '默认等级', 'commission_rate' => 0], ], 'over_rewards' => [ ['level' => 0, 'level_name' => '默认等级', 'commission_rate' => 0], ], ]; } foreach ($agent_settings as $reward_name => $setting) { foreach ($setting as $val) { $agent_settings[$reward_name][$val['level']] = $val; } } $ag_levels = array_column($agent_settings['team_rewards'], 'level'); $agent_level_settings = []; foreach ($agent_levels as $alevel) { if (!in_array($alevel['level'], $ag_levels)) { $arr = $alevel; $arr['commission_rate'] = 0; $agent_level_settings['team_rewards'][] = $arr; $agent_level_settings['equal_rewards'][] = $arr; $agent_level_settings['over_rewards'][] = $arr; } else { $arr = [ 'level' => $alevel['level'], 'level_name' => $alevel['level_name'], 'commission_rate' => $agent_settings['team_rewards'][$alevel['level']]['commission_rate'] ?? 0, ]; $agent_level_settings['team_rewards'][] = $arr; $arr['commission_rate'] = $agent_settings['equal_rewards'][$alevel['level']]['commission_rate'] ?? 0; $agent_level_settings['equal_rewards'][] = $arr; $arr['commission_rate'] = $agent_settings['over_rewards'][$alevel['level']]['commission_rate'] ?? 0; $agent_level_settings['over_rewards'][] = $arr; } } $cashier_setting['cashier_agent_settings'] = $agent_level_settings; $cashier_setting['is_agent_enable'] = $is_agent_enable; /* ---------- 经销等级相关配置处理 end ------ */ } return $this->success('success', ['basic_setting' => $basic_setting, 'store_setting' => $store_setting, 'cashier_setting' => $cashier_setting, 'categorys' => $categorys]); } else { $post = \Yii::$app->request->post(); $group = $post['group']; $mch_id = $post['mch_id']; if (empty($group) || !in_array($group, ['store_setting', 'cashier_setting', 'basic_setting']) || empty($mch_id)) { return $this->error('参数错误'); } $rules = [ 'basic_setting' => [ [['store_name', 'logo_img', 'license_img', 'share_title', 'share_desc', 'share_pic', 'longitude', 'latitude', 'address', 'share_title', 'share_desc', 'share_pic'], 'string', 'max' => 255], [['shop_mobile', 'c_id','expire_time'], 'integer'], [['customer_service'], 'safe'], ], 'store_setting' => [ [['shop_status', 'username', 'store_service_fee', 'store_service_ratio', 'store_settle_type', 'store_settle_scene','store_commission_authority', 'individual_fee_setting'], 'required'], [['shop_status', 'store_service_fee', 'store_settle_type', 'store_settle_scene','is_balance_cash_service_fee', 'individual_fee_setting'], 'integer'], [['store_service_ratio','balance_cash_service_fee'], 'number'], ['password', 'safe'], ], ]; $res = \Yii::$app->services->validate->validate($post[$group], $rules[$group]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $post[$group]['mch_id'] = $mch_id; // dd($post); if ('store_setting' == $group) { $res = MchSettings::saveStoreSetting($this->mall_id, $post[$group]); if (false === $res) { return $this->error(MchSettings::getStaticError()); } return $this->success('保存成功'); } elseif ('cashier_setting' == $group) { $res = MchSettings::setData($this->mall_id, $post[$group]); if (false === $res) { return $this->error(MchSettings::getStaticError()); } return $this->success('保存成功'); } elseif ('basic_setting' == $group) { $post[$group]['mall_id'] = $this->mall_id; $post[$group]['id'] = $mch_id; unset($post[$group]['mch_id']); $res = Mch::setData($post[$group]); if (false === $res) { return $this->error(Mch::getStaticError()); } return $this->success('保存成功'); } } } /** * 选择门店接口,外部调用 * @return mixed|void */ public function actionSelectMch(){ 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'][] = ['like', 'store_name', $params['keyword']]; $params['order'] = 'id desc'; $params['select'] = 'id,store_name,shop_owner,shop_mobile,logo_img,store_desc,shop_status,status'; $list = Mch::listPage($params); return $this->success('操作成功', $list); } } //门店删除 public function actionDel(){ try { $params = \Yii::$app->request->post(); Mch::delStore($this->mall_id,$params['id']); return $this->success(); }catch (\Exception $e){ return $this->error($e->getMessage()); } } }