StoreDowCenterController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. namespace addons\Store\api\modules\v1\controllers;
  3. use addons\BusinessBonus\common\events\user\UpgradeUserEvent;
  4. use addons\Store\common\enums\StoreEnum;
  5. use addons\Store\common\logic\store\StoreEntryLimit;
  6. use addons\Store\common\logic\StoreCreateSync;
  7. use addons\Store\common\models\Store;
  8. use addons\Store\common\models\StoreCate;
  9. use addons\Store\common\models\StoreClerk;
  10. use addons\Store\common\models\StoreMerchantReduceProfit;
  11. use addons\Store\common\models\StoreUserEnterQualification;
  12. use addons\Store\common\models\StoreSettings;
  13. use addons\Store\common\services\MerchantReduceProfitService;
  14. use api\controllers\OnAuthController;
  15. use common\enums\StatusEnum;
  16. use common\helpers\sms\Sms;
  17. use common\models\common\Region;
  18. use common\models\goods\Goods;
  19. use common\enums\ApiStatusEnum;
  20. use common\globals\GlobalInvoke;
  21. use yii\db\Exception;
  22. use addons\Store\common\models\StoreCommunity;
  23. use addons\Store\common\services\StoreAssociatedUserService;
  24. use common\models\mall\Mall;
  25. use Yii;
  26. use addons\Store\common\services\StoreService;
  27. use addons\StoreEntryLimit\common\service\SettingService;
  28. use addons\UserSync\common\models\UserSync;
  29. use common\enums\AddonsEnum;
  30. use common\helpers\ArrayHelper;
  31. use common\models\mall\MallAddons;
  32. use common\models\user\User;
  33. class StoreDowCenterController extends OnAuthController
  34. {
  35. public $modelClass = '';
  36. /**
  37. * 不用进行登录验证的方法
  38. *
  39. * 例如: ['index', 'update', 'create', 'view', 'delete']
  40. * 默认全部需要验证
  41. *
  42. * @var array
  43. */
  44. protected $authOptional = ['index'];
  45. public function actionStoreRecruit()
  46. {
  47. if (!\Yii::$app->request->isGet) {
  48. return $this->error('请求方式错误');
  49. }
  50. $settings = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter');
  51. $community_set = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
  52. $community_status = $community_set['community_status']??0;
  53. $settings['community_status'] = $community_status;
  54. if (0 == $settings['is_enable']) {
  55. return $this->error('商家入驻未开启');
  56. }
  57. //检测是否需要实名认证
  58. $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH_BUSINESS, $this->mall_id, ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'type' => 'store']);
  59. if ($need_auth === true && !empty($settings['store_auth'])) {
  60. //需要先进行实名认证
  61. return $this->error('请先进行实名认证', [], ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
  62. }
  63. // dd($settings);
  64. if (!empty($settings['goods_ids'])) {
  65. $settings['goods_ids'] = json_decode($settings['goods_ids'], true);
  66. $goods = Goods::find()
  67. ->select('id,goods_name,price,cover_pic')
  68. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $settings['goods_ids']])
  69. ->asArray()
  70. ->all();
  71. $settings['good_list'] = $goods;
  72. }
  73. // 是否有资格入驻门店
  74. $is_can_enter = 0;
  75. if (3 == $settings['mode']) {
  76. // 条件入驻
  77. if (StoreEntryLimit::check($this->user_id, $this->mall_id, $settings)) $is_can_enter = 1;
  78. if ($is_can_enter == 0) {
  79. $settings['mode_3'] = StoreEntryLimit::getMode3($this->mall_id, $this->user->parent_id, $settings);
  80. }
  81. } elseif (2 == $settings['mode']) {
  82. $qualification = StoreUserEnterQualification::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]);
  83. if (!empty($qualification)) {
  84. $is_can_enter = 1;
  85. }
  86. } else {
  87. $is_can_enter = 1;
  88. }
  89. $settings['is_can_enter'] = $is_can_enter;
  90. $store = Store::find()
  91. ->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')
  92. ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])
  93. ->asArray()
  94. ->one();
  95. // 入驻申请进度,0:未申请;1:审核中;2:审核通过;3:审核未通过
  96. $apply_step = 0;
  97. $store_account = [];
  98. if (!empty($store)) {
  99. $store['detail_address'] = $store['address'];
  100. if (StoreEnum::CHECK_WAITING == $store['check_status']) {
  101. $apply_step = 1;
  102. } else if (StoreEnum::CHECK_ADOPT == $store['check_status'] && StoreEnum::FREEZE_NO == $store['is_freeze']) {
  103. $apply_step = 2;
  104. $backend_domain = \Yii::$app->services->setting->platform->get('system.backend_url');
  105. $backend_domain = empty($backend_domain) ? '' : (string)$backend_domain;
  106. $store_account = [
  107. 'store_login_url' => trim($backend_domain, '/') . '?r=store/client/auth/login&sign=' . $this->mall['mall_sign'],
  108. 'account' => $store['shop_mobile'],
  109. 'login_pwd' => substr($store['shop_mobile'], -6),
  110. ];
  111. } else if (StoreEnum::CHECK_FAILED == $store['check_status']) {
  112. $apply_step = 3;
  113. }
  114. }
  115. $categorys = StoreCate::find()
  116. ->select('id,name')
  117. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  118. ->asArray()
  119. ->all();
  120. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
  121. $settings['service_fee'] = $basic['service_fee'];
  122. $settings['service_fee_type'] = $basic['service_fee_type'];
  123. $settings['max_platform_ratio'] = $basic['max_platform_ratio'];
  124. $settings['min_platform_ratio'] = $basic['min_platform_ratio'];
  125. $result = [
  126. 'settings' => $settings,
  127. 'apply_step' => $apply_step,
  128. 'store' => $store,
  129. 'categorys' => $categorys,
  130. 'store_account' => $store_account,
  131. 'reduce_profit_config' => (new MerchantReduceProfitService())->profit($this->mall_id, $this->user_id, 0),
  132. ];
  133. return $this->success('success', $result);
  134. }
  135. /**
  136. * @name:
  137. * @msg: 获取社区列表
  138. * @param {*}
  139. * @return {*}
  140. */
  141. public function actionCommunityList(){
  142. if(Yii::$app->request->isGet){
  143. $params = Yii::$app->request->get();
  144. if(empty($params['province_id']) || empty($params['city_id']) || empty($params['district_id']) || empty($params['street_id'])){
  145. return $this->error('参数错误');
  146. }
  147. //校验
  148. $settings = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
  149. $community_status = $settings['community_status']??0;
  150. if($community_status == 0){
  151. return $this->error('请联系相关人员开启社区管理权限');
  152. }
  153. $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();
  154. $data = ['id'=>'-1','community_name'=>'其它社区'];
  155. $community_list[] = $data;
  156. return $this->success('获取成功',['community_list'=>$community_list]);
  157. }else{
  158. return $this->error('请求方式不正确');
  159. }
  160. }
  161. /**
  162. * @name:
  163. * @msg: 草稿保存
  164. * @param {*}
  165. * @return {*}
  166. */
  167. public function actionEnterDraft()
  168. {
  169. //根据商城,用户id为键存储
  170. if (\Yii::$app->request->isPost) {
  171. $params = Yii::$app->request->post();
  172. $draft_store_apply[$this->mall_id][$this->user_id] = $params;
  173. $res = Yii::$app->cache->set('draft_store_apply', $draft_store_apply);
  174. if ($res) {
  175. return $this->success('保存成功');
  176. }
  177. }
  178. //获取存储信息
  179. if (\Yii::$app->request->isGet) {
  180. //获取草稿
  181. $res = Yii::$app->cache->get('draft_store_apply');
  182. $draftInfo = [];
  183. if (isset($res[$this->mall_id][$this->user_id])) {
  184. $draft_apply = $res[$this->mall_id][$this->user_id];
  185. $draft_apply['mall_id'] = $this->mall_id;
  186. $model = new StoreService();
  187. //处理地区信息
  188. $draftInfo = $model->getDraftRegionInfo($draft_apply);
  189. }
  190. return $this->success('获取成功', ['draft_info' => $draftInfo]);
  191. }
  192. }
  193. /**
  194. * @name:
  195. * @msg: 提交门店入驻申请
  196. * @param {*}
  197. * @return {*}
  198. */
  199. public function actionEnterApply()
  200. {
  201. if (!\Yii::$app->request->isPost) {
  202. return $this->error('请求方式错误');
  203. }
  204. $settings = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter');
  205. $upload_license_image = $settings['upload_license_image'];
  206. $post = \Yii::$app->request->post();
  207. $post['store_desc'] = isset($post['store_desc']) ? ''.$post['store_desc'] : '';
  208. $post['license_img'] = isset($post['license_img']) ? ''.$post['license_img'] : '';
  209. $rules = [
  210. [['store_desc'], 'required','message'=>'门店介绍不能为空'],
  211. [['logo_img'], 'required','message'=>'请上传商家logo'],
  212. [['license_img'], 'required','message'=>'请上传营业执照'],
  213. [['shop_owner', 'shop_mobile', 'store_name', 'c_id', 'address', 'detail_address', 'longitude', 'latitude', 'vcode'], 'required'],
  214. [['shop_owner', 'store_name', 'address', 'detail_address', 'store_desc', 'logo_img', 'license_img', 'longitude', 'latitude'], 'string', 'max' => 255],
  215. ['vcode', 'string'],
  216. [['shop_mobile', 'c_id','province_id','city_id','district_id','street_id','community_id'], 'integer'],
  217. [['province_id','city_id','district_id','street_id','community_id'], 'default', 'value' => 0],
  218. ['license_img', 'required', 'when' => function ($model) use ($upload_license_image) {
  219. return $upload_license_image == 1;
  220. }],
  221. [['id', 'store_background'], 'safe'],
  222. [['new_rate', 'store_service_ratio'], 'number'],
  223. ];
  224. $res = \Yii::$app->services->validate->validate($post, $rules);
  225. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  226. if (YII_ENV != 'dev' && !Sms::checkValidateCode($post['shop_mobile'], $post['vcode'])) {
  227. return $this->error('验证码不正确');
  228. }
  229. if (empty($post['store_background'])) unset($post['store_background']);
  230. if (empty($post['new_rate'])) unset($post['new_rate']);
  231. if (3 == $settings['mode']) {
  232. // 条件入驻
  233. if (!StoreEntryLimit::check($this->user_id, $this->user->parent_id, $this->mall_id, $settings))
  234. return $this->error('条件不满足,无法入驻');
  235. }
  236. $post['address'] = $post['detail_address'];
  237. $post['mall_id'] = $this->mall_id;
  238. $post['user_id'] = $this->user_id;
  239. unset($post['detail_address']);
  240. //这里修改 查询条件 与获取详情 查询条件不一致
  241. // $query = Store::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'shop_mobile' => $post['shop_mobile'], 'shop_owner' => $post['shop_owner']]);
  242. $query = Store::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED,'user_id'=>$this->user_id]);
  243. if (!empty($post['id'])) {
  244. $query->andWhere(['id' => $post['id']]);
  245. } else {
  246. unset($post['id']);
  247. }
  248. $store = $query->one();
  249. if (!empty($store)) {
  250. if (empty($post['id'])) {
  251. // 新提交的申请
  252. if($store['is_freeze'] == StoreEnum::FREEZE_YES){
  253. $post['id'] = $store['id'];
  254. }else{
  255. return $this->error('您已经申请过请勿重复申请');
  256. }
  257. } else {
  258. // 审核失败后重新提交的申请
  259. if ($store->check_status != StoreEnum::CHECK_FAILED) {
  260. return $this->error('门店状态异常');
  261. }
  262. $store->attributes = $post;
  263. $store->check_status = StoreEnum::CHECK_WAITING;
  264. $res = $store->save();
  265. if (!$res) {
  266. return $this->error($store->getErrorMessage());
  267. }
  268. //更新o2o门店服务费
  269. $this->updateStoreServiceRatio($this->mall_id, $store->id, $post['store_service_ratio']);
  270. return $this->success('提交成功');
  271. }
  272. }
  273. if ($settings['store_audit'] == 1) {
  274. $post['check_status'] = StoreEnum::CHECK_WAITING;
  275. } else {
  276. $post['check_status'] = StoreEnum::CHECK_ADOPT;
  277. $post['is_examine'] = 0;
  278. if($settings['is_template'] && $settings['is_front_template']) {
  279. $post['is_sync'] = 1;
  280. }
  281. }
  282. // 校验是否需要提交让利比例
  283. $profitService = new MerchantReduceProfitService();
  284. $callback = $profitService->storeApplyCreate($this->mall_id, $this->user_id, $post);
  285. if (is_string($callback)) return $this->error($callback);
  286. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
  287. $is_set_store_service_ratio = false;
  288. if ($basic['service_fee'] == 1 && $basic['service_fee_type'] == 1) {
  289. if ($post['store_service_ratio'] < $basic['min_platform_ratio'] || $post['store_service_ratio'] > $basic['max_platform_ratio']) {
  290. return $this->error('只能设置在平台服务费比例区间内的比例当前平台服务费比例:' . $basic['min_platform_ratio'] . '%~' . $basic['max_platform_ratio'] . '%');
  291. }
  292. $is_set_store_service_ratio = true;
  293. }
  294. try {
  295. $res = Store::addStore($post, $callback);
  296. } catch (Exception|\yii\base\Exception $e) {
  297. return $this->error($e->getMessage());
  298. }
  299. if (false === $res) {
  300. return $this->error(Store::getStaticError());
  301. }
  302. if ($settings['store_audit'] == 1) {
  303. $key = StoreEnum::STORE_WAITING_AUDIT_STORE_NUMBERS . ':' . $this->mall_id;
  304. \Yii::$app->redis->incrby($key, 1);//待审核门店
  305. }
  306. //更新o2o门店服务费
  307. $this->updateStoreServiceRatio($this->mall_id, $res['id'], $post['store_service_ratio']);
  308. //处理复制数据,判断是否开启模板门店、前端入驻同步模板门店
  309. if ($post['is_sync'] === 1 && $res) {
  310. \Yii::$app->services->rabbitMq->delay(10, [
  311. 'mall_id' => $this->mall_id,
  312. 'self_store_id'=> $res['id'],
  313. ], StoreCreateSync::class, 'synStoreData');
  314. }
  315. if($post['check_status']==StoreEnum::CHECK_ADOPT){
  316. if(!empty($store)){
  317. $params = [
  318. 'mall_id'=>$store['mall_id'],
  319. 'user_id'=>$store['user_id'],
  320. 'store_id'=>$store['id'],
  321. 'remark'=>'门店入驻免审核添加',
  322. ];
  323. $res = StoreClerk::setData($params);
  324. }
  325. //触发招商分红用户条件升级
  326. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_BUSINESS_BONUS)) {
  327. $event = new UpgradeUserEvent($this->mall_id);
  328. $data = ['mall_id' => $this->mall_id, 'user_id' => $this->user_id];
  329. \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  330. }
  331. }
  332. return $this->success('提交成功');
  333. }
  334. public function updateStoreServiceRatio($mall_id, $store_id, $store_service_ratio)
  335. {
  336. $basic = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
  337. if ($basic['service_fee'] == 1 && $basic['service_fee_type'] == 1) {
  338. if ($store_service_ratio < $basic['min_platform_ratio'] || $store_service_ratio > $basic['max_platform_ratio']) {
  339. return $this->error('只能设置在平台服务费比例区间内的比例当前平台服务费比例:' . $basic['min_platform_ratio'] . '%~' . $basic['max_platform_ratio'] . '%');
  340. }
  341. StoreSettings::setData($mall_id, [
  342. 'store_id'=> $store_id,
  343. 'store_service_fee'=> 1,
  344. 'store_service_ratio'=> $store_service_ratio,
  345. 'cachier_service_ratio'=> $store_service_ratio,
  346. ]);
  347. }
  348. }
  349. // 五洲-绑定关系链
  350. public function actionBindRelation()
  351. {
  352. if (!\Yii::$app->request->isPost) {
  353. return $this->error('请求方式错误');
  354. }
  355. if (!MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE_ENTRY_LIMIT)) {
  356. return $this->error('请先安装门店入驻条件限制插件');
  357. }
  358. $service = (new SettingService());
  359. $entry_setting = $service->get($this->mall_id);
  360. if ($entry_setting['is_enable'] == 0) return $this->error('门店入驻条件限制插件未开启');
  361. if (empty($entry_setting['recommend_user'])) return $this->error('推荐人不能为空');
  362. $user = User::findOne(['id' => $entry_setting['recommend_user']]);
  363. if (empty($user)) return $this->error('推荐人不存在');
  364. if ($this->user->parent_id != 0) return $this->error('不能修改推荐人');
  365. if ($this->user_id == $entry_setting['recommend_user']) return $this->error('不能推荐自己');
  366. if(MallAddons::isInstall($this->mall_id, 'UserSync')){
  367. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, 'UserSync', 'basic');
  368. if(!empty($configs['is_enable'])){
  369. $insert = [
  370. 'mall_id' => $this->mall_id,
  371. 'user_id' => $this->user_id,
  372. 'type' => 2,
  373. ];
  374. UserSync::setData($insert);
  375. }
  376. }
  377. try {
  378. $params['user_id'] = $this->user_id;
  379. $params['parent_id'] = $entry_setting['recommend_user'];
  380. $params['ip'] = ArrayHelper::get_client_ip();
  381. $res = User::updateParentByLock($params);
  382. if (\common\models\mall\MallAddons::isInstall($this->mall_id, \common\enums\AddonsEnum::ADDONS_NAME_STORE)) {
  383. //安装了o2o,获取关联门店数据
  384. $store_associated_user_service = new StoreAssociatedUserService();
  385. $store_associated_user_service->getStoreAssociated([
  386. 'user_id' => $params['user_id'],
  387. 'mall_id' => $this->mall_id,
  388. ]);
  389. }
  390. if ($res) return $this->success('修改成功');
  391. }catch (\Exception $e){
  392. return $this->error($e->getMessage());
  393. }
  394. }
  395. }