Skip to content

Commit

Permalink
Fix:修复在通知页面不能回关用户的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchunshu committed Jan 16, 2023
1 parent 58969bd commit 862b49a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 77 deletions.
Expand Up @@ -8,7 +8,8 @@
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/
namespace App\Controller\Admin;

namespace App\Controller\Admin\Hook;

use App\Middleware\AdminMiddleware;
use Hyperf\HttpServer\Annotation\Controller;
Expand All @@ -17,56 +18,85 @@
use Hyperf\Paginator\LengthAwarePaginator;
use Hyperf\Utils\Collection;
use Hyperf\Utils\Str;
use Hyperf\ViewEngine\Contract\FactoryInterface;
use Swoole\Coroutine\System;
use Symfony\Component\Finder\Finder;

#[Controller(prefix: '/admin/setting/components')]
#[Controller(prefix: '/admin/hook/components')]
#[Middleware(AdminMiddleware::class)]
class ComponentController
{
#[GetMapping(path: '')]
public function index()
{
if (! is_dir(BASE_PATH . '/resources/views/customize/component')) {
if (!is_dir(BASE_PATH . '/resources/views/customize/component')) {
System::exec('cd ' . BASE_PATH . '/resources/views' . '&& mkdir ' . 'customize && cd customize && mkdir component');
}
$page = $this->page();
return view('admin.setting.components', ['page' => $page]);
return view('admin.setting.hook.components', ['page' => $page]);
}

private function getAllComponents(): array
{
$path = BASE_PATH . '/resources/views/customize/component/';
$files = [];
$result = Finder::create()->in($path)->files()->name('*.blade.php');
$id = 1;
foreach ($result as $item) {
$Filename = $item->getFilename();
$Filename = $item->getRelativePath();
$viewName = Str::before($item->getFilename(), '.');
$RelativePath = $item->getRelativePath();
if ($RelativePath) {
$RelativePath = explode('/', $RelativePath);
$viewName = implode('.', $RelativePath) . '.' . $viewName;
}
$files[] = [
'id' => Str::random(10),
'id' => $id++,
'file_name' => $Filename,
'view' => 'customize.component.' . $viewName,
'import' => $viewName,
'path' => $item->getRealPath(),
'remark' => $this->get_remark($item->getRealPath()) ?: "暂无",
];
}
return array_values($files);
}

private function page(): LengthAwarePaginator
{
$currentPage = (int) request()->input('page', 1);
$perPage = (int) request()->input('per_page', 15);
$currentPage = (int)request()->input('page', 1);
$perPage = (int)request()->input('per_page', 15);

// 这里根据 $currentPage 和 $perPage 进行数据查询,以下使用 Collection 代替
$collection = new Collection($this->getAllComponents());

$data = array_values($collection->forPage($currentPage, $perPage)->toArray());
return new LengthAwarePaginator($data, count($this->getAllComponents()), $perPage, $currentPage);
}

/**
* get remark
* @param $file
* @return string|null
*/
private function get_remark($file): string|null
{
$content = fopen($file, 'rb');
$content = fgets($content);
$result = [];
preg_match_all('/(?:\\{{--)(.*)(?:\\--}})/i', $content, $result);
return @$result[1][0] ?: null;
}

#[GetMapping(path:"preview")]
public function preview(){
$component = request()->input('component');
$component = 'customize.component.'.$component;
$container = \Hyperf\Utils\ApplicationContext::getContainer();
$factory = $container->get(FactoryInterface::class);
if(!$factory->exists($component)){
return admin_abort('小部件不存在',403);
}
return view('admin.setting.hook.preview', ['view' => $component]);
}
}
6 changes: 3 additions & 3 deletions app/Plugins/User/resources/views/notice/userfollow.blade.php
@@ -1,13 +1,13 @@
<div class="row row-cards justify-content-center">
<div class="col-md-12">
<div class="p-4 text-center">
<span class="avatar avatar-xl mb-3 avatar-rounded" style="background-image: url({{super_avatar($user)}})"></span>
<h3 class="m-0 mb-1"><a href="/users/{{$user->id}}.html">{{$user->username}}</a></h3>
<span class="avatar avatar-xl mb-3 avatar-rounded" style="background-image: url({{auth()->data()}})"></span>
<h3 class="m-0 mb-1"><a href="/users/{{auth()->id()}}.html">{{auth()->data()->username}}</a></h3>
<div class="text-muted">关注了你!</div>
</div>
</div>
<div class="col-md-12 text-center">
<a class="btn btn-light cursor-pointer" user-click="user_follow" user-id="{{ $user->id }}">
<a class="btn btn-light cursor-pointer" user-click="user_follow" user-id="{{ auth()->id() }}">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user-plus" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<circle cx="9" cy="7" r="4"></circle>
Expand Down
@@ -1,13 +1,13 @@
<div class="row row-cards justify-content-center">
<div class="col-md-12">
<div class="p-4 text-center">
<span class="avatar avatar-xl mb-3 avatar-rounded" style="background-image: url({{super_avatar($user)}})"></span>
<h3 class="m-0 mb-1"><a href="/users/{{$user->id}}.html">{{$user->username}}</a></h3>
<span class="avatar avatar-xl mb-3 avatar-rounded" style="background-image: url({{super_avatar(auth()->data())}})"></span>
<h3 class="m-0 mb-1"><a href="/users/{{auth()->id()}}.html">{{auth()->data->username}}</a></h3>
<div class="text-muted">取关了你!</div>
</div>
</div>
<div class="col-md-12 text-center">
<a class="btn btn-light cursor-pointer" user-click="user_follow" user-id="{{ $user->id }}">
<a class="btn btn-light cursor-pointer" user-click="user_follow" user-id="{{ auth()->id() }}">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user-plus" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<circle cx="9" cy="7" r="4"></circle>
Expand Down
4 changes: 2 additions & 2 deletions app/Plugins/User/src/Controller/ApiController.php
Expand Up @@ -202,7 +202,7 @@ public function user_follow()
user_notice()->send(
$user_id,
auth()->data()->username . ' 取关了你!',
view('User::notice.userfollow_d', ['user' => auth()->data()])
view('User::notice.userfollow_d')
);
return Json_Api(201, true, ['msg' => '已取关!']);
}
Expand All @@ -212,7 +212,7 @@ public function user_follow()
user_notice()->send(
$user_id,
auth()->data()->username . ' 关注了你!',
view('User::notice.userfollow', ['user' => auth()->data()])
view('User::notice.userfollow')
);

