| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- namespace app\controller\api\user;
- use Easemob\Message;
- use Exception;
- use think\facade\Db;
- use Easemob\Auth;
- use Easemob\User;
- class Im
- {
- private $appKey = '1154240320150538#cxb-yhc';
- private $clientID = 'YXA6d1hICoWeQW6hzhQtSxqJ0A';
- private $clientSecret = 'YXA60XX-QAq1W2Lj2h1xE9WJAx9DzAM';
- //创建账号
- public function createImUser($account, $user_nickname, $password)
- {
- try {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $user = new User($auth);
- $data = array(
- 'username' => $account,
- 'password' => $password,
- 'nickname' => $user_nickname ?? $account,
- );
- Db::name("log")->insert(array('data' => '创建环信账号请求:' . json_encode($data)));//日志记录
- $result = $user->create($data);
- Db::name("log")->insert(array('data' => '创建环信账号返回:' . json_encode($result)));//日志记录
- return $result['uuid'];
- } catch (Exception $e) {
- Db::name("log")->insert(array('data' => '创建环信账号出错' . $e->getMessage()));//日志记录
- }
- }
- /**
- * 获取环信用户
- * @param $username
- * @return array
- * @throws \Exception
- */
- public function getImUser($username)
- {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $user = new User($auth);
- Db::name("log")->insert(array('data' => '获取环信账号请求:' . $username));//日志记录
- $result = $user->get($username);
- Db::name("log")->insert(array('data' => '获取环信账号返回:' . json_encode($result)));//日志记录
- return $result;
- }
- /**
- * updateUserPassword
- * @param $username
- * @return array
- * @throws \Exception
- */
- public function updateUserPassword($username,$password)
- {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $user = new User($auth);
- $data = array(
- 'username' => $username,
- 'password' => $password ? $password : '123456',
- );
- Db::name("log")->insert(array('data' => '环信修改密码:' . json_encode($data)));//日志记录
- $result = $user->updateUserPassword($username, $data['password']);
- Db::name("log")->insert(array('data' => '环信修改密码返回:' . json_encode($result)));//日志记录
- return $result;
- }
- /**
- * 创建环信用户
- * @param $account
- * @param $password
- * @param $user_number
- * @return mixed|string
- */
- public function MerCreateImUser($account, $password, $user_nickname)
- {
- try {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $user = new User($auth);
- $data = array(
- 'username' => $account,
- 'password' => $password ? $password : '123456',
- 'nickname' => $user_nickname ?? $account,
- );
- Db::name("log")->insert(array('data' => '创建环信账号请求:' . json_encode($data)));//日志记录
- $result = $user->create($data);
- Db::name("log")->insert(array('data' => '创建环信账号返回:' . json_encode($result)));//日志记录
- // if(isset($result['activated']) && $result['activated']==true){
- // Db::name('User') -> where(['account' => $account]) -> update(['im'=>1,'im_uuid'=>$result['uuid']]);
- // }
- return $result['uuid'];
- } catch (Exception $e) {
- Db::name("log")->insert(array('data' => '创建环信账号出错' . $e->getMessage()));//日志记录
- }
- return "";
- }
- /**
- * 发送文本消息
- * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
- * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
- * @param array $message 消息内容
- * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
- * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
- * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
- * @return array
- */
- public function sendText($target_type,
- $target,
- $message,
- $from = 'admin',
- $sync_device = false,
- $isOnline = false
- )
- {
- try {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $messageObj = new Message($auth);
- $result = $messageObj->text($target_type,
- $target,
- $message,
- $from,
- $sync_device,
- $isOnline
- );
- Db::name("log")->insert(array('data' => '发送文本消息返回:' . json_encode($result)));//日志记录
- return $result;
- } catch (Exception $e) {
- Db::name("log")->insert(array('data' => '发送文本消息出错' . $e->getMessage()));//日志记录
- }
- }
- /**
- * 发送图片消息
- * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
- * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
- * @param array $message 消息内容
- * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
- * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
- * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
- * @return array
- */
- public function sendImage($target_type,
- $target,
- $message,
- $from = 'admin',
- $sync_device = false,
- $isOnline = false
- )
- {
- try {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $messageObj = new Message($auth);
- $result = $messageObj->image($target_type,
- $target,
- $message,
- $from,
- $sync_device,
- $isOnline
- );
- Db::name("log")->insert(array('data' => '发送图片消息返回:' . json_encode($result)));//日志记录
- return $result;
- } catch (Exception $e) {
- Db::name("log")->insert(array('data' => '发送图片消息出错' . $e->getMessage()));//日志记录
- }
- }
- /**
- * 发送语音消息
- * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
- * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
- * @param array $message 消息内容
- * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
- * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
- * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
- * @return array
- */
- public function sendAudio($target_type,
- $target,
- $message,
- $from = 'admin',
- $sync_device = false,
- $isOnline = false
- )
- {
- try {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $messageObj = new Message($auth);
- $result = $messageObj->audio($target_type,
- $target,
- $message,
- $from,
- $sync_device,
- $isOnline
- );
- Db::name("log")->insert(array('data' => '发送语音消息返回:' . json_encode($result)));//日志记录
- return $result;
- } catch (Exception $e) {
- Db::name("log")->insert(array('data' => '发送语音消息出错' . $e->getMessage()));//日志记录
- }
- }
- /**
- * 下载消息历史文件到本地
- * @param int $datetime 时间,每次只能获取一小时的消息,格式为 yyyyMMddHH 如 2018112717。
- * @return array|string
- */
- public function getHistoryAsUri(int $datetime)
- {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $messageObj = new Message($auth);
- return $messageObj->getHistoryAsUri($datetime);
- }
- /**
- * 获取用户离线消息数
- * @param string $username 用户名
- * @return array|string
- */
- public function countMissedMessages(string $username)
- {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $messageObj = new Message($auth);
- return $messageObj->countMissedMessages($username);
- }
- /**
- * 获取用户在线状态
- * @param string $username 要获取在线状态的用户名
- * @return array|string
- */
- public function isUserOnline(string $username)
- {
- $auth = new Auth($this->appKey, $this->clientID, $this->clientSecret);
- $messageObj = new User($auth);
- return $messageObj->isUserOnline($username);
- }
- /**
- * 创建群组
- * @param $uid int 环信用户id
- * @param $groupname string 群名
- * @param $type string 群类型
- */
- public function createGroup($uid, $groupname, $type)
- {
- //p:商户群。d:代理商群
- try {
- $data['groupname'] = $groupname; //群名
- $data['desc'] = $type == 'p' ? '商户群' : '推广群'; //商户群,推广群
- switch ($type) {
- case 'p':
- $data['desc'] = '商户群';
- break;
- case 'd':
- $data['desc'] = '代理群';
- break;
- case 't':
- $data['desc'] = '推广群';
- break;
- }
- $data['public'] = true;
- $data['maxusers'] = 100; //群组成员最大数(包括群主),值为数值类型,默认值200
- $data['allowinvites'] = false;//不允许群成员邀请别人加入此群
- $data['members_only'] = false;
- $data['owner'] = $uid;//群主
- // $data['members'] = '';//群成员id
- // $data['custom'] = '';//群介绍
- $url = "http://im.bjtdba.com/api/im/create_group";
- $res = $this->posturl($url, $data);
- if ($res['code'] == 200) {
- Db::name('ImGroup')->insert([
- 'uid' => $uid,
- 'groupname' => $data['groupname'],
- 'desc' => $data['desc'],
- 'maxusers' => $data['maxusers'],
- 'allowinvites' => $data['allowinvites'],
- 'members_only' => $data['members_only'],
- 'owner' => $data['owner'],
- 'groupid' => $res['message'],
- 'type' => $type
- ]);
- return $res['message'];
- }
- } catch (Exception $e) {
- Db::name("log")->insert(array('data' => '创建环信群组出错' . $e->getMessage()));//日志记录
- }
- }
- /**
- * 加群
- * @param $group_id string 群组id
- * @param $user_id string 环信用户id
- * @param $uid int 储蓄保用户id
- */
- public function createGroupUser($group_id, $user_id, $uid)
- {
- try {
- $data['group_id'] = $group_id;
- $data['user_id'] = $user_id;
- $url = "http://im.bjtdba.com/api/im/create_group_user";
- $res = $this->posturl($url, $data);
- if ($res['code'] == 200) {
- Db::name('imGroupUsers')->insert([
- 'group_id' => $group_id,
- 'im_uid' => $user_id,
- 'uid' => $uid,
- 'time' => time()
- ]);
- }
- } catch (Exception $e) {
- Db::name("log")->insert(array('data' => '环信加群出错' . $e->getMessage()));//日志记录
- }
- }
- public function posturl($url, $data)
- {
- if (is_array($data))
- $data = http_build_query($data);
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- // curl_setopt($curl,CURLOPT_HEADER, 1 ); // 过滤HTTP头
- // curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
- curl_setopt($curl, CURLOPT_POST, true); // post传输数据
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);// post传输数据
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//严格认证
- $output = curl_exec($curl);
- curl_close($curl);
- Db::name("log")->insert(array('data' => '环信请求结果' . $output));//日志记录
- return json_decode($output, true);
- }
- }
|