Commit 2e9ccaf8 authored by clone's avatar clone

用户体系 注册返回唯一id

parent 828cebcc
...@@ -69,22 +69,28 @@ class AppChat extends Basic ...@@ -69,22 +69,28 @@ class AppChat extends Basic
public function userChat() public function userChat()
{ {
$params = array( $params = array(
"user_id" => 1, "user_id" => 2,
"mobile" => "13817616471", "mobile" => "18205625020",
"source" => 1 //1经纪人 2用户 "source" => 2 //1经纪人 2用户
); );
//$params = $this->params; //$params = $this->params;
$user_id = $params['sender']; $user_id = $params['user_id'];
$mobile = $params['receiver']; $mobile = $params['mobile'];
$source = $params['msg_type']; $source = $params['source'];
if (!$user_id || !$mobile || !$source) { if (!$user_id || !$mobile || !$source) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误"); return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
} }
$onlyId = $this->_chat->createOnlyId($user_id, $mobile, $source); $only_arr = $this->_chat->register($user_id, $mobile, $source);
if ($only_arr["code"] == 200) {
return $this->response("200", [ "only_id" => $only_arr["only_id"] ]);
} else {
return $this->response("101", $only_arr["msg"]);
}
return $this->response("200", array( "onlyId" => $onlyId ));
} }
...@@ -100,9 +106,9 @@ class AppChat extends Basic ...@@ -100,9 +106,9 @@ class AppChat extends Basic
if (!$target_type || !$target || !$type || !$msg_content || !$from) { if (!$target_type || !$target || !$type || !$msg_content || !$from) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误"); return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
} }
$result = $this->_chat->sendMsg($target_type, $target, $type, $msg_content, $from,$this->accessToken); $result = $this->_chat->sendMsg($target_type, $target, $type, $msg_content, $from, $this->accessToken);
if (count($result) > 0) { if (count($result) > 0) {
return $this->response("200",["msg" => "消息发送成功"]); return $this->response("200", [ "msg" => "消息发送成功" ]);
} }
......
<?php <?php
namespace app\chat\service; namespace app\chat\service;
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User : zw * User : zw
...@@ -9,8 +10,10 @@ namespace app\chat\service; ...@@ -9,8 +10,10 @@ namespace app\chat\service;
* Intro: * Intro:
*/ */
use app\chat\utils\RegisterUtil;
use app\chat\utils\RPush; use app\chat\utils\RPush;
use app\model\Agents; use app\model\Agents;
use app\model\ChatUser;
use app\model\Users; use app\model\Users;
class ChatService class ChatService
...@@ -32,23 +35,57 @@ class ChatService ...@@ -32,23 +35,57 @@ class ChatService
protected $userModel; protected $userModel;
protected $agentsModel; protected $agentsModel;
protected $chatUserModel;
public function __construct() public function __construct()
{ {
$this->userModel = new Users(); $this->userModel = new Users();
$this->agentsModel = new Agents(); $this->agentsModel = new Agents();
$this->chatUserModel = new ChatUser();
}
/**
* @param $user_id
* @param $mobile
* @param $source
* @return array
*/
public function register($user_id, $mobile, $source)
{
//todo
$params["type"] = $source;
$params["user_id"] = $user_id;
$params["phone"] = $mobile;
$fields = "id,type,phone,only_id,password";
$chatUser = $this->chatUserModel->getChatUser($params, $fields);
if (count($chatUser) > 0) {
return [ "code" => "200", "only_id" => $chatUser[0]["only_id"] ];
} else {
$reg = new RegisterUtil();
$only_id = $this->createOnlyId($user_id, $source);
if (!$only_id) {
return [ "code" => "101", "msg" => "user msg not found" ];
}
$response = $reg->registerByCurl($only_id, $mobile);
if ($response) {
//todo insert
$this->insertChatUser($source, $user_id, $mobile, $only_id);
}
return [ "code" => 200, "only_id" => $only_id ];
}
} }
/** /**
* 生成唯一id * 生成唯一id
* @param $userId integer 用户id * @param $userId integer 用户id
* @param $mobile string 手机号
* @param $source integer 来源 '1经纪人 2用户' * @param $source integer 来源 '1经纪人 2用户'
* @return string * @return string
*/ */
public function createOnlyId($userId, $mobile, $source) public function createOnlyId($userId, $source)
{ {
//todo 1.验证用户是否存在 经纪人和用户 2.根据规则生成唯一id 3.判断唯一id是否登录中,登陆中则调用退出接口 4.返回唯一id
$onlyId = ""; $onlyId = "";
switch ($source) { switch ($source) {
case 1: case 1:
...@@ -58,16 +95,29 @@ class ChatService ...@@ -58,16 +95,29 @@ class ChatService
break; break;
case 2: case 2:
$userResult = $this->userModel->selectUser($userId); $userResult = $this->userModel->selectUser($userId);
if (count($userResult) > 1)
if ($userResult->id)
$onlyId = self::SOURCE_TYPE_APP . $userId; $onlyId = self::SOURCE_TYPE_APP . $userId;
break; break;
} }
if (!$onlyId) { if (!$onlyId) {
return array( "code" => 101, "msg" => "没找到用户信息" ); //return array( "code" => 101, "msg" => "没找到用户信息" );
return null;
} }
return $onlyId; return $onlyId;
}
/**
* 删除注册用户
* @param $only_id
* @param $access_token
* @return bool
*/
public function deleteUser($only_id, $access_token)
{
$reg = new RegisterUtil();
$reg->deleteByCurl($only_id, $access_token);
return true;
} }
...@@ -81,7 +131,7 @@ class ChatService ...@@ -81,7 +131,7 @@ class ChatService
* @param $accessToken * @param $accessToken
* @return bool * @return bool
*/ */
public function sendMsg($target_type, $target, $type, $msg_content, $from ,$accessToken) public function sendMsg($target_type, $target, $type, $msg_content, $from, $accessToken)
{ {
switch ($target_type) { switch ($target_type) {
case self::MSG_TYPE_USERS: case self::MSG_TYPE_USERS:
...@@ -94,12 +144,12 @@ class ChatService ...@@ -94,12 +144,12 @@ class ChatService
//todo 判断聊天室是否存在 //todo 判断聊天室是否存在
break; break;
default: default:
return ["code"=> 101,"msg"=>"消息类型错误"]; return [ "code" => 101, "msg" => "消息类型错误" ];
} }
//todo //todo
$this->insertMsg(); $this->insertMsg();
$rPush = new RPush(); $rPush = new RPush();
$rPush->send($target_type, $target, $type, $msg_content, $from,$accessToken,[ $this, 'saveSendStatus' ]); $rPush->send($target_type, $target, $type, $msg_content, $from, $accessToken, [ $this, 'saveSendStatus' ]);
//返回消息发送成功 //返回消息发送成功
return true; return true;
} }
...@@ -162,5 +212,26 @@ class ChatService ...@@ -162,5 +212,26 @@ class ChatService
return true; return true;
} }
/**
* @param $type
* @param $user_id
* @param $phone
* @param $only_id
* @return bool
*/
public function insertChatUser($type, $user_id, $phone, $only_id)
{
$params["type"] = $type;
$params["user_id"] = $user_id;
$params["phone"] = $phone;
$params["only_id"] = $only_id;
$params["password"] = $phone;
$params["last_login_time"] = date("Y-m-d H:i:s", time());
$params["created_at"] = date("Y-m-d H:i:s", time());
$params["updated_at"] = date("Y-m-d H:i:s", time());
$this->chatUserModel->addChatUser($params);
return true;
}
} }
\ No newline at end of file
<?php
namespace app\chat\utils;
use app\chat\consts\ConfigConst;
use app\chat\utils;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/18
* Time : 14:47
* Intro: 注册环信用户
*/
class RegisterUtil
{
const IM_REGISTER_USER = "/users";
const IM_DELETE_USER = "/users";
/**
* @param $username
* @param $password
* @return CurlResponse|bool
*/
public function registerByCurl($username, $password)
{
$arr = array(
'username' => $username,
'password' => $password,
);
$data = json_encode($arr);
$curl = new CurlUtil();
$curl->headers = [
"Accept" => "application/json",
"Content-Type" => "application/json;charset=utf-8",
];
$curl->options = [
"CURLOPT_SSL_VERIFYPEER" => 0,
"CURLOPT_SSL_VERIFYHOST" => 2,
];
$url = $this->buildSendUrl() . self::IM_REGISTER_USER;
$response = $curl->post($url, $data);
return $response;
}
/**
* @param $username
* @param $access_token
* @return CurlResponse|bool
*/
public function deleteByCurl($username, $access_token)
{
$arr = array(
'username' => $username,
);
$data = json_encode($arr);
$curl = new CurlUtil();
$curl->delete();
$curl->headers = [
"Accept" => "application/json",
"Content-Type" => "application/json;charset=utf-8",
'Authorization' => $access_token,
];
$curl->options = [
"CURLOPT_SSL_VERIFYPEER" => 0,
"CURLOPT_SSL_VERIFYHOST" => 2,
];
$url = $this->buildSendUrl() . self::IM_DELETE_USER;
$response = $curl->post($url, $data);
return $response;
}
/**
* 请求api
* @return string
*/
private function buildSendUrl()
{
return ConfigConst::API_PATH . ConfigConst::ORG_NAME . "/" . ConfigConst::APP_NAME;
}
}
\ No newline at end of file
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
a1.easemob.com FALSE / FALSE 1516264437 rememberMe deleteMe
<?php
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/18
* Time : 15:33
* Intro:
*/
namespace app\model;
use think\Model;
use think\Db;
class ChatUser extends Model
{
// 设置当前模型对应的完整数据表名称
protected $table = 'chat_user';
protected $db;
public function __construct()
{
$this->db = Db($this->table);
}
public function getChatUser($params, $field = "only_id,phone")
{
$data = $this->db->field($field)
->where($params)
->select();
return $data;
}
public function addChatUser($params)
{
Db::startTrans();
try {
$this->save($params);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
}
}
\ 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