return Json_Api(200, true, ['msg' => '已关注']);
Expand Down
61 changes: 0 additions & 61 deletions resources/views/admin/setting/components.blade.php

This file was deleted.

72 changes: 72 additions & 0 deletions resources/views/admin/setting/hook/components.blade.php
@@ -0,0 +1,72 @@
@extends('app')

@section('title','部件管理')

@section('content')
<div class="row row-cards">
<div class="col-lg-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">部件列表</h3>
</div>
<div class="table-responsive">
<table class="table card-table table-vcenter">
<thead>
<tr>
<th>#</th>
<th>调用代码</th>
<th>备注</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@if(!$page->count())
<tr>
<td class="text-muted">暂无更多结果</td>
<td class="text-muted">暂无更多结果</td>
<td class="text-muted">暂无更多结果</td>
<td class="text-muted">暂无更多结果</td>
</tr>
@else
@foreach($page as $component)
<tr>
<td class="text-muted">
{{$component['id']}}
</td>
<td class="text-muted">
{{$component['import']}}
</td>
<td class="text-muted">
{{$component['remark']}}
</td>
<td class="text-muted w-5">
<a target="_blank" href="/admin/hook/components/preview?component={{$component['import']}}">预览</a>
</td>
<td class="text-muted w-5">
<a href="/admin/hook/components/edit?path={{$component['file_name']}}">编辑</a>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
</div>
<div class="mt-3">
{!! make_page($page) !!}
</div>
</div>
</div>
@if(!$page->count())
<div class="col-lg-12">
<div class="card card-body empty">
<div class="empty-header">403</div>
<p class="empty-title">暂无可用部件</p>
<p class="empty-subtitle text-muted">
如果你是开发者,可以在 {{BASE_PATH."/resources/views/customize/component/"}} 目录下创建
</p>
</div>
</div>
@endif
</div>
@endsection
14 changes: 14 additions & 0 deletions resources/views/admin/setting/hook/preview.blade.php
@@ -0,0 +1,14 @@
@extends('App::app')
@section('title','小部件预览')
@section('content')
<div class="row row-cards">
<div class="card">
<div class="card-header">
<h3 class="card-title">小部件预览</h3>
</div>
<div class="card-body">
@include($view)
</div>
</div>
</div>
@endsection

0 comments on commit 862b49a

Please sign in to comment.