Sms.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\library\Common;
  4. use app\common\library\Jiyan;
  5. use think\Db;
  6. use think\facade\Validate;
  7. use think\Request;
  8. use AlibabaCloud\Client\AlibabaCloud;
  9. /**
  10. * @title 发送短信
  11. * @controller Sms
  12. */
  13. class Sms extends Base
  14. {
  15. /**
  16. * @title 发送短信
  17. * @desc 发送短信
  18. * @url /api/Sms/send
  19. * @method POST
  20. * @tag 基础
  21. * @header
  22. * @param name:phone type:string require:1 default:-- desc:手机号
  23. * @param name:event type:string require:0 default:register desc:发送类型register:注册forgetpwd:找回密码login:找回密码
  24. */
  25. public function send()
  26. {
  27. $this->error('没有短信支付');
  28. $phone = input("phone");
  29. $event = input("event",'register');
  30. if (!$phone || !Validate::regex($phone, "^1\d{10}$")) {
  31. $this->error('手机号不正确');
  32. }
  33. // $lot_number = input('lot_number');
  34. // $captcha_output = input('captcha_output');
  35. // $pass_token = input('pass_token');
  36. // $gen_time = input('gen_time');
  37. // if (!$lot_number || !$captcha_output || !$pass_token || !$gen_time) $this->error('参数错误');
  38. //
  39. // $jy = new Jiyan();
  40. // $result = $jy->jy($lot_number,$captcha_output,$pass_token,$gen_time,1);
  41. // if ($result['result']=='fail') $this->error('校验失败,请稍后重试');
  42. $last = Db::name('store_sms')->where(['mobile' => $phone, 'event' => $event])
  43. ->order('id', 'DESC')
  44. ->find();
  45. if ($last && time() - $last['createtime']< 60) {
  46. $this->error('发送频繁!');
  47. }
  48. // $ipSendTotal = Db::name('store_sms')->where(['ip' => request()->ip()])->whereTime('createtime', '-1 hours')->count();
  49. // if ($ipSendTotal >= 5) {
  50. // $this->error('发送频繁!');
  51. // }
  52. $member = Db::name('store_member')
  53. ->where('phone',$phone)
  54. ->where('is_deleted',0)
  55. ->count();
  56. switch ($event){
  57. case 'register':
  58. if ($member) $this->error('手机号已注册');
  59. break;
  60. case 'forgetpwd': case 'login':
  61. if (!$member) $this->error('手机号未注册');
  62. break;
  63. }
  64. //发送阿里云短信
  65. $ret = $this->accessKeyClient($event, $phone, mt_rand(100000, 999999));
  66. if ($ret['Code'] === 'OK') {
  67. $this->success('发送成功!');
  68. } elseif ($ret['Code'] === 'isv.BUSINESS_LIMIT_CONTROL') {
  69. $this->error('发送太过频繁!');
  70. } else {
  71. $this->error($ret['msg']);
  72. }
  73. }
  74. function accessKeyClient($event, $mobile, $num)
  75. {
  76. $sett = [
  77. 'ali_accesskey',
  78. 'ali_accesskey_secret',
  79. 'templateCode',
  80. 'sign_name'
  81. ];
  82. $array = getConfig($sett);
  83. $ali_accesskey = $array['ali_accesskey'];
  84. $ali_accesskey_secret = $array['ali_accesskey_secret'];
  85. $templateCode = $array['templateCode'];
  86. AlibabaCloud::accessKeyClient($ali_accesskey, $ali_accesskey_secret)
  87. ->regionId('cn-hangzhou')
  88. ->asDefaultClient();
  89. try {
  90. $result = AlibabaCloud::rpc()
  91. ->product('Dysmsapi')
  92. // ->scheme('https') // https | http
  93. ->version('2017-05-25')
  94. ->action('SendSms')
  95. ->method('POST')
  96. ->host('dysmsapi.aliyuncs.com')
  97. ->options([
  98. 'query' => [
  99. 'PhoneNumbers' => $mobile,
  100. 'SignName' => $array['sign_name'],
  101. 'TemplateCode' => $templateCode,
  102. 'TemplateParam' => '{"code":' . $num . '}',
  103. ],
  104. ])
  105. ->request();
  106. $info = $result->toArray();
  107. if ($info['Code'] == 'OK') {
  108. $ip = request()->ip();
  109. Db::name('store_sms')->insert(['event' => $event, 'mobile' => $mobile, 'createtime'=>time(),'code' => $num, 'ip' => $ip]);
  110. }
  111. return $info;
  112. } catch (ClientException $e) {
  113. echo $e->getErrorMessage() . PHP_EOL;
  114. } catch (ServerException $e) {
  115. echo $e->getErrorMessage() . PHP_EOL;
  116. }
  117. }
  118. }