| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- /**
- * 金币套餐控制器.
- * @author zhangjunbao <junbaoz1314@gmail.com>
- * @date 2020-01-29 10:13:09
- */
- declare (strict_types=1);
- namespace Modules\Api\Controllers;
- use App\Repositories\CoinsRepository;
- use App\Repositories\Service\PaymentService;
- use App\Repositories\UserRepository;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- class CoinsController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 金币套餐页面接口
- * @param CoinsRepository $coinsRepository
- * @return mixed
- */
- public function lists(CoinsRepository $coinsRepository)
- {
- $userInfo = make(UserRepository::class)->getUserInfoFromCache($this->getUserId());
- $result = $coinsRepository->getUserCoinsPackageData($userInfo['device_type']);
- return json_success($result);
- }
- /**
- * 购买金币套餐
- * @param CoinsRepository $coinsRepository
- * @return mixed
- */
- public function buycoins(CoinsRepository $coinsRepository)
- {
- $inputData = $this->getJsonData();
- $rules = ['coin_id' => 'required|integer', 'pay_id' => 'required|integer', 'pay_type' => 'integer'];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $inputData['user_id'] = $this->getUserId();
- $order = $coinsRepository->createCoinsOrder($inputData);
- if ($order) {
- //去生成支付链接
- $order['order_type'] = 2;
- $payLink = make(PaymentService::class)->getGateway($order, $inputData['pay_id']);
- if ($payLink) {
- $data = array('jump' => 'y', 'link' => checkPayUrl($payLink));
- return json_success($data);
- }
- }
- return json_fail('亲,请重试');
- }
- }
|