// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\api\controller; use app\api\model\OrderRefund; use app\api\model\user\AccumulatePointsLog; use app\api\model\user\CommissionsDetail; use app\api\model\user\UserIdcards; use app\api\model\UserCoupon; use app\api\service\passport\Login as LoginService; use app\common\enum\order\refund\RefundStatus as RefundStatusEnum; use app\common\enum\Setting as SettingEnum; use app\common\exception\BaseException; use app\api\model\UserCoupon as UserCouponModel; use app\api\service\User as UserService; use app\common\library\helper; use app\common\model\Shops; use app\api\model\user\BonusHistory; use app\api\model\user\CommissionSteps; use app\api\model\Setting as SettingModel; use app\api\model\Order as OrderModel; use app\api\model\Coupon as CouponModel; use think\cache\driver\Redis; use app\common\model\User as UserModel; use app\api\model\User as ApiUserModel; /** * 用户管理 * Class User * @package app\api */ class User extends Controller { /** * 当前用户详情 * @return array|\think\response\Json * @throws BaseException */ public function info() { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); // 获取用户头像 $userInfo['avatar'] = $userInfo['avatar'] ?? ['preview_url' => config('chef.user_default_avatar'), 'external_url' => config('chef.user_default_avatar')]; // 获取会员等级 $userInfo['grade']; // 今日收益 $userInfo['today_profits'] = helper::bcadd(CommissionsDetail::getUserTodayProfits($userInfo->user_id), 0, 2); $userInfo['shop_name'] = ''; $userInfo['shop_type'] = 0; $userInfo['is_pickup'] = 0; if($userInfo['shop_id'] && in_array($userInfo['role'],[2,3,4])){ $shops = Shops::find($userInfo['shop_id']); $userInfo['shop_name'] = $shops['shop_name']; $userInfo['shop_type'] = $shops['is_virtual']; //门店类型 1虚拟门店 2自营店 3异业门店 if($shops['is_pickup']){ $userInfo['is_pickup'] = $shops['is_pickup'];//开启门店自提 } } $userInfo['bind_shop_name'] = Shops::find($userInfo['bind_shop_id'])['shop_name'] ?? ''; //待结算金额 $djs_amount = CommissionsDetail::where('user_id', $userInfo->user_id)->where('clearing_status', 0)->sum('clearing_money') ?? 0; $userInfo['djs_amount'] = helper::bcadd($djs_amount, 0, 2); $userInfo['show_cash_box'] = false; $wait_clearing = CommissionsDetail::getUserWaitCommission($userInfo['user_id']); //if ($wait_clearing >0 || $userInfo['can_withdraw_money'] >0){ if ($wait_clearing*100 >1 || $userInfo['ktxyj_amount']*100 >1){ $userInfo['show_cash_box'] = true; } $userInfo['can_withdraw_money'] = helper::bcsub($userInfo['can_withdraw_money'],0,2); // 佣金已结算总金额 $userInfo['have_withdrew_money'] = helper::bcsub($userInfo['have_withdrew_money'],0,2); // 已提现金额 $userInfo['ktxyj_amount'] = helper::bcsub($userInfo['ktxyj_amount'],0,2); // 可提现金额 // 上月达量奖励金 $userInfo['last_month_bonus'] = BonusHistory::lastMonthBonus($userInfo['user_id']); // 阶梯奖励金计算 $userInfo['calc_bonus_steps'] = CommissionSteps::calcBonusSteps($userInfo); // 阶梯奖励金计算 $userInfo['sales_bonus_steps'] = $list['distributor_step'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR_STEP)['distributor']; // 我的优惠券可用数量 $userInfo['user_coupon_num'] = (new UserCoupon)->getCount($userInfo['user_id']); //是否开通会员,并且会员未过期 $userInfo['is_member'] = 0; if($userInfo['member_expire_time']>0&&$userInfo['member_expire_time']>time()){ $userInfo['is_member'] = 1; } //订单数大于0 ,则为老用户 否则为新用户 $userInfo['is_new_user'] = (new OrderModel)->getCount()>0?false:true; // $userInfo['new_user_coupon_expire'] = $this->newUserCouponExpire( // $userInfo['user_id']); $receiveCouponState = 0; $key = (new UserCouponModel)->getUserCouponRdsKey($userInfo['user_id']); $rds = new Redis(config('cache.stores.redis')); $value = $rds->get($key); $userInfo['receive_coupon_state'] = intval($value??0); if($value==1){ $rds->set($key,2);// 1 代表刚领,2代表领过了 } return $this->renderSuccess(compact('userInfo')); } //新用户领的优惠券是不是都过期 public function newUserCouponExpire($user_id){ $couponIds = (new UserCoupon)->getUserCouponIds($user_id); $newUserCouponIds = (new CouponModel)->checkCouponIdBySendType($couponIds,20); $couponList = (new UserCoupon)->getUserCouponByCouponId($user_id,$newUserCouponIds); $now = Date("Y-m-d H:i:s",time()); $new_user_coupon_expire = true; foreach($couponList as $row){ $end_time = $row['end_time']; if($end_time > $now){ $new_user_coupon_expire = false; break; } } return $new_user_coupon_expire; } /** * 更新用户信息,昵称、头像、性别 * @return array * @throws BaseException * @author: zjwhust * @Time: 2022/3/2 16:15 */ public function upd() { $params = $this->request->post(); $userInfo = UserService::getCurrentLoginUser(true); $LoginService = new LoginService; $ret = $LoginService->updateWxUserInfo($params, $userInfo); if ($ret === false) { return $this->renderError('更新失败'); } return $this->renderSuccess([], '更新成功'); } /** * 更新用户生日 * @return array * @throws BaseException * @author: zjwhust * @Time: 2022/3/2 16:15 */ public function updBirthday() { $params = $this->request->post(); $birthday = $params['birthday']??''; $userInfo = new ApiUserModel; $ret = $userInfo->updateBirthday($birthday); if ($ret === false) { return $this->renderError('更新失败'); } return $this->renderSuccess([], '更新成功'); } /** * 账户资产 * @return array|\think\response\Json * @throws BaseException */ public function assets() { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); // 用户优惠券模型 $model = new UserCouponModel; // 返回数据 return $this->renderSuccess([ 'assets' => [ 'balance' => $userInfo['balance'], // 账户余额 'points' => $userInfo['points'], // 会员积分 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用) ] ]); } /** * 保存身份证和真实姓名 * @return array|\think\response\Json */ public function addIdcard(){ $param = $this->postData(); $patternIc = "/^[X0-9]{18}$/u"; if(!preg_match($patternIc,$param['id_card'])){ return $this->renderError('请输入正确的身份证号码'); } $pattern = "/^[\x{4e00}-\x{9fa5}]+$/u"; if(!preg_match($pattern,$param['real_name'])){ return $this->renderError('姓名格式不正确'); } $m = new UserIdcards(); $m->addOne($param); return $this->renderSuccess([], '保存成功'); } //下级推荐官 public function upperuser(string $kw=''){ $param = $this->postData(); $userInfo = UserService::getCurrentLoginUser(true); $filter = []; //判断是否传递了日期 if(isset($param['date']) && !empty($param['date'])){ $start_time = strtotime($param['date']); $end_time = strtotime(date('Y-m-t 23:59:59',strtotime($param['date'])));//获取月份的最后一天 $filter[] = ['created_time','>=',$start_time]; $filter[] = ['created_time','<=',$end_time]; } $user_id = $userInfo->user_id; // $user_id = 107731; // $user_id = 265; $list = UserModel::field("mobile,nick_name,user_id,avatar_id,create_time")->with(['avatar'])->where("upper_user_id",$user_id)->where(function($query) use ($kw){ if(!empty($kw)){ $query->whereOr('nick_name','like','%'.$kw.'%'); $query->whereOr('mobile','like','%'.$kw.'%'); } })->where($filter) ->where('role',99)->paginate(15)->each(function ($item){ $res = CommissionsDetail::sumUserSalesVolume($item['user_id']); $item['order_cnt'] = $res['order_cnt']; $item['order_amount'] =$res['order_amount']; //推广金额 $wheres[] = ['user_id','=',$item->user_id]; $wheres[] = ['shop_id','=',0]; $wheres[] = ['role','=',UserModel::COMMISSION_USER]; $wheres[] = ['commission_level','=',1]; $wheres[] = ['clearing_status','<',2]; $item['inviter_sale_volume'] = CommissionsDetail::sumOrderSaleVolume($wheres); $item['user_cnt'] = UserModel::where("upper_user_id",$item->user_id)->whereIn('role',[UserModel::NORMAL_USER,UserModel::COMMISSION_USER])->count(); if(strlen($item['mobile'])==11){ $item['mobile'] = substr($item['mobile'],0,3).'****'.substr($item['mobile'],7); } } ); return $this->renderSuccess(compact('list')); } /** * 在途金米粒更新到可用金米粒 * @return array * @author: zjwhust * @Time: 2022/6/11 16:01 */ public function updGoldRice(){ //获取有售后时间,并且已经过了售后时间,还有待结算的在途金米粒订单,并且没有售后中的订单 $lists = OrderModel::field('order_id,user_id,gold_rice_amount_input') ->where('refund_time','>',0) ->where('refund_time','<',time()) ->where('gold_rice_amount_input','>',0) ->where('gold_rice_is_pass',0) ->select(); // return $this->renderSuccess(compact('lists')); $num = 0; foreach ($lists as $arr){ //如果有正在退款中的订单 if(OrderRefund::where('order_id',$arr['order_id'])->where('status','<=', RefundStatusEnum::REJECTED)->count()){ continue; } //在途金米粒转到可用金米粒里 UserModel::setIncDecByField($arr['user_id'],['gold_rice'=>(float)$arr['gold_rice_amount_input']],['gold_rice_road'=>(float)$arr['gold_rice_amount_input']]); //在途金米粒更改为已结算状态 OrderModel::updateBase(['gold_rice_is_pass'=>1],['order_id'=>$arr['order_id']]); $num++; } //需要在售后成功后减掉在途的金米粒,并更新订单表里的gold_rice_amount_input字段数量 return $this->renderSuccess('成功更新'.$num.'个订单'); } public function accumulatePointsRecords(){ $list = (new AccumulatePointsLog)->getList(); return $this->renderSuccess(compact('list')); } }