UserModel.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace User\Model;
  3. use Think\Model;
  4. use Org\Net\Http;
  5. /**
  6. * 用户模型
  7. * @author Superbee
  8. *
  9. */
  10. class UserModel extends Model {
  11. protected $fields = array('id', 'uid', 'diamond', 'nickname', 'coin', 'mobile', 'last_login', 'registertime',
  12. '_pk' => 'id', '_autoinc' => false);
  13. protected $_validate = array(
  14. array('nickname', 'require', '昵称不能为空!'), //默认情况下用正则进行验证
  15. array('mobile', '', '该手机号码已被占用', 0, 'unique', 1), // 新增的时候mobile字段是否唯一
  16. // 正则验证密码 [需包含字母数字以及@*#中的一种,长度为6-22位]
  17. array('password', '/^([a-zA-Z0-9@*#]{6,22})$/', '密码格式不正确,请重新输入!', 0),
  18. array('repassword', 'password', '确认密码不正确', 0, 'confirm'), // 验证确认密码是否和密码一致
  19. array('mobile', '/^1[34578]/d{9}$/', '手机号码格式不正确', 0), // 正则表达式验证手机号
  20. );
  21. protected $_auto = array(
  22. array('password', 'md5', 3, 'function'), // 对password字段在新增和编辑的时候使md5函数处理
  23. array('regdate', 'time', 1, 'function'), // 对regdate字段在新增的时候写入当前时间戳
  24. array('regip', 'get_client_ip', 1, 'function'), // 对regip字段在新增的时候写入当前注册ip地址
  25. );
  26. public function __construct(){
  27. $this->dbName = "user";
  28. $this->db = M($this->dbName);
  29. }
  30. /**
  31. * 获取页码当前信息
  32. * @param array $where
  33. * @param int $page
  34. * @return \Think\mixed
  35. */
  36. public function getInfo($page = 0, $where = array()) {
  37. return $this->db->where($where)->page(0, PAGE_ROW)->select();
  38. }
  39. /**
  40. * 获取分页信息
  41. * @param array $where 查询条件
  42. * @return string
  43. */
  44. public function getPages($where = array()) {
  45. $p = getPages($this->db, $where, PAGE_ROW);
  46. return $p->show();
  47. }
  48. /**
  49. * 根据id找到用户
  50. * @param int $id
  51. * @return \Think\mixed
  52. */
  53. public function getUserById($id) {
  54. return $this->db->where(array("id" => $id))->find();
  55. }
  56. /**
  57. * 利用UID登陆,如果UID不存在则创建新的用户
  58. * @param string $uid 登陆唯一码
  59. * @return array dia
  60. */
  61. public function loginByUid($uid){
  62. $info = $this->db->field('id i, diamond d, coin c')->where(array("uid"=>$uid))->find();
  63. if(empty($info)) {
  64. $info['d'] = 0;
  65. $info['c'] = 0;
  66. $info['i'] = $this->createUser($uid);
  67. }
  68. if($info['i']) $this->db->where(array('id'=>$info['i']))->save(array('last_login'=>getCurrentTime())); // 更新最后登陆时间
  69. return $info;
  70. }
  71. /**
  72. * 创建新用户
  73. * @param string $uid
  74. */
  75. private function createUser($uid){
  76. $data = array('id'=>getId(), 'uid'=>$uid, 'registertime'=>getCurrentTime());
  77. $this->db->add($data);
  78. return $data['id'];
  79. }
  80. public function update(){
  81. $data = $this->db->create();
  82. return $this->db->save($data);
  83. }
  84. public function add(){
  85. $data = $this->db->create();
  86. return $this->db->add($data);
  87. }
  88. }