// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\controller; use app\api\model\Agreement as AgreementModel; use app\api\model\Invite\InviteLog; use app\api\model\User as UserModel; use app\api\model\user\BalanceLog; use app\api\model\user\GoodSource as GoodsSourceModel; use app\api\model\user\InvoiceOrder; use app\api\model\user\UserCoupon; use app\api\model\user\UserInvoice; use app\api\model\UserCoupon as UserCouponModel; use app\api\service\Feedback; use app\api\service\User as UserService; use app\common\model\dealer\Order as DealerOrderModel; use app\common\model\Order as OrderModel; use app\common\service\qrcode\InviteUser; use app\store\model\MaintenanceCategory as MaintenanceCategoryModel; use app\store\model\User as StoreUserModel; use cores\exception\BaseException; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\response\Json; /** * 用户管理 * Class User * @package app\api\controller */ class User extends Controller { /** * 注销账号 * [delete description] * @return [type] [description] */ public function delete() { $user_id = UserService::getCurrentLoginUserId(); UserModel::where('user_id', $user_id)->update(['is_delete' => 1, 'update_time' => time()]); } /** * 当前用户详情 * @return Json * @throws BaseException */ public function info(): Json { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); // 获取用户头像和会员等级 $userInfo = UserModel::related($userInfo, ['avatar', 'grade']); //获取提现次数 $userInfo['finace_count'] = BalanceLog::where(['user_id' => $userInfo->user_id, 'scene' => 50])->count() ?? 0; //获取用户收入 $userInfo['income'] = BalanceLog::where(['user_id' => $userInfo->user_id]) ->whereIn('scene', [10, 30, 40, 60]) ->where('money', '>', 0) ->sum('money') ?? 0; return $this->renderSuccess(compact('userInfo')); } /** * @notes:编辑用户信息 * @return Json|void * @throws BaseException * @author: wanghousheng */ public function editUser() { $userId = UserService::getCurrentLoginUserId(); $data = []; $avatar_id = intval($this->request->post('avatar_id')); if ($avatar_id) { $data['avatar_id'] = $avatar_id; } $nick_name = $this->request->post('nick_name'); if ($nick_name) { $data['nick_name'] = $nick_name; } $gender = intval($this->request->post('gender')); if ($gender) { $data['gender'] = $gender; } if ($data) { $model = new UserModel(); $model->where(['user_id' => $userId])->save($data); return $this->renderSuccess('操作成功'); } $this->renderError('操作失败'); } /** * 账户资产 * @return Json * @throws BaseException */ public function assets(): Json { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); // 用户优惠券模型 $model = new UserCouponModel; // 返回数据 return $this->renderSuccess([ 'assets' => [ 'balance' => $userInfo['balance'], // 账户余额 'points' => $userInfo['points'], // 会员积分 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用) ] ]); } /** * 手机号绑定 * @return Json * @throws BaseException */ public function bindMobile(): Json { $model = new UserModel; if (!$model->bindMobile($this->postForm())) { return $this->renderSuccess($model->getError() ?: '操作失败'); } return $this->renderSuccess('恭喜您,手机号绑定成功'); } /** * 修改个人信息(头像昵称) * @return Json * @throws BaseException */ public function personal(): Json { $model = new UserModel; if (!$model->personal($this->postForm())) { return $this->renderSuccess($model->getError() ?: '操作失败'); } return $this->renderSuccess('恭喜您,信息修改成功'); } /** * 提交反馈 */ public function addFeedback(): Json { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); $params = $this->request->param(); if (empty($params['content']) || (empty($params['shop_id']) && $params['object_type'] != 1) || empty($params['type'])) { return $this->renderError('请补全信息'); } $userData = [ 'user_id' => $userInfo->user_id, 'user_name' => $params['user_name'] ?? $userInfo->nick_name, 'mobile' => $params['mobile'] ? hide_mobile($params['mobile']) : $userInfo->mobile, ]; $service = new Feedback(); $res = $service->addFeedback($params, $userData); if ($res) { return $this->renderSuccess('提交成功,请耐心等待'); } else { return $this->renderError('提交失败'); } } //获取反馈列表 public function getFeedBack(): Json { $params = $this->request->param(); // 当前用户信息 if (!empty($params['is_my'])) { $userInfo = UserService::getCurrentLoginUser(true); $params['user_id'] = $userInfo->user_id; } $service = new Feedback(); $list = $service->getFeedback($params); return $this->renderSuccess(compact('list')); } //我要货源 public function addGoodsSource(): Json { $model = new GoodsSourceModel(); if (!$model->addSource($this->request->param())) { return $this->renderSuccess($model->getError() ?: '操作失败'); } return $this->renderSuccess('提交成功,请耐心等待'); } //发票抬头列表 public function myInvoicing(): Json { $service = new UserInvoice(); $list = $service->getList($this->request->param()); return $this->renderSuccess(compact('list')); } //发票抬头添加 public function addInvoicing(): Json { $service = new UserInvoice(); if (!$service->add($this->request->param())) { return $this->renderSuccess($service->getError() ?: '操作失败'); } return $this->renderSuccess('添加成功'); } //发票抬头编辑 public function editInvoicing(): Json { $service = new UserInvoice(); if (!$service->edit($this->request->param())) { return $this->renderSuccess($service->getError() ?: '操作失败'); } return $this->renderSuccess('修改成功'); } //发票抬头删除 public function delInvoicing(): Json { $service = new UserInvoice(); if (!$service->del($this->request->param())) { return $this->renderSuccess($service->getError() ?: '操作失败'); } return $this->renderSuccess('删除成功·'); } //开票记录 public function invoicingLog(): Json { $service = new InvoiceOrder(); $list = $service->getList($this->request->param()); return $this->renderSuccess(compact('list')); } //开票记录 public function invoicingDetail(): Json { $service = new InvoiceOrder(); $data = $service->getDetail($this->request->param('id')); return $this->renderSuccess(compact('data')); } //订单开票 public function invoicingAdd(): Json { $service = new InvoiceOrder(); if (!$service->invoicingAdd($this->request->param())) { return $this->renderSuccess($service->getError() ?: '操作失败'); } return $this->renderSuccess('提交成功,请耐心等待'); } //订单开票 public function invoicingEdit(): Json { $service = new InvoiceOrder(); if (!$service->invoicingEdit($this->request->param())) { return $this->renderSuccess($service->getError() ?: '操作失败'); } return $this->renderSuccess('提交成功,请耐心等待'); } //订单开票 public function invoicingDel(): Json { $service = new InvoiceOrder(); if (!$service->invoicingDel($this->request->param())) { return $this->renderSuccess($service->getError() ?: '操作失败'); } return $this->renderSuccess('提交成功,请耐心等待'); } /** * 个人中心协议 */ public function getAgreement(): Json { $params = $this->request->param(); if (empty($params['type'])) { return $this->renderSuccess("参数错误"); } $detail = AgreementModel::detail(['type' => $params['type']]); return $this->renderSuccess(compact('detail'), 'success'); } /** * 添加用户浏览记录 */ public function addView(): Json { $goods_id = $this->request->param('goods_id'); $user_id = UserService::getCurrentLoginUserId(); $userView = new UserCoupon(); if ($userView->add(['user_id' => $user_id, 'goods_id' => $goods_id, 'store_id' => $this->storeId])) { return $this->renderSuccess('记录成功'); } return $this->renderSuccess($userView->getError() ?: '记录失败'); } /** * @notes:邀请好友海报 * @return Json * @throws BaseException * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @author: wanghousheng */ public function InviteUserPoster(): Json { $userInfo = UserService::getCurrentLoginUser(true); $type = intval($this->request->post('type', 1)); // 生成二维码 $Qrcode = new InviteUser($userInfo, 'MP-WEIXIN', $type); return $this->renderSuccess(['imageUrl' => $Qrcode->getImage()]); } /** * 用户列表 * @return Json * @throws \think\db\exception\DbException */ public function list(): Json { // 用户列表 $model = new StoreUserModel; $list = $model->getList($this->request->param()); if (!$list) { return $this->renderSuccess(compact('list')); } $list = $list->toArray(); // echo "
";
        // print_r($list);
        // exit();
        foreach ($list['data'] as $key => &$value) {
            $log = InviteLog::where('invitee_user_id', $value['user_id'])->find();
            $user = UserModel::where('user_id', $log['user_id'] ?? 0)->field('user_id,nick_name,mobile')->find();
            $value['inviter'] = $user;
            $value['order_num'] = 0;
            if ($value['user_type'] == 10 || $value['user_type'] == 20) {
                $value['order_num'] = OrderModel::where('user_id', $value['user_id'])->count();
            } elseif ($value['user_type'] == 30) {
                $value['order_num'] = DealerOrderModel::where('first_user_id|second_user_id|third_user_id', '=', $value['user_id'])->count();
            }

        }

        return $this->renderSuccess(compact('list'));
    }

    public function updateUser(): Json
    {
        $params = $this->request->param();
        $user_id = $params['user_id'] ?? 0;
        unset($params['user_id']);
        UserModel::where('user_id', $user_id)->update($params);
        return $this->renderSuccess('ok');
    }

    /**
     * 生成会员码
     * [getMembershipInviteQrcode description]
     * @return Json [type] [description]
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getMembershipInviteQrcode(): Json
    {
        $user_id = $userInfo = UserService::getCurrentLoginUserId(true);
        $obj = new \app\common\service\qrcode\BaseQRcode();
        // 小程序码参数
        $scene = "refereeId:" . $user_id;
        $storeId = request()->header()['storeid'];
        // 下载小程序码
        $page = 'pages/login/index';
        $qrcode = $obj->getQrcode((int)$storeId, $scene, $page, 'MP-WEIXIN');
        return $this->renderSuccess(str_replace(root_path() . "public/", base_url(), $qrcode));
    }

    /**
     * 生成分销商码
     * [getDealerInviteQrcode description]
     * @return Json [type] [description]
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getDealerInviteQrcode(): Json
    {
        $storeId = request()->header()['storeid'];
        $user_id = $userInfo = UserService::getCurrentLoginUserId(true);
        $obj = new \app\common\service\qrcode\BaseQRcode();
        $scene = 'refereeId:' . $user_id;
        $qrcode = $obj->getQrcode((int)$storeId, $scene, 'pages/login/index', 'MP-WEIXIN');
        return $this->renderSuccess(str_replace(root_path() . "public/", base_url(), $qrcode));
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function maintenance()
    {
        $storeId = request()->header()['storeid'];
        $model = new MaintenanceCategoryModel;
        $list = $model->getList(['store_id' => $storeId])->toArray();


        return $this->renderSuccess($list);
    }
}