SmsService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. *
  4. * @author: lyh
  5. * @date: 2019/3/19
  6. */
  7. namespace App\Service;
  8. use App\Modes\ErrorLog;
  9. use Ixudra\Curl\Facades\Curl;
  10. use Mockery\Exception;
  11. class SmsService
  12. {
  13. protected static $template = [];
  14. protected static $header = [];
  15. protected static $param = [];
  16. protected static $url = '';
  17. public function __construct()
  18. {
  19. $smsConfig = config('api.sms');
  20. self::$template = $smsConfig['template'];
  21. self::$header = $smsConfig['header'];
  22. self::$param = $smsConfig['param'];
  23. self::$url = $smsConfig['url'];
  24. }
  25. /**
  26. * 获取验证码
  27. * @author lyh
  28. * @date 2019/3/19
  29. * @param $mobile
  30. * @param $type 1-忘记密码
  31. * @return int
  32. * @description
  33. */
  34. public function getSms($mobile, $type = 0)
  35. {
  36. $sms = rand(1000, 9999);
  37. \Cache::put("mobile{$mobile}", $sms, 300);
  38. if ($this->sendMsg($sms, $mobile, $type) == false) {
  39. return false;
  40. }
  41. return $sms;
  42. }
  43. /**
  44. * 验证短信
  45. * @author lyh
  46. * @date 2019/3/19
  47. * @param $mobile
  48. * @param $type 1-忘记密码 2-绑定银行卡 3-登录 4-解绑银行卡
  49. * @param $sms
  50. * @return bool
  51. * @description
  52. */
  53. public function verifySms($mobile, $sms)
  54. {
  55. if ($sms == 0000) {
  56. return true;
  57. }
  58. $cacheSms = \Cache::get("mobile{$mobile}");
  59. if ($cacheSms != $sms) {
  60. return false;
  61. }
  62. \Cache::delete("mobile{$mobile}");
  63. return true;
  64. }
  65. /**
  66. * 发送短信
  67. * @author lyh
  68. * @date 2019/3/21
  69. * @description
  70. * {
  71. * "Status": 1,
  72. * "Msg": "发送成功",
  73. * "Body": 1483933
  74. * }
  75. */
  76. public function sendMsg($sms, $phone, $type)
  77. {
  78. $smsParam = self::$param;
  79. $smsParam['paras'] = $sms;
  80. $smsParam['phone'] = $phone;
  81. $smsParam['templateCode'] = self::$template[$type];
  82. /* if($phone==18928798343){echo self::$url;exit;
  83. $r=$this->httpPost(self::$url,$smsParam,self::$header);
  84. print_r($r);exit;
  85. }*/
  86. try {
  87. $response = Curl::to(self::$url)
  88. ->withHeader('Cookie:' . self::$header['Cookie'])
  89. ->withHeader('Version:' . self::$header['Version'])
  90. ->withData($smsParam)
  91. ->post();
  92. } catch (Exception $exception) {
  93. ErrorLog::saveMsg('发送短信失败', [
  94. 'msg' => $exception->getMessage(),
  95. 'file' => $exception->getFile(),
  96. 'code' => $exception->getCode()
  97. ], 3);
  98. }
  99. if (!is_string($response)) {
  100. ErrorLog::saveMsg('短信回调', $response, 3);
  101. return $response;
  102. }
  103. if (!is_array($resData = json_decode($response, true))) {
  104. ErrorLog::saveMsg('短信解析失败', $resData, 3);
  105. return false;
  106. }
  107. if ($resData['Status'] != 1) {
  108. ErrorLog::saveMsg('短信状态不为1', $resData, 3);
  109. return false;
  110. }
  111. return true;
  112. }
  113. public function httpPost($url,$rdata,$header){
  114. $headers = array(
  115. 'Cookie:'.$header['Cookie'],
  116. 'Version:'.$header['Version'],
  117. );
  118. $ch = curl_init();
  119. curl_setopt($ch, CURLOPT_POST,1);
  120. curl_setopt($ch, CURLOPT_POSTFIELDS,$rdata);
  121. curl_setopt($ch, CURLOPT_URL,$url);
  122. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  123. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  124. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  125. $result = curl_exec($ch);
  126. var_dump( curl_error($ch) );
  127. curl_close($ch);
  128. return $result;
  129. }
  130. }