Smslog.php 7.0 KB

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