| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 | 
							- <?php
 
- namespace app\common\model;
 
- use think\Db;
 
- use think\Model;
 
- /**
 
-  * 会员模型
 
-  */
 
- class User extends Model
 
- {
 
-     // 开启自动写入时间戳字段
 
-     protected $autoWriteTimestamp = 'int';
 
-     // 定义时间戳字段名
 
-     protected $createTime = 'createtime';
 
-     protected $updateTime = 'updatetime';
 
-     // 追加属性
 
-     protected $append = [
 
-         'url',
 
-     ];
 
-     /**
 
-      * 获取个人URL
 
-      * @param string $value
 
-      * @param array  $data
 
-      * @return string
 
-      */
 
-     public function getUrlAttr($value, $data)
 
-     {
 
-         return "/u/" . $data['id'];
 
-     }
 
-     /**
 
-      * 获取头像
 
-      * @param string $value
 
-      * @param array  $data
 
-      * @return string
 
-      */
 
-     public function getAvatarAttr($value, $data)
 
-     {
 
-         if (!$value) {
 
-             //如果不需要启用首字母头像,请使用
 
-             //$value = '/assets/img/avatar.png';
 
-             $value = letter_avatar($data['nickname']);
 
-         }
 
-         return $value;
 
-     }
 
-     /**
 
-      * 获取会员的组别
 
-      */
 
-     public function getGroupAttr($value, $data)
 
-     {
 
-         return UserGroup::get($data['group_id']);
 
-     }
 
-     /**
 
-      * 获取验证字段数组值
 
-      * @param string $value
 
-      * @param array  $data
 
-      * @return  object
 
-      */
 
-     public function getVerificationAttr($value, $data)
 
-     {
 
-         $value = array_filter((array)json_decode($value, true));
 
-         $value = array_merge(['email' => 0, 'mobile' => 0], $value);
 
-         return (object)$value;
 
-     }
 
-     /**
 
-      * 设置验证字段
 
-      * @param mixed $value
 
-      * @return string
 
-      */
 
-     public function setVerificationAttr($value)
 
-     {
 
-         $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
 
-         return $value;
 
-     }
 
-     /**
 
-      * 变更会员余额
 
-      * @param int    $money   余额
 
-      * @param int    $user_id 会员ID
 
-      * @param string $memo    备注
 
-      */
 
-     public static function money($money, $user_id, $memo)
 
-     {
 
-         Db::startTrans();
 
-         try {
 
-             $user = self::lock(true)->find($user_id);
 
-             if ($user && $money != 0) {
 
-                 $before = $user->money;
 
-                 //$after = $user->money + $money;
 
-                 $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
 
-                 //更新会员信息
 
-                 $user->save(['money' => $after]);
 
-                 //写入日志
 
-                 MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
 
-             }
 
-             Db::commit();
 
-         } catch (\Exception $e) {
 
-             Db::rollback();
 
-         }
 
-     }
 
-     /**
 
-      * 变更会员积分
 
-      * @param int    $score   积分
 
-      * @param int    $user_id 会员ID
 
-      * @param string $memo    备注
 
-      */
 
-     public static function score($score, $user_id, $memo)
 
-     {
 
-         Db::startTrans();
 
-         try {
 
-             $user = self::lock(true)->find($user_id);
 
-             if ($user && $score != 0) {
 
-                 $before = $user->score;
 
-                 $after = $user->score + $score;
 
-                 $level = self::nextlevel($after);
 
-                 //更新会员信息
 
-                 $user->save(['score' => $after, 'level' => $level]);
 
-                 //写入日志
 
-                 ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
 
-             }
 
-             Db::commit();
 
-         } catch (\Exception $e) {
 
-             Db::rollback();
 
-         }
 
-     }
 
-     /**
 
-      * 根据积分获取等级
 
-      * @param int $score 积分
 
-      * @return int
 
-      */
 
-     public static function nextlevel($score = 0)
 
-     {
 
-         $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
 
-         $level = 1;
 
-         foreach ($lv as $key => $value) {
 
-             if ($score >= $value) {
 
-                 $level = $key;
 
-             }
 
-         }
 
-         return $level;
 
-     }
 
-     //设置某人($uid)
 
-     //的新上级($intro_uid)
 
-     public function updateIntro($uid,$intro_uid){
 
-         $db = Db::name("user");
 
-         //验证
 
-         if($uid == $intro_uid) return "新邀请人不能是自己!";
 
-         $rs_user = $db->where(array('id' => $uid))->find();
 
-         if(!$rs_user) return "会员 ".$uid.' 不存在!';
 
-         $rs_intro = $db->where(array('id' => $intro_uid))->find();
 
-         if(!$rs_intro) return "新邀请人 ".$intro_uid.' 不存在!';
 
-         if($rs_user['intro_uid'] == $intro_uid) return "新邀请人不能是原来的邀请人!";
 
-         //新推荐人不能是自己下级
 
-         if($rs_intro['intro_ids']) {
 
-             $ary = explode(',', $rs_intro['intro_ids']);
 
-             if(in_array($rs_user['id'], $ary)) {
 
-                 return "新邀请人不能是自己网体下级会员!";
 
-             }
 
-         }
 
-         //更新此会员的上级
 
-         $data = array();
 
-         $data['intro_ids']   = $rs_intro['intro_ids'] ?  $rs_intro['intro_ids'].','.$rs_intro['id'] : $rs_intro['id']; //新推荐人id序列
 
-         $data['intro_level'] = $rs_intro['intro_level'] + 1; //新层数
 
-         $data['intro_uid']   = $rs_intro['id']; //新推荐人id
 
-         $rs_self = $db->where(['id' => $uid])->update($data);
 
-         if($rs_self === false){
 
-             return '更新某下级自身,失败';
 
-         }
 
-         //更新此会员的下级会员
 
-         $num_c = $rs_intro['intro_level'] - $rs_user['intro_level'] + 1;//变化的层数差
 
-         $user_sub = $db->where("find_in_set('".$rs_user['id']."',intro_ids) > 0")->order("intro_level asc")->select(); //所有下级
 
-         foreach($user_sub as   $users) { //每个下级(因为是升序查询,所以每个的上级一定先更新完毕)
 
-             $data_sub = array();
 
-             $rs_tjr = $db->where(array('id' => $users['intro_uid']))->field('id,intro_ids')->find();//另查一遍,因为都变了
 
-             $data_sub['intro_level'] = $users['intro_level'] + $num_c; //新层数
 
-             $data_sub['intro_ids'] = $rs_tjr['intro_ids'].','.$rs_tjr['id']; //新推荐人id序列
 
-             $rs_down = $db->where(array('id' => $users['id']))->update($data_sub);//更新移动网体的会员
 
-             if($rs_down === false){
 
-                 return '更新某下下级自身,失败';
 
-             }
 
-         }
 
-         //更新直推、团队数
 
-         $team = count($user_sub) + 1;
 
-         if($rs_user['intro_uid']) {
 
-             $rs_intronum = $this->addIntroNum($rs_user['intro_uid'], -1, -$team); //有则 更新原上级
 
-             if($rs_intronum === false){
 
-                 return '更新原有上级推荐数量失败';
 
-             }
 
-         }
 
-         $rs_intronum_new = $this->addIntroNum($intro_uid, 1, $team); //更新新上级
 
-         if($rs_intronum_new === false){
 
-             return '更新新的上级推荐数量失败';
 
-         }
 
-         return true;
 
-     }
 
-     /**
 
-      * 更新邀请码数和推荐团队人数
 
-      * @param string $tjrname 用户id
 
-      * @param integer $num 直推增减人数
 
-      * @param integer $team 团队增减人数
 
-      */
 
-     public function addIntroNum($tjrname, $num, $team)
 
-     {
 
-         //更新直推人数
 
-         if($num < 0) {
 
-             $rs1 = Db::name('user')->where(['id' => $tjrname])->setDec('intro_num', abs($num));
 
-         } else {
 
-             $rs1 = Db::name('user')->where(['id' => $tjrname])->setInc('intro_num', $num);
 
-         }
 
-         if($rs1 === false){
 
-             return false;
 
-         }
 
-         //更新团队
 
-         $rstjr = Db::name('user')->where(['id' => $tjrname])->field('id,intro_ids')->find();
 
-         if($rstjr) {
 
-             $tjstr = $rstjr['intro_ids'] ? ($rstjr['intro_ids'] . ',' . $rstjr['id']) : $rstjr['id'];
 
-             $tjstr = trim($tjstr, ',');
 
-             $arr_intro = explode(',', $tjstr);
 
-             if($team < 0) {
 
-                 $rs2 = Db::name('user')->where(['id' => ['in', $arr_intro]])->setDec('intro_num_all', abs($team));
 
-             } else {
 
-                 $rs2 = Db::name('user')->where(['id' => ['in', $arr_intro]])->setInc('intro_num_all', $team);
 
-             }
 
-             if($rs2 === false){
 
-                 return false;
 
-             }
 
-         }
 
-         return true;
 
-     }
 
- }
 
 
  |