Sms.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2021/2/2
  6. * Time: 14:09
  7. */
  8. namespace app\common\modules\sms;
  9. use app\common\services\Session;
  10. use app\frontend\modules\member\models\smsSendLimitModel;
  11. use app\common\helpers\Cache;
  12. abstract class Sms
  13. {
  14. public $sms;
  15. private $smsDeadLine = 1000;//说改成不限制,(保留限制功能)
  16. public $template; //发送模板
  17. public $key = '';
  18. public function __construct($sms)
  19. {
  20. $this->sms = $sms;
  21. }
  22. //通用(注册)
  23. public function sendCode($mobile, $state = '86')
  24. {
  25. $this->template = 'register';
  26. if ($this->smsSendLimit($mobile)) {
  27. $res = $this->_sendCode($mobile, $state);
  28. if ($res === true) {
  29. $this->updateSmsSendTotal($mobile);
  30. return $this->show_json(1);
  31. } else {
  32. return $this->show_json(0, $res);
  33. }
  34. } else {
  35. return $this->show_json(0, '发送短信数量达到今日上限');
  36. }
  37. }
  38. //登录
  39. public function sendLog($mobile, $state = '86')
  40. {
  41. $this->template = 'login';
  42. $res = $this->_sendCode($mobile, $state);
  43. if ( $res === true) {
  44. return $this->show_json(1);
  45. } else {
  46. return $this->show_json(0, $res);
  47. }
  48. }
  49. //找回密码
  50. public function sendPwd($mobile ,$state)
  51. {
  52. $this->template = 'password';
  53. $res = $this->_sendCode($mobile, $state);
  54. if ( $res === true) {
  55. return $this->show_json(1);
  56. } else {
  57. return $this->show_json(0, $res);
  58. }
  59. }
  60. //余额定时提醒
  61. public function sendBalance($mobile, $ext)
  62. {
  63. $this->template = 'balance';
  64. $res = $this->_sendCode($mobile, '86',$ext);
  65. if ( $res === true) {
  66. return $this->show_json(1);
  67. } else {
  68. return $this->show_json(0, $res);
  69. }
  70. }
  71. //商品发货提醒
  72. public function sendGoods($mobile, $ext)
  73. {
  74. $this->template = 'goods';
  75. $res = $this->_sendCode($mobile, '86',$ext);
  76. if ( $res === true) {
  77. return $this->show_json(1);
  78. } else {
  79. return $this->show_json(0, $res);
  80. }
  81. }
  82. //会员充值提醒
  83. public function sendMemberRecharge($mobile, $ext)
  84. {
  85. $this->template = 'member_recharge';
  86. $res = $this->_sendCode($mobile, '86',$ext);
  87. if ( $res === true) {
  88. return $this->show_json(1);
  89. } else {
  90. return $this->show_json(0, $res);
  91. }
  92. }
  93. public function sendWithdrawSet($mobile, $state = '86',$key='')
  94. {
  95. $this->key = $key;
  96. $this->template = 'withdraw_set';
  97. if ($this->smsSendLimit($mobile)) {
  98. $res = $this->_sendCode($mobile, $state);
  99. if ( $res === true) {
  100. $this->updateSmsSendTotal($mobile);
  101. return $this->show_json(1);
  102. } else {
  103. return $this->show_json(0, $res);
  104. }
  105. } else {
  106. return $this->show_json(0, '发送短信数量达到今日上限');
  107. }
  108. }
  109. /**
  110. * @param $mobile
  111. * @param $state
  112. * @param null $ext
  113. * @return string|bool
  114. */
  115. abstract function _sendCode($mobile, $state, $ext = null);
  116. /**
  117. * 更新发送短信条数
  118. * 每天最多5条
  119. * @param $mobile
  120. */
  121. protected function updateSmsSendTotal($mobile)
  122. {
  123. $uniacid = \Yunshop::app()->uniacid;
  124. $curr_time = time();
  125. $mobile_info = smsSendLimitModel::getMobileInfo($uniacid, $mobile);
  126. if (!empty($mobile_info)) {
  127. $update_time = $mobile_info['created_at'];
  128. $total = $mobile_info['total'];
  129. if ($update_time <= $curr_time) {
  130. if (date('Ymd', $curr_time) == date('Ymd', $update_time)) {
  131. if ($total <= 5) {
  132. ++$total;
  133. smsSendLimitModel::updateData(array(
  134. 'uniacid' => $uniacid,
  135. 'mobile' => $mobile), array(
  136. 'total' => $total,
  137. 'created_at' => $curr_time));
  138. }
  139. } else {
  140. smsSendLimitModel::updateData(array(
  141. 'uniacid' => $uniacid,
  142. 'mobile' => $mobile), array(
  143. 'total' => 1,
  144. 'created_at' => $curr_time));
  145. }
  146. }
  147. } else {
  148. smsSendLimitModel::insertData(array(
  149. 'uniacid' => $uniacid,
  150. 'mobile' => $mobile,
  151. 'total' => 1,
  152. 'created_at' => $curr_time)
  153. );
  154. }
  155. }
  156. protected function getCode($mobile ,$key = '')
  157. {
  158. $code = rand(1000, 9999);
  159. Session::set('codetime'.$key, time());
  160. Session::set('code'.$key, $code);
  161. Session::set('code_mobile'.$key, $mobile);
  162. Cache::put('app_login_'.$mobile, $code, 5);
  163. Cache::forget('code_num_'.$mobile);
  164. return $code;
  165. }
  166. protected function smsSendLimit($mobile)
  167. {
  168. $uniacid = \Yunshop::app()->uniacid;
  169. $curr_time = time();
  170. $mobile_info = smsSendLimitModel::getMobileInfo($uniacid, $mobile);
  171. if (!empty($mobile_info)) {
  172. $update_time = $mobile_info['created_at'];
  173. $total = $mobile_info['total'];
  174. if ((date('Ymd', $curr_time) != date('Ymd', $update_time))) {
  175. $total = 0;
  176. }
  177. } else {
  178. $total = 0;
  179. }
  180. if ($total < $this->smsDeadLine) {
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. }
  186. protected function show_json($status = 1, $return = null)
  187. {
  188. return array(
  189. 'status' => $status,
  190. 'json' => $return,
  191. );
  192. }
  193. }