ArticleBooksService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ArticleBooksModel;
  13. use App\Models\MemberModel;
  14. /**
  15. * 文章分类管理-服务类
  16. * @author wesmiler
  17. * @since 2020/11/11
  18. * Class ArticleBooksService
  19. * @package App\Services
  20. */
  21. class ArticleBooksService extends BaseService
  22. {
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  26. * @since 2020/11/11
  27. * ArticleBooksService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new ArticleBooksModel();
  32. }
  33. /**
  34. * 获取列表
  35. * @return array
  36. * @since 2020/11/11
  37. * @author wesmiler
  38. */
  39. public function getList()
  40. {
  41. $params = request()->all();
  42. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  43. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  44. $dataList = $this->model::from('article_books as b')
  45. ->leftJoin('article as a', 'a.id', '=', 'b.aid')
  46. ->leftJoin('member as m', 'm.id', '=', 'b.user_id')
  47. ->where(function ($query) use ($params) {
  48. $query->where('b.mark', 1);
  49. $status = isset($params['status']) ? $params['status'] : 0;
  50. if ($status > 0) {
  51. $query->where('b.status', $status);
  52. } else {
  53. $query->whereIn('b.status', [1, 2]);
  54. }
  55. })
  56. ->where(function ($query) use ($params) {
  57. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  58. if (!empty($keyword)) {
  59. $query->where('a.title', 'like', "%{$keyword}%")
  60. ->orWhere('b.realname','like',"%{$keyword}%")
  61. ->orWhere('m.nickname','like',"%{$keyword}%");
  62. }
  63. })
  64. ->select(['b.id', 'b.aid', 'a.title', 'm.nickname', 'b.realname', 'b.phone', 'b.thumb', 'b.status', 'b.create_time', 'b.update_time', 'b.description'])
  65. ->orderBy('b.update_time', 'desc')
  66. ->paginate($pageSize);
  67. $dataList = $dataList ? $dataList->toArray() : [];
  68. if ($dataList) {
  69. foreach ($dataList['data'] as &$item) {
  70. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  71. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  72. }
  73. unset($item);
  74. }
  75. return [
  76. 'code' => 0,
  77. 'success'=> true,
  78. 'msg' => '操作成功',
  79. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  80. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  81. ];
  82. }
  83. /**
  84. * 添加或编辑
  85. * @return array
  86. * @since 2020/11/11
  87. * @author wesmiler
  88. */
  89. public function edit()
  90. {
  91. $data = request()->all();
  92. $data['update_time'] = time();
  93. return parent::edit($data); // TODO: Change the autogenerated stub
  94. }
  95. /**
  96. * 报名
  97. * @parpam array $params 参数
  98. * @return array
  99. * @since 2020/11/11
  100. * @author wesmiler
  101. */
  102. public function books($params)
  103. {
  104. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  105. $memberInfo = MemberModel::where(['id'=> $userId,'maek'=> 1,'status'=> 1])
  106. ->select(['id','nickname','openid'])
  107. ->first();
  108. if(!$memberInfo){
  109. return message('用户状态不可操作,请联系客服', false);
  110. }
  111. // 是否报名过
  112. $aid = isset($params['id'])? intval($params['id']) : 0;
  113. $info = $this->model::where(['aid'=> $aid,'user_id'=> $userId, 'mark'=> 1,'status'=> 1])
  114. ->where('create_time','>=', strtotime(date('Y-m-d')))
  115. ->first();
  116. if($info){
  117. return message('该信息下您今日已经提交过信息', false);
  118. }
  119. $data = [
  120. 'aid'=> $aid,
  121. 'user_id'=> $userId,
  122. 'realname'=> isset($params['realname'])? trim($params['realname']) : '',
  123. 'phone'=> isset($params['phone'])? trim($params['phone']) : '',
  124. 'thumb'=> isset($params['thumb'])? trim($params['thumb']) : '',
  125. 'description'=> isset($params['description'])? trim($params['description']) : '',
  126. 'create_time'=> time(),
  127. 'mark'=> 1,
  128. 'status'=> 1
  129. ];
  130. $data['update_time'] = time();
  131. return parent::edit($data); // TODO: Change the autogenerated stub
  132. }
  133. }