Commit 2e9ccaf8 authored by clone's avatar clone

用户体系 注册返回唯一id

parent 828cebcc
......@@ -36,8 +36,8 @@ class AppChat extends Basic
{
parent::__construct($request);
$this->_push = new RPush();
$this->_chat = new ChatService();
$this->_push = new RPush();
$this->_chat = new ChatService();
}
......@@ -69,22 +69,28 @@ class AppChat extends Basic
public function userChat()
{
$params = array(
"user_id" => 1,
"mobile" => "13817616471",
"source" => 1 //1经纪人 2用户
"user_id" => 2,
"mobile" => "18205625020",
"source" => 2 //1经纪人 2用户
);
//$params = $this->params;
$user_id = $params['sender'];
$mobile = $params['receiver'];
$source = $params['msg_type'];
$user_id = $params['user_id'];
$mobile = $params['mobile'];
$source = $params['source'];
if (!$user_id || !$mobile || !$source) {
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
if (!$target_type || !$target || !$type || !$msg_content || !$from) {
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) {
return $this->response("200",["msg" => "消息发送成功"]);
return $this->response("200", [ "msg" => "消息发送成功" ]);
}
......
<?php
namespace app\chat\service;
/**
* Created by PhpStorm.
* User : zw
......@@ -9,8 +10,10 @@ namespace app\chat\service;
* Intro:
*/
use app\chat\utils\RegisterUtil;
use app\chat\utils\RPush;
use app\model\Agents;
use app\model\ChatUser;
use app\model\Users;
class ChatService
......@@ -32,23 +35,57 @@ class ChatService
protected $userModel;
protected $agentsModel;
protected $chatUserModel;
public function __construct()
{
$this->userModel = new Users();
$this->agentsModel = new Agents();
$this->userModel = new Users();
$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
* @param $userId integer 用户id
* @param $mobile string 手机号
* @param $source integer 来源 '1经纪人 2用户'
* @return string
*/
public function createOnlyId($userId, $mobile, $source)
public function createOnlyId($userId, $source)
{
//todo 1.验证用户是否存在 经纪人和用户 2.根据规则生成唯一id 3.判断唯一id是否登录中,登陆中则调用退出接口 4.返回唯一id
$onlyId = "";
switch ($source) {
case 1:
......@@ -58,16 +95,29 @@ class ChatService
break;
case 2:
$userResult = $this->userModel->selectUser($userId);
if (count($userResult) > 1)
if ($userResult->id)
$onlyId = self::SOURCE_TYPE_APP . $userId;
break;
}
if (!$onlyId) {
return array( "code" => 101, "msg" => "没找到用户信息" );
//return array( "code" => 101, "msg" => "没找到用户信息" );
return null;
}
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
* @param $accessToken
* @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) {
case self::MSG_TYPE_USERS:
......@@ -94,12 +144,12 @@ class ChatService
//todo 判断聊天室是否存在
break;
default:
return ["code"=> 101,"msg"=>"消息类型错误"];
return [ "code" => 101, "msg" => "消息类型错误" ];
}
//todo
$this->insertMsg();
$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;
}
......@@ -162,5 +212,26 @@ class ChatService
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