CodeService.php 6.1 KB

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