Files

75 lines
2.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
namespace addon\wxchat\controller\api;
// app/controller/Search.php
use app\model\Message;
use think\Request;
use ywxapp\controller\FrontendBase as AddonController;
/**
* Search 类
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Search extends AddonController
{
/**
* Summary of noNeedLogin
* @var array
*/
protected $noNeedLogin = ['*'];
/**
* Summary of noNeedVerify
* @var array
*/
protected $noNeedVerify = ['*'];
/**
* 控制器初始化 initialize
* @return void
*/
protected function initialize()
{}
public function index(Request $request)
{
$userId = $request->user->id;
$keyword = $request->get('keyword');
$contactId = $request->get('contact_id');
$isGroup = $request->get('is_group', 0);
$query = Message::where(function($q) use ($userId, $contactId, $isGroup) {
if ($isGroup) {
$q->where('receiver_type', 1)->where('receiver_id', $contactId);
} else {
$q->where(function($q) use ($userId, $contactId) {
$q->where('sender_id', $contactId)->where('receiver_id', $userId);
})->whereOr(function($q) use ($userId, $contactId) {
$q->where('sender_id', $userId)->where('receiver_id', $contactId);
});
}
});
// 全文搜索(MySQL 5.7+
if (extension_loaded('mysqlnd')) {
$query->whereRaw("MATCH(content) AGAINST(?)", [$keyword]);
} else {
$query->where('content', 'like', "%{$keyword}%");
}
$messages = $query->order('create_at', 'desc')->limit(20)->select();
return json(['code' => 200, 'data' => $messages]);
}
}