'代理', 3 => '用户']; protected static $trade_id = 0; protected static $agentGrade = 0; /** * 注册用户 * @param $data * @param $identity * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function register($data,$identity = 3) { $User = new User(); $user = $User->where('phone', $data['phone'])->find(); if ($user) return 0; // 生成盐值 $salt = $this->generateSalt(); // 密码加盐值后哈希存储 $password = $this->generateHashedPassword($data['password'], $salt); $lastId = User::withTrashed()->where('id','>=',1)->order('id desc')->value('id'); $save = [ // 随机头像 'avatar' => rand_avatar(), 'password' => $password, 'salt' => $salt, 'phone' => $data['phone'], 'aid' => $data['aid'], 'identity' => $identity, 'register_time' => date("Y-m-d H:i:s",time()) ]; if ($lastId) { $rand = rand(2,50); $save['id'] = $lastId + $rand; } $User->save($save); return $User->id; } /** * 用户登陆 * @param $data * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function login($data) { // 根据用户名查询用户信息 $user = $this->where('phone', $data['phone'])->field('id,avatar,phone,password,salt,status,identity')->find(); if ($user) { $msg = $this->identityArr[$user['identity']]; if ($user['status'] != 1) return ['status' => false, 'msg' => $msg.'已被停用']; // 使用相同的盐值对输入密码进行哈希验证 $hashedPassword = $this->generateHashedPassword($data['password'], $user->salt); if ($user->password === $hashedPassword) { # 缓存用户信息 $login_user_data = $user->toArray(); unset($login_user_data['password'],$login_user_data['salt']); Session::set('login_user_data',$login_user_data); Cache::store('redis')->set('login_user_data',serialize($login_user_data),7200); // 登陆成功 return ['status' => true, 'msg' => '登陆成功', 'data' => $login_user_data]; } else { return ['status' => false, 'msg' => '密码错误']; } } return ['status' => false, 'msg' => '手机号未注册']; } /** * 找回密码 * @param $data * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function retrieve($data) { // 根据用户名查询用户信息 $user = []; $errorMsg = ''; if (isset($data['phone'])) { $errorMsg = '手机号'; $user = $this->where('phone', $data['phone'])->find(); } elseif ($data['user_id']) { $errorMsg = '用户'; $user = $this->find($data['user_id']); } if ($user) { // 生成盐值 $salt = $this->generateSalt(); $password = $this->generateHashedPassword($data['password'], $salt); // 密码加盐值后哈希存储 $user->password = $password; $user->salt = $salt; $user->update_time = date("Y-m-d H:i:s",time()); $user->save(); return ['status' => true, 'msg' => '密码重制成功']; } return ['status' => false, 'msg' => $errorMsg.'未注册']; } /** * 修改密码 * @param $data * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function modifyPassword($data,$user_id) { $user = ['id' => $user_id]; if ($user_id) { $userModel = $this->find($user['id']); $password = $this->generateHashedPassword($data['password'], $userModel->salt); if ($userModel->password == $password) { return ['status' => false, 'msg' => '新密码与原密码一致']; } // 生成盐值 $salt = $this->generateSalt(); $new_password = $this->generateHashedPassword($data['password'], $salt); // 密码加盐值后哈希存储 $userModel->password = $new_password; $userModel->salt = $salt; $userModel->update_time = date("Y-m-d H:i:s",time()); $userModel->save(); return ['status' => true, 'msg' => '修改成功']; } return ['status' => false, 'msg' => '登陆状态有误']; } /** * 验证邀请码是否有效 * @param $invite_code * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function verifyInviteCode($invite_code) { if (empty($invite_code)) return 0; $AgentUser = new AgentInfo(); $codeRes = $AgentUser->where('invite_code',$invite_code)->find(); if (empty($codeRes)) { return 0; } return $codeRes->aid; } /** * 扣减余额(消费) * @param $user_id * @param $balance * @return float|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function decrBalance($user_id,$balance) { $userModel = new User(); $user = $userModel->find($user_id); $user->balance = round($user->balance - $balance,2); $user->save(); return $user->balance; } /** * 增加余额(充值、上分) * @param $user_id * @param $balance * @return float|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function incrBalance($user_id,$balance) { $userModel = new User(); $user = $userModel->find($user_id); $user->balance = round($user->balance + $balance,2); $user->save(); return $user->balance; } /** * 扣除可提余额 (提现、下分) * @param $user_id * @param $balance * @return float|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function decrWithdrawalBalance($user_id,$balance) { $userModel = new User(); $user = $userModel->find($user_id); $user->withdrawal_balance = round($user->withdrawal_balance - $balance,2); $user->save(); return $user->withdrawal_balance; } /** * 增加可提余额(中奖) * @param $user_id * @param $balance * @return float|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function incrWithdrawalBalance($user_id,$balance) { $userModel = new User(); $user = $userModel->find($user_id); $user->withdrawal_balance = round($user->withdrawal_balance + $balance,2); $user->save(); return $user->withdrawal_balance; } /** * 检查用户|代理可提余额是否足够 * @param $user_id * @param $balance * @return array|int[] * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function inspectUserBalance($user_id,$balance) { $userModel = new User(); $user = $userModel->find($user_id); if ($user->withdrawal_balance < $balance) { return ['status' => 0, 'msg' => $userModel->identityArr[$user->identity].'可提余额不足']; } return ['status' => 1]; } /** * 可提余额 转移到 余额 * @param $user_id * @return array|int[] * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function transferBalance($user_id) { $User = self::find($user_id); if ($User->withdrawal_balance <= 0) return ['status' => 0,'msg' => '可提余额不足,无法转移']; $User->balance = bcadd($User->balance,$User->withdrawal_balance,2); $User->withdrawal_balance = 0; $User->save(); return ['status' => 1]; } /** * 检测代理|用户余额 * @param $aid * @param $balance * @return array|int[] * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function inspectBalance($aid,$balance) { $userModel = new User(); $user = $userModel->find($aid); if ($user->balance < $balance) { return ['status' => 0, 'msg' => $userModel->identityArr[$user->identity].'余额不足']; } return ['status' => 1]; } /** * 修改身份 * @param $user_id * @param $identity * @param $aid * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function changeIdentity($user_id,$identity = 2,$aid = 0) { $user = new User(); $userRes = $user->find($user_id); $userRes->identity = $identity; $userRes->aid = $aid; $userRes->save(); } /** * 添加消费返点 * @param $user_id // 充值用户 * @param $amount // 充值金额 * @param $superior_rebate_ratio // 返点比率 * @param $c_r_id // 消费id * @return false|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function addRebateRatioAmount($user_id,$amount,$superior_rebate_ratio = 0,$c_r_id = 0) { # 获取用户代理 $aid = User::where('id',$user_id)->value('aid'); if (empty($aid)) return false; # 当前操作记录ID if (empty(self::$trade_id)) self::$trade_id = $user_id; # 获取代理返点比率 $AgentInfo = AgentInfo::where('aid',$aid)->field('rebate_ratio')->find(); # 判断代理信息是否被移除 if (!$AgentInfo) return false; $rebate_ratio = $AgentInfo['rebate_ratio'] * 1; if ($rebate_ratio) { # 减去下级返点比率 $rebate_ratio -= $superior_rebate_ratio; # 计算反比金额 $rebateRatioAmount = bcmul($amount,$rebate_ratio,2); if ($rebateRatioAmount > 0 && $rebateRatioAmount >= '0.01') { # 代理可提余额增加 $residue_amount = self::incrWithdrawalBalance($aid,$rebateRatioAmount); # 代理充值返点金额 #RechargeRecords::createRecords($aid,$rebateRatioAmount,$residue_amount,1,4,self::$trade_id); # 返利记录 RebateRecords::createRecords(self::$trade_id,$aid,$c_r_id,$amount,$rebateRatioAmount,$rebate_ratio); } } # 只反二级 if (self::$agentGrade == 1) return false; self::$agentGrade += 1; # 获取用户代理上级代理 self::addRebateRatioAmount($aid,$amount,$rebate_ratio,$c_r_id); } /** * 查询获取用户信息(包括已删除的) * @param $user_id * @return User|array|mixed|Model|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function withTrashedUserInfo($user_id) { $UserData = User::withTrashed()->where('id',$user_id)->field('phone,avatar')->find(); $UserData['avatar'] = get_image_url($UserData['avatar']); return $UserData; } /** * 生成盐值 * @return string */ private function generateSalt() { return generate_random_str(6); } /** * 密码加盐值后哈希存储 * @param $password * @param $salt * @return string */ private function generateHashedPassword($password, $salt) { return md5(md5($password) . md5($salt)); } }