3 Star 13 Fork 3

天然呆 / wyx2013

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
Clone or download
Chat.php 7.00 KB
Copy Edit Web IDE Raw Blame History
天然呆 authored 2019-10-18 21:59 . 10-18
<?php
namespace app\index\controller;
use app\common\MemberService;
use app\common\MPChatService;
use app\index\model\AdminUser;
use app\index\model\AppOpen;
use app\index\model\ChatGroupRoom;
use app\index\model\ChatUser;
use app\index\model\WxWebUser;
use EasyWeChat\Factory;
use jwt\JWT;
use service\File;
use think\Db;
use think\Exception;
use think\facade\Cookie;
use think\Controller;
use think\facade\Request;
use think\facade\Session;
class Chat extends Controller
{
public function initialize()
{
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT');
if(Request::isOptions())return;
parent::initialize(); // TODO: Change the autogenerated stub
}
/*
* 聊天室页面
* */
public function index()
{
return view();
}
/*
* 手机聊天室
* */
public function mobile()
{
return view();
}
/*网页用户聊天页面登录*/
public function chatWebUser()
{
if(!Request::isAjax()){ return; }
$data=Request::only(['password','username'], 'post');
$no_status=Request::get('no_status');//不记录session
$member_service=new MemberService();
$response=$member_service->ajaxLogin($data,$no_status);
if($response['code']!==0){
return json($response);
}
$user_info=$member_service->jwt($response);
return json($user_info);
}
/*用户注册页面*/
public function register()
{
$data=Request::only(['password','username','email','nickname'], 'post');
$member_service=new MemberService();
$response=$member_service->register($data);
return json($response);
}
/**
* 修改用户信息
* */
public function updataUserInfo()
{
$auth=$this->checkJWT();
if(!$auth){
return json(['err'=>'用户鉴权失败','code'=>4313]);
}
$data=Request::only(['nickname','figureurl'],'post');
$chat_id=Request::post('chat_user_id');
$chat=ChatUser::where('id',$chat_id)->find();
if(!$chat){
return json(['err'=>'无此用户','code'=>4318]);
}
$user=$chat->user;
if(!$user){
return json(['err'=>'非注册用户无法修改','code'=>4319]);
}
$arr= AdminUser::where(['id'=>$user['id']])->update($data);
if($arr !==1){
return json(['err'=>'修改失败','code'=>4320]);
}
$data['id']=$chat_id;
return json($data);
}
/**
* 验证用户名是否可用页面
* */
public function checkUserName()
{
if(input('?get.username')){
$username=strtolower( input('get.username') );
$user=AdminUser::where('username',$username)->find();
if($user){
$response=['err'=>'用户名重复','code'=>20];
}else{
$response=['username'=>$username];
}
return json($response);
}
return null;
}
/*小程序用户进入聊天室页面*/
public function chatMiniProgramUser()
{
if(!input('?get.id')) return;
$str=(string)file_get_contents("php://input");
if(empty($str)){return false;}
$post=json_decode($str,true);
return json( (new MPChatService($post))->jwt() );
}
/*
* 上传文件
* */
public function uploadFile()
{
$tyep=input('get.type');
$file=Request::file('file');
//有传入的$_POST,可以做鉴权
$auth=$this->checkJWT();
if(!$auth){
return json(['err'=>'用户鉴权失败','code'=>4313]);
}
if($tyep==='mp3'){
$info=$file->rule('date')->move('./upload/');
}elseif ($tyep==='img'){
if(input('post.ext')){
$name=time().rand(10000,1000000).'.jpg';
$info=$file->rule('date')->validate(['size'=>200000])->move('./upload/img/',$name,false,false);
}else{
$info=$file->rule('date')->validate(['size'=>200000,'ext'=>'jpg,png,gif,webp'])->move('./upload/');
}
}else{
return json(['err'=>'末指定类型','code'=>4312]);
}
if($info){
if(input('post.ext')){
$url=Request::root(true).'/upload/img/' .$info->getSaveName();
}else{
$url=Request::root(true).'/upload/' .$info->getSaveName();
}
return json(['url'=>$url]);
}else{
return json(['err'=>$file->getError(),'code'=>4311]);
}
}
/*
* 新建群聊房间
* */
public function buildChatRoom()
{
$auth=$this->checkJWT();
if(!$auth){
return json(['err'=>'用户鉴权失败','code'=>4313]);
}
$data['chat_user_id']=input('post.chat_user_id');
$data['title']=input('post.title');
if(input('?post.notice')){
$data['notice']=input('post.notice');
}
$data['create_time']=time();
$count=ChatGroupRoom::where('chat_user_id',$data['chat_user_id'])->count();
if($count>3){
return json(['err'=>'用户新建房间超过4个','code'=>45001]);
}
$user=ChatUser::find($data['chat_user_id']);
$room=$user->chatGroupRoom()->save($data);
if($room){
$chat_group_room_id=$room['chat_group_room_id'];
return json(ChatGroupRoom::find($chat_group_room_id));
}
}
/*取得用户所在群聊房间信息*/
public function getChatRooms()
{
$auth=$this->checkJWT();
if(!$auth){
return json(['err'=>'用户鉴权失败','code'=>4313]);
}
$rooms=ChatUser::with('chatGroupRoom')->where('id',input('post.chat_user_id'))->field('id')->find();
return json($rooms);
}
/*查询房间信息*/
public function findChatRooms()
{
$auth=$this->checkJWT();
if(!$auth){
return json(['err'=>'用户鉴权失败','code'=>4313]);
}
$rooms=ChatUser::with('chatGroupRoom')->where('id',input('post.chat_user_id'))->field('id')->find();
return json($rooms);
}
public function joinGroup()
{
}
//检测JWT是否通过
private function checkJWT()
{
$jwt=input('post.token');
$chat_user_id=input('post.chat_user_id');
$user=ChatUser::where('id',$chat_user_id)->find();
if(!$user || empty($user['token'])){
return false;
}
try{
$decoded = JWT::decode($jwt, $user['token'], array('HS256'));
$decoded =(array)$decoded;
if(empty($decoded['data']) || empty($decoded['exp'])){
return false;
}
$decoded['data']=(array)$decoded['data'];
return $decoded;
}catch (\Exception $e){
return false;
}
}
}

Comment ( 0 )

Sign in for post a comment

PHP
1
https://gitee.com/wuxq1985/wyx2013.git
git@gitee.com:wuxq1985/wyx2013.git
wuxq1985
wyx2013
wyx2013
master

Search