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.

265 lines
7.2 KiB

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
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
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. $limit = isset($limit) ? $limit : 10;
  41. $offset = isset($offset) ? $offset : 0;
  42. $model = new User;
  43. if(isset($group_id)){$where['group_id'] = $group_id;}
  44. if(isset($parent_id)){$where['parent_id'] = $parent_id;}
  45. if(isset($where)){$model = $model->where($where);}
  46. $res['count'] = $model->count();
  47. $res['list'] = array();
  48. if($res['count']>0)
  49. {
  50. $res['list'] = $model->select('id','user_name','email','sex','money','commission','point','mobile','nickname','head_img','add_time')->skip($offset)->take($limit)->orderBy('id','desc')->get();
  51. foreach($res['list'] as $k=>$v)
  52. {
  53. $res['list'][$k]['user_name'] = !empty($res['list'][$k]['mobile']) ? $res['list'][$k]['mobile'] : $res['list'][$k]['user_name'];
  54. }
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. return $res;
  61. }
  62. //获取一条用户信息
  63. public static function getOne($id)
  64. {
  65. $user = self::where('id', $id)->first();
  66. if(!$user){return false;}
  67. $user['reciever_address'] = UserAddress::getOne($user->address_id);
  68. return $user;
  69. }
  70. public static function add(array $data)
  71. {
  72. if ($id = self::insertGetId($data))
  73. {
  74. return $id;
  75. }
  76. return false;
  77. }
  78. public static function modify($where, array $data)
  79. {
  80. if (self::where($where)->update($data))
  81. {
  82. return true;
  83. }
  84. return false;
  85. }
  86. //删除一条记录
  87. public static function remove($id)
  88. {
  89. if (!self::whereIn('id', explode(',', $id))->delete())
  90. {
  91. return false;
  92. }
  93. return true;
  94. }
  95. //获取一条用户信息
  96. public static function getOneUser($where)
  97. {
  98. $user = self::where($where)->first();
  99. if(!$user){return false;}
  100. return $user;
  101. }
  102. //获取用户信息
  103. public static function getUserInfo($user_id)
  104. {
  105. $user = self::where('id', $user_id)->first();
  106. if(!$user){return false;}
  107. $user->reciever_address = UserAddress::getOne($user->address_id);
  108. $user->collect_goods_count = CollectGoods::where('user_id', $user_id)->count();
  109. $user->bonus_count = UserBonus::where(array('user_id'=>$user_id,'status'=>0))->count();
  110. $userinfo = $user->makeVisible(array('pay_password'))->toArray();
  111. $user->pay_password = 0;
  112. if($userinfo['pay_password']){$user->pay_password = 1;}
  113. return $user;
  114. }
  115. //修改用户密码、支付密码
  116. public static function userPasswordUpdate($where,array $param)
  117. {
  118. extract($param);
  119. $data = '';
  120. $user = self::where($where)->first();
  121. if(!$user){return false;}
  122. $user = $user->makeVisible(array('password','pay_password'))->toArray();
  123. if(isset($old_password) && $old_password!=$user['password']){return false;} //旧密码错误
  124. if(isset($password) && $password==''){return false;} //新密码为空
  125. if(isset($old_pay_password) && $old_pay_password!=$user['pay_password']){return false;}
  126. if(isset($pay_password) && $pay_password==''){return false;}
  127. if(isset($password)){$data['password'] = $password;}
  128. if(isset($pay_password)){$data['pay_password'] = $pay_password;}
  129. if ($data != '' && self::where($where)->update($data))
  130. {
  131. return true;
  132. }
  133. return false;
  134. }
  135. //注册
  136. public static function wxRegister(array $param)
  137. {
  138. extract($param); //参数
  139. if(isset($user_name)){$data['user_name'] = $user_name;}
  140. if(isset($mobile)){$data['mobile'] = $mobile;}
  141. if(isset($password)){$data['password'] = $password;} //md5加密
  142. if(isset($parent_id) && !empty($parent_id)){$data['parent_id'] = $parent_id;}
  143. if(isset($openid)){$data['openid'] = $openid;}
  144. if(isset($sex)){$data['sex'] = $sex;}
  145. if(isset($head_img)){$data['head_img'] = $head_img;}
  146. if(isset($nickname)){$data['nickname'] = $nickname;}
  147. if (isset($data) && $id = self::add($data))
  148. {
  149. //生成token
  150. return Token::getToken(Token::TYPE_WEIXIN, $id);
  151. }
  152. return false;
  153. }
  154. //用户登录
  155. public static function wxLogin(array $param)
  156. {
  157. extract($param); //参数
  158. if(isset($openid) && !empty($openid))
  159. {
  160. $user = self::where(array('openid'=>$openid))->first();
  161. }
  162. else
  163. {
  164. $user = self::where(array('mobile'=>$user_name,'password'=>$password))->orWhere(array('user_name'=>$user_name,'password'=>$password))->first();
  165. }
  166. if(!isset($user)){return false;}
  167. $res = self::getUserInfo($user->id);
  168. $token = Token::getToken(Token::TYPE_WEIXIN, $user->id);
  169. foreach($token as $k=>$v)
  170. {
  171. $res->$k = $v;
  172. }
  173. return $res;
  174. }
  175. //获取性别文字:0未知,1男,2女
  176. public static function getSexText($where)
  177. {
  178. $res = '';
  179. if($where['sex'] === 0)
  180. {
  181. $res = '未知';
  182. }
  183. elseif($where['sex'] === 1)
  184. {
  185. $res = '男';
  186. }
  187. elseif($where['sex'] === 2)
  188. {
  189. $res = '女';
  190. }
  191. return $res;
  192. }
  193. //获取用户状态文字:1正常 2 删除 3锁定
  194. public static function getStatusText($where)
  195. {
  196. $res = '';
  197. if($where['status'] === 1)
  198. {
  199. $res = '正常';
  200. }
  201. elseif($where['status'] === 2)
  202. {
  203. $res = '删除';
  204. }
  205. elseif($where['status'] === 3)
  206. {
  207. $res = '锁定';
  208. }
  209. return $res;
  210. }
  211. }