StoreDowCenterController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @version:
  5. * @Author: ewgof
  6. * @Date: 2022-06-09 14:34:20
  7. * @LastEditors: ewgof
  8. * @LastEditTime: 2022-06-10 10:42:23
  9. */
  10. namespace addons\Mch\api\modules\v1\controllers;
  11. use addons\Mch\common\enums\MchEnum;
  12. use addons\Mch\common\models\Mch;
  13. use addons\Mch\common\models\MchCate;
  14. use addons\Mch\common\models\MchUserEnterQualification;
  15. use api\controllers\OnAuthController;
  16. use common\enums\StatusEnum;
  17. use common\helpers\sms\Sms;
  18. use common\models\common\Region;
  19. use common\models\goods\Goods;
  20. use common\enums\ApiStatusEnum;
  21. use common\globals\GlobalInvoke;
  22. class StoreDowCenterController extends OnAuthController
  23. {
  24. public $modelClass = '';
  25. /**
  26. * 不用进行登录验证的方法
  27. *
  28. * 例如: ['index', 'update', 'create', 'view', 'delete']
  29. * 默认全部需要验证
  30. *
  31. * @var array
  32. */
  33. protected $authOptional = ['index'];
  34. public function actionMchRecruit()
  35. {
  36. if (!\Yii::$app->request->isGet) {
  37. return $this->error('请求方式错误');
  38. }
  39. $settings = \Yii::$app->services->setting->addons->get($this->mall_id, MchEnum::ADDONS_NAME, 'enter');
  40. if (0 == $settings['is_enable']) {
  41. return $this->error('商家入驻未开启');
  42. }
  43. //检测是否需要实名认证
  44. $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH_BUSINESS, $this->mall_id, ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'type' => 'store']);
  45. if ($need_auth === true) {
  46. //需要先进行实名认证
  47. return $this->error('请先进行实名认证', [], ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
  48. }
  49. if (!empty($settings['goods_ids'])) {
  50. $settings['goods_ids'] = json_decode($settings['goods_ids'], true);
  51. $goods = Goods::find()
  52. ->select('id,goods_name,price,cover_pic')
  53. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $settings['goods_ids']])
  54. ->asArray()
  55. ->all();
  56. $settings['good_list'] = $goods;
  57. }
  58. // 是否有资格入驻门店
  59. $is_can_enter = 0;
  60. if (2 == $settings['mode']) {
  61. $qualification = MchUserEnterQualification::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]);
  62. if (!empty($qualification)) {
  63. $is_can_enter = 1;
  64. }
  65. } else {
  66. $is_can_enter = 1;
  67. }
  68. $settings['is_can_enter'] = $is_can_enter;
  69. $mch = Mch::find()
  70. ->select('id,mall_id,user_id,shop_owner,shop_mobile,store_name,c_id,store_desc,
  71. logo_img,license_img,address,longitude,latitude,check_status,check_remark, store_background')
  72. ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])
  73. ->asArray()
  74. ->one();
  75. // 入驻申请进度,0:未申请;1:审核中;2:审核通过;3:审核未通过
  76. $apply_step = 0;
  77. $mch_account = [];
  78. if (!empty($mch)) {
  79. $mch['detail_address'] = $mch['address'];
  80. if (MchEnum::CHECK_WAITING == $mch['check_status']) {
  81. $apply_step = 1;
  82. } elseif (MchEnum::CHECK_ADOPT == $mch['check_status']) {
  83. $apply_step = 2;
  84. $backend_domain = \Yii::$app->services->setting->platform->get('system.backend_url');
  85. $backend_domain = empty($backend_domain) ? '' : (string)$backend_domain;
  86. $mch_account = [
  87. 'mch_login_url' => trim($backend_domain, '/') . '?r=mch/client/auth/login&sign=' . $this->mall['mall_sign'],
  88. 'account' => $mch['shop_mobile'],
  89. 'login_pwd' => substr($mch['shop_mobile'], -6),
  90. ];
  91. } else if (MchEnum::CHECK_FAILED == $mch['check_status']) {
  92. $apply_step = 3;
  93. }
  94. }
  95. $categorys = MchCate::find()
  96. ->select('id,name')
  97. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  98. ->asArray()
  99. ->all();
  100. $result = [
  101. 'settings' => $settings,
  102. 'apply_step' => $apply_step,
  103. 'mch' => $mch,
  104. 'categorys' => $categorys,
  105. 'mch_account' => $mch_account,
  106. ];
  107. return $this->success('success', $result);
  108. }
  109. /**
  110. * @name:
  111. * @msg: 提交商户入驻申请
  112. * @param {*}
  113. * @return {*}
  114. */
  115. public function actionEnterApply()
  116. {
  117. if (!\Yii::$app->request->isPost) {
  118. return $this->error('请求方式错误');
  119. }
  120. $settings = \Yii::$app->services->setting->addons->get($this->mall_id, MchEnum::ADDONS_NAME, 'enter');
  121. $upload_license_image = $settings['upload_license_image'];
  122. $post = \Yii::$app->request->post();
  123. $rules = [
  124. [['store_desc'], 'required','message'=>'商户介绍不能为空'],
  125. [['logo_img'], 'required','message'=>'请上传商家logo'],
  126. [['store_background'], 'required','message'=>'请上传店铺背景图'],
  127. [['shop_owner', 'shop_mobile', 'store_name', 'c_id', 'vcode'], 'required'],
  128. [['shop_owner', 'store_name', 'store_desc', 'logo_img', 'license_img', 'longitude', 'latitude','store_background'], 'string', 'max' => 255],
  129. ['vcode', 'string'],
  130. [['shop_mobile', 'c_id'], 'integer'],
  131. ['license_img', 'required', 'when' => function ($model) use ($upload_license_image) {
  132. return $upload_license_image == 1;
  133. }],
  134. [['id'], 'safe'],
  135. ];
  136. $res = \Yii::$app->services->validate->validate($post, $rules);
  137. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  138. if (!YII_DEBUG && !Sms::checkValidateCode($post['shop_mobile'], $post['vcode'])) {
  139. return $this->error('验证码不正确');
  140. }
  141. preg_match('/(.*?(?:省|自治区|北京市|天津市|上海市|重庆市))+(.*?(?:市|自治州|地区|区划|县))+(.*?(?:市|区|县|镇|乡|街道))/', $post['address'], $matches);
  142. // dd($matches);
  143. if (count($matches) > 1) {
  144. $query = Region::find();
  145. $province = $query->where(['name' => $matches[1]])->one();
  146. $city = $query->where(['name' => $matches[2]])->one();
  147. $district = $query->where(['name' => $matches[3]])->one();
  148. // dd($province->name, $city->name, $district->name);
  149. $post['province_id'] = $province->id ?? 0;
  150. $post['city_id'] = $city->id ?? 0;
  151. $post['district_id'] = $district->id ?? 0;
  152. }
  153. $post['address'] .= $post['detail_address'];
  154. $post['mall_id'] = $this->mall_id;
  155. $post['user_id'] = $this->user_id;
  156. unset($post['detail_address']);
  157. $query = Mch::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'shop_mobile' => $post['shop_mobile'], 'shop_owner' => $post['shop_owner']]);
  158. if (!empty($post['id'])) {
  159. $query->andWhere(['id' => $post['id']]);
  160. } else {
  161. unset($post['id']);
  162. }
  163. $mch = $query->one();
  164. if (!empty($mch)) {
  165. if (empty($post['id'])) {
  166. // 新提交的申请
  167. return $this->error('您已经申请过请勿重复申请');
  168. } else {
  169. // 审核失败后重新提交的申请
  170. if ($mch->check_status != MchEnum::CHECK_FAILED) {
  171. return $this->error('商户状态异常');
  172. }
  173. $mch->attributes = $post;
  174. $mch->check_status = MchEnum::CHECK_WAITING;
  175. $res = $mch->save();
  176. if (!$res) {
  177. return $this->error($mch->getErrorMessage());
  178. }
  179. return $this->success('提交成功');
  180. }
  181. }
  182. if ($settings['store_audit'] == 1) {
  183. $post['check_status'] = MchEnum::CHECK_WAITING;
  184. } else {
  185. $post['check_status'] = MchEnum::CHECK_ADOPT;
  186. }
  187. //设置到期时间
  188. if($settings['time_status'] == 1 && !empty($settings['expired_days'])){
  189. $post['expire_time'] = time() + ($settings['expired_days'] * 60 * 60 * 24);
  190. }
  191. //dd($post);
  192. $res = Mch::addStore($post);
  193. if (false === $res) {
  194. return $this->error(Mch::getStaticError());
  195. }
  196. return $this->success('提交成功');
  197. }
  198. }