MemTokenModel.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * MemTokenModel.php UTF-8
  4. * 玩家Token表
  5. *
  6. * @date : 2018/6/26 16:22
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : Beibao 1.0
  11. */
  12. namespace huo\model\member;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use huolib\constant\DeviceTypeConst;
  17. use huolib\constant\MemConst;
  18. use huolib\tool\StrUtils;
  19. use think\Cache;
  20. class MemTokenModel extends CommonModel {
  21. protected $name = 'mem_token';
  22. protected $cache_tag = CacheConst::TAG_MEM_TOKEN_DATA;
  23. protected $cache_prefix = CacheConst::CACHE_MEM_TOKEN_PREFIX;
  24. /**
  25. * 头像获取器
  26. *
  27. * @param $value
  28. *
  29. * @return string
  30. */
  31. public function getAvatarAttr($value) {
  32. if (!empty($value)) {
  33. return cmf_get_image_url($value);
  34. }
  35. return $value;
  36. }
  37. /**
  38. * 关联member表
  39. */
  40. public function member() {
  41. return $this->belongsTo('huo\model\member\MemberModel', 'id', 'mem_id')->setEagerlyType(0);
  42. }
  43. /**
  44. * 添加玩家Token
  45. *
  46. * @param int $mem_id
  47. * @param string $device_type
  48. *
  49. * @return bool|string
  50. */
  51. public function addToken($mem_id, $device_type = DeviceTypeConst::DEVICE_TYPE_WAP) {
  52. $_data['mem_id'] = $mem_id;
  53. $_data['create_time'] = time();
  54. $_data['update_time'] = $_data['create_time'];
  55. $_data['expire_time'] = $_data['update_time'] + MemConst::DEFAULT_EXPIRE_TIME;
  56. $_data['device_type'] = $device_type;
  57. $_data['token'] = StrUtils::genToken($_data['mem_id'].$device_type);
  58. if ($_obj = self::create($_data, true)) {
  59. $_data['id'] = $_obj->id;
  60. $_md_cache_key = $this->cache_prefix.$_data['mem_id'].$_data['device_type'];
  61. Cache::set($_md_cache_key, $_data, CommonConst::CONST_DAY_SECONDS);
  62. $_token_cache_key = $this->cache_prefix.$_data['token'].$_data['device_type'];
  63. Cache::set($_token_cache_key, $_data, CommonConst::CONST_DAY_SECONDS);
  64. return $_data['token'];
  65. } else {
  66. return false;
  67. }
  68. }
  69. /**
  70. * 更新玩家Token
  71. *
  72. * @param array $data
  73. *
  74. * @param int $id
  75. *
  76. * @return bool|string
  77. */
  78. public function editToken($data = [], $id) {
  79. $_map['id'] = $id;
  80. $_data = $data;
  81. $_data['update_time'] = time();
  82. $_data['expire_time'] = $_data['update_time'] + MemConst::DEFAULT_EXPIRE_TIME;
  83. $_data['token'] = StrUtils::genToken($_data['mem_id'].$_data['device_type'].$_data['token']);
  84. $_rs = self::update($_data, $_map, true);
  85. if (false === $_rs) {
  86. return false;
  87. } else {
  88. $_md_cache_key = $this->cache_prefix.$_data['mem_id'].$_data['device_type'];
  89. Cache::set($_md_cache_key, $_data, CommonConst::CONST_DAY_SECONDS);
  90. $_token_cache_key = $this->cache_prefix.$_data['token'].$_data['device_type'];
  91. Cache::set($_token_cache_key, $_data, CommonConst::CONST_DAY_SECONDS);
  92. $_old_token_cache_key = $this->cache_prefix.$data['token'].$_data['device_type'];
  93. Cache::set($_old_token_cache_key, $data, CommonConst::CONST_MINUTE_SECONDS);
  94. return $_data['token'];
  95. }
  96. }
  97. /**
  98. * 通过玩家设备获取Token信息
  99. *
  100. * @param int $mem_id
  101. * @param string $device_type
  102. *
  103. * @return bool|array
  104. */
  105. public function getTokenInfoByMemDevice($mem_id = 0, $device_type = DeviceTypeConst::DEVICE_TYPE_WAP) {
  106. $_map['mem_id'] = $mem_id;
  107. $_map['device_type'] = $device_type;
  108. $_md_cache_key = $this->cache_prefix.$mem_id.$device_type;
  109. $_data = Cache::get($_md_cache_key);
  110. if (!empty($_data)) {
  111. return $_data;
  112. }
  113. $_info = $this->where($_map)->find();
  114. if (false === $_info) {
  115. return false;
  116. }
  117. if (is_object($_info)) {
  118. $_info = $_info->toArray();
  119. }
  120. if (empty($_info)) {
  121. return [];
  122. }
  123. Cache::set($_md_cache_key, $_info, CommonConst::CONST_DAY_SECONDS);
  124. $_token_cache_key = $this->cache_prefix.$_info['token'].$device_type;
  125. Cache::set($_token_cache_key, $_info, CommonConst::CONST_DAY_SECONDS);
  126. return $_info;
  127. }
  128. /**
  129. * 更新玩家Token
  130. *
  131. * @param string $token
  132. * @param string $device_type
  133. *
  134. * @return bool|array
  135. */
  136. public function getTokenInfoByToken($token, $device_type) {
  137. $_map['token'] = $token;
  138. $_map['device_type'] = $device_type;
  139. $_token_cache_key = $this->cache_prefix.$token.$device_type;
  140. $_data = Cache::get($_token_cache_key);
  141. if (!empty($_data)) {
  142. return $_data;
  143. }
  144. $_info = $this->where($_map)->find();
  145. if (false === $_info) {
  146. return false;
  147. }
  148. if (is_object($_info)) {
  149. $_info = $_info->toArray();
  150. }
  151. if (empty($_info)) {
  152. return [];
  153. }
  154. Cache::set($_token_cache_key, $_info, CommonConst::CONST_DAY_SECONDS);
  155. $_md_cache_key = $this->cache_prefix.$_info['mem_id'].$_info['device_type'];
  156. Cache::set($_md_cache_key, $_info, CommonConst::CONST_DAY_SECONDS);
  157. return $_info;
  158. }
  159. /**
  160. * 删除玩家id关联的token
  161. *
  162. * @param $mem_id
  163. *
  164. * @return bool
  165. */
  166. public function deleteToken($mem_id) {
  167. $_map = ['mem_id' => $mem_id];
  168. $_list = $this->where($_map)->field('id,mem_id,token,device_type')->select();
  169. if (false == $_list) {
  170. return false;
  171. }
  172. if (empty($_list)) {
  173. return true;
  174. }
  175. if (is_object($_list)) {
  176. $_list = $_list->toArray();
  177. }
  178. foreach ($_list as $data) {
  179. $_map = ['id' => $data['id']];
  180. $_rs = $this->where($_map)->delete();
  181. if (false == $_rs) {
  182. return false;
  183. }
  184. $_token_cache_key = $this->cache_prefix.$data['token'].$data['device_type'];
  185. Cache::rm($_token_cache_key);
  186. $_md_cache_key = $this->cache_prefix.$data['mem_id'].$data['device_type'];
  187. Cache::rm($_md_cache_key);
  188. }
  189. return true;
  190. }
  191. }