CodeService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Cache;
  4. class CodeService extends Service
  5. {
  6. const CACHE_LOGIN = "LoginCode";//登录验证码前缀
  7. const CACHE_CONFIRM = "ConfirmCode";//确认验证码前缀
  8. //生成验证码
  9. public function createCode(){
  10. $length = 6;
  11. $characters = '0123456789';
  12. $code = '';
  13. $characterLength = strlen($characters);
  14. for ($i = 0; $i < $length; $i++) {
  15. $randomIndex = mt_rand(0, $characterLength - 1);
  16. $code .= $characters[$randomIndex];
  17. }
  18. return $code;
  19. }
  20. //登录验证码
  21. public function sendLoginCode($data){
  22. if(empty($data['account'])) return [false,'请先输入账号!'];
  23. //键名
  24. $cacheKey = self::CACHE_LOGIN . $data['account'];
  25. //限制发送验证码
  26. if(Cache::has($cacheKey)) return [false,'已发送验证码,请勿重复发送!'];
  27. //获取验证码
  28. $code = $this->createCode();
  29. //发送验证码到手机 TODO
  30. list($status,$msg) = $this->sendCode($code);
  31. if(! $status) return [false,'发送验证码失败!'];
  32. //成功后 缓存code 60s
  33. Cache::add($cacheKey,$code,60);
  34. return [true,''];
  35. }
  36. //确认验证码
  37. public function sendConfirmCode($data){
  38. if(empty($data['id'])) return [false,'ID不能为空!'];
  39. //键名
  40. $cacheKey = self::CACHE_CONFIRM . $data['id'];
  41. //限制发送验证码
  42. if(Cache::has($cacheKey)) return [false,'已发送验证码,请勿重复发送!'];
  43. //获取验证码
  44. $code = $this->createCode();
  45. //发送验证码到手机 TODO
  46. list($status,$msg) = $this->sendCode($code);
  47. if(! $status) return [false,'发送验证码失败!'];
  48. //成功后 缓存code 60s
  49. Cache::add($cacheKey,$code,60);
  50. return [true,''];
  51. }
  52. //发送验证码 TODO
  53. public function sendCode($code){
  54. return [true,''];
  55. }
  56. //验证登录验证码
  57. public function loginCodeRule($data){
  58. if(empty($data['account'])) return [false,'账号不能为空!'];
  59. if(empty($data['code'])) return [false,'验证码不能为空!'];
  60. $cacheKey = self::CACHE_LOGIN . $data['account'];
  61. if(Cache::has($cacheKey)){
  62. $code = Cache::get($cacheKey);
  63. if($code != $data['code']) return [false,'验证码填写错误!'];
  64. return [true, ''];
  65. }
  66. return [false, '验证码不正确!'];
  67. }
  68. //验证确认验证码
  69. public function ConfirmCodeRule($data){
  70. if(empty($data['code'])) return [false,'验证码不能为空!'];
  71. $cacheKey = self::CACHE_CONFIRM . $data['id'];
  72. if(Cache::has($cacheKey)){
  73. $code = Cache::get($cacheKey);
  74. if($code != $data['code']) return [false,'验证码填写错误!'];
  75. return [true, ''];
  76. }
  77. return [false,'验证码不存在!'];
  78. }
  79. //------------------暂时用不到下面------------------------//
  80. public function sendCodeToWx($data,$token){
  81. $cacheKey = "code_" . $token;
  82. if(! $this->isSubmitlimitation($cacheKey)) return [false,'已发送验证码,请勿重复操作!'];
  83. //生成验证码
  84. $code = $this->createCode();
  85. //发送验证码
  86. list($status,$msg) = $this->sendCode($code);
  87. if($status){
  88. Cache::add($cacheKey,$code,1);
  89. return [true,''];
  90. }else{
  91. return [false,$msg];
  92. }
  93. }
  94. //微信公众号发送
  95. // public function sendCode($code){
  96. // $serivce = new WxService();
  97. // list($status,$msg) = $serivce->getToken();
  98. // if(! $status) return [false,$msg];
  99. //
  100. // list($status,$msg) = $serivce->sendToWx($msg,$code);
  101. // return [$status,$msg];
  102. // }
  103. public function isSubmitlimitation($cacheKey){
  104. if(Cache::has($cacheKey)){
  105. return false;
  106. }
  107. return true;
  108. }
  109. }