BaseController.Class.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/11/6
  6. * Time: 11:15 AM
  7. */
  8. namespace Service;
  9. use Mall\Framework\Factory;
  10. class BaseController {
  11. /**
  12. * webstock链接保存在那个key下面
  13. */
  14. static $webstockAndKey = 'webstockAndKey';
  15. public function sendOutput($data, $errorcode = 0)
  16. {
  17. $errorcode = intval($errorcode);
  18. if ($errorcode) {
  19. $sendMessage = [
  20. 'state' => false,
  21. 'data' => $data,
  22. 'errorcode' => $errorcode
  23. ];
  24. } else {
  25. $sendMessage = [
  26. 'state' => true,
  27. 'data' => $data,
  28. 'errorcode' => $errorcode
  29. ];
  30. }
  31. return json_encode($sendMessage, JSON_UNESCAPED_UNICODE);
  32. }
  33. // 暂存一下每个webstock链接保存的位置
  34. public function webstockConnectInfo($fromId, $redisKey)
  35. {
  36. // webstock连接和存储关系
  37. $writeCache = Factory::cache('default')->hset(self::$webstockAndKey, $fromId, $redisKey);
  38. }
  39. // 销毁连接
  40. public function closeWebstock($fromId)
  41. {
  42. $data = Factory::cache('default')->hget(self::$webstockAndKey, $fromId);
  43. if(empty($data)){
  44. return false;
  45. }
  46. $data = explode('|', $data);
  47. $result = Factory::cache('default')->hdel($data[0], $data[1]);
  48. $result = Factory::cache('default')->hdel(self::$webstockAndKey, $fromId);
  49. return $result;
  50. }
  51. }