| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace addons\Community\api\modules\v1\controllers;
- use addons\Community\common\enums\CommunityEnum;
- use addons\Community\common\enums\ConfigEnum;
- use addons\Community\common\service\ApplyService;
- use addons\Community\common\service\HeadService;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use common\helpers\sms\Sms;
- use common\models\common\Region;
- use Overtrue\EasySms\Exceptions\InvalidArgumentException;
- use Yii;
- class ApplyController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = [];
- /**
- * 入驻首页
- * @return mixed|null
- */
- public function actionIndex()
- {
- // 需要取出图片
- if ((new HeadService())->existByUserId($this->mall_id, $this->user_id) == StatusEnum::ENABLED) {
- return $this->error('你已经是社区团长');
- }
- $config = Yii::$app->services->setting->addons->get($this->mall_id, CommunityEnum::ADDONS_NAME, 'basic');
- return $this->success('获取成功', [
- 'pic' => !empty($config['apply_pic']) ? $config['apply_pic'] : '',
- 'protocol_title' => !empty($config['protocol_title']) ? $config['protocol_title'] : '',
- 'protocol_content' => !empty($config['protocol_content']) ? $config['protocol_content'] : ''
- ]);
- }
- /**
- * 用户提交数据
- * @return mixed|null
- * @throws \Exception
- */
- public function actionCreate()
- {
- // 是否需要校验验证码
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['id', 'province_id', 'city_id', 'district_id', 'is_home_delivery', 'contact_phone'], 'integer'],
- [['name', 'intro', 'logo', 'license_pic', 'contact_name', 'street', 'captcha'], 'string'],
- [['lng', 'lat'], 'number'],
- [['name', 'intro', 'logo', 'license_pic', 'contact_name', 'contact_phone',
- 'province_id', 'city_id', 'street', 'lng', 'lat', 'captcha'], 'required'],
- [['is_home_delivery'], 'in', 'range' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
- [['id_card_pic'], 'safe']
- ], [
- 'name' => '自提点名称',
- 'province_id' => '省份',
- 'city_id' => '城市',
- 'district_id' => '地区',
- 'intro' => '自提点介绍',
- 'logo' => '自提点LOGO',
- 'license_pic' => '营业执照',
- 'id_card_pic' => '身份证件照',
- 'contact_name' => '联系人姓名',
- 'contact_phone' => '联系人电话',
- 'street' => '详细地址',
- 'lng' => '经度',
- 'lat' => '纬度',
- 'is_home_delivery' => '支持送货上门',
- 'captcha' => '短信验证码'
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 校验经纬度
- if ($data['lng'] < -180 || $data['lng'] > 180 || $data['lng'] == 0) {
- return $this->error('经度错误');
- }
- if ($data['lat'] < -180 || $data['lat'] > 180 || $data['lat'] == 0) {
- return $this->error('纬度错误');
- }
- if (empty($data['id_card_pic'])) return $this->error('身份证件照不能为空');
- if (!is_array($data['id_card_pic'])) return $this->error('身份证件照格式错误');
- $data['id_card_pic'] = array_filter($data['id_card_pic'], function ($val) {
- return !empty($val);
- });
- if (count($data['id_card_pic']) != 2) return $this->error('身份证件照需要正反面各一张');
- $dIn = [$data['province_id'], $data['city_id']];
- if (!empty($data['district_id'])) $dIn[] = $data['district_id'];
- $regions = Region::find()->where(['in', 'id', $dIn])
- ->select('name,id,parent_id,level')->orderBy('id asc')->asArray()->all();
- if (count($regions) != count($dIn)) return $this->error('地区错误');
- $regions = array_reduce($regions, function (&$regions, $val) {
- $regions[$val['id']] = $val;
- return $regions;
- });
- $province = $regions[$data['province_id']] ?? '';
- if (empty($province)) return $this->error('省份不存在');
- $city = $regions[$data['city_id']] ?? '';
- if (empty($city)) return $this->error('城市不存在');
- $district = $regions[$data['district_id']] ?? '';
- $district_name = $district['name'] ?? '';
- // if (empty($district)) return $this->error('区县不存在');
- $data['area'] = "{$province['name']} {$city['name']} {$district_name}";
- if (YII_ENV == 'prod') {
- //短信验证码
- if (!Sms::checkValidateCode($data['contact_phone'], $data['captcha'],ConfigEnum::COMMUNITY_SMS_TYPE)) return $this->error('验证码不正确');
- }
- $ret = (new ApplyService())->create($this->mall_id, $data, $this->user_id);
- return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
- }
- /**
- * 入驻协议
- * @return mixed|null
- */
- public function actionProtocol()
- {
- // 是否需要校验验证码
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['protocol'], 'integer'],
- [['protocol'], 'in', 'range' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
- [['protocol'], 'required', 'message' => '请勾选协议'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if ($data['protocol'] != StatusEnum::ENABLED) return $this->error('请勾选协议');
- $ret = (new ApplyService())->createProtocol($this->mall_id, $this->user_id);
- return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
- }
- /**
- * 联系人信息
- * @return mixed|null
- * @throws \Exception
- */
- public function actionContact()
- {
- // 是否需要校验验证码
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['contact_name', 'contact_phone', 'captcha'], 'string'],
- [['contact_name', 'contact_phone', 'captcha'], 'required'],
- ], [
- 'contact_name' => '联系人姓名',
- 'contact_phone' => '联系人电话',
- 'captcha' => '短信验证码'
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (YII_ENV == 'prod') {
- //短信验证码
- if (!Sms::checkValidateCode($data['contact_phone'], $data['captcha'],ConfigEnum::COMMUNITY_SMS_TYPE)) return $this->error('验证码不正确');
- }
- $ret = (new ApplyService())->createContact($this->mall_id, $data, $this->user_id);
- return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
- }
- /**
- * 门店信息提交
- * @return mixed|null
- */
- public function actionHeadInfo()
- {
- // 是否需要校验验证码
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['province_id', 'city_id', 'district_id', 'is_home_delivery'], 'integer'],
- [['name', 'intro', 'logo', 'license_pic', 'street', 'captcha'], 'string'],
- [['lng', 'lat'], 'number'],
- [['name', 'intro', 'logo', 'license_pic', 'id_card_pic',
- 'province_id', 'city_id', 'district_id', 'street', 'lng', 'lat'], 'required'],
- [['is_home_delivery'], 'in', 'range' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
- [['id_card_pic'], 'safe']
- ], [
- 'name' => '自提点名称',
- 'province_id' => '省份',
- 'city_id' => '城市',
- 'district_id' => '地区',
- 'intro' => '自提点介绍',
- 'logo' => '自提点LOGO',
- 'license_pic' => '营业执照',
- 'id_card_pic' => '身份证件照',
- 'street' => '详细地址',
- 'lng' => '经度',
- 'lat' => '纬度',
- 'is_home_delivery' => '支持送货上门'
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (empty($data['id_card_pic'])) return $this->error('身份证件照不能为空');
- if (!is_array($data['id_card_pic'])) return $this->error('身份证件照格式错误');
- if (count($data['id_card_pic']) != 2) return $this->error('身份证件照需要正反面各一张');
- $regions = Region::find()->where(['in', 'id', [$data['province_id'], $data['city_id'], $data['district_id']]])
- ->select('name,id,parent_id,level')->orderBy('id asc')->asArray()->all();
- if (count($regions) != 3) return $this->error('地区错误');
- $regions = array_reduce($regions, function (&$regions, $val) {
- $regions[$val['id']] = $val;
- return $regions;
- });
- $province = $regions[$data['province_id']] ?? '';
- if (empty($province)) return $this->error('省份不存在');
- $city = $regions[$data['city_id']] ?? '';
- if (empty($city)) return $this->error('城市不存在');
- $district = $regions[$data['district_id']] ?? '';
- if (empty($district)) return $this->error('区县不存在');
- $data['area'] = "{$province['name']}{$city['name']}{$district['name']}";
- $ret = (new ApplyService())->createHeadInfo($this->mall_id, $data, $this->user_id);
- return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
- }
- /**
- * 提交审核
- * @return mixed|null
- */
- public function actionSubmit()
- {
- $ret = (new ApplyService())->createSubmit($this->mall_id, $this->user_id);
- return is_string($ret) ? $this->error($ret) : $this->success('提交成功', []);
- }
- /**
- * 获取详情
- * @return mixed|null
- */
- public function actionDetail()
- {
- $ret = (new ApplyService())->fetch($this->mall_id, $this->user_id);
- return $this->success('获取成功', $ret);
- }
- /**
- * @return mixed|null
- * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
- * @throws InvalidArgumentException
- */
- public function actionSms()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['contact_phone'], 'required', 'message' => '请输入手机号码'],
- [['contact_phone','area_code'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $area_code = '';// 初始化区号
- $gateways = 'domestic';// 默认国内短信
- // 检测是登录并且区号不为空则使用国际短信
- if (!empty($this->user) && !empty($this->user->area_code) && $this->user->area_code != 86) {
- $gateways = 'international';
- $area_code = $this->user->area_code;
- }
- // 判断是否有区号
- if (!empty($params['area_code']) && $params['area_code'] != 86) {
- $gateways = 'international';
- $area_code = $params['area_code'];
- }
- //$params['sms_type'] = $params['sms_type'] ?: 'register';
- $sms = new Sms($this->mall_id, $gateways);
- // $sms->easySms->send("", "");
- try {
- if ($sms->sendCaptcha($params['contact_phone'], $area_code, ConfigEnum::COMMUNITY_SMS_TYPE) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('发送成功');
- }
- }
|