Commit ae77e04f authored by zhuwei's avatar zhuwei

组群创建

parent 123b7782
...@@ -134,11 +134,11 @@ class AppChat extends Basic ...@@ -134,11 +134,11 @@ class AppChat extends Basic
|| !isset($params['source']) || !isset($params['is_user']) || !isset($params['from'])) { || !isset($params['source']) || !isset($params['is_user']) || !isset($params['from'])) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误"); return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
} }
/* $params = array( /* $params = array(
'msg_content' => '111', 'msg_content' => 'ceshi666',
'target_type' => 'users', 'target_type' => 'users',
'source' => '1', 'source' => '1',
'from' => 'app_64', 'from' => 'admin',
'type' => '1', 'type' => '1',
'is_user' => '0', 'is_user' => '0',
'target' => '18112347151', 'target' => '18112347151',
......
...@@ -10,13 +10,61 @@ namespace app\chat\controller; ...@@ -10,13 +10,61 @@ namespace app\chat\controller;
* Intro: * Intro:
*/ */
use app\chat\extend\Basic; use app\chat\extend\Basic;
use app\chat\utils\UGroup;
use app\model\ChatGroup;
use app\model\ChatGroupMember;
class Group extends Basic class Group extends Basic
{ {
public function createGroupByOnlyId() public function createGroupByOnlyId()
{ {
//todo 组群创建 1.存表,2调用环信接口 //todo 组群创建 1.存表,2调用环信接口
//$params = $this->params;
//exit;
$params['group_name']='雷神先锋';
$params['group_users']=["18112347151"];
$params['group_users_admin']="admin";
if (!isset($params['group_name']) || !isset($params['group_users']) || !isset($params['group_users_admin'])) {
return $this->response("300", "参数不全", ['remote_groupid'=>'']);
}
$group_name = $params['group_name'];
$group_users = $params['group_users'];
$group_users_admin = $params['group_users_admin'];
$UGroup= new UGroup();
$result = $UGroup->createGroup($group_name, $group_users, $group_users_admin,$this->accessToken);
$result=json_decode($result,true);
//var_dump($result);
//判断环信群是否建立成功
if(isset($result['data']['groupid'])){
//群表记录数据
$user = new ChatGroup;
$user->group_name = $group_name;
$user->platform_group_id = $result['data']['groupid'];
$user->type = 0;
$user->is_close = 1;
$user->save();
//群id
$group_id = $user->id;
//var_dump($user->id);
//群成员表表记录数据
$group_users[]=$group_users_admin;
$msgModel = new ChatGroupMember();
foreach ($group_users as $k => $v) {
$msgModel->addChatMsgExt([
'unique_id' => $v,
'group_id' => $group_id,
"type" => ($v == $group_users_admin) ? 1 : 0 ,
"is_close" => 1]);
}
return $this->response("200", "request success", ['remote_groupid'=>$result['data']['groupid']]);
}else{
return $this->response("400", "建群失败", ['remote_groupid'=>'']);
}
} }
public function updateGroup() public function updateGroup()
...@@ -27,6 +75,59 @@ class Group extends Basic ...@@ -27,6 +75,59 @@ class Group extends Basic
public function delGroup() public function delGroup()
{ {
//todo 删除群组 1.改变我们group表的状态2.调用环信删群接口 //todo 删除群组 1.改变我们group表的状态2.调用环信删群接口
//$params = $this->params;
//exit;
$params['group_id']='雷神先锋';
if (!isset($params['group_name'])) {
return $this->response("300", "参数不全", ['remote_groupid'=>'']);
}
$group_id = $params['group_id'];
$UGroup= new UGroup();
$result = $UGroup->deleteGroup($group_id,$this->accessToken);
/* {
"action" : "delete",
"application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5",
"params" : { },
"uri" : "https://a1.easemob.com/easemob-demo/chatdemoui",
"entities" : [ ],
"data" : {
"success" : true,
"groupid" : "1411527886490154"
},
"timestamp" : 1411528112078,
"duration" : 15,
"organization" : "easemob-demo",
"applicationName" : "chatdemoui"
}*/
$result=json_decode($result,true);
//var_dump($result);
//判断环信群是否建立成功
if(isset($result['data']['success']) and $result['data']['success']){
//群表记录数据
$user = new ChatGroup;
$user->group_name = $group_name;
$user->type = 0;
$user->is_close = 1;
$user->save();
//群id
$group_id = $user->id;
//var_dump($user->id);
//群成员表表记录数据
$group_users[]=$group_users_admin;
$msgModel = new ChatGroupMember();
foreach ($group_users as $k => $v) {
$msgModel->addChatMsgExt([
'unique_id' => $v,
'group_id' => $group_id,
"type" => ($v == $group_users_admin) ? 1 : 0 ,
"is_close" => 1]);
}
return $this->response("200", "request success", ['remote_groupid'=>$result['data']['groupid']]);
}else{
return $this->response("400", "建群失败", ['remote_groupid'=>'']);
}
} }
/** /**
......
<?php
namespace app\chat\utils;
use app\chat\consts\ConfigConst;
use Think\Log;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/8
* Time : 17:17
* Intro: 发送消息类
*/
class UGroup
{
/** 创建一个群
* 朱伟 2018年01月25日
*/
public function createGroup($group_name, $group_users, $group_users_admin='admin',$access_token)
{
$arr= array(
"groupname"=>$group_name, //群组名称,此属性为必须的
"desc"=>"good", //群组描述,此属性为必须的
"public"=>true, //是否是公开群,此属性为必须的
"maxusers"=>300, //群组成员最大数(包括群主),值为数值类型,默认值200,最大值2000,此属性为可选的
"members_only"=>true, // 加入群是否需要群主或者群管理员审批,默认是false
"allowinvites"=> true, //是否允许群成员邀请别人加入此群。 true:允许群成员邀请人加入此群,false:只有群主或者管理员才可以往群里加人。
"owner"=>$group_users_admin, //群组的管理员,此属性为必须的
"members"=>$group_users //群组成员,此属性为可选的,但是如果加了此项,数组元素至少一个(注:群主jma1不需要写入到members里面)
);
$data = json_encode($arr);
$curl = new \app\chat\utils\CurlUtil();
$curl->headers = [
"Accept" => "application/json",
"Content-Type" => "application/json;charset=utf-8",
'Authorization' => "Bearer " . $access_token,
];
$curl->options = [
"CURLOPT_SSL_VERIFYPEER" => 0,
"CURLOPT_SSL_VERIFYHOST" => 2,
];
$url = $this->buildSendUrl();
$response = $curl->post($url, $data);
Log::record('------createGroup-------' . json_encode($response), "info");
return $response;
}
/** 创建一个群
* 朱伟 2018年01月25日
*/
public function deleteGroup($group_id,$access_token)
{
$arr= array();
$data = json_encode($arr);
$curl = new \app\chat\utils\CurlUtil();
$curl->headers = [
"Accept" => "application/json",
"Content-Type" => "application/json;charset=utf-8",
'Authorization' => "Bearer " . $access_token,
];
$curl->options = [
"CURLOPT_SSL_VERIFYPEER" => 0,
"CURLOPT_SSL_VERIFYHOST" => 2,
];
$url = $this->buildSendUrl().'/'.$group_id;
$response = $curl->delete($url, $data);
Log::record('-------deleteGroup------' . json_encode($response), "info");
return $response;
}
/**
* 请求api
* @return string
*/
private function buildSendUrl()
{
return ConfigConst::API_PATH . ConfigConst::ORG_NAME . "/" . ConfigConst::APP_NAME . "/chatgroups";
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/18
* Time : 15:33
* Intro:
*/
namespace app\model;
use think\Model;
use think\Db;
class ChatGroup extends Model
{
// 设置当前模型对应的完整数据表名称
protected $table = 'chat_group';
protected $db;
public function __construct()
{
$this->db = Db::name($this->table);
}
public function addChatMsgExt($params)
{
Db::startTrans();
try {
$this->db->insert($params);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
return 0;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/18
* Time : 15:33
* Intro:
*/
namespace app\model;
use think\Model;
use think\Db;
class ChatGroupMember extends Model
{
// 设置当前模型对应的完整数据表名称
protected $table = 'chat_group_member';
protected $db;
public function __construct()
{
$this->db = Db::name($this->table);
}
public function addChatMsgExt($params)
{
Db::startTrans();
try {
$this->db->insert($params);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
return 0;
}
}
\ 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