123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- /**
- * 发送短信验证码类
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/11/04
- * Time: 10:40
- */
- namespace Jindouyun\Controller\Common;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Factory;
- use JinDouYun\Cache\SmsVerification;
- use JinDouYun\Controller\BaseController;
- use JinDouYun\Model\UserCenter\MUserCenterRegister;
- use JinDouYun\Model\Oem\MOem;
- use Util\TencentCloud\Sms;
- class SmsCode extends BaseController
- {
- public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken=false)
- {
- parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
- }
- /**
- * 发送短信验证码
- *
- * @return void
- * @throws Exception
- */
- public function sendMobileCode()
- {
- $params = $this->request->getRawJson();
- if (empty($params)) {
- parent::sendOutput('参数为空', ErrorCode::$paramError);
- }
- $data = [
- 'mobile' => trim($params['mobile']),
- 'source' => (int)$params['source'],//标识 1:后台注册 2:后台忘记密码 3:小程序注册
- ];
- foreach ($data as $key => $value) {
- if (empty($value) && $value !== 0) {
- parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- //获取当前地址栏域名替换短信签名发送
- $objMOem = new MOem();
- // pc页面根据domain获取配置
- if( in_array($data['source'],[1,2]) ){
- $data['domain'] = trim(getArrayItem($params, 'domain', ''));
- if( empty($data['domain']) ){
- parent::sendOutput( 'domain参数错误', ErrorCode::$paramError);
- }
- $result = $objMOem->getOemInfoByDomain($data['domain']);
- if(!$result->isSuccess()){
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- $configData = $result -> getData();
- } else { // 小程序通过token获取配置
- self::getEnterpriseIdByToken();
- $result = $objMOem->getOemInfoByenterpriseId($this->onlineEnterpriseId);
- if(!$result->isSuccess()){
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- $configData = $result -> getData();
- }
- //验证手机号格式
- $this->checkMobile($data['mobile']);
- //查询手机号是否存在
- $MUserCenterRegister = new MUserCenterRegister();
- switch ($data['source']) {
- case 1:
- //后台注册
- $resultMobileIs = $MUserCenterRegister->mobileIsRegister($data['mobile']);
- if($resultMobileIs) {
- parent::sendOutput('手机号已注册');
- }
- break;
- case 2:
- //后台忘记密码
- $resultMobileIs = $MUserCenterRegister->mobileIsRegister($data['mobile']);
- if(!$resultMobileIs) {
- parent::sendOutput('手机号未注册',ErrorCode::$contentNotExists);
- }
- break;
- case 3:
- //前台注册,后台创建客户
- parent::getEnterpriseIdByToken();
- $resultMobileIs = $MUserCenterRegister->mobileIsRegister($data['mobile'], $this->onlineEnterpriseId);
- if($resultMobileIs) {
- parent::sendOutput('手机号已注册');
- }
- break;
- case 4:
- //前台忘记密码
- parent::getEnterpriseIdByToken();
- $resultMobileIs = $MUserCenterRegister->mobileIsRegister($data['mobile'], $this->onlineEnterpriseId);
- if(!$resultMobileIs) {
- parent::sendOutput('手机号未注册');
- }
- break;
- case 5:
- //小程序绑定
- parent::getEnterpriseIdByToken();
- break;
- case 6:
- //短信登录
- break;
- default:
- break;
- }
- //验证当天发送验证码次数是否超过上限
- $SmsVerification = new SmsVerification();
- $SmsCount = $SmsVerification->getRegisterSmsCount($data['mobile']);
- if($SmsCount >= 5){
- parent::sendOutput('当天发送验证码次数已达上限', ErrorCode::$configEroor);
- }
- //生成短信验证码
- $mobileCode = generate_code(6);
- #调用第三方短信平台
- /*$tencentCloudConfigData = Factory::config()->get('tencentCloud');
- if(empty($tencentCloudConfigData)){
- $this->sendOutput('腾讯云配置错误',ErrorCode::$configEroor);
- }
- $smsCodeKey = Factory::config()->get('sms');
- if(empty($smsCodeKey) || !isset($smsCodeKey['registerCodeKey']) || !isset($smsCodeKey['SmsSdkAppid']) || !isset($smsCodeKey['Sign'])){
- $this->sendOutput('短信模板获取失败', ErrorCode::$configEroor);
- }*/
- if(empty($configData)){
- parent::sendOutput('查询配置信息失败', ErrorCode::$contentNotExists);
- }
- $tencentCloudConfigData = json_decode($configData['tencentCloud'], true);
- if(empty($tencentCloudConfigData)){
- parent::sendOutput('腾讯云配置项为空', ErrorCode::$contentNotExists);
- }
- $smsCodeKey = json_decode($configData['sms'], true);
- if(empty($smsCodeKey)){
- parent::sendOutput('短信配置项为空', ErrorCode::$contentNotExists);
- }
- $objSms = new Sms($tencentCloudConfigData['secretId'], $tencentCloudConfigData['secretKey'], $smsCodeKey['SmsSdkAppid'], $smsCodeKey['Sign']);
- $result = $objSms->SendSms($smsCodeKey['registerCodeKey'], ["+86".$data['mobile']], ["$mobileCode","5"]);
- if (!$result->isSuccess()) {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- //调用redis保存发送验证码记录
- $result = $SmsVerification->userMobileCodeCache($data['mobile'], $mobileCode);
- if($result->isSuccess()){
- parent::sendOutput('发送短信成功');
- }else{
- parent::sendOutput('', $result->getData());
- }
- }
- public function test()
- {
- $tencentCloudConfigData = Factory::config()->get('tencentCloud');
- if(empty($tencentCloudConfigData)){
- $this->sendOutput('腾讯云配置错误',ErrorCode::$configEroor);
- }
- $objSms = new Sms($tencentCloudConfigData['secretId'], $tencentCloudConfigData['secretKey']);
- $smsCodeKey = Factory::config()->get('sms');
- if(empty($smsCodeKey) || !isset($smsCodeKey['registerCodeKey'])){
- $this->sendOutput('短信模板获取失败', ErrorCode::$configEroor);
- }
- $result = $objSms->SendSms($smsCodeKey['registerCodeKey'], ["+8617600105150"], ["123456","5"]);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- } else {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- }
- }
|