123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace App\Service;
- use Illuminate\Support\Facades\Cache;
- class CodeService extends Service
- {
- const CACHE_LOGIN = "LoginCode";//登录验证码前缀
- const CACHE_CONFIRM = "ConfirmCode";//确认验证码前缀
- //生成验证码
- public function createCode(){
- $length = 6;
- $characters = '0123456789';
- $code = '';
- $characterLength = strlen($characters);
- for ($i = 0; $i < $length; $i++) {
- $randomIndex = mt_rand(0, $characterLength - 1);
- $code .= $characters[$randomIndex];
- }
- return $code;
- }
- //登录验证码
- public function sendLoginCode($data){
- if(empty($data['account'])) return [false,'请先输入账号!'];
- //键名
- $cacheKey = self::CACHE_LOGIN . $data['account'];
- //限制发送验证码
- if(Cache::has($cacheKey)) return [false,'已发送验证码,请勿重复发送!'];
- //获取验证码
- $code = $this->createCode();
- //发送验证码到手机 TODO
- list($status,$msg) = $this->sendCode($code);
- if(! $status) return [false,$msg];
- //成功后 缓存code 60s
- Cache::add($cacheKey,$code,60);
- return [true,''];
- }
- //确认验证码
- public function sendConfirmCode($data){
- if(empty($data['id'])) return [false,'ID不能为空!'];
- //键名
- $cacheKey = self::CACHE_CONFIRM . $data['id'];
- //限制发送验证码
- if(Cache::has($cacheKey)) return [false,'已发送验证码,请勿重复发送!'];
- //获取验证码
- $code = $this->createCode();
- //获取金额数据
- $finance = (new FinanceService())->getData($data['id']);
- if(empty($finance)) return [false,'未找到出账数据!'];
- $send = [
- "code"=> $code,
- "type"=> 2,
- "account" => $finance['account'],
- "bankAccount" => $finance['finance_account_name'],
- "ifsc" => $finance['ifsc'],
- "amount" => $finance['amount'],
- ];
- $send['sign'] = $this->sign($send);
- //发送验证码到
- list($status,$msg) = $this->sendCode($send);
- if(! $status) return [false, $msg];
- //成功后 缓存code 60s
- Cache::add($cacheKey,$code,60);
- return [true,''];
- }
- //加密
- public function sign($data){
- $str = [];
- sort($data);
- foreach ($data as $k=>$v){
- $str[] = $k.'='.$v;
- }
- $vaild = implode(',',$str).'doTAKtnpiG';
- return md5($vaild);
- }
- //发送验证码
- public function sendCode($send){
- $url = "https://api2.indiacashpayment.in/sendTelegramCode";
- $result = $this->curlURL($url,$send);
- if($result['status'] == 201){
- return [false, $result['msg']];
- }
- return [true,''];
- }
- //验证登录验证码
- public function loginCodeRule($data){
- if(empty($data['account'])) return [false,'账号不能为空!'];
- if(empty($data['code'])) return [false,'验证码不能为空!'];
- $cacheKey = self::CACHE_LOGIN . $data['account'];
- if(Cache::has($cacheKey)){
- $code = Cache::get($cacheKey);
- if($code != $data['code']) return [false,'验证码填写错误!'];
- return [true, ''];
- }
- return [false, '验证码不正确!'];
- }
- //验证确认验证码
- public function ConfirmCodeRule($data){
- if(empty($data['code'])) return [false,'验证码不能为空!'];
- $cacheKey = self::CACHE_CONFIRM . $data['id'];
- if(Cache::has($cacheKey)){
- $code = Cache::get($cacheKey);
- if($code != $data['code']) return [false,'验证码填写错误!'];
- return [true, ''];
- }
- return [false,'验证码不存在!'];
- }
- public function curlURL($url,$data){
- $data = json_encode($data);
- $header[] = "Content-Type:application/json";
- $ch=curl_init($url);
- curl_setopt($ch,CURLOPT_POST,1);
- curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
- curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- $response=curl_exec($ch);
- file_put_contents('charge.txt',$response.PHP_EOL,8);
- $response=json_decode($response,true);
- if(curl_errno($ch) ){
- sc(curl_error($ch));
- }
- return $response;
- }
- //------------------暂时用不到下面------------------------//
- public function sendCodeToWx($data,$token){
- $cacheKey = "code_" . $token;
- if(! $this->isSubmitlimitation($cacheKey)) return [false,'已发送验证码,请勿重复操作!'];
- //生成验证码
- $code = $this->createCode();
- //发送验证码
- list($status,$msg) = $this->sendCode($code);
- if($status){
- Cache::add($cacheKey,$code,1);
- return [true,''];
- }else{
- return [false,$msg];
- }
- }
- //微信公众号发送
- // public function sendCode($code){
- // $serivce = new WxService();
- // list($status,$msg) = $serivce->getToken();
- // if(! $status) return [false,$msg];
- //
- // list($status,$msg) = $serivce->sendToWx($msg,$code);
- // return [$status,$msg];
- // }
- public function isSubmitlimitation($cacheKey){
- if(Cache::has($cacheKey)){
- return false;
- }
- return true;
- }
- }
|