|
|
@@ -12,8 +12,13 @@
|
|
|
namespace App\Services\Api;
|
|
|
|
|
|
use App\Models\AccountLogModel;
|
|
|
+use App\Models\PayMealsModel;
|
|
|
+use App\Models\PayOrdersModel;
|
|
|
use App\Services\BaseService;
|
|
|
+use App\Services\PaymentService;
|
|
|
use App\Services\RedisService;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\F;
|
|
|
|
|
|
/**
|
|
|
* 交易管理-服务类
|
|
|
@@ -166,4 +171,144 @@ class AccountService extends BaseService
|
|
|
|
|
|
return $data;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 充值套餐
|
|
|
+ * @param $type
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getPayMealList($type)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:accounts:mealList_{$type}";
|
|
|
+ $datas = RedisService::get($cacheKey);
|
|
|
+ if($datas){
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ $datas = PayMealsModel::where(['type'=>$type,'status'=>1,'mark'=>1])
|
|
|
+ ->orderBy('sort','desc')
|
|
|
+ ->orderBy('id','asc')
|
|
|
+ ->get();
|
|
|
+ $datas = $datas? $datas->toAray() :[];
|
|
|
+ if($datas){
|
|
|
+ RedisService::set($cacheKey, $datas, rand(300,600));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生活充值
|
|
|
+ * @param $userId 用户ID
|
|
|
+ * @param $params
|
|
|
+ * @return array|false
|
|
|
+ */
|
|
|
+ public function recharge($userId, $params)
|
|
|
+ {
|
|
|
+ $mealId = isset($params['id']) ? $params['id'] : 0; // 套餐ID
|
|
|
+ $account = isset($params['account']) ? $params['account'] : ''; // 充值账号/手机号
|
|
|
+ $cacheKey = "caches:members:pay:{$userId}_{$mealId}";
|
|
|
+ if (RedisService::get($cacheKey . '_lock')) {
|
|
|
+ $this->error = '请不要频繁提交~';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(empty($account)){
|
|
|
+ $this->error = '请输入充值账号';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ RedisService::set($cacheKey, ['date' => date('Y-m-d H:i:s')], rand(2, 3));
|
|
|
+ $mealInfo = PayMealsModel::where(['id' => $mealId, 'status' => 1, 'mark' => 1])
|
|
|
+ ->select(['id','product_id', 'money', 'type', 'discount', 'remark', 'status'])
|
|
|
+ ->first();
|
|
|
+ $money = isset($mealInfo['money']) ? floatval($mealInfo['money']) : 0;
|
|
|
+ $discount = isset($mealInfo['discount']) ? intval($mealInfo['discount']) : 0;
|
|
|
+ $mealType = isset($mealInfo['type']) && $mealInfo['type'] ? intval($mealInfo['type']) : 1;
|
|
|
+ $productId= isset($mealInfo['product_id']) ? $mealInfo['product_id'] : 0;
|
|
|
+ $remark= isset($mealInfo['remark']) ? $mealInfo['remark'] : '';
|
|
|
+ $price = $discount?round($money*$discount/100,2): $money;
|
|
|
+ if (empty($mealInfo)) {
|
|
|
+ $this->error = '该套餐不存在';
|
|
|
+ RedisService::clear($cacheKey . '_lock');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($price <= 0 || $money <= 0 || $productId<=0) {
|
|
|
+ $this->error = '该套餐参数错误';
|
|
|
+ RedisService::clear($cacheKey . '_lock');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $info = $this->model->where(['id' => $userId, 'mark' => 1])
|
|
|
+ ->select(['id', 'openid', 'mobile', 'status'])
|
|
|
+ ->first();
|
|
|
+ $openid = isset($info['openid']) ? $info['openid'] : '';
|
|
|
+ if (!$info || $info['status'] != 1) {
|
|
|
+ $this->error = 1045;
|
|
|
+ RedisService::clear($cacheKey . '_lock');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($openid)) {
|
|
|
+ $this->error = '用户参数错误,请重新授权登录后尝试~';
|
|
|
+ RedisService::clear($cacheKey . '_lock');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建订单
|
|
|
+ $orderNo = get_order_num('PR');
|
|
|
+ $types = ['','话费充值','电费充值','燃气充值'];
|
|
|
+ $remark = isset($types[$mealType])? $types[$mealType] : '生活充值';
|
|
|
+ $order = [
|
|
|
+ 'order_no' => $orderNo,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'meal_id' => $mealId,
|
|
|
+ 'product_id' => $productId,
|
|
|
+ 'total' => $price,
|
|
|
+ 'type' => $mealType,
|
|
|
+ 'pay_total' => $price,
|
|
|
+ 'account' => $account,
|
|
|
+ 'discount' => $discount,
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'remark' => $remark,
|
|
|
+ 'status' => 1,
|
|
|
+ 'mark' => 1
|
|
|
+ ];
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ if (!$orderId = PayOrdersModel::insertGetId($order)) {
|
|
|
+ $this->error = '创建充值订单失败';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* TODO 支付处理 */
|
|
|
+ $payOrder = [
|
|
|
+ 'type' => 1,
|
|
|
+ 'order_no' => $orderNo,
|
|
|
+ 'pay_money' => $price,
|
|
|
+ 'body' => $remark,
|
|
|
+ 'openid' => $openid
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 调起支付
|
|
|
+ $payment = PaymentService::make()->minPay($info, $payOrder, 'pay');
|
|
|
+ if (empty($payment)) {
|
|
|
+ DB::rollBack();
|
|
|
+ RedisService::clear($cacheKey . '_lock');
|
|
|
+ $this->error = PaymentService::make()->getError();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户操作记录
|
|
|
+ DB::commit();
|
|
|
+ $this->error = '创建充值订单成功,请前往支付~';
|
|
|
+ RedisService::clear($cacheKey . '_lock');
|
|
|
+ return [
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'payment' => $payment,
|
|
|
+ 'total' => $payOrder['pay_money'],
|
|
|
+ 'pay_type' => 10,
|
|
|
+ ];
|
|
|
+ }
|
|
|
}
|