CodeService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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,$msg];
  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. //获取金额数据
  46. $finance = (new FinanceService())->getData($data['id']);
  47. if(empty($finance)) return [false,'未找到出账数据!'];
  48. $send = [
  49. "code"=> $code,
  50. "type"=> 2,
  51. "account" => $finance['account'],
  52. "bankAccount" => $finance['finance_account_name'],
  53. "ifsc" => $finance['ifsc'],
  54. "amount" => $finance['amount'],
  55. ];
  56. $send['sign'] = $this->sign($send);
  57. //发送验证码到
  58. list($status,$msg) = $this->sendCode($send);
  59. if(! $status) return [false, $msg];
  60. //成功后 缓存code 60s
  61. Cache::add($cacheKey,$code,60);
  62. return [true,''];
  63. }
  64. //加密
  65. public function sign($data){
  66. $str = [];
  67. sort($data);
  68. foreach ($data as $k=>$v){
  69. $str[] = $k.'='.$v;
  70. }
  71. $vaild = implode(',',$str).'doTAKtnpiG';
  72. return md5($vaild);
  73. }
  74. //发送验证码
  75. public function sendCode($send){
  76. $url = "https://api2.indiacashpayment.in/sendTelegramCode";
  77. $result = $this->curlURL($url,$send);
  78. if($result['status'] == 201){
  79. return [false, $result['msg']];
  80. }
  81. return [true,''];
  82. }
  83. //验证登录验证码
  84. public function loginCodeRule($data){
  85. if(empty($data['account'])) return [false,'账号不能为空!'];
  86. if(empty($data['code'])) return [false,'验证码不能为空!'];
  87. $cacheKey = self::CACHE_LOGIN . $data['account'];
  88. if(Cache::has($cacheKey)){
  89. $code = Cache::get($cacheKey);
  90. if($code != $data['code']) return [false,'验证码填写错误!'];
  91. return [true, ''];
  92. }
  93. return [false, '验证码不正确!'];
  94. }
  95. //验证确认验证码
  96. public function ConfirmCodeRule($data){
  97. if(empty($data['code'])) return [false,'验证码不能为空!'];
  98. $cacheKey = self::CACHE_CONFIRM . $data['id'];
  99. if(Cache::has($cacheKey)){
  100. $code = Cache::get($cacheKey);
  101. if($code != $data['code']) return [false,'验证码填写错误!'];
  102. return [true, ''];
  103. }
  104. return [false,'验证码不存在!'];
  105. }
  106. public function curlURL($url,$data){
  107. $data = json_encode($data);
  108. $header[] = "Content-Type:application/json";
  109. $ch=curl_init($url);
  110. curl_setopt($ch,CURLOPT_POST,1);
  111. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  112. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  113. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  114. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  115. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  116. $response=curl_exec($ch);
  117. file_put_contents('charge.txt',$response.PHP_EOL,8);
  118. $response=json_decode($response,true);
  119. if(curl_errno($ch) ){
  120. sc(curl_error($ch));
  121. }
  122. return $response;
  123. }
  124. //------------------暂时用不到下面------------------------//
  125. public function sendCodeToWx($data,$token){
  126. $cacheKey = "code_" . $token;
  127. if(! $this->isSubmitlimitation($cacheKey)) return [false,'已发送验证码,请勿重复操作!'];
  128. //生成验证码
  129. $code = $this->createCode();
  130. //发送验证码
  131. list($status,$msg) = $this->sendCode($code);
  132. if($status){
  133. Cache::add($cacheKey,$code,1);
  134. return [true,''];
  135. }else{
  136. return [false,$msg];
  137. }
  138. }
  139. //微信公众号发送
  140. // public function sendCode($code){
  141. // $serivce = new WxService();
  142. // list($status,$msg) = $serivce->getToken();
  143. // if(! $status) return [false,$msg];
  144. //
  145. // list($status,$msg) = $serivce->sendToWx($msg,$code);
  146. // return [$status,$msg];
  147. // }
  148. public function isSubmitlimitation($cacheKey){
  149. if(Cache::has($cacheKey)){
  150. return false;
  151. }
  152. return true;
  153. }
  154. }