SystemMsgController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2019/7/1
  6. * Time: 14:55
  7. */
  8. namespace app\backend\modules\sysMsg\controllers;
  9. use app\common\components\BaseController;
  10. use app\common\models\systemMsg\SysMsgLog;
  11. use app\common\services\SystemMsgService;
  12. use Illuminate\Support\Facades\DB;
  13. class SystemMsgController extends BaseController
  14. {
  15. private $logCount = [];
  16. public function __construct()
  17. {
  18. $data = SysMsgLog::uniacid()
  19. ->select(DB::raw('type_id,COUNT(id) as log_count'))
  20. ->where('is_read', 0)
  21. ->groupBy('type_id')
  22. ->get()->toArray();
  23. $data = array_column($data,null,'type_id');
  24. $this->logCount = SystemMsgService::$msg_type;
  25. $total = 0;
  26. foreach ($this->logCount as $k => $item) {
  27. $this->logCount[$k]['has_many_log_count'] = $data[$item['id']]['log_count'] ? : 0;
  28. $total += $this->logCount[$k]['has_many_log_count'];
  29. }
  30. $this->logCount[] = [
  31. 'id' => 0,
  32. 'type_name' => '全部消息',
  33. 'icon_src' => '',
  34. 'has_many_log_count' => $total
  35. ];
  36. $this->logCount = array_column($this->logCount, null, 'id');
  37. asort($this->logCount);
  38. }
  39. //进去的首页
  40. public function index()
  41. {
  42. //订单收货测试
  43. $search = request()->search;
  44. $pageSize = 10;
  45. $list = SysMsgLog::getLogList(0, $search)
  46. // ->with('belongsToType')
  47. ->orderBy('created_at', 'desc')
  48. ->paginate($pageSize)
  49. ->toArray();
  50. return view('sysMsg.index',[
  51. 'list' => $list,
  52. 'search' => $search,
  53. 'msgType' => $this->logCount
  54. ])->render();
  55. }
  56. public function allMessage()
  57. {
  58. return $this->getList(0);
  59. }
  60. public function sysMessage()
  61. {
  62. return $this->getList(1);
  63. }
  64. public function orderMessage()
  65. {
  66. return $this->getList(2);
  67. }
  68. public function withdrawalMessage()
  69. {
  70. return $this->getList(3);
  71. }
  72. public function applyMessage()
  73. {
  74. return $this->getList(4);
  75. }
  76. public function stockMessage()
  77. {
  78. return $this->getList(5);
  79. }
  80. public function couponMessage()
  81. {
  82. return $this->getList(6);
  83. }
  84. public function refundMessage()
  85. {
  86. return $this->getList(7);
  87. }
  88. public function getList($type = 0)
  89. {
  90. $search = request()->search;
  91. $pageSize = 10;
  92. $list = SysMsgLog::getLogList($type, $search)
  93. // ->with('belongsToType')
  94. ->orderBy('created_at', 'desc')
  95. ->paginate($pageSize)
  96. ->toArray();
  97. return $this->successJson('ok',[
  98. 'list' => $list,
  99. 'search' => $search,
  100. 'msgType' => $this->logCount
  101. ]);
  102. }
  103. //更改消息已读状态
  104. public function readLog()
  105. {
  106. $type = request()->type;
  107. if (!empty($type) && $type == 1) {
  108. //全部标记已读
  109. SysMsgLog::uniacid()
  110. ->where('is_read', 0)
  111. ->update(['is_read' => 1]);
  112. return $this->successJson('ok');
  113. }
  114. $id = request()->id;
  115. if(empty($id)){
  116. return $this->errorJson('参数错误');
  117. }
  118. $log = SysMsgLog::uniacid()->find($id);
  119. if (empty($log)) {
  120. return $this->errorJson('未找到消息或已删除');
  121. }
  122. if ($log->is_read == 0) {
  123. $log->is_read = 1;
  124. $log->read_at = time();
  125. $log->save();
  126. }
  127. return $this->successJson('ok');
  128. }
  129. //查看系统通知类型消息详情
  130. public function readSystemMessage()
  131. {
  132. $id = \YunShop::request()->id;
  133. if(empty($id)){
  134. return $this->message('参数错误','','error');
  135. }
  136. $log = SysMsgLog::uniacid()->find($id);
  137. if (empty($log)) {
  138. return $this->message('未找到消息或已删除','','error');
  139. }
  140. if ($log->is_read == 0) {
  141. $log->is_read = 1;
  142. $log->read_at = time();
  143. $log->save();
  144. }
  145. return view('sysMsg.detail',[
  146. 'data' => $log,
  147. ])->render();
  148. }
  149. }