SmsController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\admin\controller\sms;
  3. use app\model\Member;
  4. use app\model\MemberQuota;
  5. use Overtrue\EasySms\EasySms;
  6. use support\Redis;
  7. use support\Request;
  8. use Hhink\WebmanSms\Sms;
  9. class SmsController
  10. {
  11. const KEY_PREFIX = 'SMS:CODE:';
  12. public function sendSmsCodeByQuota(Request $request)
  13. {
  14. $quotaIds = $request->post('quota_id', []);
  15. if (!$quotaIds) {
  16. return json_fail("参数异常");
  17. }
  18. $quotas = MemberQuota::whereIn('member_quota_id', $quotaIds)->get()->toArray();
  19. $memberIds = array_column($quotas, 'join_quota_member_id');
  20. if (count(array_unique($memberIds)) > 1) {
  21. return json_fail("不可以同时核销多个会员权益");
  22. }
  23. try {
  24. $member = Member::find(current($memberIds));
  25. $mobile = $member->member_mobile;
  26. $code = random_string(6, 'number');
  27. $key = self::KEY_PREFIX . 'QUOTA:' . $mobile;
  28. Redis::set($key, $code);
  29. Redis::expire($key, 600);
  30. // Sms::app()->send(15910622969, [
  31. // 'template' => 'SMS_173171292',
  32. // 'data' => [
  33. // 'code' => $code
  34. // ],
  35. // ]);
  36. $config = [
  37. // HTTP 请求的超时时间(秒)
  38. 'timeout' => 5.0,
  39. // 默认发送配置
  40. 'default' => [
  41. // 网关调用策略,默认:顺序调用
  42. 'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
  43. // 默认可用的发送网关
  44. 'gateways' => [
  45. 'aliyun',
  46. ],
  47. ],
  48. // 可用的网关配置
  49. 'gateways' => [
  50. 'errorlog' => [
  51. 'file' => '/tmp/easy-sms.log',
  52. ],
  53. 'aliyun' => [
  54. 'access_key_id' => 'LTAI4Ff7ZPSG4XWj7pEhMQBB',
  55. 'access_key_secret' => 'kmXJO9JH33XDstQxEBAmr08bjhsN19',
  56. 'sign_name' => '',
  57. ],
  58. //...
  59. ],
  60. ];
  61. $easySms = new EasySms($config);
  62. $easySms->send(15910622969, [
  63. 'content' => '验证码'.$code.',您正在登录,若非本人操作,请勿泄露。',
  64. 'template' => 'SMS_173171292',
  65. 'data' => [
  66. 'code' => $code
  67. ],
  68. ]);
  69. } catch (\Exception $e) {
  70. dump($e->getTrace());
  71. return json_fail("短信发送失败,请稍后重试");
  72. }
  73. return json_success('success');
  74. }
  75. }