You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/service/Identity.php

175 lines
5.0 KiB

10 months ago
<?php
namespace app\api\service;
use app\api\model\Payment as PaymentModel;
use app\api\service\identity\Payment;
use app\api\service\User as UserService;
10 months ago
use app\common\enum\user\IdentityEnum;
10 months ago
use app\common\service\BaseService;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
class Identity extends BaseService
{
// 提示信息
private string $message = '';
// 支付方式 (微信支付、支付宝、余额)
private string $method;
// 下单的客户端
private string $client;
/**
* 设置当前支付方式
* @param string $method 支付方式
* @return $this
*/
public function setMethod(string $method): Identity
{
$this->method = $method;
return $this;
}
/**
* 设置下单的客户端
* @param string $client 客户端
* @return $this
*/
public function setClient(string $client): Identity
{
$this->client = $client;
return $this;
}
/**
10 months ago
* 开通会员页面数据
10 months ago
* @param string $client 当前客户端
* @return array
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
10 months ago
public function userCenter(string $client): array
10 months ago
{
// 当期用户信息
$userInfo = UserService::getCurrentLoginUser(true);
8 months ago
// //是否分销商
// if (UserService::isDealerMember() || UserService::isStore()) {
// throwError('非法操作');
// }
10 months ago
// 获取充值方案列表
$model = new \app\api\model\user\Identity();
10 months ago
$planList = $model->getList(['type' => IdentityEnum::MEMBER]);
8 months ago
if (!$planList->isEmpty()) {
$planList = $this->cheapPrice($planList->toArray());
10 months ago
}
//计算优惠价格
10 months ago
// 根据指定客户端获取可用的支付方式
$PaymentModel = new PaymentModel;
$methods = $PaymentModel->getMethodsByClient($client);
// 返回数据
return [
'personal' => $userInfo,
'list' => $planList,
'paymentMethods' => $methods
];
}
8 months ago
private function cheapPrice($data): array
10 months ago
{
10 months ago
$one_data = $data[0];
10 months ago
foreach ($data as $key => $value) {
$data[$key]['cheap_price'] = 0;
if (!empty($one_data)) {
if ($key > 0) {
10 months ago
$price = $value['month'] * $one_data['price'];
10 months ago
$data[$key]['cheap_price'] = $price - $value['price'];
}
}
}
return $data;
}
10 months ago
/**
* 开通分销页面数据
* @param string $client 当前客户端
* @return array
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function dealerCenter(string $client): array
{
// 当期用户信息
$userInfo = UserService::getCurrentLoginUser(true);
// if (UserService::isStore()) {
// throwError('非法操作');
// }
10 months ago
// 获取充值方案列表
$model = new \app\api\model\user\Identity();
$planList = $model->getList(['type' => IdentityEnum::DEALER]);
8 months ago
if (!$planList->isEmpty()) {
$planList = $this->cheapPrice($planList->toArray());
10 months ago
}
10 months ago
// 根据指定客户端获取可用的支付方式
$PaymentModel = new PaymentModel;
$methods = $PaymentModel->getMethodsByClient($client);
// 返回数据
return [
'personal' => $userInfo,
'list' => $planList,
'paymentMethods' => $methods
];
}
/**
* 确认订单支付事件
* @param int $identityId
* @param array $extra 附加数据
* @return array[]
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function orderPay(int $identityId, array $extra = []): array
{
$PaymentService = new Payment();
$result = $PaymentService->setMethod($this->method)
->setClient($this->client)
->orderPay($identityId, $extra);
$this->message = $PaymentService->getMessage();
return $result;
}
/**
* 交易查询
* 查询第三方支付订单是否付款成功
* @param string $outTradeNo 商户订单号
* @return bool
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function tradeQuery(string $outTradeNo): bool
{
10 months ago
$PaymentService = new Payment();
10 months ago
return $PaymentService->setMethod($this->method)->setClient($this->client)->tradeQuery($outTradeNo);
}
/**
* 返回消息提示
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
}