Smslog.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Smslog extends BaseModel
  12. {
  13. public $page_info;
  14. /**
  15. * 发送验证码
  16. * @author csdeshang
  17. * @param type $smslog_phone 手机号
  18. * @param type $smslog_param 短信参数
  19. * @param type $smslog_type 类型
  20. * @param type $smslog_captcha 验证码
  21. * @param type $member_id 会员ID
  22. * @param type $member_name 会员名
  23. * @param type $if_queue 是否放入队列
  24. * @return type
  25. */
  26. function sendSms($smslog_phone, $smslog_param, $smslog_type = '', $smslog_captcha = '', $member_id = '0', $member_name = '', $if_queue = false)
  27. {
  28. $smslog_msg = $smslog_param['message'];
  29. //通过手机号判断是否允许发送短信
  30. $begin_add_time = strtotime(date('Y-m-d', TIMESTAMP));
  31. $end_add_time = strtotime(date('Y-m-d', TIMESTAMP)) + 24 * 3600;
  32. //同一IP 每天只能发送20条短信
  33. $condition = array();
  34. $condition[] = array('smslog_ip', '=', request()->ip());
  35. $condition[] = array('smslog_smstime', 'between', array($begin_add_time, $end_add_time));
  36. if ($smslog_captcha && $this->getSmsCount($condition) > 20) {
  37. return array('state' => FALSE, 'code' => 10001, 'message' => '同一IP地址一天内只能发送20条短信,请勿多次获取动态码!');
  38. }
  39. //同一手机号,60秒才能提交发送一次
  40. $condition = array();
  41. $condition[] = array('smslog_phone', '=', $smslog_phone);
  42. $condition[] = array('smslog_smstime', 'between', array(TIMESTAMP - 30, TIMESTAMP));
  43. if ($smslog_captcha && $this->getSmsCount($condition) > 0) {
  44. return array('state' => FALSE, 'code' => 10001, 'message' => '同一手机30秒后才能再次发送短信,请勿多次获取动态码!');
  45. }
  46. //同一手机号,每天只能发送5条短信
  47. $condition = array();
  48. $condition[] = array('smslog_phone', '=', $smslog_phone);
  49. $condition[] = array('smslog_smstime', 'between', array($begin_add_time, $end_add_time));
  50. if ($smslog_captcha && $this->getSmsCount($condition) > 5) {
  51. return array('state' => FALSE, 'code' => 10001, 'message' => '同一手机一天内只能发送5条短信,请勿多次获取动态码!');
  52. }
  53. // 相同的短信内容,一天不能发送3次
  54. $condition = array();
  55. $condition[] = array('smslog_msg', '=', $smslog_msg);
  56. $condition[] = array('smslog_smstime', 'between', array($begin_add_time, $end_add_time));
  57. if ($this->getSmsCount($condition) > 3) {
  58. return array('state' => FALSE, 'code' => 10001, 'message' => '相同的短信内容,一天不能发送3次!');
  59. }
  60. //通过手机号获取现绑定的客户信息
  61. if (empty($member_id) || empty($member_name)) {
  62. //通过手机号查询用户名
  63. $member = model('member')->getMemberInfo(array('member_mobile' => $smslog_phone));
  64. $member_id = isset($member['member_id']) ? $member['member_id'] : '0';
  65. $member_name = isset($member['member_name']) ? $member['member_name'] : '';
  66. }
  67. $smslog_state = 0;
  68. if (!$if_queue) {
  69. $sms = new \sendmsg\Sms();
  70. $send_result = $sms->send($smslog_phone, $smslog_param);
  71. if ($send_result['code'] == true) {
  72. $smslog_state = 1;
  73. } else {
  74. return array('state' => FALSE, 'code' => 10001, 'message' => $send_result['msg']);
  75. }
  76. } else {
  77. $smslog_msg = json_encode($smslog_param);
  78. }
  79. $log['smslog_phone'] = $smslog_phone;
  80. $log['smslog_captcha'] = $smslog_captcha;
  81. $log['smslog_ip'] = request()->ip();
  82. $log['smslog_msg'] = $smslog_msg;
  83. $log['smslog_type'] = $smslog_type;
  84. $log['smslog_smstime'] = TIMESTAMP;
  85. $log['member_id'] = $member_id;
  86. $log['member_name'] = $member_name;
  87. $log['smslog_state'] = $smslog_state;
  88. $result = $this->addSms($log);
  89. if ($result >= 0) {
  90. return array('state' => TRUE, 'code' => 10000, 'message' => '');
  91. } else {
  92. return array('state' => FALSE, 'code' => 10001, 'message' => '手机短信发送失败');
  93. }
  94. }
  95. /**
  96. * 修改短信记录
  97. * @access public
  98. * @author csdeshang
  99. * @param type $log_array 日志数组
  100. * @return type
  101. */
  102. public function editSms($update, $condition)
  103. {
  104. return Db::name('smslog')->where($condition)->update($update);
  105. }
  106. /**
  107. * 增加短信记录
  108. * @access public
  109. * @author csdeshang
  110. * @param type $log_array 日志数组
  111. * @return type
  112. */
  113. public function addSms($log_array)
  114. {
  115. $log_id = Db::name('smslog')->insertGetId($log_array);
  116. return $log_id;
  117. }
  118. /**
  119. * 查询单条记录
  120. * @access public
  121. * @author csdeshang
  122. * @param type $condition 条件
  123. * @return boolean
  124. */
  125. public function getSmsInfo($condition)
  126. {
  127. if (empty($condition)) {
  128. return false;
  129. }
  130. $result = Db::name('smslog')->where($condition)->order('smslog_id desc')->find();
  131. return $result;
  132. }
  133. /**
  134. * 查询记录
  135. * @access public
  136. * @author csdeshang
  137. * @param type $condition 条件
  138. * @param type $pagesize 分页
  139. * @param type $limit 限制
  140. * @param type $order 排序
  141. * @return type
  142. */
  143. public function getSmsList($condition = array(), $pagesize = '', $limit = 0, $order = 'smslog_id desc')
  144. {
  145. if ($pagesize) {
  146. $result = Db::name('smslog')->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  147. $this->page_info = $result;
  148. $result = $result->items();
  149. } else {
  150. $result = Db::name('smslog')->where($condition)->limit($limit)->order($order)->select()->toArray();
  151. }
  152. return $result;
  153. }
  154. /**
  155. * 获取数据条数
  156. * @access public
  157. * @author csdeshang
  158. * @param type $condition 条件
  159. * @return type
  160. */
  161. public function getSmsCount($condition)
  162. {
  163. return Db::name('smslog')->where($condition)->count();
  164. }
  165. /**
  166. * 删除短信记录
  167. */
  168. public function delSmsLog($condition)
  169. {
  170. return Db::name('smslog')->where($condition)->delete();
  171. }
  172. }