CodeService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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'] = $this->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['account'],
  59. "bankAccount" => $finance['finance_account_name'],
  60. "ifsc" => $finance['ifsc'],
  61. "amount" => $finance['amount'],
  62. "ip" => $this->getIP()
  63. ];
  64. $send['sign'] = $this->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 sign($data){
  74. $str = [];
  75. sort($data);
  76. foreach ($data as $k=>$v){
  77. $str[] = $k.'='.$v;
  78. }
  79. $vaild = implode(',',$str).'doTAKtnpiG';
  80. return md5($vaild);
  81. }
  82. //发送验证码
  83. public function sendCode($send){
  84. $url = "https://api2.indiacashpayment.in/sendTelegramCode";
  85. $result = $this->curlURL($url,$send);
  86. if($result['status'] == 201){
  87. return [false, $result['msg']];
  88. }
  89. return [true,''];
  90. }
  91. //验证登录验证码
  92. public function loginCodeRule($data){
  93. if(empty($data['account'])) return [false,'账号不能为空!'];
  94. if(empty($data['code'])) return [false,'验证码不能为空!'];
  95. $cacheKey = self::CACHE_LOGIN . $data['account'];
  96. if(Cache::has($cacheKey)){
  97. $code = Cache::get($cacheKey);
  98. if($code != $data['code']) return [false,'验证码填写错误!'];
  99. return [true, ''];
  100. }
  101. return [false, '验证码不正确!'];
  102. }
  103. //验证确认验证码
  104. public function ConfirmCodeRule($data){
  105. if(empty($data['code'])) return [false,'验证码不能为空!'];
  106. $cacheKey = self::CACHE_CONFIRM . $data['id'];
  107. if(Cache::has($cacheKey)){
  108. $code = Cache::get($cacheKey);
  109. if($code != $data['code']) return [false,'验证码填写错误!'];
  110. return [true, ''];
  111. }
  112. return [false,'验证码不存在!'];
  113. }
  114. public function curlURL($url,$data){
  115. $data = json_encode($data);
  116. $header[] = "Content-Type:application/json";
  117. $ch=curl_init($url);
  118. curl_setopt($ch,CURLOPT_POST,1);
  119. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  120. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  121. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  122. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  123. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  124. $response=curl_exec($ch);
  125. file_put_contents('charge.txt',$response.PHP_EOL,8);
  126. $response=json_decode($response,true);
  127. if(curl_errno($ch) ){
  128. sc(curl_error($ch));
  129. }
  130. return $response;
  131. }
  132. public function getIP(){
  133. if (getenv('HTTP_CLIENT_IP')) {
  134. $ip = getenv('HTTP_CLIENT_IP');
  135. }
  136. elseif (getenv('HTTP_X_REAL_IP')) {
  137. $ip = getenv('HTTP_X_REAL_IP');
  138. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  139. $ip = getenv('HTTP_X_FORWARDED_FOR');
  140. $ips = explode(',', $ip);
  141. $ip = $ips[0];
  142. } elseif (getenv('REMOTE_ADDR')) {
  143. $ip = getenv('REMOTE_ADDR');
  144. } else {
  145. $ip = '0.0.0.0';
  146. }
  147. return $ip;
  148. }
  149. //------------------暂时用不到下面------------------------//
  150. public function sendCodeToWx($data,$token){
  151. $cacheKey = "code_" . $token;
  152. if(! $this->isSubmitlimitation($cacheKey)) return [false,'已发送验证码,请勿重复操作!'];
  153. //生成验证码
  154. $code = $this->createCode();
  155. //发送验证码
  156. list($status,$msg) = $this->sendCode($code);
  157. if($status){
  158. Cache::add($cacheKey,$code,1);
  159. return [true,''];
  160. }else{
  161. return [false,$msg];
  162. }
  163. }
  164. //微信公众号发送
  165. // public function sendCode($code){
  166. // $serivce = new WxService();
  167. // list($status,$msg) = $serivce->getToken();
  168. // if(! $status) return [false,$msg];
  169. //
  170. // list($status,$msg) = $serivce->sendToWx($msg,$code);
  171. // return [$status,$msg];
  172. // }
  173. public function isSubmitlimitation($cacheKey){
  174. if(Cache::has($cacheKey)){
  175. return false;
  176. }
  177. return true;
  178. }
  179. }