mb_user_token.model.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * 手机端令牌模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class mb_user_tokenModel extends Model
  11. {
  12. public function __construct()
  13. {
  14. parent::__construct('mb_user_token');
  15. }
  16. /**
  17. * 查询
  18. *
  19. * @param array $condition 查询条件
  20. * @return array
  21. */
  22. public function getMbUserTokenInfo($condition)
  23. {
  24. return $this->where($condition)->find();
  25. }
  26. public function getMbUserTokenInfoByToken($token)
  27. {
  28. if (empty($token)) {
  29. return null;
  30. }
  31. return $this->getMbUserTokenInfo(array('token' => $token));
  32. }
  33. /**
  34. * 新增
  35. *
  36. * @param array $param 参数内容
  37. * @return bool 布尔类型的返回结果
  38. */
  39. public function addMbUserToken($param)
  40. {
  41. return $this->insert($param);
  42. }
  43. /**
  44. * 生成token
  45. *
  46. * @param array $param 参数内容
  47. * @return bool 布尔类型的返回结果
  48. */
  49. public function gen_token($member_id, $member_name, $client)
  50. {
  51. //生成新的token
  52. $mb_user_token_info = array();
  53. $token = md5($member_name . strval(time()) . strval(rand(0, 999999)));
  54. $mb_user_token_info['member_id'] = $member_id;
  55. $mb_user_token_info['member_name'] = $member_name;
  56. $mb_user_token_info['token'] = $token;
  57. $mb_user_token_info['login_time'] = time();
  58. $mb_user_token_info['client_type'] = $client;
  59. $condition['member_id'] = $member_id;
  60. $result = $this->where($condition)->select();
  61. foreach($result as $value){
  62. $key = func::gen_token_key($value['token']);
  63. dcache($key);
  64. }
  65. $this->where($condition)->delete();
  66. $ret = $this->addMbUserToken($mb_user_token_info);
  67. if ($ret) {
  68. $key = func::gen_token_key($token);
  69. wcache($key, array('info' => serialize($mb_user_token_info)),'',func::token_expire);
  70. return $token;
  71. } else {
  72. return null;
  73. }
  74. }
  75. /**
  76. * 删除token
  77. *
  78. * @param $token
  79. * @return mixed
  80. */
  81. public function del_token($token){
  82. $key = func::gen_token_key($token);
  83. dcache($key);
  84. return $this->where(array('token'=>$token))->delete();
  85. }
  86. /**
  87. * 删除
  88. *
  89. * @param int $condition 条件
  90. * @return bool 布尔类型的返回结果
  91. */
  92. public function delMbUserToken($condition)
  93. {
  94. return $this->where($condition)->delete();
  95. }
  96. }