QqCache.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * QqCache.php UTF-8
  4. * QQ信息
  5. *
  6. * @date : 2018/5/3 15:12
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\help;
  13. use huo\controller\common\Base;
  14. use huo\model\conf\QqConfModel;
  15. use huolib\constant\CommonConst;
  16. use think\Cache;
  17. class QqCache extends Base {
  18. protected $cache_tag = 'game_help_tag';
  19. public static function ins() {
  20. return new static();
  21. }
  22. /**
  23. * 获取QQ KEY
  24. *
  25. * @param string $qq_id
  26. *
  27. * @return string
  28. */
  29. private function getQQKey($qq_id) {
  30. return 'qq_key_'.$qq_id;
  31. }
  32. /**
  33. * 获取账户信息
  34. *
  35. * @param string $qq_id
  36. *
  37. * @return array|bool|mixed
  38. */
  39. public function getInfoByQqId($qq_id) {
  40. $_key = $this->getQQKey($qq_id);
  41. $_qq_data_json = Cache::get($_key);
  42. $_qq_data = json_decode($_qq_data_json, true);
  43. if (!is_array($_qq_data)) {
  44. $_qq_data = $_qq_data_json;
  45. }
  46. if (!is_array($_qq_data) || empty($_qq_data)) {
  47. $_qq_data = (new QqConfModel())->getInfoByQqId($qq_id);
  48. if (empty($_qq_data)) {
  49. return false;
  50. }
  51. $this->saveQqCache($qq_id, $_qq_data);
  52. }
  53. return $_qq_data;
  54. }
  55. /**
  56. * 保存玩家cache 数据
  57. *
  58. * @param $qq_id
  59. * @param $_qq_data
  60. * @param int $ttl
  61. */
  62. public function saveQqCache($qq_id, $_qq_data, $ttl = CommonConst::CONST_HOUR_SECONDS) {
  63. $_key = $this->getQQKey($qq_id);
  64. Cache::tag($this->cache_tag)->set($_key, json_encode($_qq_data), $ttl);
  65. }
  66. /**
  67. * 更新帐号信息
  68. *
  69. * @param string $qq_id
  70. * @param array $qq_data
  71. *
  72. * @return bool
  73. */
  74. public function updateQq($qq_id, $qq_data) {
  75. //$_key = $this->getQQKey($qq_id);
  76. Cache::clear($this->cache_tag);
  77. return (new QqConfModel())->updateQq($qq_data, $qq_id);
  78. }
  79. /**
  80. * 获取多QQ号
  81. *
  82. * @param $qq_ids
  83. * @param $type
  84. *
  85. * @return array|bool|mixed|null
  86. */
  87. public function getQqByIds($qq_ids, $type) {
  88. $_tag = $this->cache_tag;
  89. $_key = md5($_tag.json_encode($qq_ids).$type);
  90. $_qq_data_json = Cache::get($_key);
  91. $_qq_data = json_decode($_qq_data_json, true);
  92. if (!is_array($_qq_data)) {
  93. $_qq_data = $_qq_data_json;
  94. }
  95. if (!is_array($_qq_data) || empty($_qq_data)) {
  96. $_qq_data = (new QqConfModel())->getInfoByQqIds($qq_ids, $type);
  97. if (empty($_qq_data)) {
  98. return false;
  99. }
  100. Cache::tag($this->cache_tag)->set($_key, json_encode($_qq_data), CommonConst::CONST_HOUR_SECONDS);
  101. }
  102. return $_qq_data;
  103. }
  104. }