| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- namespace app\api\service;
- use app\api\model\user\Grade;
- use app\api\model\user\GradeLog;
- use app\common\model\dealer\Setting;
- use think\Cache;
- use think\Db;
- use think\Exception;
- use app\api\model\User as UserModel;
- class User
- {
- /**
- * 记忆上门自提联系人
- * @param $userId
- * @param $linkman
- * @param $phone
- * @return bool
- */
- public static function setLastExtract($userId, $linkman, $phone)
- {
- // 缓存时间30天
- $expire = 86400 * 30;
- return Cache::set("{$userId}_LastExtract", compact('linkman', 'phone'), $expire);
- }
- /**
- * 记忆上门自提联系人
- * @param $userId
- * @return mixed
- */
- public static function getLastExtract($userId)
- {
- if ($lastExtract = Cache::get("{$userId}_LastExtract")) {
- return $lastExtract;
- }
- return ['linkman' => '', '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();
- // 推荐奖金
- self::settleAward($user_id, $wxapp_id);
- // 更新用户等级
- 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('升级处理失败');
- }
- 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;
- }
- }
|