chatmodel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018-10-11
  6. * Time: 下午 06:19
  7. */
  8. class ChatModel
  9. {
  10. public $roole_name = null;
  11. public $roole_password = null;
  12. public $roole_type = null;
  13. public $request = null;
  14. public $time = null;
  15. protected $uid = null;
  16. protected static $config = null;
  17. private $user_info = null;
  18. private $utils = null;
  19. private $service = null;
  20. //
  21. private static $pact = null;
  22. private static $clien_data = null;
  23. private $swoole = null;
  24. private $clien_fd = null;
  25. private $message = null;
  26. private $friendid_info = null;
  27. public function __construct($swoole_server = null, $fd = null, $data = null)
  28. {
  29. log_message::info("++++++++++++++++++ fd", $fd);
  30. $this->utils = new Utils();
  31. $this->service = new ChataSer();
  32. $this->time = date('Y-m-d H:i:s', time());
  33. $this->swoole = $swoole_server;
  34. $this->clien_fd = $fd;
  35. log_message::info("+++++++++++++++++++ clien_info", $data);
  36. $this->message = Utils::decodeDate($data);
  37. self::$pact = self::isParame($this->message, 'pact');
  38. self::$clien_data = self::isParame($this->message, 'data');
  39. log_message::info("++++++++++++++++++ pact", self::$pact);
  40. log_message::info("++++++++++++++++++ clien_data", self::$clien_data);
  41. self::$config = Utils::config();
  42. log_message::info("++++++++++++++++++config", json_encode(self::$config));
  43. $pact_action = $this->IsPact();
  44. log_message::info("++++++++++++++++++ pact_action ", $pact_action);
  45. if (method_exists('ChatModel', $pact_action)) {
  46. log_message::info("++++++++++++++++++", $pact_action);
  47. return $this->$pact_action();
  48. } else {
  49. log_message::info("++++++++++++++++++ pact_action is function false", $pact_action);
  50. }
  51. }
  52. /***
  53. * 初始基础配置数据
  54. * @return bool
  55. */
  56. public function httpData($type = null)
  57. {
  58. //$request = file_get_contents("php://input");
  59. /* $this->request = $_REQUEST;
  60. log_message::info(json_encode($_REQUEST));
  61. $this->roole_name = self::isParame($_REQUEST, 'account');
  62. $this->roole_password = self::isParame($_REQUEST, 'paw');
  63. $this->roole_type = self::isParame($_REQUEST, 'type');*/
  64. }
  65. public function friendInfo()
  66. {
  67. $uid = self::$clien_data['id'];
  68. $res = $this->service->friendidInfo($uid);
  69. if ($res) {
  70. return Utils::errorCode($res, SUCCESS);
  71. }
  72. return Utils::errorCode('好友列表空');
  73. }
  74. /***
  75. * 登录
  76. */
  77. public function login()
  78. {
  79. log_message::info("~~~~~~~~~~~~~~~~ is login");
  80. $account = self::$clien_data['account'];
  81. $password = self::$clien_data['password'];
  82. if (isDatas($account) && isDatas($password)) {
  83. log_message::info("~~~~~~~~~~~~~~~~ is login and account a pass is ok");
  84. $time = date('Y-m-d H:i:s', time());
  85. $data = [
  86. 'account' => $account,
  87. 'nick_name' => $account,
  88. 'password' => $password,
  89. 'creat_at' => $time,
  90. 'login_at' => $time,
  91. 'fd' => $this->clien_fd,
  92. 'status' => ONE,
  93. ];
  94. $this->user_info = $this->service->saveCharUserInfo($data);
  95. if (isDatas($this->user_info)) {
  96. // 获取好友列表信息
  97. //
  98. $this->user_info['friendinfo'] = $this->service->friendidInfo($this->user_info['id']);
  99. log_message::info(Utils::errorCode($this->user_info, SUCCESS, 'login'));
  100. return $this->swoole_pull(Utils::errorCode($this->user_info, SUCCESS, 'login'));
  101. }
  102. }
  103. log_message::info("账号密码错误!");
  104. $this->swoole_pull(Utils::errorCode("账号密码错误"));
  105. return false;
  106. }
  107. public function intopic()
  108. {
  109. //{'pact': 'intopic', 'data': {"history_receive_info": receive_info, "receive_info": data}};
  110. $recive_dat = self::$clien_data['receive_info'];
  111. $history_recive_dat = self::$clien_data['history_receive_info'];
  112. log_message::info("intopic******recive_dat*************", $recive_dat);
  113. log_message::info("intopic******history_recive_dat*************", $history_recive_dat);
  114. echo "-------------------------" . "<br>";
  115. //$recive_dat = json_decode($recive_dat, true);
  116. //$history_recive_dat = json_decode ($history_recive_dat, true);
  117. $sumdata = null;
  118. $history_recive = isDatas($history_recive_dat) ? $history_recive_dat : $recive_dat;
  119. $dim = Utils::arrayDimension($history_recive);
  120. log_message::info("intopic dim**********", $dim);
  121. if ($dim == ONE) {
  122. $sumdata[] = $history_recive;
  123. $sumdata[] = $recive_dat;
  124. $indata = json_encode($sumdata, JSON_UNESCAPED_UNICODE);
  125. log_message::info("[][][][][][][]intopic dim**********", $dim, $indata);
  126. return $this->swoole_pull(Utils::errorCode($indata, SUCCESS, 'intopic'));
  127. }
  128. if ($dim == TWO) {
  129. //$sumdata = array_push($history_recive_dat, $recive_dat);
  130. $history_recive_dat[time()] = $recive_dat;
  131. $sumdata = $history_recive_dat;
  132. $indata = json_encode($sumdata, JSON_UNESCAPED_UNICODE);
  133. log_message::info("[][][][][][][]intopic*************dim", $dim, $indata);
  134. return $this->swoole_pull(Utils::errorCode($indata, SUCCESS, 'intopic'));
  135. }
  136. return $this->swoole_pull(Utils::errorCode('msg data is null', SUCCESS, 'intopic'));
  137. }
  138. public function sende_message()
  139. {
  140. //data = {"pact": "sende_message", "data": {"msg": msg, "friendid": friendid, "user_id": userid}};
  141. $uid = self::$clien_data['user_id'];
  142. // 接收者id
  143. $friendid = self::$clien_data['friendid'];
  144. $info = self::$clien_data['msg'];
  145. $this->user_info = $this->service->getByIdUserinfo($uid);
  146. // 接收者信息
  147. $this->friendid_info = $this->service->getByIdUserinfo($friendid);
  148. $friendid_info_fd = $this->friendid_info['fd'];
  149. if ($friendid_info_fd) {
  150. // 中心根据接收者
  151. // 当返回数据的时候好友的相关角色就互换了
  152. // 接收者就成了自己
  153. // 发消息的就成了推送者 及 friend_info
  154. $data = [
  155. 'friend_info' => $this->user_info, //
  156. 'user_info' => $this->friendid_info,
  157. 'info' => $info
  158. ];
  159. $data = Utils::errorCode($data, SUCCESS, 'sende_message');
  160. // 如果接收者不在线那么存储到数据缓存等到上线的时候进行推送给他
  161. if ($this->friendid_info['status'] == ZERO) {
  162. $topic_data = [
  163. 'receiveuid' => $friendid,
  164. 'senduid' => $uid,
  165. 'send_user_name' => $this->user_info['nick_name'],
  166. 'message_status' => ZERO,
  167. 'message' => $info,
  168. 'send_at' => $this->time
  169. ];
  170. $res = $this->service->addTopitInfo($topic_data);
  171. if (!$res) {
  172. log_message::info("发送消息失败!");
  173. $this->swoole_pull(Utils::errorCode("发送消息失败!"));
  174. }
  175. }
  176. $this->swoole->push($friendid_info_fd, $data);
  177. }
  178. // data = {"pact": "sende_message", "data": {"msg": msg, "friendid": friendid, "user_id": userid}};
  179. }
  180. public function index($pact)
  181. {
  182. switch ($pact) {
  183. case 'login' :
  184. $this->login();
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. /**
  191. * 退出
  192. */
  193. public function quit()
  194. {
  195. $this->clien_fd;
  196. $res = $this->service->setFdUser($this->clien_fd, ['status' => ZERO]);
  197. if ($res) {
  198. // 推送用户已经退出了
  199. log_message::info("退出状态更新成功");
  200. log_message::info("quit 已经退出了");
  201. } else {
  202. log_message::info("退出状态更新失败");
  203. }
  204. }
  205. /***
  206. * 注册
  207. * @return bool
  208. */
  209. public function registered()
  210. {
  211. $account = self::$clien_data['account'];
  212. $password = self::$clien_data['password'];
  213. if (isDatas($account) && isDatas($password)) {
  214. $time = date('Y-m-d H:i:s', time());
  215. $data = [
  216. 'nick_name' => $account,
  217. 'password' => $password,
  218. 'creat_at' => $time,
  219. 'login_at' => $time,
  220. 'fd' => $this->clien_fd,
  221. ];
  222. $req = $this->service->addUserInfo($data);
  223. if ($req) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. $this->swoole_pull(Utils::errorCode("账号密码为空"));
  229. return false;
  230. }
  231. /***
  232. * pact解码
  233. */
  234. public function IsPact()
  235. {
  236. if (self::isParame($this->message, 'pact')) {
  237. log_message::info("IsPact isParame 11");
  238. // 是否存在协议
  239. log_message::info("IsPact message 1110", $this->message['pact']);
  240. if ($this->isAction($this->message['pact'])) {
  241. log_message::info("IsPact isParame 22");
  242. return $this->message['pact'];
  243. }
  244. }
  245. log_message::info(Utils::errorCode('协议异常', FAILURE));
  246. $this->swoole_pull(Utils::errorCode('协议异常', FAILURE));
  247. return false;
  248. }
  249. public function isAction($pact)
  250. {
  251. log_message::info("isAction", json_encode(self::$config['server_pact']));
  252. log_message::info("isAction22", self::isParame(self::$config['server_pact'], $pact));
  253. if (in_array($pact, self::$config['server_pact'])) {
  254. return $pact;
  255. }
  256. return false;
  257. }
  258. public function swoole_pull($data)
  259. {
  260. $this->swoole->push($this->clien_fd, $data);
  261. }
  262. /***
  263. * 参数是否为空
  264. * @param $data
  265. * @return bool
  266. */
  267. public static function isParame($data, $key)
  268. {
  269. if ($red = isDatas($data, $key)) {
  270. return $red;
  271. }
  272. log_message::info("$key is null ");
  273. //exit(Utils::sendResults("$key is null"));
  274. //exit(Utils::errorCode("$key is null ", FAILURE));
  275. return null;
  276. }
  277. public function parameVerif()
  278. {
  279. if (isBlank($this->roole_name)) {
  280. log_message::info('roole_name is null');
  281. exit(Utils::sendResults("roole_name is null "));
  282. }
  283. if (isBlank($this->roole_password)) {
  284. log_message::info('roole_password is null');
  285. exit(Utils::sendResults("roole_password is null "));
  286. }
  287. $_SESSION['account'] = $this->roole_name;
  288. $_SESSION['password'] = $this->roole_password;
  289. $_SESSION['type'] = $this->roole_type;
  290. return true;
  291. }
  292. /***
  293. * 添加好友
  294. */
  295. public function addFriend()
  296. {
  297. }
  298. /***
  299. *
  300. */
  301. public function userVerif()
  302. {
  303. // 获取服务端用户密码
  304. // 正确返回true 跳转登录界面
  305. // else false 提示失败
  306. $this->initializeUserinfo();
  307. return $this->verifAccount();
  308. }
  309. /***
  310. * @param array $userdata
  311. * @return bool
  312. */
  313. public function initializeUserinfo($userdata = [])
  314. {
  315. $data = [
  316. 'nick_name' => $this->roole_name,
  317. 'account' => $this->roole_name,
  318. 'password' => $this->roole_password,
  319. 'creat_at' => date('Ymd', time()),
  320. 'type' => $this->roole_type
  321. //'sign_rule'=>$ruleinfo,
  322. ];
  323. $data = isDatas($userdata) ? $userdata : $data;
  324. $this->user_info = $this->service->saveCharUserInfo($data);
  325. // 如果type 是注册 但是已经存在用户或者 空的进行注册 返回注册成功 二类 返回已经存在用户
  326. // 如果type 是登录 但是
  327. // 最好是分开设置注册就是注册的函数 登录就是登录的函数 每次只要
  328. if ($this->user_info) {
  329. return $this->user_info;
  330. }
  331. return Utils::sendResults("获取信息失败");
  332. }
  333. public function verifAccount()
  334. {
  335. $roole_name = $this->roole_name;
  336. $roole_password = $this->roole_password;
  337. if ($roole_name = $this->user_info['account'] && $roole_password == $this->user_info['password']) {
  338. log_message::info(Utils::sendResults('ok...', $this->user_info));
  339. return Utils::sendResults('ok', $this->user_info);;
  340. }
  341. return false;
  342. }
  343. public function userLogin()
  344. {
  345. $db_user_info = $this->service->getdbCharUserInfo($this->roole_name);
  346. $this->uid = $db_user_info['id'];
  347. $this->user_info = $db_user_info;
  348. $roole_name = $this->roole_name;
  349. $roole_password = $this->roole_password;
  350. if ($roole_name = $db_user_info['account'] && $roole_password == $db_user_info['password']) {
  351. return Utils::sendResults('登录成功', $this->user_info, SUCCESS);
  352. }
  353. return Utils::sendResults('账号或密码错误!', $this->user_info, FAILURE);;
  354. }
  355. public function friendList()
  356. {
  357. $this->service->friendidInfo($this->uid);
  358. }
  359. //~
  360. }