SmsCode.Class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * 发送短信验证码类
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/04
  7. * Time: 10:40
  8. */
  9. namespace Jindouyun\Controller\Common;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Factory;
  12. use JinDouYun\Cache\SmsVerification;
  13. use JinDouYun\Controller\BaseController;
  14. use JinDouYun\Model\UserCenter\MUserCenterRegister;
  15. use JinDouYun\Model\Oem\MOem;
  16. use Util\TencentCloud\Sms;
  17. class SmsCode extends BaseController
  18. {
  19. public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken=false)
  20. {
  21. parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
  22. }
  23. /**
  24. * 发送短信验证码
  25. *
  26. * @return void
  27. * @throws Exception
  28. */
  29. public function sendMobileCode()
  30. {
  31. $params = $this->request->getRawJson();
  32. if (empty($params)) {
  33. parent::sendOutput('参数为空', ErrorCode::$paramError);
  34. }
  35. $data = [
  36. 'mobile' => trim($params['mobile']),
  37. 'source' => (int)$params['source'],//标识 1:后台注册 2:后台忘记密码 3:小程序注册
  38. ];
  39. foreach ($data as $key => $value) {
  40. if (empty($value) && $value !== 0) {
  41. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  42. }
  43. }
  44. //获取当前地址栏域名替换短信签名发送
  45. $objMOem = new MOem();
  46. // pc页面根据domain获取配置
  47. if( in_array($data['source'],[1,2]) ){
  48. $data['domain'] = trim(getArrayItem($params, 'domain', ''));
  49. if( empty($data['domain']) ){
  50. parent::sendOutput( 'domain参数错误', ErrorCode::$paramError);
  51. }
  52. $result = $objMOem->getOemInfoByDomain($data['domain']);
  53. if(!$result->isSuccess()){
  54. parent::sendOutput($result->getData(), $result->getErrorCode());
  55. }
  56. $configData = $result -> getData();
  57. } else { // 小程序通过token获取配置
  58. self::getEnterpriseIdByToken();
  59. $result = $objMOem->getOemInfoByenterpriseId($this->onlineEnterpriseId);
  60. if(!$result->isSuccess()){
  61. parent::sendOutput($result->getData(), $result->getErrorCode());
  62. }
  63. $configData = $result -> getData();
  64. }
  65. //验证手机号格式
  66. $this->checkMobile($data['mobile']);
  67. //查询手机号是否存在
  68. $MUserCenterRegister = new MUserCenterRegister();
  69. switch ($data['source']) {
  70. case 1:
  71. //后台注册
  72. $resultMobileIs = $MUserCenterRegister->mobileIsRegister($data['mobile']);
  73. if($resultMobileIs) {
  74. parent::sendOutput('手机号已注册');
  75. }
  76. break;
  77. case 2:
  78. //后台忘记密码
  79. $resultMobileIs = $MUserCenterRegister->mobileIsRegister($data['mobile']);
  80. if(!$resultMobileIs) {
  81. parent::sendOutput('手机号未注册',ErrorCode::$contentNotExists);
  82. }
  83. break;
  84. case 3:
  85. //前台注册,后台创建客户
  86. parent::getEnterpriseIdByToken();
  87. $resultMobileIs = $MUserCenterRegister->mobileIsRegister($data['mobile'], $this->onlineEnterpriseId);
  88. if($resultMobileIs) {
  89. parent::sendOutput('手机号已注册');
  90. }
  91. break;
  92. case 4:
  93. //前台忘记密码
  94. parent::getEnterpriseIdByToken();
  95. $resultMobileIs = $MUserCenterRegister->mobileIsRegister($data['mobile'], $this->onlineEnterpriseId);
  96. if(!$resultMobileIs) {
  97. parent::sendOutput('手机号未注册');
  98. }
  99. break;
  100. case 5:
  101. //小程序绑定
  102. parent::getEnterpriseIdByToken();
  103. break;
  104. case 6:
  105. //短信登录
  106. break;
  107. default:
  108. break;
  109. }
  110. //验证当天发送验证码次数是否超过上限
  111. $SmsVerification = new SmsVerification();
  112. $SmsCount = $SmsVerification->getRegisterSmsCount($data['mobile']);
  113. if($SmsCount >= 5){
  114. parent::sendOutput('当天发送验证码次数已达上限', ErrorCode::$configEroor);
  115. }
  116. //生成短信验证码
  117. $mobileCode = generate_code(6);
  118. #调用第三方短信平台
  119. /*$tencentCloudConfigData = Factory::config()->get('tencentCloud');
  120. if(empty($tencentCloudConfigData)){
  121. $this->sendOutput('腾讯云配置错误',ErrorCode::$configEroor);
  122. }
  123. $smsCodeKey = Factory::config()->get('sms');
  124. if(empty($smsCodeKey) || !isset($smsCodeKey['registerCodeKey']) || !isset($smsCodeKey['SmsSdkAppid']) || !isset($smsCodeKey['Sign'])){
  125. $this->sendOutput('短信模板获取失败', ErrorCode::$configEroor);
  126. }*/
  127. if(empty($configData)){
  128. parent::sendOutput('查询配置信息失败', ErrorCode::$contentNotExists);
  129. }
  130. $tencentCloudConfigData = json_decode($configData['tencentCloud'], true);
  131. if(empty($tencentCloudConfigData)){
  132. parent::sendOutput('腾讯云配置项为空', ErrorCode::$contentNotExists);
  133. }
  134. $smsCodeKey = json_decode($configData['sms'], true);
  135. if(empty($smsCodeKey)){
  136. parent::sendOutput('短信配置项为空', ErrorCode::$contentNotExists);
  137. }
  138. $objSms = new Sms($tencentCloudConfigData['secretId'], $tencentCloudConfigData['secretKey'], $smsCodeKey['SmsSdkAppid'], $smsCodeKey['Sign']);
  139. $result = $objSms->SendSms($smsCodeKey['registerCodeKey'], ["+86".$data['mobile']], ["$mobileCode","5"]);
  140. if (!$result->isSuccess()) {
  141. parent::sendOutput($result->getData(), $result->getErrorCode());
  142. }
  143. //调用redis保存发送验证码记录
  144. $result = $SmsVerification->userMobileCodeCache($data['mobile'], $mobileCode);
  145. if($result->isSuccess()){
  146. parent::sendOutput('发送短信成功');
  147. }else{
  148. parent::sendOutput('', $result->getData());
  149. }
  150. }
  151. public function test()
  152. {
  153. $tencentCloudConfigData = Factory::config()->get('tencentCloud');
  154. if(empty($tencentCloudConfigData)){
  155. $this->sendOutput('腾讯云配置错误',ErrorCode::$configEroor);
  156. }
  157. $objSms = new Sms($tencentCloudConfigData['secretId'], $tencentCloudConfigData['secretKey']);
  158. $smsCodeKey = Factory::config()->get('sms');
  159. if(empty($smsCodeKey) || !isset($smsCodeKey['registerCodeKey'])){
  160. $this->sendOutput('短信模板获取失败', ErrorCode::$configEroor);
  161. }
  162. $result = $objSms->SendSms($smsCodeKey['registerCodeKey'], ["+8617600105150"], ["123456","5"]);
  163. if ($result->isSuccess()) {
  164. parent::sendOutput($result->getData());
  165. } else {
  166. parent::sendOutput($result->getData(), $result->getErrorCode());
  167. }
  168. }
  169. }