| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- namespace addons\Store\api\modules\v1\controllers;
- use addons\BusinessBonus\common\events\user\UpgradeUserEvent;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\logic\store\StoreEntryLimit;
- use addons\Store\common\logic\StoreCreateSync;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCate;
- use addons\Store\common\models\StoreClerk;
- use addons\Store\common\models\StoreMerchantReduceProfit;
- use addons\Store\common\models\StoreUserEnterQualification;
- use addons\Store\common\models\StoreSettings;
- use addons\Store\common\services\MerchantReduceProfitService;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use common\helpers\sms\Sms;
- use common\models\common\Region;
- use common\models\goods\Goods;
- use common\enums\ApiStatusEnum;
- use common\globals\GlobalInvoke;
- use yii\db\Exception;
- use addons\Store\common\models\StoreCommunity;
- use addons\Store\common\services\StoreAssociatedUserService;
- use common\models\mall\Mall;
- use Yii;
- use addons\Store\common\services\StoreService;
- use addons\StoreEntryLimit\common\service\SettingService;
- use addons\UserSync\common\models\UserSync;
- use common\enums\AddonsEnum;
- use common\helpers\ArrayHelper;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- class StoreDowCenterController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- public function actionStoreRecruit()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $settings = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter');
- $community_set = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
- $community_status = $community_set['community_status']??0;
- $settings['community_status'] = $community_status;
- if (0 == $settings['is_enable']) {
- return $this->error('商家入驻未开启');
- }
- //检测是否需要实名认证
- $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH_BUSINESS, $this->mall_id, ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'type' => 'store']);
- if ($need_auth === true && !empty($settings['store_auth'])) {
- //需要先进行实名认证
- return $this->error('请先进行实名认证', [], ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
- }
- // dd($settings);
- if (!empty($settings['goods_ids'])) {
- $settings['goods_ids'] = json_decode($settings['goods_ids'], true);
- $goods = Goods::find()
- ->select('id,goods_name,price,cover_pic')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $settings['goods_ids']])
- ->asArray()
- ->all();
- $settings['good_list'] = $goods;
- }
- // 是否有资格入驻门店
- $is_can_enter = 0;
- if (3 == $settings['mode']) {
- // 条件入驻
- if (StoreEntryLimit::check($this->user_id, $this->mall_id, $settings)) $is_can_enter = 1;
- if ($is_can_enter == 0) {
- $settings['mode_3'] = StoreEntryLimit::getMode3($this->mall_id, $this->user->parent_id, $settings);
- }
- } elseif (2 == $settings['mode']) {
- $qualification = StoreUserEnterQualification::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]);
- if (!empty($qualification)) {
- $is_can_enter = 1;
- }
- } else {
- $is_can_enter = 1;
- }
- $settings['is_can_enter'] = $is_can_enter;
- $store = Store::find()
- ->select('id,mall_id,user_id,shop_owner,shop_mobile,store_name,c_id,store_desc,logo_img,license_img,address,longitude,latitude,check_status,check_remark,is_freeze')
- ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->one();
- // 入驻申请进度,0:未申请;1:审核中;2:审核通过;3:审核未通过
- $apply_step = 0;
- $store_account = [];
- if (!empty($store)) {
- $store['detail_address'] = $store['address'];
- if (StoreEnum::CHECK_WAITING == $store['check_status']) {
- $apply_step = 1;
- } else if (StoreEnum::CHECK_ADOPT == $store['check_status'] && StoreEnum::FREEZE_NO == $store['is_freeze']) {
- $apply_step = 2;
- $backend_domain = \Yii::$app->services->setting->platform->get('system.backend_url');
- $backend_domain = empty($backend_domain) ? '' : (string)$backend_domain;
- $store_account = [
- 'store_login_url' => trim($backend_domain, '/') . '?r=store/client/auth/login&sign=' . $this->mall['mall_sign'],
- 'account' => $store['shop_mobile'],
- 'login_pwd' => substr($store['shop_mobile'], -6),
- ];
- } else if (StoreEnum::CHECK_FAILED == $store['check_status']) {
- $apply_step = 3;
- }
- }
- $categorys = StoreCate::find()
- ->select('id,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->all();
- $basic = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
- $settings['service_fee'] = $basic['service_fee'];
- $settings['service_fee_type'] = $basic['service_fee_type'];
- $settings['max_platform_ratio'] = $basic['max_platform_ratio'];
- $settings['min_platform_ratio'] = $basic['min_platform_ratio'];
- $result = [
- 'settings' => $settings,
- 'apply_step' => $apply_step,
- 'store' => $store,
- 'categorys' => $categorys,
- 'store_account' => $store_account,
- 'reduce_profit_config' => (new MerchantReduceProfitService())->profit($this->mall_id, $this->user_id, 0),
- ];
- return $this->success('success', $result);
- }
- /**
- * @name:
- * @msg: 获取社区列表
- * @param {*}
- * @return {*}
- */
- public function actionCommunityList(){
- if(Yii::$app->request->isGet){
- $params = Yii::$app->request->get();
- if(empty($params['province_id']) || empty($params['city_id']) || empty($params['district_id']) || empty($params['street_id'])){
- return $this->error('参数错误');
- }
- //校验
- $settings = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
- $community_status = $settings['community_status']??0;
- if($community_status == 0){
- return $this->error('请联系相关人员开启社区管理权限');
- }
- $community_list = StoreCommunity::find()->where(['mall_id'=>$this->mall_id,'province_id'=>$params['province_id'],'city_id'=>$params['city_id'],'district_id'=>$params['district_id'],'street_id'=>$params['street_id'],'status'=>0])->asArray()->all();
- $data = ['id'=>'-1','community_name'=>'其它社区'];
- $community_list[] = $data;
- return $this->success('获取成功',['community_list'=>$community_list]);
- }else{
- return $this->error('请求方式不正确');
- }
- }
- /**
- * @name:
- * @msg: 草稿保存
- * @param {*}
- * @return {*}
- */
- public function actionEnterDraft()
- {
- //根据商城,用户id为键存储
- if (\Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $draft_store_apply[$this->mall_id][$this->user_id] = $params;
- $res = Yii::$app->cache->set('draft_store_apply', $draft_store_apply);
- if ($res) {
- return $this->success('保存成功');
- }
- }
- //获取存储信息
- if (\Yii::$app->request->isGet) {
- //获取草稿
- $res = Yii::$app->cache->get('draft_store_apply');
- $draftInfo = [];
- if (isset($res[$this->mall_id][$this->user_id])) {
- $draft_apply = $res[$this->mall_id][$this->user_id];
- $draft_apply['mall_id'] = $this->mall_id;
- $model = new StoreService();
- //处理地区信息
- $draftInfo = $model->getDraftRegionInfo($draft_apply);
- }
- return $this->success('获取成功', ['draft_info' => $draftInfo]);
- }
- }
- /**
- * @name:
- * @msg: 提交门店入驻申请
- * @param {*}
- * @return {*}
- */
- public function actionEnterApply()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $settings = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter');
- $upload_license_image = $settings['upload_license_image'];
- $post = \Yii::$app->request->post();
- $post['store_desc'] = isset($post['store_desc']) ? ''.$post['store_desc'] : '';
- $post['license_img'] = isset($post['license_img']) ? ''.$post['license_img'] : '';
- $rules = [
- [['store_desc'], 'required','message'=>'门店介绍不能为空'],
- [['logo_img'], 'required','message'=>'请上传商家logo'],
- [['license_img'], 'required','message'=>'请上传营业执照'],
- [['shop_owner', 'shop_mobile', 'store_name', 'c_id', 'address', 'detail_address', 'longitude', 'latitude', 'vcode'], 'required'],
- [['shop_owner', 'store_name', 'address', 'detail_address', 'store_desc', 'logo_img', 'license_img', 'longitude', 'latitude'], 'string', 'max' => 255],
- ['vcode', 'string'],
- [['shop_mobile', 'c_id','province_id','city_id','district_id','street_id','community_id'], 'integer'],
- [['province_id','city_id','district_id','street_id','community_id'], 'default', 'value' => 0],
- ['license_img', 'required', 'when' => function ($model) use ($upload_license_image) {
- return $upload_license_image == 1;
- }],
- [['id', 'store_background'], 'safe'],
- [['new_rate', 'store_service_ratio'], 'number'],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- if (YII_ENV != 'dev' && !Sms::checkValidateCode($post['shop_mobile'], $post['vcode'])) {
- return $this->error('验证码不正确');
- }
- if (empty($post['store_background'])) unset($post['store_background']);
- if (empty($post['new_rate'])) unset($post['new_rate']);
- if (3 == $settings['mode']) {
- // 条件入驻
- if (!StoreEntryLimit::check($this->user_id, $this->user->parent_id, $this->mall_id, $settings))
- return $this->error('条件不满足,无法入驻');
- }
- $post['address'] = $post['detail_address'];
- $post['mall_id'] = $this->mall_id;
- $post['user_id'] = $this->user_id;
- unset($post['detail_address']);
- //这里修改 查询条件 与获取详情 查询条件不一致
- // $query = Store::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'shop_mobile' => $post['shop_mobile'], 'shop_owner' => $post['shop_owner']]);
- $query = Store::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED,'user_id'=>$this->user_id]);
- if (!empty($post['id'])) {
- $query->andWhere(['id' => $post['id']]);
- } else {
- unset($post['id']);
- }
- $store = $query->one();
- if (!empty($store)) {
- if (empty($post['id'])) {
- // 新提交的申请
- if($store['is_freeze'] == StoreEnum::FREEZE_YES){
- $post['id'] = $store['id'];
- }else{
- return $this->error('您已经申请过请勿重复申请');
- }
- } else {
- // 审核失败后重新提交的申请
- if ($store->check_status != StoreEnum::CHECK_FAILED) {
- return $this->error('门店状态异常');
- }
- $store->attributes = $post;
- $store->check_status = StoreEnum::CHECK_WAITING;
- $res = $store->save();
- if (!$res) {
- return $this->error($store->getErrorMessage());
- }
- //更新o2o门店服务费
- $this->updateStoreServiceRatio($this->mall_id, $store->id, $post['store_service_ratio']);
- return $this->success('提交成功');
- }
- }
- if ($settings['store_audit'] == 1) {
- $post['check_status'] = StoreEnum::CHECK_WAITING;
- } else {
- $post['check_status'] = StoreEnum::CHECK_ADOPT;
- $post['is_examine'] = 0;
- if($settings['is_template'] && $settings['is_front_template']) {
- $post['is_sync'] = 1;
- }
- }
- // 校验是否需要提交让利比例
- $profitService = new MerchantReduceProfitService();
- $callback = $profitService->storeApplyCreate($this->mall_id, $this->user_id, $post);
- if (is_string($callback)) return $this->error($callback);
- $basic = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
- $is_set_store_service_ratio = false;
- if ($basic['service_fee'] == 1 && $basic['service_fee_type'] == 1) {
- if ($post['store_service_ratio'] < $basic['min_platform_ratio'] || $post['store_service_ratio'] > $basic['max_platform_ratio']) {
- return $this->error('只能设置在平台服务费比例区间内的比例当前平台服务费比例:' . $basic['min_platform_ratio'] . '%~' . $basic['max_platform_ratio'] . '%');
- }
- $is_set_store_service_ratio = true;
- }
- try {
- $res = Store::addStore($post, $callback);
- } catch (Exception|\yii\base\Exception $e) {
- return $this->error($e->getMessage());
- }
- if (false === $res) {
- return $this->error(Store::getStaticError());
- }
- if ($settings['store_audit'] == 1) {
- $key = StoreEnum::STORE_WAITING_AUDIT_STORE_NUMBERS . ':' . $this->mall_id;
- \Yii::$app->redis->incrby($key, 1);//待审核门店
- }
- //更新o2o门店服务费
- $this->updateStoreServiceRatio($this->mall_id, $res['id'], $post['store_service_ratio']);
- //处理复制数据,判断是否开启模板门店、前端入驻同步模板门店
- if ($post['is_sync'] === 1 && $res) {
- \Yii::$app->services->rabbitMq->delay(10, [
- 'mall_id' => $this->mall_id,
- 'self_store_id'=> $res['id'],
- ], StoreCreateSync::class, 'synStoreData');
- }
- if($post['check_status']==StoreEnum::CHECK_ADOPT){
- if(!empty($store)){
- $params = [
- 'mall_id'=>$store['mall_id'],
- 'user_id'=>$store['user_id'],
- 'store_id'=>$store['id'],
- 'remark'=>'门店入驻免审核添加',
- ];
- $res = StoreClerk::setData($params);
- }
- //触发招商分红用户条件升级
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_BUSINESS_BONUS)) {
- $event = new UpgradeUserEvent($this->mall_id);
- $data = ['mall_id' => $this->mall_id, 'user_id' => $this->user_id];
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- }
- }
- return $this->success('提交成功');
- }
- public function updateStoreServiceRatio($mall_id, $store_id, $store_service_ratio)
- {
- $basic = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
- if ($basic['service_fee'] == 1 && $basic['service_fee_type'] == 1) {
- if ($store_service_ratio < $basic['min_platform_ratio'] || $store_service_ratio > $basic['max_platform_ratio']) {
- return $this->error('只能设置在平台服务费比例区间内的比例当前平台服务费比例:' . $basic['min_platform_ratio'] . '%~' . $basic['max_platform_ratio'] . '%');
- }
- StoreSettings::setData($mall_id, [
- 'store_id'=> $store_id,
- 'store_service_fee'=> 1,
- 'store_service_ratio'=> $store_service_ratio,
- 'cachier_service_ratio'=> $store_service_ratio,
- ]);
- }
- }
- // 五洲-绑定关系链
- public function actionBindRelation()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- if (!MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE_ENTRY_LIMIT)) {
- return $this->error('请先安装门店入驻条件限制插件');
- }
- $service = (new SettingService());
- $entry_setting = $service->get($this->mall_id);
- if ($entry_setting['is_enable'] == 0) return $this->error('门店入驻条件限制插件未开启');
- if (empty($entry_setting['recommend_user'])) return $this->error('推荐人不能为空');
- $user = User::findOne(['id' => $entry_setting['recommend_user']]);
- if (empty($user)) return $this->error('推荐人不存在');
- if ($this->user->parent_id != 0) return $this->error('不能修改推荐人');
- if ($this->user_id == $entry_setting['recommend_user']) return $this->error('不能推荐自己');
- if(MallAddons::isInstall($this->mall_id, 'UserSync')){
- $configs = \Yii::$app->services->setting->addons->get($this->mall_id, 'UserSync', 'basic');
- if(!empty($configs['is_enable'])){
- $insert = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $this->user_id,
- 'type' => 2,
- ];
- UserSync::setData($insert);
- }
- }
- try {
- $params['user_id'] = $this->user_id;
- $params['parent_id'] = $entry_setting['recommend_user'];
- $params['ip'] = ArrayHelper::get_client_ip();
- $res = User::updateParentByLock($params);
- if (\common\models\mall\MallAddons::isInstall($this->mall_id, \common\enums\AddonsEnum::ADDONS_NAME_STORE)) {
- //安装了o2o,获取关联门店数据
- $store_associated_user_service = new StoreAssociatedUserService();
- $store_associated_user_service->getStoreAssociated([
- 'user_id' => $params['user_id'],
- 'mall_id' => $this->mall_id,
- ]);
- }
- if ($res) return $this->success('修改成功');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- }
|