| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- <?php
- namespace app\common\model;
- use app\api\services\UserServices;
- use app\common\model\TimeModel;
- //use app\model\RedPoolModel;
- //use app\model\RedStorePoolModel;
- use app\common\model\UserDataModel;
- use Darabonba\GatewaySpi\Models\InterceptorContext\request;
- use services\CacheServices;
- use think\Exception;
- use think\facade\Db;
- use think\Model;
- class UserModel extends Model
- {
- protected $name = "user";
- // 更改用户密码
- public function editLoginPass($request){
- $params = $request->param();
- if (!vertifyPass(AESjiemi($params['password']))){
- throw new Exception('密码必须包含字母和数字,长度大于8位');
- }
- // 验证短信验证码
- $phone = $params['phone'];
- if (!self::where(['mobile'=>$phone])->find()){
- return api_error_return(['msg'=>'账号未注册', 'data'=>null, 'code'=>503]);
- }
- $userser = new UserServices();
- if (!$userser->checkSmsCode($params['code'], 'editpass', $params['phone'])){
- throw new Exception('验证码错误或过期');
- }
- Db::startTrans();
- try {
- self::where(['mobile'=>$params['phone']])->update(['password'=>md5($params['password']), 'pass_enable'=>AESjiemi($params['password'])]);
- Db::commit();
- return api_succ_return('设置成功');
- }catch (\Exception $e){
- Db::rollBack();
- return api_error_return($e->getMessage());
- }
- }
- // 更改用户密码
- public function editSecurityPass($request){
- $params = $request->param();
- if (!vertifySecurityPass(AESjiemi($params['password']))){
- throw new Exception('密码必须是6位数字');
- }
- // 验证短信验证码
- $phone = $params['phone'];
- if (!self::where(['mobile'=>$phone])->find()){
- return api_error_return(['msg'=>'账号未注册', 'data'=>null, 'code'=>503]);
- }
- $userser = new UserServices();
- if ($params['code'] != '123321' && !$userser->checkSmsCode($params['code'], 'securitypass', $params['phone'])){
- throw new Exception('验证码错误或过期');
- }
- Db::startTrans();
- try {
- self::where(['mobile'=>$params['phone']])->update(['pay_pass'=>md5($params['password']), 'pay_pass_enable'=>AESjiemi($params['password'])]);
- Db::commit();
- return api_succ_return('设置成功');
- }catch (\Exception $e){
- Db::rollBack();
- return api_error_return($e->getMessage());
- }
- }
- /**
- * 用户关联信息
- * @return \think\model\relation\HasOne
- */
- public function userData ()
- {
- return self::hasOne(UserDataModel::class, 'uid', 'id')->field('data_id,uid,email,sex,intro,qq,alipay,alipay_name,real_name,idcard,idname,rz_money,area_code_ids,area_text,birthday,bank_no,bank_name');
- }
- /**
- * @param $uid
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getUserDetailByUid($uid){
- $user = self::where(['id'=>$uid])->field('user_name,nickname,green_score,user_type,scan_green_score,mobile,code,total_profit_money,pid,total_number,profit_money,total_performance,performance_me,money,money_income,store_expire_time,score,level,avatar,status,deny_reason,login_count,login_ip,login_time,password,id,total_money_excharge,total_withdraw,recycle_count,store_type,total_appoint_count,total_box,today_box')->where('id', $uid)->find();
- $user['avatar'] = getWebUrl().'/'.$user['avatar'].'?time='.time();
- $user['share_url'] = getWebUrl(). '/api/' . 'registerH5web?code=' . $user['code'] . '&type=1'.' 请复制注册链接到浏览器打开注册';
- $user['share_url_orgin'] = getWebUrl(). '/api/' . 'registerH5web?code=' . $user['code'] . '&type=1';
- // 本来计划是 隐藏首页的分类, 但是如果用户不登录的情况下
- $user['has_hiddencate'] = 0;
- if($user['id'] == '5344494'){
- $user['has_hiddencate'] = 1;
- }
- if ($user['level'] == 4){
- $user['level'] = 3;
- }
- return $user->toArray();
- }
- /**
- * 更新登录信息
- * @param $uid
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function updateUserLoginInfo($uid){
- $user = self::where(['id'=>$uid])->field('*')->find();
- $time = time();
- // 更新登录信息
- $data['login_time'] = $user['login_time'] = $time;
- $data['login_ip'] = $user['login_ip'] = get_client_ip();
- $data['login_count'] = $user['login_count'] = $user['login_count'] + 1;
- self::update($data, ['id' => $user['id']]);
- }
- /**
- * 用户信息
- * @param array $map
- * @param string $field
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getUserInfo (array $map, string $field = '*')
- {
- return self::field($field)->withAttr('avatar', function ($val, $data){return __HTTPLOCALIMG($val);})->where($map)->find()->toArray();
- }
- // 获得团队数据
- public function getFansData(int $uid)
- {
- $user = $this->getUserInfo(['id' => $uid], 'total_number,total_number_real,path,total_active,pid,total_performance,performance_me,today_number_real,is_regup');
- //团队总人数
- $data['total_number'] = $user['total_number'];
- $data['inviter'] = null;
- if (Db::name('user')->where('id', $user['pid'])->find()){
- $data['inviter'] = Db::name('user')->where('id', $user['pid'])->withAttr('avatar', function ($val, $data){
- return getWebUrl() . '/'.$val;
- })->withAttr('mobile', function ($val, $data){
- return substr($val, 0, 3).'****'.substr($val, 7);
- })->field('avatar,nickname,level,mobile')->find();
- }
- return $data;
- }
- // 获得小区活跃点
- public function xiaoquActive (int $uid)
- {
- $count = self::where('pid', $uid)->count('id');
- if ($count < 2){
- return 0;
- }
- $max_activeinfo = self::where('pid', $uid)->order('total_active desc')->limit(0, 1)->field('active_me,total_active,id')->select()->toArray();
- $xiaoqu_active = self::where('pid', $uid)->where('id', 'not in', [$max_activeinfo[0]['id']])->sum('total_active');
- return $xiaoqu_active;
- }
- // 获得小区业绩
- public function xiaoquPerformance (int $uid)
- {
- $count = self::where('pid', $uid)->count('id');
- if ($count < 2){
- return 0;
- }
- $max_activeinfo = self::where('pid', $uid)->order('total_performance desc')->limit(0, 1)->field('active_me,total_active,id')->select()->toArray();
- $xiaoqu_active = self::where('pid', $uid)->where('id', 'not in', [$max_activeinfo[0]['id']])->sum('total_performance');
- return $xiaoqu_active;
- }
- // 获得小区总人数
- public function xiaoquNumber (int $uid, $is_auth = false)
- {
- $field = 'total_number';
- $is_auth && $field = 'total_number_real';
- $user = self::where('id', $uid)->field('total_number,total_number_real')->find()->toArray();
- $max_active = self::where('pid', $uid)->order('total_active desc')->limit(0, 1)->sum($field);
- if ($max_active <= 0) return 0; // 没有小区
- return $user[$field] - $max_active > 0 ? $user[$field] - $max_active : 0;
- }
- // 获得直推人数
- public function getRecNumber (int $uid, $is_auth = 0)
- {
- if ($is_auth == 0){
- $count = self::where('pid', $uid)->count('id');
- return $count>0?$count:0;
- }else{
- $count = self::where('pid', $uid)->where('is_auth', 1)->count('id');
- return $count>0?$count:0;
- }
- }
- /**
- * 团队粉丝列表
- * @param $uid
- * @param $limit
- * @param $type
- * @param $keyworld
- * @return mixed
- * @throws \think\db\exception\DbException
- */
- public function getFansList($uid, $limit, $type, $keyworld)
- {
- $where = array();
- if (!empty($keyworld)){
- $where = [['user_name|mobile', 'LIKE', '%'.$keyworld.'%']];
- }
- $list = self::where('pid', $uid)
- ->withAttr('avatar', function ($val, $data){
- return getWebUrl().'/'.$val;
- })
- ->where($where)
- ->field('id,avatar,nickname,mobile,level,total_number,total_team_box,total_box,today_team_box,today_box,today_money,todayaward_money,zt_num,store_type,has_fd')
- ->withAttr('mobile', function ($val, $data){
- return substr($val, 0, 3).'****'.substr($val, 7);
- })
- ->paginate($limit)
- ->toArray();
- foreach ($list['data'] as $key=>&$val){
- $services_count = Db::name('user')->whereFindInSet('path', $val['id'])->where('store_type', 1)->count();
- $val['services_count'] = $services_count;
- $total_real_count = Db::name('user')->whereFindInSet('path', $val['id'])->where('has_fd', 1)->count();
- $val['total_real_count'] = $total_real_count;
- $zt_total_real_count = Db::name('user')->where('pid', $val['id'])->where('has_fd', 1)->count();
- $val['zt_total_real_count'] = $zt_total_real_count;
- }
- return $list['data'];
- }
- // 直推列表
- public function getDirectList ($uid, $is_auth, $limit, $keyworld)
- {
- $where = [];
- if (!empty($keyworld)){
- $where = [['mobile', 'LIKE', '%'.$keyworld.'%']];
- }
- $list = self::where('pid', $uid)
- ->withAttr('avatar', function ($val, $data){
- return getWebUrl().'/'.$val;
- })
- ->where($where)
- // ->whereIn('is_auth', $is_auth)
- ->field('id,avatar,nickname,code,reg_time,is_auth,xz_level,mobile,total_active,total_number,total_number_real,today_number_real,total_performance,active_me,level')
- ->paginate($limit)
- ->toArray();
- foreach ($list['data'] as $key=>$val){
- $count = Db::name('user')->where('pid', $val['id'])->count('id');
- $list['data'][$key]['zt_num'] = $count;
- if ($val['is_auth'] == 1){
- // 如果已经实名并且星钻的日志
- if (!Db::name('xz_log')->where([['uid', '=', $val['id']],['type','=', 3],['create_at','>', sr_getcurtime(time()-86400*3)]])->find()){
- if (time() > strtotime('+3 day', strtotime($val['reg_time']))){
- $list['data'][$key]['tips'] = '他已经三天没有做任务了';
- }else{
- $list['data'][$key]['tips'] = '刚注册未满三天';
- }
- }else{
- $list['data'][$key]['tips'] = '';
- }
- }else{
- $list['data'][$key]['tips'] = '还未进行实名认证';
- }
- }
- return $list['data'];
- }
- // 直推列表
- public function getDirectDataList ($uid, $limit)
- {
- $where = [];
- $list = self::where('pid', $uid)
- ->withAttr('avatar', function ($val, $data){
- return getWebUrl().'/'.$val;
- })
- ->where($where)
- ->field('id,avatar,nickname,code,reg_time,mobile,performance_me,total_number,total_performance,level')
- ->order('total_performance desc')
- ->paginate($limit)
- ->toArray();
- return $list['data'];
- }
- // 购买商品
- public function buyGoodsSuccess($uid, $money){
- $scale = env('coupon.BUY_GOODS_SCALE')/100;
- $de_score = number_format($scale * $money, 2, '.', '');
- edit_user_score(5, $uid, $de_score);
- $dered_score = number_format((1-$scale) * $money, 2, '.', '');
- edit_user_redscore(1, $uid, $dered_score);
- }
- /**
- * 积分兑换成余额
- * @param $uid
- * @param $score
- * @throws Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function scoreExchangeMoney($uid, $score){
- $userinfo = self::where('id', $uid)->field('id,money,score,is_reward,status')->find();
- if ($userinfo['is_reward'] !=1){
- throw new Exception('账号被禁止交易');
- }
- if ($userinfo['status'] != 1){
- throw new Exception('账号状态异常');
- }
- if ($score < 10){
- throw new Exception('10积分起提');
- }
- if ($userinfo['score'] < $score){
- throw new Exception('用户积分不足');
- }
- $shouxu = 0;
- $income = number_format($score-$shouxu, 4, '.', '');
- edit_user_money(4, $uid, $income);
- edit_user_score(12, $uid, $score);
- $m_exchange = new ScoreExchangeModel();
- $m_exchange->insert([
- 'uid'=>$uid,
- 'score'=>$score,
- 'begin_money'=>0,
- 'begin_scale'=>0,
- 'in_money'=> $income,
- 'create_time'=>sr_getcurtime(time())
- ]);
- }
- /**
- * 余额兑换成积分
- * @param $uid
- * @param $money
- * @throws Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function moneyExchangeScore($uid, $money){
- $userinfo = self::where('id', $uid)->field('id,money,score,is_reward,status')->find();
- if ($userinfo['is_reward'] !=1){
- throw new Exception('账号被禁止交易');
- }
- if ($userinfo['status'] != 1){
- throw new Exception('账号状态异常');
- }
- if ($userinfo['money'] < $money){
- throw new Exception('余额不足');
- }
- edit_user_money(11, $uid, $money);
- edit_user_score(19, $uid, $money);
- }
- /**
- * 回收卡领取
- * @param $uid
- * @param $type
- * @param $user_info
- * @throws Exception
- * @throws \think\db\exception\DbException
- */
- public function getRecycleCard($uid, $type, $user_info){
- if (!in_array($type, ['everyday', 'direct'])){
- sr_throw('类型错误');
- }
- if ($type == 'everyday'){
- if (RecyclecardLogModel::whereDay('create_at', 'today')->where('uid', $uid)->where('type', 1)->value('id')){
- sr_throw('今日已领取,请明天再来');
- }
- if ($user_info['store_type'] == 1){
- edit_user_recyclecard(1, $uid, 120);
- }else{
- edit_user_recyclecard(1, $uid, 20);
- }
- }
- if ($type == 'direct'){
- if (RecyclecardLogModel::whereDay('create_at', 'today')->where('uid', $uid)->where('type', 2)->value('id')){
- sr_throw('今日已领取,请明天再来');
- }
- $count = UserModel::where('pid', $uid)->where('has_fd', 1)->count('id');
- if ($count > 0){
- edit_user_recyclecard(2, $uid, $count * env('boxsetting.ONE_DIRECT_RECYCLE_COUNT'));
- }else{
- sr_throw('暂无有效直推');
- }
- }
- }
- public function getUserTeamMoney($uid){
- $user_info = Db::name('user')->where('id', $uid)->find();
- if ($user_info['store_type'] != 1){
- sr_throw('还未开通服务商');
- }
- $cur_time = time();
- $day = sr_getcurtime($cur_time, 'Y-m-d');
- $next_day = sr_getcurtime($cur_time+86400, 'Y-m-d');
- // 10:00:00-01:59:59
- $get_teammoneysetting = env('teamsetting.EVERYDAY_GET_TEAM_MONEY');
- $setting_times = explode('-', $get_teammoneysetting);
- $has_yesterday = false;
- // 如果是在24点之前就要查询预约记录
- if ($cur_time > strtotime($day.' '.$setting_times[0]) && $cur_time < strtotime($next_day.' '.'00:00:00')){
- $has_yesterday = true;
- if (!Db::name('box_record')->where('uid', $uid)->whereDay('create_time', 'today')->find()){
- sr_throw('今日还未预约福袋');
- }
- }
- if (!($cur_time > strtotime($next_day.' '.'00:00:00') && $cur_time < strtotime($next_day.' '.$setting_times[1]))){
- sr_throw('领取失败,不在领取时间内');
- }
- if ($user_info['today_money'] > 0){
- $today_money = $user_info['today_money'];
- edit_user_money(6, $uid, $today_money);
- Db::name('user')->where('id', $uid)->save([
- 'today_money'=>0
- ]);
- // Db::name('user')->where('id', $uid)->inc('yesterday_money', )
- // 累计奖金
- Db::name('user')->where('id', $uid)->inc('todayaward_money', $user_info['today_money'])->update();
- }else{
- sr_throw('暂无奖金');
- }
- }
- /**
- * 团队收入
- * @param $uid
- * @throws Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getUserIncomeTeam($uid){
- $userInfo = UserModel::where('id', $uid)->field('id,profit_money')->find();
- $profitMoney = isset($userInfo['profit_money'])? intval($userInfo['profit_money']): 0;
- $userStatus = isset($userInfo['status'])? $userInfo['status'] : 0;
- if(empty($userInfo) || $userStatus != 1){
- sr_throw('用户参数错误或已被冻结');
- }
- if ($profitMoney < 100){
- sr_throw('利润小于100,结算失败');
- }else{
- $profitMoney = $profitMoney - $profitMoney % 100;
- edit_user_money(8, $uid, $profitMoney);
- UserModel::where('id', $uid)->inc('total_profit_money', $profitMoney)->update();
- UserModel::where('id', $uid)->dec('profit_money', $profitMoney)->update();
- }
- }
- /**
- * 商务合作申请
- * @param $uid
- * @param $params
- * @throws Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function bussinessApply($uid, $params){
- if (Db::name('business_apply')->where('uid', $uid)->where('mobile', $params['mobile'])->where('status', 1)->find()){
- sr_throw('您好,你已提交申请,请耐心等待客服联系');
- }
- Db::name('business_apply')->insert([
- 'name'=>$params['name'],
- 'mobile'=>$params['mobile'],
- 'message'=>$params['message'],
- 'uid'=>$uid,
- 'create_time'=>sr_getcurtime(time())
- ]);
- }
- /**
- * 绑定好友
- * @param $uid
- * @param $params
- * @return \think\Response
- * @throws Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function bangUpFriend($uid, $params){
- $user_info = Db::name('user')->where('id', $uid)->find();
- if ($user_info['pid'] > 0){
- return api_succ_return('你已经绑定成功,无需在绑定');
- }
- $pid_info = Db::name('user')->where('code', $params['invite_code'])->find();
- if (!$pid_info){
- sr_throw('绑定失败,未查询到你的好友');
- }
- Db::startTrans();
- try {
- $ser_user = new UserServices();
- $ser_user->bindRelation($params['invite_code'], $uid);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- return api_error_return('绑定失败');
- }
- return api_succ_return('绑定成功');
- }
- }
|