MessageService.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * 信息服务
  4. * @author wesmielr
  5. */
  6. namespace app\index\service;
  7. use think\Db;
  8. class MessageService
  9. {
  10. /**
  11. * 获取咨询留言列表
  12. * @param $params 参数
  13. * @param int $pageSize 记录大小
  14. * @param string $field 返回字段
  15. * @return $this
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. * @throws \think\exception\DbException
  19. */
  20. public static function getList($params, $pageSize=10, $field=''){
  21. $field = $field? $field : 'id,jm_id,jm_title,name,address,ip,content,reply';
  22. return Db::name('message')
  23. ->where(function($query) use ($params){
  24. $type = isset($params['type'])? $params['type'] : 0;
  25. if($type){
  26. $query->where('type', $type);
  27. }
  28. })
  29. ->field($field)
  30. ->order('create_time desc')
  31. ->limit($pageSize)
  32. ->select()
  33. ->each(function($item, $k){
  34. if(isset($item['name'])){
  35. $item['name'] = $item['name']? formatName($item['name']) : '游客';
  36. }
  37. $item['address'] = isset($item['address'])? $item['address'] : '';
  38. $item['address'] = $item['address']? $item['address'] : (isset($item['ip'])? IpService::getAddress($item['ip']) : '未知地区');
  39. return $item;
  40. });
  41. }
  42. /**
  43. * 获取今天提交次数
  44. * @param $where
  45. */
  46. public static function getTodyCount($where){
  47. $todaytime = strtotime(date('Y-m-d'));
  48. return Db::name('message')
  49. ->where($where)
  50. ->where('create_time','gt',$todaytime)
  51. ->count();
  52. }
  53. /**
  54. * 获取本周提交次数
  55. * @param $where
  56. */
  57. public static function getWeekCount($where){
  58. // 当前日期
  59. $sdefaultDate = date("Y-m-d");
  60. // $first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  61. $first=1;
  62. // 获取当前周的第几天 周日是0 周一到周六是 1 - 6
  63. $w = date('w', strtotime($sdefaultDate));
  64. // 获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  65. $week_start=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
  66. return Db::name('message')
  67. ->where($where)
  68. ->where('create_time','gt',$week_start)
  69. ->count();
  70. }
  71. }