You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\Token;
  4. class User extends BaseModel
  5. {
  6. //用户模型
  7. protected $table = 'user';
  8. public $timestamps = false;
  9. /**
  10. * 不能被批量赋值的属性
  11. *
  12. * @var array
  13. */
  14. protected $guarded = array();
  15. protected $hidden = array('password','pay_password');
  16. /**
  17. * 获取关联到用户的角色
  18. */
  19. public function userrole()
  20. {
  21. return $this->belongsTo(UserRole::class, 'role_id', 'id');
  22. }
  23. //签到
  24. public static function signin()
  25. {
  26. $user = self::where(['id'=>Token::$uid])->first();
  27. $signin_time='';
  28. if(!empty($user->signin_time)){$signin_time = date('Ymd',strtotime($user->signin_time));} //签到时间
  29. $today = date('Ymd',time()); //今日日期
  30. if($signin_time==$today){return '今日已签到!';}
  31. $signin_point = (int)Sysconfig::where(['varname'=>'CMS_SIGN_POINT'])->value('value'); //签到积分
  32. User::where(['id'=>Token::$uid])->update(['point'=>($user->point+$signin_point),'signin_time'=>date('Y-m-d H:i:s')]); //更新用户积分,及签到时间
  33. UserPoint::insert(['type'=>1,'point'=>$signin_point,'des'=>'签到','user_id'=>Token::$uid]); //添加签到积分记录
  34. return true;
  35. }
  36. //获取列表
  37. public static function getList(array $param)
  38. {
  39. extract($param); //参数:limit,offset
  40. $where = '';
  41. $limit = isset($limit) ? $limit : 10;
  42. $offset = isset($offset) ? $offset : 0;
  43. $model = new User;
  44. if(isset($group_id)){$where['group_id'] = $group_id;}
  45. if($where != '')
  46. {
  47. $model = $model->where($where);
  48. }
  49. $res['count'] = $model->count();
  50. $res['list'] = array();
  51. if($res['count']>0)
  52. {
  53. $res['list'] = $model->select('id','user_name','email','sex','money','point','mobile','nickname','add_time')->skip($offset)->take($limit)->orderBy('id','desc')->get()->toArray();
  54. }
  55. else
  56. {
  57. return false;
  58. }
  59. return $res;
  60. }
  61. //获取一条用户信息
  62. public static function getOne($id)
  63. {
  64. $user = self::where('id', $id)->first();
  65. if(!$user){return false;}
  66. $user['reciever_address'] = UserAddress::getOne($user->address_id);
  67. return $user;
  68. }
  69. public static function add(array $data)
  70. {
  71. if ($id = self::insertGetId($data))
  72. {
  73. return $id;
  74. }
  75. return false;
  76. }
  77. public static function modify($where, array $data)
  78. {
  79. if (self::where($where)->update($data))
  80. {
  81. return true;
  82. }
  83. return false;
  84. }
  85. //删除一条记录
  86. public static function remove($id)
  87. {
  88. if (!self::whereIn('id', explode(',', $id))->delete())
  89. {
  90. return false;
  91. }
  92. return true;
  93. }
  94. //获取一条用户信息
  95. public static function getOneUser($where)
  96. {
  97. $user = self::where($where)->first();
  98. if(!$user){return false;}
  99. return $user;
  100. }
  101. //获取用户信息
  102. public static function getUserInfo($user_id)
  103. {
  104. $user = self::where('id', $user_id)->first();
  105. if(!$user){return false;}
  106. $user->reciever_address = UserAddress::getOne($user->address_id);
  107. $user->collect_goods_count = CollectGoods::where('user_id', $user_id)->count();
  108. $user->bonus_count = UserBonus::where(array('user_id'=>$user_id,'status'=>0))->count();
  109. $userinfo = $user->makeVisible(array('pay_password'))->toArray();
  110. $user->pay_password = 0;
  111. if($userinfo['pay_password']){$user->pay_password = 1;}
  112. return $user;
  113. }
  114. //修改用户密码、支付密码
  115. public static function userPasswordUpdate($where,array $param)
  116. {
  117. extract($param);
  118. $data = '';
  119. $user = self::where($where)->first();
  120. if(!$user){return false;}
  121. $user = $user->makeVisible(array('password','pay_password'))->toArray();
  122. if(isset($old_password) && $old_password!=$user['password']){return false;} //旧密码错误
  123. if(isset($password) && $password==''){return false;} //新密码为空
  124. if(isset($old_pay_password) && $old_pay_password!=$user['pay_password']){return false;}
  125. if(isset($pay_password) && $pay_password==''){return false;}
  126. if(isset($password)){$data['password'] = $password;}
  127. if(isset($pay_password)){$data['pay_password'] = $pay_password;}
  128. if ($data != '' && self::where($where)->update($data))
  129. {
  130. return true;
  131. }
  132. return false;
  133. }
  134. //注册
  135. public static function wxRegister(array $param)
  136. {
  137. extract($param); //参数
  138. if(isset($user_name)){$data['user_name'] = $user_name;}
  139. if(isset($mobile)){$data['mobile'] = $mobile;}
  140. if(isset($password)){$data['password'] = $password;} //md5加密
  141. if(isset($parent_id)){$data['parent_id'] = $parent_id;}
  142. if (isset($data) && $id = self::add($data))
  143. {
  144. //生成token
  145. return Token::getToken(Token::TYPE_WEIXIN, $id);
  146. }
  147. return false;
  148. }
  149. //用户登录
  150. public static function wxLogin(array $param)
  151. {
  152. extract($param); //参数
  153. $user = self::where(array('mobile'=>$user_name,'password'=>$password))->orWhere(array('user_name'=>$user_name,'password'=>$password))->first();
  154. if(!$user){return false;}
  155. $res = self::getUserInfo($user->id);
  156. $token = Token::getToken(Token::TYPE_WEIXIN, $user->id);
  157. foreach($token as $k=>$v)
  158. {
  159. $res->$k = $v;
  160. }
  161. return $res;
  162. }
  163. }