Sms.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $phone = input("phone");
  28. $event = input("event",'register');
  29. if (!$phone || !Validate::regex($phone, "^1\d{10}$")) {
  30. $this->error('手机号不正确');
  31. }
  32. // $lot_number = input('lot_number');
  33. // $captcha_output = input('captcha_output');
  34. // $pass_token = input('pass_token');
  35. // $gen_time = input('gen_time');
  36. // if (!$lot_number || !$captcha_output || !$pass_token || !$gen_time) $this->error('参数错误');
  37. //
  38. // $jy = new Jiyan();
  39. // $result = $jy->jy($lot_number,$captcha_output,$pass_token,$gen_time,1);
  40. // if ($result['result']=='fail') $this->error('校验失败,请稍后重试');
  41. $last = Db::name('store_sms')->where(['mobile' => $phone, 'event' => $event])
  42. ->order('id', 'DESC')
  43. ->find();
  44. if ($last && (time() - $last['createtime'])< 60) {
  45. $this->error('发送频繁!');
  46. }
  47. // $ipSendTotal = Db::name('store_sms')->where(['ip' => request()->ip()])->whereTime('createtime', '-1 hours')->count();
  48. // if ($ipSendTotal >= 5) {
  49. // $this->error('发送频繁!');
  50. // }
  51. $member = Db::name('store_member')
  52. ->where('phone',$phone)
  53. ->where('is_deleted',0)
  54. ->count();
  55. switch ($event){
  56. case 'register':
  57. if ($member) $this->error('手机号已注册');
  58. break;
  59. case 'forgetpwd': case 'login':
  60. if (!$member) $this->error('手机号未注册');
  61. break;
  62. }
  63. //发送阿里云短信
  64. $ret = $this->accessKeyClient($event, $phone, mt_rand(100000, 999999));
  65. if ($ret['Code'] === 'OK') {
  66. $this->success('发送成功!');
  67. } elseif ($ret['Code'] === 'isv.BUSINESS_LIMIT_CONTROL') {
  68. $this->error('发送太过频繁!');
  69. } else {
  70. $this->error($ret['msg']);
  71. }
  72. }
  73. function accessKeyClient($event, $mobile, $num)
  74. {
  75. $sett = [
  76. 'ali_accesskey',
  77. 'ali_accesskey_secret',
  78. 'templateCode',
  79. 'sign_name'
  80. ];
  81. $array = getConfig($sett);
  82. $ali_accesskey = $array['ali_accesskey'];
  83. $ali_accesskey_secret = $array['ali_accesskey_secret'];
  84. $templateCode = $array['templateCode'];
  85. AlibabaCloud::accessKeyClient($ali_accesskey, $ali_accesskey_secret)
  86. ->regionId('cn-hangzhou')
  87. ->asDefaultClient();
  88. try {
  89. $result = AlibabaCloud::rpc()
  90. ->product('Dysmsapi')
  91. // ->scheme('https') // https | http
  92. ->version('2017-05-25')
  93. ->action('SendSms')
  94. ->method('POST')
  95. ->host('dysmsapi.aliyuncs.com')
  96. ->options([
  97. 'query' => [
  98. 'PhoneNumbers' => $mobile,
  99. 'SignName' => $array['sign_name'],
  100. 'TemplateCode' => $templateCode,
  101. 'TemplateParam' => '{"code":' . $num . '}',
  102. ],
  103. ])
  104. ->request();
  105. $info = $result->toArray();
  106. if ($info['Code'] == 'OK') {
  107. $ip = request()->ip();
  108. Db::name('store_sms')->insert(['event' => $event, 'mobile' => $mobile, 'createtime'=>time(),'code' => $num, 'ip' => $ip]);
  109. }
  110. return $info;
  111. } catch (ClientException $e) {
  112. echo $e->getErrorMessage() . PHP_EOL;
  113. } catch (ServerException $e) {
  114. echo $e->getErrorMessage() . PHP_EOL;
  115. }
  116. }
  117. }