VipService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\api\service;
  3. use app\admin\model\membership\Config;
  4. use app\api\model\ThirdPayLog;
  5. use app\api\model\User;
  6. use redis\RedLock;
  7. use think\Exception;
  8. class VipService extends BaseService
  9. {
  10. private $userModel;
  11. private $thirdPayLogModel;
  12. private $thirdPayService;
  13. private $membershipConfigModel;
  14. public function __construct()
  15. {
  16. $this->userModel = new User();
  17. $this->membershipConfigModel = new Config();
  18. $this->thirdPayLogModel = new ThirdPayLog();
  19. $this->thirdPayService = new ThirdPayService();
  20. }
  21. static function UMKey($user_id)
  22. {
  23. return 'user:membership:' . $user_id;
  24. }
  25. /**
  26. * 续费会员
  27. * @param $user_id
  28. * @param $payment_type
  29. * @param $membership_config_id
  30. * @param $platform
  31. * @return \SResult
  32. */
  33. public function renew($user_id, $membership_config_id, $payment_type, $platform)
  34. {
  35. $user = $this->userModel->findById($user_id);
  36. if (!$user)
  37. return $this->fail("用户不存在!");
  38. $config = $this->membershipConfigModel->get($membership_config_id);
  39. if (is_null($config) || !($config["real_price"] > 0))
  40. return $this->fail("会员卡价格设置错误!");
  41. $redLock = new RedLock();
  42. $lock = $redLock->lock(self::UMKey($user_id));
  43. if (!is_array($lock))
  44. return $this->fail("请稍后再试!");
  45. try {
  46. $no = "RBM" . time() . rand(10000, 99999);
  47. $log = [
  48. "no" => $no,
  49. "user_id" => $user_id,
  50. "platform" => $payment_type,
  51. "type" => null,
  52. "value" => $config["real_price"],
  53. "membership_config_id" => $config["id"],
  54. "amount" => $config["real_price"],
  55. "reason" => null,
  56. "status" => \E_BASE_STATUS::Default,
  57. "relation_no" => $no,
  58. "createtime" => time(),
  59. "updatetime" => time()
  60. ];
  61. if (\E_ORDER_PAY_TYPE::Wechat === $payment_type) {
  62. $openid = null;
  63. if ($platform == "app")
  64. $openid = $user["app_openid"];
  65. if ($platform == "web")
  66. $openid = $user["web_openid"];
  67. if ($platform == "applet")
  68. $openid = $user["applet_openid"];
  69. if (is_null($openid) || mb_strlen($openid) === 0)
  70. throw new Exception("请先绑定微信再进行支付!");
  71. $res = $this->thirdPayService->payWechat($platform, $openid, $log["no"], $config["real_price"], "充值会员");
  72. $log["type"] = \E_USER_BILL_CHANGE_TYPE::MemberCharge[0];
  73. $log["reason"] = \E_USER_BILL_CHANGE_TYPE::MemberCharge[1];
  74. } else {
  75. $log["type"] = \E_USER_BILL_CHANGE_TYPE::MemberCharge[0];
  76. $log["reason"] = \E_USER_BILL_CHANGE_TYPE::MemberCharge[1];
  77. $res = $this->thirdPayService->payAli([]);
  78. }
  79. if ($res->code())
  80. $this->thirdPayLogModel->save($log);
  81. return $res;
  82. } catch (Exception $e) {
  83. return $this->fail($e->getMessage());
  84. } finally {
  85. $redLock->unlock($lock);
  86. }
  87. }
  88. }