Files

203 lines
5.5 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')->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')->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')->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);
}
}