SmsService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2021/2/2
  6. * Time: 14:35
  7. */
  8. namespace app\common\modules\sms;
  9. use app\common\modules\sms\factory\SmsFactory;
  10. use app\common\services\Session;
  11. use app\common\helpers\Cache;
  12. class SmsService
  13. {
  14. //通用(注册)
  15. public function sendCode($mobile, $state = '86',$type = 0)
  16. {
  17. $class = SmsFactory::getSmsFactory($type);
  18. if (get_class($class)) {
  19. return $class->sendCode($mobile, $state);
  20. }
  21. return $this->show_json(0, '发送短信失败,请检查后台短信配置');
  22. }
  23. //找回密码
  24. public function sendPwd($mobile, $state = '86',$type = 0)
  25. {
  26. $class = SmsFactory::getSmsFactory($type);
  27. if (get_class($class)) {
  28. return $class->sendPwd($mobile, $state);
  29. }
  30. return $this->show_json(0, '发送短信失败,请检查后台短信配置');
  31. }
  32. //登录
  33. public function sendLog($mobile, $state = '86',$type = 0)
  34. {
  35. $class = SmsFactory::getSmsFactory($type);
  36. if (get_class($class)) {
  37. return $class->sendLog($mobile, $state);
  38. }
  39. return $this->show_json(0, '发送短信失败,请检查后台短信配置');
  40. }
  41. //余额定时提醒
  42. public function sendBalance($mobile, $etx = [])
  43. {
  44. $class = SmsFactory::getSmsFactory(0);
  45. if (get_class($class)) {
  46. return $class->sendBalance($mobile, $etx);
  47. }
  48. return $this->show_json(0, '发送短信失败,请检查后台短信配置');
  49. }
  50. //商品发货提醒
  51. public function sendGoods($mobile, $etx = [])
  52. {
  53. $class = SmsFactory::getSmsFactory(0);
  54. if (get_class($class)) {
  55. return $class->sendGoods($mobile, $etx);
  56. }
  57. return $this->show_json(0, '发送短信失败,请检查后台短信配置');
  58. }
  59. //会员充值提醒
  60. public function sendMemberRecharge($mobile, $etx = [])
  61. {
  62. $class = SmsFactory::getSmsFactory(0);
  63. if (get_class($class)) {
  64. return $class->sendMemberRecharge($mobile, $etx);
  65. }
  66. return $this->show_json(0, '发送短信失败,请检查后台短信配置');
  67. }
  68. //设置提现校验手机号
  69. public function sendWithdrawSet($mobile, $state = '86',$key='')
  70. {
  71. $class = SmsFactory::getSmsFactory(0);
  72. if (get_class($class)) {
  73. return $class->sendWithdrawSet($mobile, $state,$key);
  74. }
  75. return $this->show_json(0, '发送短信失败,请检查后台短信配置');
  76. }
  77. public function checkCode($mobile, $code,$key = '')
  78. {
  79. //app验证码统一用cache
  80. if (request()->type == 14 || request()->type == 15) {
  81. return $this->checkAppCode($mobile,$code);
  82. }
  83. if ((Session::get('codetime'.$key) + 60 * 5) < time()) {
  84. return $this->show_json(0, '验证码已过期,请重新获取');
  85. }
  86. if (Session::get('code_mobile'.$key) != $mobile) {
  87. return $this->show_json(0, '手机号错误,请重新获取');
  88. }
  89. //增加次数验证
  90. if (Cache::get('code_num_'.$mobile) >= 5) {
  91. return $this->show_json(0, '验证码错误次数过多,请重新获取');
  92. }
  93. if (Session::get('code'.$key) != $code) {
  94. Cache::increment('code_num_'.$mobile);
  95. return $this->show_json(0, '验证码错误,请重新获取');
  96. }
  97. return $this->show_json(1);
  98. }
  99. public function checkAppCode($mobile, $code)
  100. {
  101. $key = 'app_login_'. $mobile;
  102. if (!Cache::has($key)) {
  103. return $this->show_json('0','验证码已过期,请重新获取');
  104. }
  105. $value = Cache::get($key);
  106. //增加次数验证
  107. if (Cache::get('code_num_'.$mobile) >= 5) {
  108. return $this->show_json(0, '验证码错误次数过多,请重新获取');
  109. }
  110. if ($code != $value) {
  111. Cache::increment('code_num_'.$mobile);
  112. return $this->show_json('0','验证失败,请重新获取验证码');
  113. }
  114. Cache::forget($key);
  115. return $this->show_json('1');
  116. }
  117. protected function show_json($status = 1, $return = null)
  118. {
  119. return array(
  120. 'status' => $status,
  121. 'json' => $return,
  122. );
  123. }
  124. }