作者 liuzhen
1 个管道 的构建 通过 耗费 7 秒

用户与群聊写进支付回调流程

... ... @@ -313,6 +313,65 @@ class Common extends Api
return $data ;
}
/**
* @ApiTitle (记录融云聊天内容)
* @ApiSummary (记录融云聊天内容)
* @ApiMethod (POST)
* @ApiRoute (/api/common/setOrderChat)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="interger", required=true, description="订单id")
* @ApiParams (name="chat_id", type="string", required=true, description="融云群聊id")
* @ApiParams (name="send_id", type="string", required=true, description="发送人id")
* @ApiParams (name="receive_id", type="string", required=true, description="接收人id")
* @ApiParams (name="type", type="interger", required=true, description="消息类型:1=文字2=图片3=文件")
* @ApiParams (name="text", type="string", required=false, description="文字内容")
* @ApiParams (name="image", type="string", required=false, description="图片")
* @ApiParams (name="file", type="string", required=false, description="文件")
*
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
}
})
*/
public function setOrderChat()
{
$validate = new Validate([
'order_id' => 'require',
'chat_id' => 'require',
'send_id' => 'require',
'receive_id' => 'require',
'type' => 'require|in:1,2,3',
'text' => 'require',
'image' => 'require',
'file' => 'require',
]);
$validate->message([
'order_id.require' => '缺少参数order_id!',
'chat_id.require' => '缺少参数chat_id!',
'send_id.require' => '缺少参数send_id!',
'receive_id.require' => '缺少参数receive_id!',
'type.require' => '缺少参数type!',
'type.in' => 'type参数错误!',
'text.require' => '缺少参数text!',
'image.require' => '缺少参数image!',
'file.require' => '缺少参数file!',
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$data['createtime'] = $data['updatetime'] = time();
Db::name("order_chat")->insertGetId($data);
$this->success('消息记录成功');
}
public function create()
{
$url = 'http://api-cn.ronghub.com/chatroom/create.json';
... ...
... ... @@ -11,8 +11,10 @@ namespace app\api\controller;
use app\common\controller\Api;
use EasyWeChat\Foundation\Application;
use RongCloud\RongCloud;
use think\Db;
use EasyWeChat\Payment\Order;
use think\Log;
/**
* 支付回调(无需调用)
... ... @@ -93,6 +95,12 @@ class Pay extends Api
$coupon['expirationtime'] = $coupon['createtime']+7*86400;
Db::name('coupon')->insertGetId($coupon);
Db::name('teacher')->where('id',$order['teacher_id'])->setInc('help_num');
// 创建融云用户、群组
$rongyun_chat = $this->rongyun_handle($is_one['user_id'].','.$is_one['teacher_id']);
if($rongyun_chat) {
$update['chat_id'] = $rongyun_chat['chat_id'];
$update['chat_name'] = $rongyun_chat['chat_name'];
}
} else { // 用户支付失败
$update['status']=1;
}
... ... @@ -168,4 +176,51 @@ class Pay extends Api
});
$response->send();
}
// 融云创建用户、群组
public function rongyun_handle($user_ids) {
$config = config('rongyun.config'); // 应用配置参数
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$users = Db::name('user')->whereIn('id',$user_ids)->column('id,nickname,avatar,rongyun_id,is_create'); // 获取用户信息
if($users) {
$user_rongyun_ids = [];
$User = $RongSDK->getUser();
foreach ($users as $k=>$v) {
$user_rongyun_ids[] = [
'id' => $v['rongyun_id']
];
if($v['is_create'] == 0) {
// 创建新用户
$portrait = $v['avatar']; // 头像
$user_params = [
'id'=> $v['rongyun_id'],//用户id
'name'=> $v['nickname'],//用户名称
'portrait'=> $portrait //用户头像
];
$user_return = $User->register($user_params);
if($user_return['code'] == 200) {
Db::name('user')->where('id',$v['id'])->update(['is_create'=>1,'rongyun_token'=>$user_return['token']]); // 融云账号状态更新为已创建
}
Log::write(date('Y-m-d H:i') . '用户注册成功,返回结果:' . json_encode($user_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
}
}
// 创建群组
$chat_id = '';
$chat_name = '';
$Group = $RongSDK->getGroup();
$group_params = [
'id'=> $chat_id,//群组 id
'name'=> $chat_name,//群组名称
'members'=> $user_rongyun_ids //群成员 列表
];
$group_return = $Group->create($group_params);
Log::write(date('Y-m-d H:i') . '创建群组成功,返回结果:' . json_encode($group_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
return [
'chat_id' => $chat_id,
'chat_name' => $chat_name
];
}
return [];
}
}
\ No newline at end of file
... ...
... ... @@ -180,6 +180,7 @@ class User extends Api
'updatetime' => $currentTime,
'loginip' => $ip,
'logintime' => $currentTime,
'rongyun_id' => 'xiaowei_'.substr($openid,-6)
]);
$row = Db::name("third")->insert([
'openid' => $openid,
... ...
... ... @@ -163,6 +163,8 @@ return [
'path' => LOG_PATH,
// 日志记录级别
'level' => [],
// crontab日志单独记录
'apart_level' => ['rongyun_log'],
],
// +----------------------------------------------------------------------
// | Trace设置 开启 app_trace 后 有效
... ...
<?php
return [
'config' => [
'appkey' => '3argexb63q3he',
'appsecret' => '1nb2RBxREmTICB',
]
];
\ No newline at end of file
... ...
... ... @@ -5,6 +5,8 @@ namespace app\index\controller;
use app\common\controller\Frontend;
use RongCloud\Lib\Utils;
use RongCloud\RongCloud;
use think\Db;
use think\Log;
class Index extends Frontend
{
... ... @@ -24,32 +26,57 @@ class Index extends Frontend
return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
}
// 融云创建用户
// 融云创建用户、群组
public function test() {
// 应用配置参数
$config = [
'appkey' => '3argexb63q3he',
'appsecret' => '1nb2RBxREmTICB',
];
$user_ids = '3,4';
$config = config('rongyun.config'); // 应用配置参数
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$portrait = "http://7xogjk.com1.z0.glb.clouddn.com/IuDkFprSQ1493563384017406982"; // 头像
$User = $RongSDK->getUser();
$params = [
'id'=> 'xiaowei_master',//用户id
'name'=> 'xiaowei_admin',//用户名称
'portrait'=> $portrait //用户头像
];
Utils::dump("用户注册成功",$User->register($params));
$users = Db::name('user')->whereIn('id',$user_ids)->column('id,nickname,avatar,rongyun_id,is_create'); // 获取用户信息
if($users) {
$user_rongyun_ids = [];
$User = $RongSDK->getUser();
foreach ($users as $k=>$v) {
$user_rongyun_ids[] = [
'id' => $v['rongyun_id']
];
if($v['is_create'] == 0) {
// 创建新用户
$portrait = $v['avatar']; // 头像
$user_params = [
'id'=> $v['rongyun_id'],//用户id
'name'=> $v['nickname'],//用户名称
'portrait'=> $portrait //用户头像
];
$user_return = $User->register($user_params);
if($user_return['code'] == 200) {
Db::name('user')->where('id',$v['id'])->update(['is_create'=>1,'rongyun_token'=>$user_return['token']]); // 融云账号状态更新为已创建
}
Log::write(date('Y-m-d H:i') . '用户注册成功,返回结果:' . json_encode($user_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
}
}
// 创建群组
$chat_id = '';
$chat_name = '';
$Group = $RongSDK->getGroup();
$group_params = [
'id'=> $chat_id,//群组 id
'name'=> $chat_name,//群组名称
'members'=> $user_rongyun_ids //群成员 列表
];
$group_return = $Group->create($group_params);
Log::write(date('Y-m-d H:i') . '创建群组成功,返回结果:' . json_encode($group_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
return [
'chat_id' => $chat_id,
'chat_name' => $chat_name
];
}
return [];
}
// 创建群组
public function group() {
// 应用配置参数
$config = [
'appkey' => '3argexb63q3he',
'appsecret' => '1nb2RBxREmTICB',
];
$config = config('rongyun.config');
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$Group = $RongSDK->getGroup();
... ... @@ -66,10 +93,7 @@ class Index extends Frontend
// 群组用户禁言
public function group_dump_add() {
// 应用配置参数
$config = [
'appkey' => '3argexb63q3he',
'appsecret' => '1nb2RBxREmTICB',
];
$config = config('rongyun.config');
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$Group = $RongSDK->getGroup()->Gag();
$params = [
... ... @@ -85,10 +109,7 @@ class Index extends Frontend
// 解除群组用户禁言
public function group_dump_remove() {
// 应用配置参数
$config = [
'appkey' => '3argexb63q3he',
'appsecret' => '1nb2RBxREmTICB',
];
$config = config('rongyun.config');
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$Group = $RongSDK->getGroup()->Gag();
$params = [
... ... @@ -99,4 +120,21 @@ class Index extends Frontend
];
Utils::dump("解除禁言成功",$Group->remove($params));
}
// 指定群组用户全部禁言
public function group_mute() {
// 应用配置参数
$config = config('rongyun.config');
$RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
$time = time();
$orders = Db::name('order')->whereBetween('expirationtime',[$time-60,$time])->select(); // 查询已经过期的群聊
$Group = $RongSDK->getGroup()->MuteAllMembers();
foreach ($orders as $k=>$v) {
$params = [
'id'=> $v['chat_id'],//群组 id
];
$return = $Group->add($params);
Log::write(date('Y-m-d H:i') . '添加指定群组全部禁言成功,返回结果:' . json_encode($return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
}
}
}
... ...