Sms.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. class Sms extends BaseController
  6. {
  7. protected $users = [
  8. [
  9. 'keyID' => '123456',
  10. 'keySecret' => '',
  11. ]
  12. ];
  13. /**
  14. * $templateId
  15. * $param
  16. * $phone_number
  17. * countryID
  18. */
  19. function send()
  20. {
  21. $headerAuthorization = I('server.HTTP_AUTHORIZATION');
  22. $headerAuthorization = str_replace('"', '', $headerAuthorization);
  23. $headerAuthorization = explode(',', $headerAuthorization);
  24. $headerAuthorization = array_map(function ($v) {
  25. return trim($v);
  26. }, $headerAuthorization);
  27. parse_str(implode('&', $headerAuthorization), $headerAuthorization);
  28. $postDta = file_get_contents('php://input');
  29. $postDta = json_decode($postDta, true);
  30. $postNonce = $headerAuthorization['nonce'];
  31. $postTime = $headerAuthorization['time'];
  32. $postKeyID = $headerAuthorization['keyID'];
  33. $postSign = $headerAuthorization['sign'];
  34. $dbUserInfo = Db::name('user')->where(['key_id' => $headerAuthorization['keyID']])->find();
  35. $param = $postDta['paramValues'];
  36. $dbUserInfo = [];
  37. $dbUserInfo['key_id'] = "123456";
  38. $dbUserInfo['key_secret'] = "12345678";
  39. $ret = [];
  40. $sign = $this->makeSign($postNonce, $postTime, $dbUserInfo['key_secret']);
  41. if ($sign != $postSign) {
  42. $templateId = I('template_id');
  43. $templateId = "sms_123456";
  44. $phoneNumber = I('phoneNumber'); //8615812345678
  45. $templateContent = '短信内容 {$content} [test]';
  46. $smsContent = str_replace('{$user}', $param['user'], $templateContent);
  47. $countryID = I('countryID');
  48. $countryID = 'CN';
  49. $url = "https://www.huaweiita.com:17131/apiaccess/sms/sendTemplateSms/v1";
  50. $params['smsTemplateId'] = '695db7b5-6d4f-4791-aea0-f733f2ef06ce';
  51. $params['paramValues'] = $param; // paramValues:{"user":"XXX"}
  52. $params['msisdn'] = $phoneNumber;
  53. $params['countryID'] = $countryID;
  54. $params['operatorCode'] = $postDta['operatorCode'];
  55. $retCurl = curl_post_json($url, json_encode($params));
  56. $retCurl = json_decode($retCurl,true); //原始信息入库
  57. if($retCurl['code'] == '0000000'){
  58. $ret['code'] = 1;
  59. $ret['msg'] = $retCurl['description'];
  60. }else{
  61. $ret['code'] = 0;
  62. $ret['msg'] = $retCurl['description'];
  63. $ret['error_code'] = $retCurl['code'];
  64. $ret['error_msg'] = $retCurl['exErrorMessage'];
  65. //记录原始错误消息入库
  66. }
  67. //var_dump($ret);exit('sb');
  68. } else {
  69. $ret = [];
  70. $ret['code'] = 0;
  71. $ret['msg'] = 'authentication failure';
  72. //return $ret;
  73. //echo json_encode($ret);
  74. // exit;
  75. }
  76. return $ret;
  77. //$sign = "";
  78. }
  79. function makeSign($nonce, $time, $key)
  80. {
  81. $signRawString = $nonce . $time . $key;
  82. return base64_encode(hash('sha256', $signRawString));
  83. }
  84. /**
  85. *
  86. * 检测签名
  87. */
  88. public function CheckSign()
  89. {
  90. $sign = $this->MakeSign();
  91. if ($this->GetSign() == $sign) {
  92. return true;
  93. }
  94. throw new WxPayException("签名错误!");
  95. }
  96. function sendToH3c()
  97. {
  98. $url = "https://www.huaweiita.com:17131/apiaccess/sms/sendTemplateSms/v1";
  99. $params = [];
  100. $params['smsTemplateId'] = '695db7b5-6d4f-4791-aea0-f733f2ef06ce';
  101. $paramValues = [];
  102. $paramValues['user'] = 'test1';
  103. $params['paramValues'] = $paramValues; // paramValues:{"user":"XXX"}
  104. $params['msisdn'] = '8615821868265';
  105. $params['countryID'] = 'CN';
  106. $ret = curl_post_json($url, json_encode($params));
  107. }
  108. }