ApplyController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace addons\Community\api\modules\v1\controllers;
  3. use addons\Community\common\enums\CommunityEnum;
  4. use addons\Community\common\enums\ConfigEnum;
  5. use addons\Community\common\service\ApplyService;
  6. use addons\Community\common\service\HeadService;
  7. use api\controllers\OnAuthController;
  8. use common\enums\StatusEnum;
  9. use common\helpers\sms\Sms;
  10. use common\models\common\Region;
  11. use Overtrue\EasySms\Exceptions\InvalidArgumentException;
  12. use Yii;
  13. class ApplyController extends OnAuthController
  14. {
  15. public $modelClass = '';
  16. /**
  17. * 不用进行登录验证的方法
  18. *
  19. * 例如: ['index', 'update', 'create', 'view', 'delete']
  20. * 默认全部需要验证
  21. *
  22. * @var array
  23. */
  24. protected $authOptional = [];
  25. /**
  26. * 入驻首页
  27. * @return mixed|null
  28. */
  29. public function actionIndex()
  30. {
  31. // 需要取出图片
  32. if ((new HeadService())->existByUserId($this->mall_id, $this->user_id) == StatusEnum::ENABLED) {
  33. return $this->error('你已经是社区团长');
  34. }
  35. $config = Yii::$app->services->setting->addons->get($this->mall_id, CommunityEnum::ADDONS_NAME, 'basic');
  36. return $this->success('获取成功', [
  37. 'pic' => !empty($config['apply_pic']) ? $config['apply_pic'] : '',
  38. 'protocol_title' => !empty($config['protocol_title']) ? $config['protocol_title'] : '',
  39. 'protocol_content' => !empty($config['protocol_content']) ? $config['protocol_content'] : ''
  40. ]);
  41. }
  42. /**
  43. * 用户提交数据
  44. * @return mixed|null
  45. * @throws \Exception
  46. */
  47. public function actionCreate()
  48. {
  49. // 是否需要校验验证码
  50. $data = Yii::$app->request->post();
  51. $valid = Yii::$app->services->validate->validate($data, [
  52. [['id', 'province_id', 'city_id', 'district_id', 'is_home_delivery', 'contact_phone'], 'integer'],
  53. [['name', 'intro', 'logo', 'license_pic', 'contact_name', 'street', 'captcha'], 'string'],
  54. [['lng', 'lat'], 'number'],
  55. [['name', 'intro', 'logo', 'license_pic', 'contact_name', 'contact_phone',
  56. 'province_id', 'city_id', 'street', 'lng', 'lat', 'captcha'], 'required'],
  57. [['is_home_delivery'], 'in', 'range' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
  58. [['id_card_pic'], 'safe']
  59. ], [
  60. 'name' => '自提点名称',
  61. 'province_id' => '省份',
  62. 'city_id' => '城市',
  63. 'district_id' => '地区',
  64. 'intro' => '自提点介绍',
  65. 'logo' => '自提点LOGO',
  66. 'license_pic' => '营业执照',
  67. 'id_card_pic' => '身份证件照',
  68. 'contact_name' => '联系人姓名',
  69. 'contact_phone' => '联系人电话',
  70. 'street' => '详细地址',
  71. 'lng' => '经度',
  72. 'lat' => '纬度',
  73. 'is_home_delivery' => '支持送货上门',
  74. 'captcha' => '短信验证码'
  75. ]);
  76. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  77. // 校验经纬度
  78. if ($data['lng'] < -180 || $data['lng'] > 180 || $data['lng'] == 0) {
  79. return $this->error('经度错误');
  80. }
  81. if ($data['lat'] < -180 || $data['lat'] > 180 || $data['lat'] == 0) {
  82. return $this->error('纬度错误');
  83. }
  84. if (empty($data['id_card_pic'])) return $this->error('身份证件照不能为空');
  85. if (!is_array($data['id_card_pic'])) return $this->error('身份证件照格式错误');
  86. $data['id_card_pic'] = array_filter($data['id_card_pic'], function ($val) {
  87. return !empty($val);
  88. });
  89. if (count($data['id_card_pic']) != 2) return $this->error('身份证件照需要正反面各一张');
  90. $dIn = [$data['province_id'], $data['city_id']];
  91. if (!empty($data['district_id'])) $dIn[] = $data['district_id'];
  92. $regions = Region::find()->where(['in', 'id', $dIn])
  93. ->select('name,id,parent_id,level')->orderBy('id asc')->asArray()->all();
  94. if (count($regions) != count($dIn)) return $this->error('地区错误');
  95. $regions = array_reduce($regions, function (&$regions, $val) {
  96. $regions[$val['id']] = $val;
  97. return $regions;
  98. });
  99. $province = $regions[$data['province_id']] ?? '';
  100. if (empty($province)) return $this->error('省份不存在');
  101. $city = $regions[$data['city_id']] ?? '';
  102. if (empty($city)) return $this->error('城市不存在');
  103. $district = $regions[$data['district_id']] ?? '';
  104. $district_name = $district['name'] ?? '';
  105. // if (empty($district)) return $this->error('区县不存在');
  106. $data['area'] = "{$province['name']} {$city['name']} {$district_name}";
  107. if (YII_ENV == 'prod') {
  108. //短信验证码
  109. if (!Sms::checkValidateCode($data['contact_phone'], $data['captcha'],ConfigEnum::COMMUNITY_SMS_TYPE)) return $this->error('验证码不正确');
  110. }
  111. $ret = (new ApplyService())->create($this->mall_id, $data, $this->user_id);
  112. return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
  113. }
  114. /**
  115. * 入驻协议
  116. * @return mixed|null
  117. */
  118. public function actionProtocol()
  119. {
  120. // 是否需要校验验证码
  121. $data = Yii::$app->request->post();
  122. $valid = Yii::$app->services->validate->validate($data, [
  123. [['protocol'], 'integer'],
  124. [['protocol'], 'in', 'range' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
  125. [['protocol'], 'required', 'message' => '请勾选协议'],
  126. ]);
  127. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  128. if ($data['protocol'] != StatusEnum::ENABLED) return $this->error('请勾选协议');
  129. $ret = (new ApplyService())->createProtocol($this->mall_id, $this->user_id);
  130. return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
  131. }
  132. /**
  133. * 联系人信息
  134. * @return mixed|null
  135. * @throws \Exception
  136. */
  137. public function actionContact()
  138. {
  139. // 是否需要校验验证码
  140. $data = Yii::$app->request->post();
  141. $valid = Yii::$app->services->validate->validate($data, [
  142. [['contact_name', 'contact_phone', 'captcha'], 'string'],
  143. [['contact_name', 'contact_phone', 'captcha'], 'required'],
  144. ], [
  145. 'contact_name' => '联系人姓名',
  146. 'contact_phone' => '联系人电话',
  147. 'captcha' => '短信验证码'
  148. ]);
  149. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  150. if (YII_ENV == 'prod') {
  151. //短信验证码
  152. if (!Sms::checkValidateCode($data['contact_phone'], $data['captcha'],ConfigEnum::COMMUNITY_SMS_TYPE)) return $this->error('验证码不正确');
  153. }
  154. $ret = (new ApplyService())->createContact($this->mall_id, $data, $this->user_id);
  155. return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
  156. }
  157. /**
  158. * 门店信息提交
  159. * @return mixed|null
  160. */
  161. public function actionHeadInfo()
  162. {
  163. // 是否需要校验验证码
  164. $data = Yii::$app->request->post();
  165. $valid = Yii::$app->services->validate->validate($data, [
  166. [['province_id', 'city_id', 'district_id', 'is_home_delivery'], 'integer'],
  167. [['name', 'intro', 'logo', 'license_pic', 'street', 'captcha'], 'string'],
  168. [['lng', 'lat'], 'number'],
  169. [['name', 'intro', 'logo', 'license_pic', 'id_card_pic',
  170. 'province_id', 'city_id', 'district_id', 'street', 'lng', 'lat'], 'required'],
  171. [['is_home_delivery'], 'in', 'range' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
  172. [['id_card_pic'], 'safe']
  173. ], [
  174. 'name' => '自提点名称',
  175. 'province_id' => '省份',
  176. 'city_id' => '城市',
  177. 'district_id' => '地区',
  178. 'intro' => '自提点介绍',
  179. 'logo' => '自提点LOGO',
  180. 'license_pic' => '营业执照',
  181. 'id_card_pic' => '身份证件照',
  182. 'street' => '详细地址',
  183. 'lng' => '经度',
  184. 'lat' => '纬度',
  185. 'is_home_delivery' => '支持送货上门'
  186. ]);
  187. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  188. if (empty($data['id_card_pic'])) return $this->error('身份证件照不能为空');
  189. if (!is_array($data['id_card_pic'])) return $this->error('身份证件照格式错误');
  190. if (count($data['id_card_pic']) != 2) return $this->error('身份证件照需要正反面各一张');
  191. $regions = Region::find()->where(['in', 'id', [$data['province_id'], $data['city_id'], $data['district_id']]])
  192. ->select('name,id,parent_id,level')->orderBy('id asc')->asArray()->all();
  193. if (count($regions) != 3) return $this->error('地区错误');
  194. $regions = array_reduce($regions, function (&$regions, $val) {
  195. $regions[$val['id']] = $val;
  196. return $regions;
  197. });
  198. $province = $regions[$data['province_id']] ?? '';
  199. if (empty($province)) return $this->error('省份不存在');
  200. $city = $regions[$data['city_id']] ?? '';
  201. if (empty($city)) return $this->error('城市不存在');
  202. $district = $regions[$data['district_id']] ?? '';
  203. if (empty($district)) return $this->error('区县不存在');
  204. $data['area'] = "{$province['name']}{$city['name']}{$district['name']}";
  205. $ret = (new ApplyService())->createHeadInfo($this->mall_id, $data, $this->user_id);
  206. return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
  207. }
  208. /**
  209. * 提交审核
  210. * @return mixed|null
  211. */
  212. public function actionSubmit()
  213. {
  214. $ret = (new ApplyService())->createSubmit($this->mall_id, $this->user_id);
  215. return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
  216. }
  217. /**
  218. * 获取详情
  219. * @return mixed|null
  220. */
  221. public function actionDetail()
  222. {
  223. $ret = (new ApplyService())->fetch($this->mall_id, $this->user_id);
  224. return $this->success('获取成功', $ret);
  225. }
  226. /**
  227. * @return mixed|null
  228. * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
  229. * @throws InvalidArgumentException
  230. */
  231. public function actionSms()
  232. {
  233. $params = Yii::$app->request->post();
  234. $res = Yii::$app->services->validate->validate($params, [
  235. [['contact_phone'], 'required', 'message' => '请输入手机号码'],
  236. [['contact_phone','area_code'], 'integer'],
  237. ]);
  238. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  239. $area_code = '';// 初始化区号
  240. $gateways = 'domestic';// 默认国内短信
  241. // 检测是登录并且区号不为空则使用国际短信
  242. if (!empty($this->user) && !empty($this->user->area_code) && $this->user->area_code != 86) {
  243. $gateways = 'international';
  244. $area_code = $this->user->area_code;
  245. }
  246. // 判断是否有区号
  247. if (!empty($params['area_code']) && $params['area_code'] != 86) {
  248. $gateways = 'international';
  249. $area_code = $params['area_code'];
  250. }
  251. //$params['sms_type'] = $params['sms_type'] ?: 'register';
  252. $sms = new Sms($this->mall_id, $gateways);
  253. // $sms->easySms->send("", "");
  254. try {
  255. if ($sms->sendCaptcha($params['contact_phone'], $area_code, ConfigEnum::COMMUNITY_SMS_TYPE) === false) return $this->error('发送失败');
  256. }catch (\Exception $e){
  257. return $this->error($e->getMessage());
  258. }
  259. return $this->success('发送成功');
  260. }
  261. }