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

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

@@ -313,6 +313,65 @@ class Common extends Api @@ -313,6 +313,65 @@ class Common extends Api
313 return $data ; 313 return $data ;
314 } 314 }
315 315
  316 + /**
  317 + * @ApiTitle (记录融云聊天内容)
  318 + * @ApiSummary (记录融云聊天内容)
  319 + * @ApiMethod (POST)
  320 + * @ApiRoute (/api/common/setOrderChat)
  321 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  322 + *
  323 + * @ApiParams (name="order_id", type="interger", required=true, description="订单id")
  324 + * @ApiParams (name="chat_id", type="string", required=true, description="融云群聊id")
  325 + * @ApiParams (name="send_id", type="string", required=true, description="发送人id")
  326 + * @ApiParams (name="receive_id", type="string", required=true, description="接收人id")
  327 + * @ApiParams (name="type", type="interger", required=true, description="消息类型:1=文字2=图片3=文件")
  328 + * @ApiParams (name="text", type="string", required=false, description="文字内容")
  329 + * @ApiParams (name="image", type="string", required=false, description="图片")
  330 + * @ApiParams (name="file", type="string", required=false, description="文件")
  331 + *
  332 + * @ApiReturn({
  333 + "code": 1,
  334 + "msg": "SUCCESS",
  335 + "time": "1553839125",
  336 + "data": {
  337 +
  338 + }
  339 + })
  340 + */
  341 + public function setOrderChat()
  342 + {
  343 + $validate = new Validate([
  344 + 'order_id' => 'require',
  345 + 'chat_id' => 'require',
  346 + 'send_id' => 'require',
  347 + 'receive_id' => 'require',
  348 + 'type' => 'require|in:1,2,3',
  349 + 'text' => 'require',
  350 + 'image' => 'require',
  351 + 'file' => 'require',
  352 + ]);
  353 +
  354 + $validate->message([
  355 + 'order_id.require' => '缺少参数order_id!',
  356 + 'chat_id.require' => '缺少参数chat_id!',
  357 + 'send_id.require' => '缺少参数send_id!',
  358 + 'receive_id.require' => '缺少参数receive_id!',
  359 + 'type.require' => '缺少参数type!',
  360 + 'type.in' => 'type参数错误!',
  361 + 'text.require' => '缺少参数text!',
  362 + 'image.require' => '缺少参数image!',
  363 + 'file.require' => '缺少参数file!',
  364 + ]);
  365 +
  366 + $data = $this->request->param();
  367 + if (!$validate->check($data)) {
  368 + $this->error($validate->getError());
  369 + }
  370 + $data['createtime'] = $data['updatetime'] = time();
  371 + Db::name("order_chat")->insertGetId($data);
  372 + $this->success('消息记录成功');
  373 + }
  374 +
