VipShortMsgService.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2020/7/16
  6. * Time: 18:23
  7. */
  8. namespace app\common\service;
  9. use app\common\constants\ShortMsgConstants;
  10. use app\common\library\Redis;
  11. use think\Config;
  12. class VipShortMsgService
  13. {
  14. protected static $instance;
  15. public static function instance(){
  16. if (is_null(self::$instance)) {
  17. self::$instance = new static();
  18. }
  19. return self::$instance;
  20. }
  21. /**
  22. * 登录账号异常后,短信通知管理员
  23. * @param string $throw_msg 登录异常的推送信息
  24. */
  25. public function throwNotice($throw_msg)
  26. {
  27. // 1.获取管理员手机号
  28. $mobiles = explode(',', str_replace(',', ',', trim(Config::get('site.throw_mobiles'))));
  29. // 2.遍历手机号,推送告警信息
  30. if ($mobiles) {
  31. foreach ($mobiles as $k => $mobile) {
  32. if ($mobile && strlen($mobile) == 11) {
  33. ShortMessageService::instance()->singleMsg($throw_msg, $mobile);
  34. LogService::info("ThrowNotice:发送内容:" . $throw_msg . ":手机号:" . $mobile);
  35. }
  36. }
  37. }
  38. }
  39. /**
  40. * 发送短信验证码
  41. * @param $phone 手机号
  42. * @return int
  43. */
  44. public function sendShortMsg($phone)
  45. {
  46. $code = ShortMessageService::instance()->generateSMSCode();
  47. $content = "您的验证码为{$code},五分钟内有效,切勿将验证码泄露与他人。如有问题请联系客服!";
  48. try {
  49. $data = ShortMessageService::instance()->singleShortMsg($content, $phone);
  50. if ($data['code']) {
  51. return $code;
  52. } else {
  53. LogService::error('VipShortMsgService:' . $data['code']);
  54. return false;
  55. }
  56. } catch (\Exception $e) {
  57. LogService::error($e->getMessage());
  58. return false;
  59. }
  60. }
  61. }