Smslog.php 6.9 KB

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