| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\api\service;
- use app\admin\model\membership\Config;
- use app\api\model\ThirdPayLog;
- use app\api\model\User;
- use redis\RedLock;
- use think\Exception;
- class VipService extends BaseService
- {
- private $userModel;
- private $thirdPayLogModel;
- private $thirdPayService;
- private $membershipConfigModel;
- public function __construct()
- {
- $this->userModel = new User();
- $this->membershipConfigModel = new Config();
- $this->thirdPayLogModel = new ThirdPayLog();
- $this->thirdPayService = new ThirdPayService();
- }
- static function UMKey($user_id)
- {
- return 'user:membership:' . $user_id;
- }
- /**
- * 续费会员
- * @param $user_id
- * @param $payment_type
- * @param $membership_config_id
- * @param $platform
- * @return \SResult
- */
- public function renew($user_id, $membership_config_id, $payment_type, $platform)
- {
- $user = $this->userModel->findById($user_id);
- if (!$user)
- return $this->fail("用户不存在!");
- $config = $this->membershipConfigModel->get($membership_config_id);
- if (is_null($config) || !($config["real_price"] > 0))
- return $this->fail("会员卡价格设置错误!");
- $redLock = new RedLock();
- $lock = $redLock->lock(self::UMKey($user_id));
- if (!is_array($lock))
- return $this->fail("请稍后再试!");
- try {
- $no = "RBM" . time() . rand(10000, 99999);
- $log = [
- "no" => $no,
- "user_id" => $user_id,
- "platform" => $payment_type,
- "type" => null,
- "value" => $config["real_price"],
- "membership_config_id" => $config["id"],
- "amount" => $config["real_price"],
- "reason" => null,
- "status" => \E_BASE_STATUS::Default,
- "relation_no" => $no,
- "createtime" => time(),
- "updatetime" => time()
- ];
- if (\E_ORDER_PAY_TYPE::Wechat === $payment_type) {
- $openid = null;
- if ($platform == "app")
- $openid = $user["app_openid"];
- if ($platform == "web")
- $openid = $user["web_openid"];
- if ($platform == "applet")
- $openid = $user["applet_openid"];
- if (is_null($openid) || mb_strlen($openid) === 0)
- throw new Exception("请先绑定微信再进行支付!");
- $res = $this->thirdPayService->payWechat($platform, $openid, $log["no"], $config["real_price"], "充值会员");
- $log["type"] = \E_USER_BILL_CHANGE_TYPE::MemberCharge[0];
- $log["reason"] = \E_USER_BILL_CHANGE_TYPE::MemberCharge[1];
- } else {
- $log["type"] = \E_USER_BILL_CHANGE_TYPE::MemberCharge[0];
- $log["reason"] = \E_USER_BILL_CHANGE_TYPE::MemberCharge[1];
- $res = $this->thirdPayService->payAli([]);
- }
- if ($res->code())
- $this->thirdPayLogModel->save($log);
- return $res;
- } catch (Exception $e) {
- return $this->fail($e->getMessage());
- } finally {
- $redLock->unlock($lock);
- }
- }
- }
|