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); } } }