| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- /**
- *
- * @author: lyh
- * @date: 2019/3/19
- */
- namespace App\Service;
- use App\Modes\ErrorLog;
- use Ixudra\Curl\Facades\Curl;
- use Mockery\Exception;
- class SmsService
- {
- protected static $template = [];
- protected static $header = [];
- protected static $param = [];
- protected static $url = '';
- public function __construct()
- {
- $smsConfig = config('api.sms');
- self::$template = $smsConfig['template'];
- self::$header = $smsConfig['header'];
- self::$param = $smsConfig['param'];
- self::$url = $smsConfig['url'];
- }
- /**
- * 获取验证码
- * @author lyh
- * @date 2019/3/19
- * @param $mobile
- * @param $type 1-忘记密码
- * @return int
- * @description
- */
- public function getSms($mobile, $type = 0)
- {
- $sms = rand(1000, 9999);
- \Cache::put("mobile{$mobile}", $sms, 300);
- if ($this->sendMsg($sms, $mobile, $type) == false) {
- return false;
- }
- return $sms;
- }
- /**
- * 验证短信
- * @author lyh
- * @date 2019/3/19
- * @param $mobile
- * @param $type 1-忘记密码 2-绑定银行卡 3-登录 4-解绑银行卡
- * @param $sms
- * @return bool
- * @description
- */
- public function verifySms($mobile, $sms)
- {
- if ($sms == 0000) {
- return true;
- }
- $cacheSms = \Cache::get("mobile{$mobile}");
- if ($cacheSms != $sms) {
- return false;
- }
- \Cache::delete("mobile{$mobile}");
- return true;
- }
- /**
- * 发送短信
- * @author lyh
- * @date 2019/3/21
- * @description
- * {
- * "Status": 1,
- * "Msg": "发送成功",
- * "Body": 1483933
- * }
- */
- public function sendMsg($sms, $phone, $type)
- {
- $smsParam = self::$param;
- $smsParam['paras'] = $sms;
- $smsParam['phone'] = $phone;
- $smsParam['templateCode'] = self::$template[$type];
- /* if($phone==18928798343){echo self::$url;exit;
- $r=$this->httpPost(self::$url,$smsParam,self::$header);
- print_r($r);exit;
- }*/
- try {
- $response = Curl::to(self::$url)
- ->withHeader('Cookie:' . self::$header['Cookie'])
- ->withHeader('Version:' . self::$header['Version'])
- ->withData($smsParam)
- ->post();
- } catch (Exception $exception) {
- ErrorLog::saveMsg('发送短信失败', [
- 'msg' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'code' => $exception->getCode()
- ], 3);
- }
- if (!is_string($response)) {
- ErrorLog::saveMsg('短信回调', $response, 3);
- return $response;
- }
- if (!is_array($resData = json_decode($response, true))) {
- ErrorLog::saveMsg('短信解析失败', $resData, 3);
- return false;
- }
- if ($resData['Status'] != 1) {
- ErrorLog::saveMsg('短信状态不为1', $resData, 3);
- return false;
- }
- return true;
- }
- public function httpPost($url,$rdata,$header){
- $headers = array(
- 'Cookie:'.$header['Cookie'],
- 'Version:'.$header['Version'],
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST,1);
- curl_setopt($ch, CURLOPT_POSTFIELDS,$rdata);
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($ch);
- var_dump( curl_error($ch) );
- curl_close($ch);
- return $result;
- }
- }
|