123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\facade\Log;
- class User extends Base
- {
- protected $pk = 'uid';
- public function userAuth()
- {
- return $this->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;
- }
- }
|