Commit 415adc0a authored by clone's avatar clone

聊天服务

parent 66666288
<?php
//配置文件
return [
];
\ No newline at end of file
<?php
namespace app\chat\controller;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/8
* Time : 16:54
* Intro:
*/
use app\chat\extend\Basic;
use app\chat\service\ChatService;
use RPush;
use Think\Log;
use think\Request;
class AppChat extends Basic
{
/**
* @var RPush
*/
private $_push;//环信接口消息推送
/**
* @var ChatService
*/
private $_chat;
public function __construct($request = null)
{
parent::__construct($request);
$this->_push = new RPush();
$this->_chat = new ChatService();
}
public function index()
{
/*
log 常规日志,用于记录日志
error 错误,一般会导致程序的终止
notice 警告,程序可以运行但是还不够完美的错误
info 信息,程序输出信息
debug 调试,用于调试信息
sql SQL语句,用于SQL记录,只在数据库的调试模式开启时有效
*/
Log::record('日志', 'notice');
return null;
}
/**
* 验证用户角色,生成唯一id
*
* @return \think\Response
*/
public function userChat()
{
$this->_chat->createOnlyId();
return $this->response("200");
}
}
<?php
namespace app\chat\extend;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/8
* Time : 16:54
* Intro:
*/
use think\Controller;
use think\Request;
use think\Response;
class Basic extends Controller
{
/**
* 访问请求对象
* @var Request
*/
public $request;
public $params;
/**
* 基础接口SDK
* @param Request|null $request
*/
public function __construct(Request $request = null)
{
// CORS 跨域 Options 检测响应
$this->corsOptionsHandler();
// 输入对象
$this->request = is_null($request) ? Request::instance() : $request;
if (strtoupper($this->request->method()) === "GET") {
$this->params = $this->request->param();
} elseif (strtoupper($this->request->method()) === "POST") {
$this->params = $this->request->param() != null ? $this->request->param() : null;
}
}
/**
* 输出返回数据
* @param string $msg 提示消息内容
* @param string $code 业务状态码
* @param mixed $data 要返回的数据
* @param string $type 返回类型 JSON XML
* @return Response
*/
public function response($code = 'SUCCESS', $msg, $data = [], $type = 'json')
{
$result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, 'type' => strtolower($type) ];
return Response::create($result, $type);
}
}
<?php
namespace app\chat\service;
class ChatService
{
public $connection;
public $logs;
const MSG_CONTENT_TYPE_TEXT = 1; //文字
const MSG_CONTENT_TYPE_VOICE = 2; //语音
const MSG_CONTENT_TYPE_VIDEO = 3; //视频
const SOURCE_TYPE_APP = 'app_'; // 用户 c端
const SOURCE_TYPE_AGENT = 'agent_';//经纪人 b端
/**
* 生成唯一id
* @param $userId integer 用户id
* @param $mobile string 手机号
* @param $source integer 来源 '1经纪人 2用户'
* @return string
*/
public function createOnlyId($userId, $mobile, $source)
{
//todo 1.验证用户是否存在 经纪人和用户 2.根据规则生成唯一id 3.判断唯一id是否登录中,登陆中则调用退出接口 4.返回唯一id
$onlyId = "";
switch ($source) {
case 1:
$onlyId = self::SOURCE_TYPE_APP;
break;
case 2:
break;
default:
}
return $onlyId;
}
}
\ No newline at end of file
<?php
use Payment\Utils\Curl;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/8
* Time : 17:17
* Intro: 发送消息类
*/
class RPush{
/**
* curl参数拼凑
* @param $push_type
* @param $sender
* @param $receiver
* @param $msg_content
* @param $msg_file_name
* @param $msg_file_url
* @return bool|CurlResponse
*/
public function sendRequestByCurl($push_type, $sender, $receiver,$msg_content,
$msg_file_name, $msg_file_url){
$this->_log->info("-------Rpush==>params:push_type==>" . $push_type .'sender==>'.$sender
.'receiver==>'.$receiver .'msg_content==>'.$msg_content );
$msg_content['extended'] = "
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";//让msg很长不会在push中显示出来
$arr = array(
'pushType' => $push_type,
'appId' => $this->app_id,
'sender' => $sender,
'receiver' => $receiver,
'msgType' => 1,
'msgContent' => json_encode($msg_content),
'msgDomain' => '',
'msgFileName' => $msg_file_name,
'msgFileUrl' => $msg_file_url,
);
$data = json_encode($arr);
$curl = new Curl();
$curl->headers = [
"Accept" => "application/json",
"Content-Type" => "application/json;charset=utf-8",
'Authorization' => $this->main_auth,
];
$curl->options = [
"CURLOPT_SSL_VERIFYPEER" => 0,
"CURLOPT_SSL_VERIFYHOST" => 1,
];
$url = $this->buildSendUrl();
$response = $curl->post($url, $data);
$this->_log->info(sprintf("%s::%s,url[%s],data[%s] response[%s]",
__CLASS__, __FUNCTION__, $url, json_encode($data), json_encode($response)));
return $response;
}
/**
* @return string
*/
private function buildSendUrl()
{
return $this->base_url . '/Accounts/' . $this->account_sid .
'/IM/PushMsg?sig=' . $this->main_sig;
}
}
\ No newline at end of file
...@@ -136,4 +136,10 @@ Route::group('api', [ ...@@ -136,4 +136,10 @@ Route::group('api', [
]); ]);
Route::group('chat', [
'userChat' => [ 'chat/AppChat/userChat', [ 'method' => 'get' ] ],
]);
//Route::miss('api/index/miss');//处理错误的url //Route::miss('api/index/miss');//处理错误的url
\ No newline at end of file
...@@ -22,11 +22,10 @@ return [ ...@@ -22,11 +22,10 @@ return [
'view' => ['index/index'], 'view' => ['index/index'],
],*/ ],*/
// 其他更多的模块定义 // 其他更多的模块定义
'app' => [ 'chat' => [
'__file__' => ['common.php'], '__file__' => ['common.php'],
'__dir__' => ['behavior', 'controller', 'model', 'view'], '__dir__' => ['behavior', 'controller', 'service', 'utils'],
'controller' => ['Index', 'Test', 'UserType'], 'controller' => ['Index'],
'model' => ['User', 'UserType'], 'service' => ['IndexService'],
'view' => ['index/index'],
], ],
]; ];
<?php
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/8
* Time : 15:56
* Intro:
*/
// [ 应用入口文件 ]
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
// 读取自动生成定义文件
/*$build = include './../build.php';
// 运行自动生成
\think\Build::run($build);*/
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment