'', 'phone' => '']; } /** * 设置支付密码 * @param $userId * @param $password * @return UserModel * @throws Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function setPayPassword($userId, $password) { if(empty($password)){ throw new Exception('请输入6位数字密码'); } if(!preg_match("/^[0-9]\d{5}$/", $password)){ throw new Exception('密码格式不正确,请输入6位数字密码'); } $payPassword = makePassword($password); return UserModel::where('user_id', $userId)->update(['pay_password'=>$payPassword,'update_time'=>time()]); } /** * 设置修改资料 * @param $userId * @param $post * @return UserModel * @throws Exception */ public static function setProfile($userId, $post) { $nickname = isset($post['nickname'])? trim($post['nickname']) : ''; if(empty($nickname)){ throw new Exception('请填写用户昵称'); } $mobile = isset($post['mobile'])? trim($post['mobile']) : ''; if(empty($mobile) || !isMobile($mobile)){ throw new Exception('请填写正确的手机号码'); } if(UserModel::where(['mobile'=> $mobile])->whereNotIn('user_id', $userId)->value('user_id')){ throw new Exception('手机号码已被使用'); } return UserModel::where('user_id', $userId)->update(['nickName'=>$nickname,'mobile'=>$mobile,'update_time'=>time()]); } /** * 用户升级 * @param $user_id * @param int $totalPrice * @param int $wxapp_id * @return bool * @throws Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function upgrade($user_id, $totalPrice, $wxapp_id=0) { $info = UserModel::alias('a') ->join('user_grade ug', 'ug.grade_id=a.grade_id','left') ->where('user_id', $user_id) ->field('a.user_id,a.grade_id,ug.name,a.expend_upgrade_money') ->find(); if(empty($info)){ return false; } $gradeId = isset($info['grade_id'])? intval($info['grade_id']) : 0; //$upgradeMoney = isset($info['expend_upgrade_money'])? floatval($info['expend_upgrade_money']) : 0; if($totalPrice>0){ $gradeList = Grade::where(['status'=> 1, 'is_delete'=> 0,'wxapp_id'=>$wxapp_id]) ->where('grade_id','>', $gradeId) ->field('grade_id,name,upgrade') ->order('weight asc') ->select(); $gradeList = $gradeList? $gradeList->toArray() : []; if($gradeList){ $newGradeId = 0; foreach ($gradeList as $v){ $id = isset($v['grade_id'])? $v['grade_id'] : 0; $upgrade = isset($v['upgrade']) && $v['upgrade']? $v['upgrade'] : []; $upgradeGradeMoney = isset($upgrade['expend_money'])? floatval($upgrade['expend_money']) : 0; if($id>$gradeId && $upgradeGradeMoney<=$totalPrice){ $newGradeId = $id; } } // 处理 if($newGradeId>0){ Db::startTrans(); if(!UserModel::where(['user_id'=>$user_id])->update(['grade_id'=> $newGradeId,'update_time'=> time()])){ Db::rollback(); throw new Exception('升级处理失败'); } $log = [ 'user_id'=> $user_id, 'old_grade_id'=> $gradeId, 'new_grade_id'=> $newGradeId, 'change_type'=> 20, 'wxapp_id'=> $wxapp_id, 'remark'=> "消费[¥{$totalPrice}]升级", 'create_time'=> time() ]; if(!GradeLog::insertgetId($log)){ Db::rollback(); throw new Exception('升级处理失败'); } // 推荐奖金 self::settleAward($user_id, $wxapp_id); Db::commit(); return true; } } } return false; } /** * 获取用户信息 * @param $userId * @param $wxapp_id * @return mixed */ public static function getUserInfo($userId, $wxapp_id) { if(empty($userId)){ return false; } $model = new \app\common\model\dealer\User(); return $dealerUserInfo = $model::alias('a') ->join('user u','u.user_id=a.user_id','left') ->join('user_grade ug','ug.grade_id=u.grade_id','left') ->where(['a.user_id'=>$userId,'a.wxapp_id'=>$wxapp_id,'a.is_delete'=>0]) ->field('a.user_id,a.money,a.referee_id,u.grade_id,ug.weight as level,ug.name as grade_name') ->find(); } /** * 结算推荐佣金 */ public static function settleAward($userId, $wxapp_id) { // 奖金配置参数 $config = Setting::getItem('commission', $wxapp_id); if(empty($config)){ return false; } $dealerUserInfo = self::getUserInfo($userId, $wxapp_id); $userLevel = isset($dealerUserInfo['level'])? $dealerUserInfo['level'] : 0; $firstId = isset($dealerUserInfo['referee_id'])? $dealerUserInfo['referee_id'] : 0; if(empty($dealerUserInfo) || $firstId<=0){ return false; } $firstInfo = self::getUserInfo($firstId, $wxapp_id); $firstLevel = isset($firstInfo['level'])? $firstInfo['level'] : 0; $firstLevelName = isset($firstInfo['grade_name'])? $firstInfo['grade_name'] : ''; $secondId = isset($firstInfo['referee_id'])? $firstInfo['referee_id'] : 0; $secondInfo = self::getUserInfo($secondId, $wxapp_id); $secondLevel = isset($secondInfo['level'])? $secondInfo['level'] : 0; $secondLevelName = isset($secondInfo['grade_name'])? $secondInfo['grade_name'] : ''; // 平级奖 (vip以及以上等级) $model = new \app\common\model\dealer\User(); if($firstInfo && $userLevel>=1){ // 直推平级奖 $equalScore1 = isset($config['equal_score_1'])? floatval($config['equal_score_1']) : 0; if($userLevel==$firstLevel && $equalScore1>0){ $model->grantMoney($firstId, $equalScore1, 2, "来自用户[{$userId}]直推[{$firstLevelName}]平级奖",0, $userId); } // 间推平级奖 $equalScore2 = isset($config['equal_score_2'])? floatval($config['equal_score_2']) : 0; if($secondInfo && $userLevel==$secondLevel && $equalScore2>0){ $model->grantMoney($secondId, $equalScore2, 2, "来自用户[{$userId}]间推[{$secondLevelName}]平级奖",0, $userId); } } // 反推奖(上级的级别大于当前用户) $reverseScore = isset($config['reverse_score_'.$firstLevel])? floatval($config['reverse_score_'.$firstLevel]) : 0; //var_dump($equalScore1.'+++'.$equalScore2.'++'.$reverseScore); if($firstLevel>$userLevel && $reverseScore>0){ $model->grantMoney($firstId, $reverseScore, 3, "来自用户[{$userId}]反推[{$firstLevelName}]奖",0, $userId); } return true; } }