Permalink
Browse files

添加@某个用户的功能

  • Loading branch information...
tracenull committed Jan 9, 2019
1 parent 8ed18f9 commit 5e8a5c81a14504807ddfe7b18f35b93a20c39051
@@ -6,6 +6,7 @@
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ReplyRequest;
use App\Models\User;
class RepliesController extends Controller
{
@@ -21,6 +22,11 @@ public function store(ReplyRequest $request, Reply $reply)
if (empty($content)) {
return redirect()->back()->with('danger', '回复内容错误!');
}
// fixme只能@一个用户
$username = $this->get_between($content, '@', ' ');
$uid = User::query()->where('name', $username)->pluck('id')->toArray();
$replace = "<a style='color:blue' href='/users/" . $uid[0] . "' title='" . "$username'>@" . $username . "</a>";
$content = str_replace('@' . $username, $replace, $content);
$reply->content = $content;
$reply->user_id = \Auth::id();
$reply->topic_id = $request->topic_id;
@@ -36,4 +42,26 @@ public function destroy(Reply $reply)
return redirect()->to($reply->topic->link())->with('success', '删除成功!');
}
public function atwho(Request $request)
{
$name = $request->input('q', '');
//fixme 只能 at话题作者或者是自己的粉丝
$users = User::query()->where('name', 'like', $name . '%')->pluck('name');
// laravel集合可以当做数组用,laravel默认返回的是json响应
return $users;
}
/**
* php截取指定两个字符之间字符串
* @param $input
* @param $start
* @param $end
* @return bool|string
*/
public function get_between($input, $start, $end)
{
$substr = substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));
return $substr;
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -4,9 +4,28 @@
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="topic_id" value="{{ $topic->id }}">
<div class="form-group">
<textarea class="form-control" rows="3" placeholder="分享你的想法" name="content"></textarea>
<textarea class="form-control inputor" rows="3" placeholder="分享你的想法" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-share"></i>回复</button>
</form>
</div>
<hr>
<hr>
@section('styles')
<link href="{{asset('css/jquery.atwho.min.css')}}" rel="stylesheet">
@endsection
@section('scripts')
<script src="{{asset('js/jquery.atwho.min.js')}}"></script>
<script src="{{asset('js/jquery.caret.min.js')}}"></script>
<script>
$('.inputor').atwho({
at: "@",
callbacks: {
remoteFilter: function (query, callback) {
$.getJSON("/atwho", {q: query}, function (data) {
callback(data)
})
}
}
})
</script>
@endsection
@@ -47,6 +47,8 @@
Route::resource('categories', 'CategoriesController', ['only' => ['show']]);
Route::post('upload_image', 'TopicsController@uploadImage')->name('topics.upload_image');
Route::resource('replies', 'RepliesController', ['only' => ['store', 'destroy']]);
Route::get('atwho', 'RepliesController@atwho')->name('replies.atwho');
Route::resource('notifications', 'NotificationsController', ['only' => ['index']]);
Route::get('pages/notification_count', 'PagesController@notificationCount')->name('pages.notification_count');
Route::get('permission-denied', 'PagesController@permissionDenied')->name('permission-denied');
@@ -14,4 +14,6 @@ let mix = require('laravel-mix');
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css')
.copyDirectory('resources/assets/editor/js', 'public/js')
.copyDirectory('resources/assets/editor/css', 'public/css');
.copyDirectory('resources/assets/editor/css', 'public/css')
.copyDirectory('resources/assets/atwho/js', 'public/js')
.copyDirectory('resources/assets/atwho/css', 'public/css');

0 comments on commit 5e8a5c8

Please sign in to comment.