Files
YwxAppThink/addon/wxchat/controller/api/Friend.php
T
ywxapp 1d49e6f5ee feat: 公共表更名 common/member 前缀 + 插件命令自动加载 + 版本 1.1.1
- 18 张公共表更名(addon/attachment/configure/links/spider_log/spider_stat/sms/notice/ad/task/prop/medal/help/card -> common_*,member_wallets->member_wallet,score_rule/score_log -> member_*,addon_config->common_addonconf),模型全部对齐新表名,Db 直引用清零
- Attachment 模型补  表名绑定,修复富文本上传查 wxapp_attachment 1146 隐患
- install.sql + 迁移 SQL:backend_admin/backend_role delete_at 默认 0,修复软删除(NULL != 0)误过滤导致后台菜单为空
- AppService::boot() 支持插件 info.php 声明 commands 自动注册插件命令(psr-4 自动加载,坏类名自动跳过)
- 各插件(haonav/mqttbroker/wxchat/articles/blog/forum 等)字段与配置同步调整
- 框架版本 1.1.0 -> 1.1.1
2026-08-20 20:19:15 +08:00

203 lines
5.6 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\wxchat\controller\api;
use addon\wxchat\model\WxchatFriend as FriendModel;
use think\facade\Db;
use think\facade\Request;
use ywxapp\controller\FrontendBase;
/**
* Friend 类
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Friend extends FrontendBase
{
/**
* Summary of noNeedLogin
* @var array
*/
protected $noNeedLogin = [];
/**
* Summary of noNeedVerify
* @var array
*/
protected $noNeedVerify = ['*'];
/**
* 控制器初始化 initialize
* @return void
*/
protected function initialize()
{}
/**
* 当前用户好友列表(已通过)
*/
public function index()
{
$uid = $this->currentUid();
$friends = FriendModel::where('uid', $uid)
->where('status', 1)
->order('create_at', 'desc')
->select();
$list = [];
foreach ($friends as $f) {
$u = Db::name('member_user')->where('uid', $f->fid)
->field('uid,nickname,avatar,phone')->find();
if ($u) {
$u['friend_id'] = $f->id;
$u['remark'] = $f->remark;
$list[] = $u;
}
}
$this->success(['list' => $list, 'total' => count($list)]);
}
/**
* 待处理的好友请求(别人加我,status=0)
*/
public function requests()
{
$uid = $this->currentUid();
$rows = FriendModel::where('fid', $uid)
->where('status', 0)
->order('create_at', 'desc')
->select();
$list = [];
foreach ($rows as $r) {
$u = Db::name('member_user')->where('uid', $r->uid)
->field('uid,nickname,avatar')->find();
if ($u) {
$u['friend_id'] = $r->id;
$u['remark'] = $r->remark;
$list[] = $u;
}
}
$this->success(['list' => $list, 'total' => count($list)]);
}
/**
* 发送好友请求
*/
public function create()
{
$uid = $this->currentUid();
$fid = (int) Request::post('fid');
$remark = Request::post('remark', '');
if ($fid <= 0 || $fid == $uid) {
$this->error('无效的好友ID');
}
$exist = FriendModel::where('uid', $uid)->where('fid', $fid)->find();
if ($exist) {
if ($exist->status == 1) {
$this->error('你们已经是好友了');
}
// 重新发起请求
$exist->status = 0;
$exist->remark = $remark;
$exist->save();
$this->success([], '已重新发送好友请求');
}
FriendModel::create([
'uid' => $uid,
'fid' => $fid,
'remark' => $remark,
'status' => 0,
]);
$this->success([], '好友请求已发送');
}
/**
* 处理好友请求(接受/拒绝)
* post: { id, action } action=1 接受, action=2 拒绝
*/
public function save()
{
$uid = $this->currentUid();
$id = (int) Request::post('id');
$action = (int) Request::post('action', 1);
$row = FriendModel::where('id', $id)->where('fid', $uid)->where('status', 0)->find();
if (! $row) {
$this->error('请求不存在或已处理');
}
if ($action == 2) {
$row->delete();
$this->success([], '已拒绝');
}
$row->status = 1;
$row->save();
$this->success([], '已添加为好友');
}
/**
* 好友详情
*/
public function read()
{
$uid = $this->currentUid();
$id = (int) Request::post('id');
$row = FriendModel::where('id', $id)->where('uid', $uid)->find();
if (! $row) {
$this->error('好友不存在');
}
$u = Db::name('member_user')->where('uid', $row->fid)
->field('uid,nickname,avatar,phone')->find();
if ($u) {
$u['friend_id'] = $row->id;
$u['remark'] = $row->remark;
}
$this->success($u);
}
/**
* 修改备注
* post: { id, remark }
*/
public function update()
{
$uid = $this->currentUid();
$id = (int) Request::post('id');
$row = FriendModel::where('id', $id)->where('uid', $uid)->find();
if (! $row) {
$this->error('好友不存在');
}
$row->remark = Request::post('remark', '');
$row->save();
$this->success([], '备注已更新');
}
/**
* 删除好友
*/
public function delete()
{
$uid = $this->currentUid();
$id = (int) Request::post('id');
$row = FriendModel::where('id', $id)->where('uid', $uid)->find();
if (! $row) {
$this->error('好友不存在');
}
$row->delete();
$this->success([], '好友已删除');
}
/**
* 取当前登录用户 uid
*/
private function currentUid() : int
{
return (int) ($this->auth->model->uid ?? $this->auth->uid ?? 0);
}
}