316 public function create() 375 public function create()
317 { 376 {
318 $url = 'http://api-cn.ronghub.com/chatroom/create.json'; 377 $url = 'http://api-cn.ronghub.com/chatroom/create.json';
@@ -11,8 +11,10 @@ namespace app\api\controller; @@ -11,8 +11,10 @@ namespace app\api\controller;
11 11
12 use app\common\controller\Api; 12 use app\common\controller\Api;
13 use EasyWeChat\Foundation\Application; 13 use EasyWeChat\Foundation\Application;
  14 +use RongCloud\RongCloud;
14 use think\Db; 15 use think\Db;
15 use EasyWeChat\Payment\Order; 16 use EasyWeChat\Payment\Order;
  17 +use think\Log;
16 18
17 /** 19 /**
18 * 支付回调(无需调用) 20 * 支付回调(无需调用)
@@ -93,6 +95,12 @@ class Pay extends Api @@ -93,6 +95,12 @@ class Pay extends Api
93 $coupon['expirationtime'] = $coupon['createtime']+7*86400; 95 $coupon['expirationtime'] = $coupon['createtime']+7*86400;
94 Db::name('coupon')->insertGetId($coupon); 96 Db::name('coupon')->insertGetId($coupon);
95 Db::name('teacher')->where('id',$order['teacher_id'])->setInc('help_num'); 97 Db::name('teacher')->where('id',$order['teacher_id'])->setInc('help_num');
  98 + // 创建融云用户、群组
  99 + $rongyun_chat = $this->rongyun_handle($is_one['user_id'].','.$is_one['teacher_id']);
  100 + if($rongyun_chat) {
  101 + $update['chat_id'] = $rongyun_chat['chat_id'];
  102 + $update['chat_name'] = $rongyun_chat['chat_name'];
  103 + }
96 } else { // 用户支付失败 104 } else { // 用户支付失败
97 $update['status']=1; 105 $update['status']=1;
98 } 106 }
@@ -168,4 +176,51 @@ class Pay extends Api @@ -168,4 +176,51 @@ class Pay extends Api
168 }); 176 });
169 $response->send(); 177 $response->send();
170 } 178 }
  179 +
  180 +
  181 + // 融云创建用户、群组
  182 + public function rongyun_handle($user_ids) {
  183 + $config = config('rongyun.config'); // 应用配置参数
  184 + $RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
  185 + $users = Db::name('user')->whereIn('id',$user_ids)->column('id,nickname,avatar,rongyun_id,is_create'); // 获取用户信息
  186 + if($users) {
  187 + $user_rongyun_ids = [];
  188 + $User = $RongSDK->getUser();
  189 + foreach ($users as $k=>$v) {
  190 + $user_rongyun_ids[] = [
  191 + 'id' => $v['rongyun_id']
  192 + ];
  193 + if($v['is_create'] == 0) {
  194 + // 创建新用户
  195 + $portrait = $v['avatar']; // 头像
  196 + $user_params = [
  197 + 'id'=> $v['rongyun_id'],//用户id
  198 + 'name'=> $v['nickname'],//用户名称
  199 + 'portrait'=> $portrait //用户头像
  200 + ];
  201 + $user_return = $User->register($user_params);
  202 + if($user_return['code'] == 200) {
  203 + Db::name('user')->where('id',$v['id'])->update(['is_create'=>1,'rongyun_token'=>$user_return['token']]); // 融云账号状态更新为已创建
  204 + }
  205 + Log::write(date('Y-m-d H:i') . '用户注册成功,返回结果:' . json_encode($user_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
  206 + }
  207 + }
  208 + // 创建群组
  209 + $chat_id = '';
  210 + $chat_name = '';
  211 + $Group = $RongSDK->getGroup();
  212 + $group_params = [
  213 + 'id'=> $chat_id,//群组 id
  214 + 'name'=> $chat_name,//群组名称
  215 + 'members'=> $user_rongyun_ids //群成员 列表
  216 + ];
  217 + $group_return = $Group->create($group_params);
  218 + Log::write(date('Y-m-d H:i') . '创建群组成功,返回结果:' . json_encode($group_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
  219 + return [
  220 + 'chat_id' => $chat_id,
  221 + 'chat_name' => $chat_name
  222 + ];
  223 + }
  224 + return [];
  225 + }
171 } 226 }
@@ -180,6 +180,7 @@ class User extends Api @@ -180,6 +180,7 @@ class User extends Api
180 'updatetime' => $currentTime, 180 'updatetime' => $currentTime,
181 'loginip' => $ip, 181 'loginip' => $ip,
182 'logintime' => $currentTime, 182 'logintime' => $currentTime,
  183 + 'rongyun_id' => 'xiaowei_'.substr($openid,-6)
183 ]); 184 ]);
184 $row = Db::name("third")->insert([ 185 $row = Db::name("third")->insert([
185 'openid' => $openid, 186 'openid' => $openid,
@@ -163,6 +163,8 @@ return [ @@ -163,6 +163,8 @@ return [
163 'path' => LOG_PATH, 163 'path' => LOG_PATH,
164 // 日志记录级别 164 // 日志记录级别
165 'level' => [], 165 'level' => [],
  166 + // crontab日志单独记录
  167 + 'apart_level' => ['rongyun_log'],
166 ], 168 ],
167 // +---------------------------------------------------------------------- 169 // +----------------------------------------------------------------------
168 // | Trace设置 开启 app_trace 后 有效 170 // | Trace设置 开启 app_trace 后 有效
  1 +<?php
  2 +
  3 +return [
  4 + 'config' => [
  5 + 'appkey' => '3argexb63q3he',
  6 + 'appsecret' => '1nb2RBxREmTICB',
  7 + ]
  8 +];
@@ -5,6 +5,8 @@ namespace app\index\controller; @@ -5,6 +5,8 @@ namespace app\index\controller;
5 use app\common\controller\Frontend; 5 use app\common\controller\Frontend;
6 use RongCloud\Lib\Utils; 6 use RongCloud\Lib\Utils;
7 use RongCloud\RongCloud; 7 use RongCloud\RongCloud;
  8 +use think\Db;
  9 +use think\Log;
8 10
9 class Index extends Frontend 11 class Index extends Frontend
10 { 12 {
@@ -24,32 +26,57 @@ class Index extends Frontend @@ -24,32 +26,57 @@ class Index extends Frontend
24 return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']); 26 return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
25 } 27 }
26 28
27 - // 融云创建用户 29 + // 融云创建用户、群组
28 public function test() { 30 public function test() {
29 - // 应用配置参数  
30 - $config = [  
31 - 'appkey' => '3argexb63q3he',  
32 - 'appsecret' => '1nb2RBxREmTICB',  
33 - ]; 31 + $user_ids = '3,4';
  32 + $config = config('rongyun.config'); // 应用配置参数
34 $RongSDK = new RongCloud($config['appkey'],$config['appsecret']); 33 $RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
35 - $portrait = "http://7xogjk.com1.z0.glb.clouddn.com/IuDkFprSQ1493563384017406982"; // 头像  
36 - $User = $RongSDK->getUser();  
37 -  
38 - $params = [  
39 - 'id'=> 'xiaowei_master',//用户id  
40 - 'name'=> 'xiaowei_admin',//用户名称  
41 - 'portrait'=> $portrait //用户头像  
42 - ];  
43 - Utils::dump("用户注册成功",$User->register($params)); 34 + $users = Db::name('user')->whereIn('id',$user_ids)->column('id,nickname,avatar,rongyun_id,is_create'); // 获取用户信息
  35 + if($users) {
  36 + $user_rongyun_ids = [];
  37 + $User = $RongSDK->getUser();
  38 + foreach ($users as $k=>$v) {
  39 + $user_rongyun_ids[] = [
  40 + 'id' => $v['rongyun_id']
  41 + ];
  42 + if($v['is_create'] == 0) {
  43 + // 创建新用户
  44 + $portrait = $v['avatar']; // 头像
  45 + $user_params = [
  46 + 'id'=> $v['rongyun_id'],//用户id
  47 + 'name'=> $v['nickname'],//用户名称
  48 + 'portrait'=> $portrait //用户头像
  49 + ];
  50 + $user_return = $User->register($user_params);
  51 + if($user_return['code'] == 200) {
  52 + Db::name('user')->where('id',$v['id'])->update(['is_create'=>1,'rongyun_token'=>$user_return['token']]); // 融云账号状态更新为已创建
  53 + }
  54 + Log::write(date('Y-m-d H:i') . '用户注册成功,返回结果:' . json_encode($user_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
  55 + }
  56 + }
  57 + // 创建群组
  58 + $chat_id = '';
  59 + $chat_name = '';
  60 + $Group = $RongSDK->getGroup();
  61 + $group_params = [
  62 + 'id'=> $chat_id,//群组 id
  63 + 'name'=> $chat_name,//群组名称
  64 + 'members'=> $user_rongyun_ids //群成员 列表
  65 + ];
  66 + $group_return = $Group->create($group_params);
  67 + Log::write(date('Y-m-d H:i') . '创建群组成功,返回结果:' . json_encode($group_return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
  68 + return [
  69 + 'chat_id' => $chat_id,
  70 + 'chat_name' => $chat_name
  71 + ];
  72 + }
  73 + return [];
44 } 74 }
45 75
46 // 创建群组 76 // 创建群组
47 public function group() { 77 public function group() {
48 // 应用配置参数 78 // 应用配置参数
49 - $config = [  
50 - 'appkey' => '3argexb63q3he',  
51 - 'appsecret' => '1nb2RBxREmTICB',  
52 - ]; 79 + $config = config('rongyun.config');
53 $RongSDK = new RongCloud($config['appkey'],$config['appsecret']); 80 $RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
54 $Group = $RongSDK->getGroup(); 81 $Group = $RongSDK->getGroup();
55 82
@@ -66,10 +93,7 @@ class Index extends Frontend @@ -66,10 +93,7 @@ class Index extends Frontend
66 // 群组用户禁言 93 // 群组用户禁言
67 public function group_dump_add() { 94 public function group_dump_add() {
68 // 应用配置参数 95 // 应用配置参数
69 - $config = [  
70 - 'appkey' => '3argexb63q3he',  
71 - 'appsecret' => '1nb2RBxREmTICB',  
72 - ]; 96 + $config = config('rongyun.config');
73 $RongSDK = new RongCloud($config['appkey'],$config['appsecret']); 97 $RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
74 $Group = $RongSDK->getGroup()->Gag(); 98 $Group = $RongSDK->getGroup()->Gag();
75 $params = [ 99 $params = [
@@ -85,10 +109,7 @@ class Index extends Frontend @@ -85,10 +109,7 @@ class Index extends Frontend
85 // 解除群组用户禁言 109 // 解除群组用户禁言
86 public function group_dump_remove() { 110 public function group_dump_remove() {
87 // 应用配置参数 111 // 应用配置参数
88 - $config = [  
89 - 'appkey' => '3argexb63q3he',  
90 - 'appsecret' => '1nb2RBxREmTICB',  
91 - ]; 112 + $config = config('rongyun.config');
92 $RongSDK = new RongCloud($config['appkey'],$config['appsecret']); 113 $RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
93 $Group = $RongSDK->getGroup()->Gag(); 114 $Group = $RongSDK->getGroup()->Gag();
94 $params = [ 115 $params = [
@@ -99,4 +120,21 @@ class Index extends Frontend @@ -99,4 +120,21 @@ class Index extends Frontend
99 ]; 120 ];
100 Utils::dump("解除禁言成功",$Group->remove($params)); 121 Utils::dump("解除禁言成功",$Group->remove($params));
101 } 122 }
  123 +
  124 + // 指定群组用户全部禁言
  125 + public function group_mute() {
  126 + // 应用配置参数
  127 + $config = config('rongyun.config');
  128 + $RongSDK = new RongCloud($config['appkey'],$config['appsecret']);
  129 + $time = time();
  130 + $orders = Db::name('order')->whereBetween('expirationtime',[$time-60,$time])->select(); // 查询已经过期的群聊
  131 + $Group = $RongSDK->getGroup()->MuteAllMembers();
  132 + foreach ($orders as $k=>$v) {
  133 + $params = [
  134 + 'id'=> $v['chat_id'],//群组 id
  135 + ];
  136 + $return = $Group->add($params);
  137 + Log::write(date('Y-m-d H:i') . '添加指定群组全部禁言成功,返回结果:' . json_encode($return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
  138 + }
  139 + }
102 } 140 }