coupon-admin/app/controller/Account.php

513 lines
15 KiB
PHP

<?php
namespace app\controller;
use app\exception\RepositoryException;
use app\repository\AccountRepository;
use app\repository\CommonRepository;
use app\repository\GoodsRepository;
use app\service\Kd100;
use app\validate\Account as VAccount;
use app\repository\OrderRepository;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use think\response\Json;
use think\response\View;
class Account extends Base
{
protected $middleware = ['login'];
/**
* 个人中心
*
* @return View
* @throws DbException
*/
public function index(): View
{
$this->data['countList'] = OrderRepository::getInstance()->findOrderCount($this->authId);
$this->data['markList'] = OrderRepository::getInstance()->auctionMarkList('', [$this->authId]);
$this->data['biddingList'] = GoodsRepository::getInstance()->auctionBiddingList($this->authId);
return $this->view();
}
/**
* 个人资料
*
* @return View|Json
* @throws RepositoryException
*/
public function profile()
{
$repo = AccountRepository::getInstance();
$item = $repo->findById($this->authId);
if ($this->request->isPost()) {
$post = input('post.');
$update = [];
$update['headimgurl'] = $post['headimgurl'] ?? '';
$update['sex'] = $post['sex'];
$update['nickname'] = $post['nickname'];
if (!empty($post['nickname']) && $item['nickname'] !== $post['nickname']) {
if ($item['nickname_time'] > 0) {
return $this->json(4001, '每人仅先修改一次昵称,您已修改过了。');
}
$update['nickname_time'] = $item['nickname_time'] + 1;
}
$item->save($update);
return $this->json();
}
$this->data['item'] = $item;
return $this->view();
}
/**
* 我的订单
*
* @return View
*/
public function order(): View
{
$accountId = $this->auth['id'] ?? 0;
$tag = input('get.tag/s', 'waiting');
$this->data['items'] = OrderRepository::getInstance()->getOrderGoods($accountId, $tag);
$this->data['tag'] = $tag;
return $this->view();
}
/**
* 保证金
*
* @return View
* @throws RepositoryException
*/
public function deposit(): View
{
$accountId = $this->auth['id'] ?? 0;
if (!$accountId) {
$this->error('请先登录');
}
$item = AccountRepository::getInstance()->findById($accountId, ['quota', 'id', 'deposit']);
$this->data['item'] = $item;
return $this->view();
}
/**
* 实名认证
*
* @return View|Json
* @throws RepositoryException
*/
public function realName()
{
$accountId = $this->auth['id'] ?? 0;
$account = AccountRepository::getInstance()->findById($accountId);
if ($this->request->isPost()) {
if (!$account) {
return $this->json(4001, '请先登录');
}
if ($account['is_real_name']) {
return $this->json(4002, '您已通过认证');
}
$post = input('post.');
$validate = new VAccount();
if (!$validate->scene('real_name')->check($post)) {
return $this->json(4003, $validate->getError());
}
$post['is_real_name'] = 1;
$account->save($post);
return $this->json();
}
$this->data['item'] = $account;
return $this->view();
}
/**
* 个人账户
*
* @return View
* @throws RepositoryException
*/
public function info(): View
{
$accountId = $this->auth['id'] ?? 0;
$item = AccountRepository::getInstance()->findById($accountId);
$rechargeList = AccountRepository::getInstance()->fetchRechargeList($accountId);
$withdrawalList = AccountRepository::getInstance()->fetchWithdrawalList($accountId);
$this->data['item'] = $item;
$this->data['rechargeList'] = $rechargeList['list'];
$this->data['withdrawalList'] = $withdrawalList['list'];
return $this->view();
}
/**
* 中标清单
*
* @return View
* @throws DbException
*/
public function biddingList(): View
{
$accountId = $this->auth['id'] ?? 0;
$pageParams['query'] = input('get.');
$list = OrderRepository::getInstance()->auctionMarkList('', [$accountId], $pageParams);
$this->data['list'] = $list;
return $this->view();
}
/**
* 竞价清单
*
* @return View
*/
public function biddingLog(): View
{
$list = GoodsRepository::getInstance()->auctionBiddingList($this->authId);
$this->data['items'] = $list;
return $this->view();
}
/**
* 关注的拍品
*
* @return View
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function auctionCollection(): View
{
$accountId = $this->auth['id'] ?? 0;
$items = AccountRepository::getInstance()->fetchCollection($accountId, GoodsRepository::TYPE_AUCTION);
$this->data['items'] = $items;
return $this->view();
}
/**
* 商品收藏
*
* @return View
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function goodsCollection(): View
{
$accountId = $this->auth['id'] ?? 0;
$items = AccountRepository::getInstance()->fetchCollection($accountId, GoodsRepository::TYPE_GOODS);
$this->data['items'] = $items;
return $this->view();
}
/**
* 地址管理
*
* @return View
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function address(): View
{
$accountId = $this->auth['id'] ?? 0;
//地址列表
$addressList = AccountRepository::getInstance()->findAddressByUid($accountId);
$this->data['addressList'] = $addressList;
return $this->view();
}
/**
* 地址详情
*
* @return Json|View
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function addressDetail()
{
$resp = AccountRepository::getInstance();
$accountId = $this->auth['id'] ?? 0;
$id = input('param.id/d', 0);
$item = $resp->findAddressById($id);
if ($this->request->isPost()) {
$postItem = input('post.');
$postItem['user_id'] = $postItem['user_id'] ?? $accountId;
$resp->saveAddress($postItem);
return $this->json();
}
$this->data['item'] = $item;
return $this->view();
}
/**
* 地址删除
*
* @return Json
*/
public function addressDel(): Json
{
if ($this->request->isPost()) {
$id = input('post.id/d', 0);
AccountRepository::getInstance()->delAddress($id);
return $this->json();
}
return $this->json(4002, '请求方式错误');
}
/**
* 修改密码
*
* @return Json
*/
public function editPwd(): Json
{
$post = input('post.');
$accountId = $this->auth['id'] ?? 0;
$validate = new VAccount();
if (!$validate->scene('edit_password')->check($post)) {
return $this->json(4002, $validate->getError());
}
try {
AccountRepository::getInstance()->modifyPwd($accountId, $post['old_password'], $post['password']);
} catch (Exception $e) {
$this->json(4003, $e->getMessage());
}
return $this->json();
}
/**
* 取消收藏|关注
*
* @return Json
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function unCollect(): Json
{
$accountId = $this->auth['id'] ?? 0;
$goodsId = input('post.id/d');
AccountRepository::getInstance()->unCollectGoods($accountId, $goodsId);
return $this->json();
}
/**
* 操作保证金 充值|退回 相应改变额度
*
* @return Json
* @throws RepositoryException
*/
public function operateDeposit(): Json
{
if ($this->request->isPost()) {
$accountId = $this->auth['id'] ?? 0;
$amount = input('post.value', 0);
$type = input('post.type/s', 'recharge');
if (!AccountRepository::getInstance()->isEnough($accountId, $amount, 'deposit')) {
return $this->json(4000, '余额不足');
}
try {
AccountRepository::getInstance()->operateDeposit($accountId, $amount, $type);
} catch (RepositoryException $e) {
return $this->json(4002, $e->getMessage());
} catch (Exception $e) {
CommonRepository::log('保证金充值失败', $e);
return $this->json(5001, '充值失败');
}
return $this->json();
}
return $this->json(4002, '请求方式错误');
}
/**
* 充值
*
* @return Json
* @throws GuzzleException
*/
public function rechargeAmount(): Json
{
if ($this->request->isPost()) {
$accountId = $this->auth['id'] ?? 0;
$amount = input('post.amount', 0);
$type = input('post.type/s');
if (!$accountId) {
return $this->json(4001, '请先登录');
}
try {
$qr = AccountRepository::getInstance()->recharge($accountId, (float) $amount, $type);
return $this->json(0, 'success', ['payment_order_number' => $qr['order_id'], 'code_url' => $qr['qr']]);
} catch (Exception $e) {
CommonRepository::log('充值失败', $e);
return $this->json(5001, '充值失败:'.$e->getMessage());
}
}
return $this->json(4002, '请求方式错误');
}
/**
* 重新充值
*
* @return Json
* @throws GuzzleException
*/
public function rechargeAgain(): Json
{
if ($this->request->isPost()) {
$accountId = $this->auth['id'] ?? 0;
$orderId = input('post.order_id', 0);
$type = input('post.type/s');
if (!$accountId) {
return $this->json(4001, '请先登录');
}
try {
$qr = AccountRepository::getInstance()->rechargeAgain($accountId, $orderId, $type);
return $this->json(0, 'success', ['payment_order_number' => $qr['order_id'], 'code_url' => $qr['qr']]);
} catch (Exception $e) {
CommonRepository::log('充值失败', $e);
return $this->json(5001, '充值失败:'.$e->getMessage());
}
}
return $this->json(4002, '请求方式错误');
}
/**
* 查询订单
*
* @return array|Json
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function checkPayment()
{
if ($this->request->isPost()) {
$res['code'] = 0;
$res['msg'] = 'success';
$res['data']['paid'] = true;
$orderId = input('post.order_id/s', ''); //订单ID
$type = input('post.type/s', 'all'); //支付方式
if (!$orderId || !$type) {
$res['code'] = 1;
$res['msg'] = '参数错误';
return $res;
}
//根据支付类型 请求接口查询订单
$hasPaid = AccountRepository::getInstance()->hasPaid($orderId, $type);
if ($hasPaid) {
AccountRepository::getInstance()->paymentLogPaid($orderId, $type);
}
$res['data']['paid'] = $hasPaid;
return $res;
}
return $this->json(1, '非法请求!');
}
/**
* 提现
*
* @return Json
*/
public function withdrawal(): Json
{
if ($this->request->isPost()) {
$amount = input('post.amount', '');
$accountId = $this->auth['id'] ?? 0;
try {
AccountRepository::getInstance()->withdrawal($accountId, $amount);
return $this->json();
} catch (Exception $e) {
CommonRepository::log('提现失败', $e);
return $this->json(5001, '提现失败');
}
}
return $this->json(1, '非法请求!');
}
/**
* 获取已有订单号的支付二维码
*
* @return Json
* @throws GuzzleException
*/
public function getPaymentQr(): Json
{
if ($this->request->isPost()) {
$type = input('post.type', '');
$orderId = input('post.order_id', '');
try {
$codeQr = OrderRepository::getInstance()->generatePaymentQrCode($type, $orderId);
return $this->json(0, 'success', ['payment_order_number' => $orderId, 'code_url' => $codeQr]);
} catch (Exception $e) {
CommonRepository::log('获取支付二维码', $e);
return $this->json(5001, '获取支付二维码');
}
}
}
/**
* 物流查询
*
* @return Json
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function logistics(): Json
{
$orderId = input('post.order_id');
$type = input('post.type', 'goods');
// $com = 'shunfeng';
// $num = 'SF1980860132707';
if ($type == 'goods') {
if (!$delivery = OrderRepository::getInstance()->findExpressByOrder($orderId)) {
return $this->json(4001, '物流信息不存在');
}
} else {
if (!$delivery = OrderRepository::getInstance()->findExpressByAuction($orderId)) {
return $this->json(4001, '物流信息不存在');
}
}
$com = $delivery['express_code'];
$num = $delivery['express_number'];
return $this->json(0, 'success', ['res' => Kd100::query($com, $num)]);
}
}