hasMany('UserAuths', 'uid', 'uid'); } public function invitation() { return $this->hasOne('InviteRelation', 'uid', 'uid'); } /** * 用户登录模型 * @param $param * @param int $type * @return array|int|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function login($param, $type = 1) { $user = array(); $auth = array(); switch ($type) { case 1: $auth['identity_type'] = 'username'; $auth['identifier'] = $param['username']; break; case 2: $auth['identity_type'] = 'email'; $auth['identifier'] = $param['username']; break; case 3: $auth['identity_type'] = 'mobile'; $auth['identifier'] = $param['username']; break; case 4: Log::record($param['username']); $auth['identity_type'] = $param['username']['channel']; $auth['identifier'] = $param['username']['openid']; break; } $user = $this->userAuth()->where($auth)->find(); if (isset($user['uid']) && $user['uid']) { $uinfo = $this->where('uid',$user['uid'])->field('inviter_code,social_type')->find(); $data = array( 'uid' => $user['uid'], 'inviter_code' => $uinfo['inviter_code'] ); if (isset($uinfo['social_type']) && $uinfo['social_type']) { $data['social_type'] = $uinfo['social_type']; } if ($auth['identity_type'] == 'weixin') { $data['openid'] = $auth['identifier']; } } else { $inviter_code = null; $user['nickname'] = 'hm_'. rand_string(8); $user['gender'] = 1; $user['avatar'] = 'http://www.wercf.com/img/avatar_1.png'; if ($type == 2) { $user['email'] = $param['username']; } elseif ($type == 3) { $user['mobile'] = $param['username']; } elseif ($type == 4) { $user['nickname'] = $param['username']['nick']; $user['gender'] = $param['username']['gender']; $user['avatar'] = $param['username']['avatar']; } if (isset($param['inviter_code']) && $param['inviter_code'] != '') { $inviter_code = $param['inviter_code']; } $auth['credential'] = $param['password']; $data = $this->register($auth, $user, $inviter_code); } return $data; } /** * 注册 * @param $auth * @param array $user * @return int|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function register($auth, $user = [], $inviter_code = null) { $auth['salt'] = rand_string(6); $user['inviter_code'] = createInviteCode(); if ($auth['identity_type'] === 'username') { $auth['credential'] = md5($auth['credential'] . $auth['salt']); } $this->startTrans(); try { $this->save($user); $auth['uid'] = $this->uid; $user_discount = [ 'uid' => $this->uid, 'discount_id' => 1, 'created_at' =>date("Y-m-d",time()), 'end_at' => date("Y-m-d",time()+3600*24*14) ]; Db::name('user_discount')->insert($user_discount); $this->userAuth()->save($auth); if ($inviter_code) { $data = array( 'uid' => $auth['uid'], 'inviter' => $this->where('inviter_code', $inviter_code)->value('uid'), 'status' => 1, 'create_timestamp' => date('Y-m-d H:i:s') ); $this->invitation()->save($data); $user_discount['discount_id'] = 2; Db::name('user_discount')->insert($user_discount); } $ret = array( 'uid' => $this->uid, 'inviter_code' => $user['inviter_code'], ); if($auth['identity_type'] === 'weixin') { $ret['openid'] = $auth['identifier']; } $this->commit(); } catch (\Exception $e) { $ret = -1; $this->rollback(); } return $ret; } /** * @param $uid * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getInvitationList($uid){ return $this->table('hippo_invite_relation') ->alias('a') ->leftJoin('hippo_user b', 'a.uid = b.uid') ->field('a.uid,a.status,b.avatar,b.nickname,a.create_timestamp,b.realname') ->where('a.inviter',$uid) ->select(); } //获取一级邀请列表 pc端 public function getInvitationListpc($uid,$page,$number){ return $this->table("hippo_invite_relation") ->alias('a') ->leftJoin('hippo_user b', 'a.uid = b.uid') ->field('a.uid,a.status,b.avatar,b.nickname,a.create_timestamp,b.realname') -> where("inviter=$uid") ->page($page.','.$number) ->order('create_timestamp asc') -> select(); } //获取二级邀请列表 pc端 public function getInvitationListpcchildren($uid,$page,$number){ $firstlist = $this->table("hippo_invite_relation") ->field('uid') -> where("inviter=$uid") -> select(); return $firstlist; } public function logout() { session('user_auth', null); session('user_auth_sign', null); } public function getInfo($uid) { $data = $this->where(array('uid' => $uid))->find(); return $data; } /** * @param $uid * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getDiscountList($uid){ $list = Db::table('hippo_user_discount') ->alias('a') ->leftJoin('hippo_discount b', 'a.discount_id = b.id') ->field('a.id,a.end_at,b.money') ->where("a.uid=$uid AND a.status=0") ->select(); if(!empty($list)){ $array = array(); foreach($list as $k => $v){ $array[$k]['id'] = $list[$k]['id']; $array[$k]['money'] = '-¥'.$list[$k]['money']; $array[$k]['end_at'] = $list[$k]['end_at']; } return $array; } return $list; } //获取用户余额 public function getUserBalance($uid) { $data = Db::table('hippo_user')->field('balance')->where(array('uid' => $uid))->find(); return $data; } }