235 lines
11 KiB
PHP
235 lines
11 KiB
PHP
<?php
|
|
|
|
namespace app\api\model;
|
|
|
|
use think\Model;
|
|
use think\Db;
|
|
|
|
class UserRelation extends Model
|
|
{
|
|
//提交申请用户关系
|
|
public function apply_user_relation($uid, $recived_uid, $type)
|
|
{
|
|
|
|
if ($uid == $recived_uid) {
|
|
return ['code' => 201, 'msg' => '不能对自己发起申请', 'data' => null];
|
|
}
|
|
$map = [];
|
|
$map[] = ['uid', '=', $uid];
|
|
$map[] = ['recived_uid', '=', $recived_uid];
|
|
$map[] = ['type', '=', $type];
|
|
$map[] = ['status', '=', 1];
|
|
$user_relation_apply_info = db::name('user_relation_apply')->where($map)->find();
|
|
if (!empty($user_relation_apply_info)) {
|
|
return ['code' => 201, 'msg' => '您已发送申请', 'data' => null];
|
|
}
|
|
|
|
$map = [];
|
|
$map[] = ['uid', '=', $uid];
|
|
$map[] = ['recived_uid', '=', $recived_uid];
|
|
$map[] = ['type', '=', $type];
|
|
$map[] = ['is_delete', '=', 1];
|
|
$user_relation_info = db::name('user_relation')->where($map)->find();
|
|
if (!empty($user_relation_info)) {
|
|
return ['code' => 200, 'msg' => '你们已经存在当前关系', 'data' => null];
|
|
}
|
|
|
|
|
|
$map = [];
|
|
$map[] = ['uid', '=', $recived_uid];
|
|
$recived_user_info = Db::name('user')->where($map)->find();
|
|
if (empty($recived_user_info)) {
|
|
return ['code' => 201, 'msg' => '用户信息不存在', 'data' => null];
|
|
}
|
|
$recived_user_info['nick_name'] = mb_convert_encoding(base64_decode($recived_user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$data = [];
|
|
$data['uid'] = $uid;
|
|
$data['recived_uid'] = $recived_uid;
|
|
$data['type'] = $type;
|
|
$data['status'] = 1;
|
|
$validate = validate('admin/UserRelationApply');
|
|
$reslut = $validate->scene('apiAdd')->check($data);
|
|
if ($reslut !== true) {
|
|
return ['code' => 201, 'msg' => $validate->getError(), 'data' => null];
|
|
}
|
|
$model = model('admin/UserRelationApply');
|
|
$reslut = $model->save($data);
|
|
|
|
if (!$reslut) {
|
|
return ['code' => 201, 'msg' => '发送失败', 'data' => null];
|
|
} else {
|
|
//通知被邀请人
|
|
$data = [];
|
|
$data['code'] = 1000;
|
|
$data['data'] = ['aid' => $model->aid];
|
|
|
|
model('admin/Jpush')->push_notification_message($recived_uid, '消息提醒', "有人邀请您建立关系,快来看看吧", $data);
|
|
|
|
$type_desc = model('admin/UserRelation')->get_relation_type_lable($type);
|
|
$content = $recived_user_info['nick_name'] . "申请与你建立" . $type_desc . "关系";
|
|
model('api/user_message')->send_message($recived_uid, 3, $model->aid, "我的关系", $content);
|
|
return ['code' => 200, 'msg' => '已发送申请', 'data' => null];
|
|
}
|
|
}
|
|
//获取用户关系
|
|
public function get_apply_user_relation_info($uid, $aid)
|
|
{
|
|
$map = [];
|
|
$map[] = ['aid', '=', $aid];
|
|
$map[] = ['uid|recived_uid', '=', $uid];
|
|
$user_relation_info = db::name('user_relation_apply')->where($map)->find();
|
|
if (empty($user_relation_info)) {
|
|
return ['code' => 201, 'msg' => '信息不存在', 'data' => null];
|
|
}
|
|
$map = [];
|
|
$map[] = ['uid', 'in', [$user_relation_info['uid'], $user_relation_info['recived_uid']]];
|
|
$user_data = db::name('user')->where($map)->column('*', 'uid');
|
|
$user_relation_info['user_info']['uid'] = $user_data[$user_relation_info['uid']]['uid'];
|
|
$user_relation_info['user_info']['nick_name'] = mb_convert_encoding(base64_decode($user_data[$user_relation_info['uid']]['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$user_relation_info['user_info']['head_pic'] = localpath_to_netpath($user_data[$user_relation_info['uid']]['head_pic']);
|
|
$user_relation_info['reciveduser_info']['uid'] = $user_data[$user_relation_info['uid']]['uid'];
|
|
$user_relation_info['reciveduser_info']['nick_name'] = mb_convert_encoding(base64_decode($user_data[$user_relation_info['uid']]['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$user_relation_info['reciveduser_info']['head_pic'] = localpath_to_netpath($user_data[$user_relation_info['uid']]['head_pic']);
|
|
|
|
return ['code' => 200, 'msg' => '获取成功', 'data' => $user_relation_info];
|
|
}
|
|
//申请关系处理
|
|
public function deal_user_relation_apply($uid, $aid, $status)
|
|
{
|
|
$map = [];
|
|
$map[] = ['aid', '=', $aid];
|
|
$user_relation_apply_info = db::name('user_relation_apply')->where($map)->find();
|
|
if (empty($user_relation_apply_info)) {
|
|
return ['code' => 201, 'msg' => '信息不存在', 'data' => null];
|
|
}
|
|
if ($user_relation_apply_info['recived_uid'] != $uid) {
|
|
return ['code' => 201, 'msg' => '无权限操作', 'data' => null];
|
|
}
|
|
if ($user_relation_apply_info['status'] != 1) {
|
|
return ['code' => 201, 'msg' => '该申请已处理', 'data' => null];
|
|
}
|
|
$update_data = [];
|
|
$update_data['aid'] = $aid;
|
|
$update_data['status'] = $status;
|
|
$validate = validate('admin/UserRelationApply');
|
|
$reslut = $validate->scene('apiEdit')->check($update_data);
|
|
if ($reslut !== true) {
|
|
return ['code' => 201, 'msg' => $validate->getError(), 'data' => null];
|
|
}
|
|
|
|
if ($user_relation_apply_info['type'] == 2) {
|
|
//判断是否存在
|
|
$map = [];
|
|
$map[] = ['uid|recived_uid', '=', $uid];
|
|
$map[] = ['type', '=', 1];
|
|
$map[] = ['is_delete', '=', 1];
|
|
$user_relation_info = db::name('user_relation')->where($map)->find();
|
|
if (!empty($user_relation_info)) {
|
|
return ['code' => 200, 'msg' => '同时只能与一人建立情缘', 'data' => null];
|
|
}
|
|
} elseif ($user_relation_apply_info['type'] == 3) {
|
|
$map = [];
|
|
$map[] = ['uid', '=', $uid];
|
|
$map[] = ['type', '=', 2];
|
|
$map[] = ['is_delete', '=', 1];
|
|
$user_relation_info = db::name('user_relation')->where($map)->find();
|
|
if (!empty($user_relation_info)) {
|
|
|
|
return ['code' => 200, 'msg' => '同时只能与一人拜师', 'data' => null];
|
|
}
|
|
}
|
|
|
|
|
|
$user_info = db::name('user')->where('uid', $user_relation_apply_info['uid'])->find();
|
|
$user_info['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$type_desc = model('admin/UserRelation')->get_relation_type_lable($user_relation_apply_info['type']);
|
|
if ($status == 2) {
|
|
//添加关系
|
|
$data = [];
|
|
$data['type'] = $user_relation_apply_info['type'];
|
|
$data['uid'] = $user_relation_apply_info['uid'];
|
|
$data['recived_uid'] = $user_relation_apply_info['recived_uid'];
|
|
model('admin/UserRelation')->save($data);
|
|
|
|
$content = $user_info['nick_name'] . "对方已与你建立{$type_desc}关系";
|
|
$data = [];
|
|
$data['code'] = 1000;
|
|
$data['data'] = ['aid' => $user_relation_apply_info['aid']];
|
|
model('admin/Jpush')->push_notification_message($user_relation_apply_info['uid'], '消息提醒', $content, $data);
|
|
model('api/user_message')->send_message($user_relation_apply_info['uid'], 3, $user_relation_apply_info['aid'], "我的关系", $content);
|
|
} elseif ($status == 3) {
|
|
$content = $user_info['nick_name'] . "已拒绝与你建立{$type_desc}关系";
|
|
$data = [];
|
|
$data['code'] = 1000;
|
|
$data['data'] = ['aid' => $user_relation_apply_info['aid']];
|
|
model('admin/Jpush')->push_notification_message($user_relation_apply_info['recived_uid'], '消息提醒', $content, $data);
|
|
model('api/user_message')->send_message($user_relation_apply_info['uid'], 3, $user_relation_apply_info['aid'], "我的关系", $content);
|
|
}
|
|
$reslut = model('admin/UserRelationApply')->isUpdate(true)->save($update_data);
|
|
//修改申请状态
|
|
if (!$reslut) {
|
|
return ['code' => 201, 'msg' => '关系建立失败', 'data' => null];
|
|
} else {
|
|
return ['code' => 200, 'msg' => '关系建立成功', 'data' => null];
|
|
}
|
|
}
|
|
//删除用户关系
|
|
public function delete_user_relation($uid, $rid)
|
|
{
|
|
$map = [];
|
|
$map[] = ['uid|recived_uid', '=', $uid];
|
|
$map[] = ['rid', '=', $rid];
|
|
$map[] = ['is_delete', '=', 1];
|
|
$user_relation_info = db::name('user_relation')->where($map)->find();
|
|
if (empty($user_relation_info)) {
|
|
return ['code' => 201, 'msg' => '信息不存在', 'data' => null];
|
|
}
|
|
if ($user_relation_info['uid'] != $uid && $user_relation_info['recived_uid'] != $uid) {
|
|
return ['code' => 201, 'msg' => '无权限操作', 'data' => null];
|
|
}
|
|
$reslut = db::name('user_relation')->where('rid', $rid)->delete();
|
|
if (!$reslut) {
|
|
return ['code' => 201, 'msg' => '解除失败', 'data' => null];
|
|
} else {
|
|
|
|
// $data = [];
|
|
// $data['code'] = 1003;
|
|
// $data['data'] = ['rid' => $user_relation_info['rid']];
|
|
// if ($uid == $user_relation_info['uid']) {
|
|
// model('admin/Jpush')->push_notification_message($user_relation_info['recived_uid'], '消息提醒', "已与你解除情缘关系", $data);
|
|
// } else {
|
|
// model('admin/Jpush')->push_notification_message($user_relation_info['uid'], '消息提醒', "已与你解除情缘关系", $data);
|
|
// }
|
|
|
|
return ['code' => 200, 'msg' => '解除成功', 'data' => null];
|
|
}
|
|
}
|
|
//获取用户关系列表
|
|
public function get_user_relation_list($uid, $type, $page, $page_limit)
|
|
{
|
|
$page = intval($page);
|
|
$page_limit = $page_limit < 10 ? $page_limit : 10;
|
|
$map = [];
|
|
$map[] = ['type', '=', $type];
|
|
$map[] = ['uid|recived_uid', '=', $uid];
|
|
$map[] = ['is_delete', '=', 1];
|
|
$user_relation_list = db::name('user_relation')->where($map)->page($page, $page_limit)->select();
|
|
if (empty($user_relation_list)) {
|
|
return ['code' => 200, 'msg' => '获取成功', 'data' => $user_relation_list];
|
|
}
|
|
$uid_list = array_merge(array_column($user_relation_list, 'uid'), array_column($user_relation_list, 'recived_uid'));
|
|
$map = [];
|
|
$map[] = ['uid', 'in', $uid_list];
|
|
$user_data = db::name('user')->where($map)->column('*', 'uid');
|
|
foreach ($user_relation_list as $k => &$v) {
|
|
$v['user_info']['uid'] = $user_data[$v['uid']]['uid'];
|
|
$v['user_info']['nick_name'] = mb_convert_encoding(base64_decode($user_data[$v['uid']]['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$v['user_info']['head_pic'] = localpath_to_netpath($user_data[$v['uid']]['head_pic']);
|
|
$v['reciveduser_info']['uid'] = $user_data[$v['uid']]['uid'];
|
|
$v['reciveduser_info']['nick_name'] = mb_convert_encoding(base64_decode($user_data[$v['uid']]['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$v['reciveduser_info']['head_pic'] = localpath_to_netpath($user_data[$v['uid']]['head_pic']);
|
|
}
|
|
return ['code' => 200, 'msg' => '获取成功', 'data' => $user_relation_list];
|
|
}
|
|
}
|