Smslog.php 7.2 KB

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