小程序用户改用门店登录
This commit is contained in:
@@ -11,34 +11,34 @@ use Modules\AnnoRoute\Attribute\PutRoute;
|
||||
use Modules\AnnoRoute\Attribute\RequestAttribute;
|
||||
|
||||
/**
|
||||
* 小程序通知(本人通知 + 全员广播)
|
||||
* 小程序通知(本店通知 + 全员广播)
|
||||
*
|
||||
* 广播已读处理:user_id=0 的广播是全局共享记录,直接改 is_read 会影响其他用户,
|
||||
* 因此标记已读时复制一条本人专属的已读记录(data.broadcast_from 记来源),
|
||||
* 广播已读处理:store_id=0 的广播是全局共享记录,直接改 is_read 会影响其他门店,
|
||||
* 因此标记已读时复制一条本店专属的已读记录(data.broadcast_from 记来源),
|
||||
* 列表查询时排除已有已读副本的广播。
|
||||
*/
|
||||
#[RequestAttribute('/mini', 'mini', authGuard: 'users')]
|
||||
class NoticeController extends BaseMiniController
|
||||
{
|
||||
/** 本人通知 + 全员广播(user_id in [0, 当前id]),分页 + unread_count */
|
||||
/** 本店通知 + 全员广播(store_id in [0, 当前id]),分页 + unread_count */
|
||||
#[GetRoute('/notice', authorize: true)]
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$user = $this->currentUser($request);
|
||||
$store = $this->currentStore($request);
|
||||
|
||||
// 本人已读过的广播来源ID(已读副本记录)
|
||||
// 本店已读过的广播来源ID(已读副本记录)
|
||||
$readBroadcastIds = NoticeModel::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('store_id', $store->id)
|
||||
->whereNotNull('data->broadcast_from')
|
||||
->pluck('data')
|
||||
->map(static fn (?array $data) => $data['broadcast_from'] ?? null)
|
||||
->filter()
|
||||
->values();
|
||||
|
||||
$query = NoticeModel::query()->where(function ($q) use ($user, $readBroadcastIds) {
|
||||
$q->where('user_id', $user->id)
|
||||
$query = NoticeModel::query()->where(function ($q) use ($store, $readBroadcastIds) {
|
||||
$q->where('store_id', $store->id)
|
||||
->orWhere(function ($broadcastQuery) use ($readBroadcastIds) {
|
||||
$broadcastQuery->where('user_id', NoticeModel::BROADCAST_USER_ID);
|
||||
$broadcastQuery->where('store_id', NoticeModel::BROADCAST_STORE_ID);
|
||||
if ($readBroadcastIds->isNotEmpty()) {
|
||||
$broadcastQuery->whereNotIn('id', $readBroadcastIds->all());
|
||||
}
|
||||
@@ -55,24 +55,24 @@ class NoticeController extends BaseMiniController
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/** 标记已读(广播 → 复制本人已读副本;个人通知 → 直接更新) */
|
||||
/** 标记已读(广播 → 复制本店已读副本;定向通知 → 直接更新) */
|
||||
#[PutRoute('/notice/{id}/read', authorize: true, where: ['id' => '[0-9]+'])]
|
||||
public function read(int $id, Request $request): JsonResponse
|
||||
{
|
||||
$user = $this->currentUser($request);
|
||||
$store = $this->currentStore($request);
|
||||
|
||||
$notice = NoticeModel::whereIn('user_id', [NoticeModel::BROADCAST_USER_ID, $user->id])->find($id);
|
||||
$notice = NoticeModel::whereIn('store_id', [NoticeModel::BROADCAST_STORE_ID, $store->id])->find($id);
|
||||
if ($notice === null) {
|
||||
throw new RepositoryException('通知不存在');
|
||||
}
|
||||
|
||||
if ($notice->user_id === NoticeModel::BROADCAST_USER_ID) {
|
||||
$exists = NoticeModel::where('user_id', $user->id)
|
||||
if ($notice->store_id === NoticeModel::BROADCAST_STORE_ID) {
|
||||
$exists = NoticeModel::where('store_id', $store->id)
|
||||
->where('data->broadcast_from', $notice->id)
|
||||
->exists();
|
||||
if (! $exists) {
|
||||
NoticeModel::create([
|
||||
'user_id' => $user->id,
|
||||
'store_id' => $store->id,
|
||||
'type' => $notice->type,
|
||||
'title' => $notice->title,
|
||||
'content' => $notice->content,
|
||||
|
||||
Reference in New Issue
Block a user