ComEmailCodeService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ywl
  5. * Date: 2017/4/14
  6. * Time: 11:38
  7. */
  8. namespace App\Common\Services;
  9. use AlibabaCloud\Client\Exception\ClientException;
  10. use App\Common\Models\ComEmailCodeModel;
  11. use App\Exceptions\ApiException;
  12. use App\Services\CommonService;
  13. use Illuminate\Database\QueryException;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Mail;
  17. class ComEmailCodeService extends CommonService
  18. {
  19. protected $cache = true;
  20. protected $cacheBucket = 'ComEmailCode:';
  21. /**
  22. * LogSendCodeService constructor.
  23. * @param ComEmailCodeModel $model
  24. */
  25. public function __construct(ComEmailCodeModel $model)
  26. {
  27. $this->model = $model;
  28. }
  29. /**
  30. * 发送验证码
  31. * @param array $params phone email type 验证类型 0 手机验证 1邮箱验证
  32. * @return bool|mixed
  33. * @throws \App\Exceptions\ApiException;
  34. * @throws ClientException
  35. */
  36. public function sendValidateCode($email,$isSend=true,$language='zh-cn',$footer='')
  37. {
  38. $resultData=['id'=>0,'code'=>''];
  39. $code = mt_rand(100000, 999999);
  40. $resultData['code']=$code;
  41. $currentTime = time(); //当前时间
  42. $title='邮箱验证码';//邮件标题
  43. if(empty($email)){
  44. throw new ApiException(10021);
  45. }
  46. if($isSend){
  47. $httpData=[];//邮件内容变量参数
  48. $httpData['email']=$email;
  49. $httpData['code']=$code;
  50. $httpData['footer']=$footer;
  51. $mailTemp='mail.validateCodeModel';
  52. if($language=='en-us'){
  53. $mailTemp= 'mail.en.validateCodeModel';
  54. }
  55. Mail::send($mailTemp, $httpData, function ($message) use ($email,$title) {
  56. $message->to($email)->subject($title);
  57. });
  58. }
  59. $data = [
  60. 'email' => $email,
  61. 'code' => $code,
  62. 'error' => '',
  63. ];
  64. $data['status'] = 1;
  65. $data['expire_time']=$currentTime + config('cache.sms_time');
  66. $key=$email;
  67. $cacheKey=md5($key);
  68. $cacheTime=config('cache.sms_time');
  69. $this->setCacheData($cacheKey,$data,$cacheTime);
  70. $logId = $this->saveLogSendCode($data);
  71. $resultData['id']=$logId;
  72. $resultData['code']=$code;
  73. $resultData['email']=$email;
  74. return $resultData;
  75. }
  76. /**
  77. * 短信记录
  78. * @param array $params 短信存储参数
  79. * @return bool|mixed
  80. * @throws App\Base\Exceptions\ApiException
  81. */
  82. public function saveLogSendCode($params = [])
  83. {
  84. //手机验证码信息
  85. $nowTime=nowTime();
  86. $codeData = [
  87. 'code' => $params['code'],
  88. 'email' =>empty($params['email']) ? '' : $params['email'],
  89. 'status' => $params['status'],
  90. 'error' => empty($params['error']) ? '' : $params['error'],
  91. 'create_time' => $nowTime
  92. ];
  93. if(!empty($params['expire_time'])){
  94. $codeData['expire_time']= date('Y-m-d H:i:s', $params['expire_time']);
  95. }
  96. try {
  97. $id=$this->model->insertGetId($codeData);
  98. if (!$id) {
  99. Log::info('method:saveLogSendCode:保存验证码失败');
  100. throw new ApiException(500);
  101. }
  102. return $id;
  103. } catch (QueryException $ex) {
  104. //异常处理
  105. Log::info('method:saveLogSendCode:' . $ex->getMessage());
  106. Log::info('method:saveLogSendCode:' . $ex->getTraceAsString());
  107. throw new ApiException('common.server_busy', '服务器忙,请稍候重试~');
  108. }
  109. }
  110. /**
  111. * 校验短信验证码是否有效
  112. * @param string $key 手机号/邮箱
  113. * @param string $code 验证码
  114. * @param integer $type 验证类型 0 手机验证 1邮箱验证
  115. * @return array -1 失败重新获取验证码 -2验证码错误重新输入,1 成功是否验证成功
  116. */
  117. public function validateCode($key, $code)
  118. {
  119. //todo 通过redis 缓存来验证
  120. $result=['code'=>-1];
  121. $current_time = time();
  122. $cacheKey=md5($key);
  123. $cacheData=$this->getCacheData($cacheKey);
  124. if($cacheData){
  125. if(!empty($cacheData['expire_time'])&&$cacheData['expire_time']>$current_time&&$code==$cacheData['code']){
  126. $result['code']=1;
  127. $this->removeByKey($cacheKey);
  128. }else if($code!=$cacheData['code']){
  129. $result['code']=-2;
  130. }
  131. }
  132. return $result;
  133. }
  134. /**
  135. * 校验短信验证码一分钟内是否重复发送
  136. * */
  137. public function validatePhoneMinute($phone){
  138. $result=['code'=>0];
  139. $where=[
  140. 'phone' => $phone,
  141. 'status' => 1,
  142. ];
  143. $smsTime= config('cache.sms_time');
  144. $addTime=($smsTime/60)-1;
  145. $expireTime = date("Y-m-d H:i:s",strtotime("+".$addTime." minute",strtotime(nowTime()))); // 短信有效时间为30分钟。,当前时间+29
  146. $smsData = $this->model->where($where)->where('expire_time','>',$expireTime)->first();
  147. if(!empty($smsData)){
  148. $result['code']=-1;
  149. }
  150. return $result;
  151. }
  152. /**
  153. * 通用缓存方法
  154. * @param string $key
  155. * @param array $data
  156. * */
  157. public function setCacheData($key, $data,$cacheTime=0)
  158. {
  159. $cacheTime=empty($cacheTime)?config('cache.def_time'):$cacheTime;
  160. Cache::put($this->getCacheKey($key), $data, $cacheTime);
  161. }
  162. /**
  163. * 获取通用缓存数据key
  164. * @param string $key
  165. * @return string
  166. * */
  167. private function getCacheKey($key)
  168. {
  169. $prefix='VERIFICATION';
  170. return $this->cacheBucket.$prefix.$key;
  171. }
  172. /**
  173. * 通用缓存方法
  174. * @param string $key
  175. * @param array $data
  176. * */
  177. public function getCacheData($key)
  178. {
  179. $dataKey= $this->getCacheKey($key);
  180. return Cache::get($dataKey);
  181. }
  182. }