mb_user_token.model.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const token_expire = 3; // 单位:分钟
  13. public function __construct()
  14. {
  15. parent::__construct('mb_user_token');
  16. }
  17. /**
  18. * 查询
  19. *
  20. * @param array $condition 查询条件
  21. * @return array
  22. */
  23. public function getMbUserTokenInfo($condition)
  24. {
  25. return $this->where($condition)->find();
  26. }
  27. public function getMbUserTokenInfoByToken($token)
  28. {
  29. if (empty($token)) {
  30. return null;
  31. }
  32. return $this->getMbUserTokenInfo(array('token' => $token));
  33. }
  34. /**
  35. * 新增
  36. *
  37. * @param array $param 参数内容
  38. * @return bool 布尔类型的返回结果
  39. */
  40. public function addMbUserToken($param)
  41. {
  42. return $this->insert($param);
  43. }
  44. /**
  45. * 生成token
  46. *
  47. * @param array $param 参数内容
  48. * @return bool 布尔类型的返回结果
  49. */
  50. public function gen_token($member_id, $member_name, $client)
  51. {
  52. //生成新的token
  53. $mb_user_token_info = array();
  54. $token = md5($member_name . strval(TIMESTAMP) . strval(rand(0, 999999)));
  55. $mb_user_token_info['member_id'] = $member_id;
  56. $mb_user_token_info['member_name'] = $member_name;
  57. $mb_user_token_info['token'] = $token;
  58. $mb_user_token_info['login_time'] = TIMESTAMP;
  59. $mb_user_token_info['client_type'] = $client;
  60. $condition['member_id'] = $member_id;
  61. $result = $this->where($condition)->select();
  62. foreach($result as $value){
  63. $key = func::gen_token_key($value['token']);
  64. dcache($key);
  65. }
  66. $this->where($condition)->delete();
  67. $ret = $this->addMbUserToken($mb_user_token_info);
  68. if ($ret) {
  69. $key = func::gen_token_key($token);
  70. wcache($key, array('info' => serialize($mb_user_token_info)),'',self::token_expire);
  71. return $token;
  72. } else {
  73. return null;
  74. }
  75. }
  76. /**
  77. * 删除
  78. *
  79. * @param int $condition 条件
  80. * @return bool 布尔类型的返回结果
  81. */
  82. public function delMbUserToken($condition)
  83. {
  84. return $this->where($condition)->delete();
  85. }
  86. }