123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- 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;
- $type = intval( $params['type'] ?? 0);
- if($type == 1){//昵称
- $params = ['nickName' => $params['nickName']];
- }elseif($type == 2){//头像
- $params = ['avatarUrl' => $params['avatarUrl']];
- }
- $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'));
- }
-
- }
|