Grade.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\user;
  13. use app\common\library\helper;
  14. use app\common\service\BaseService;
  15. use app\api\service\User as UserService;
  16. use app\api\model\user\Grade as UserGradeModel;
  17. use cores\exception\BaseException;
  18. /**
  19. * 服务类: 会员等级
  20. * Class Grade
  21. * @package app\api\service\user
  22. */
  23. class Grade extends BaseService
  24. {
  25. /**
  26. * 获取当前登录用户的会员等级信息
  27. * @param bool $isForce 是否强制验证登录, 如果未登录将抛错
  28. * @throws BaseException
  29. */
  30. public static function getCurrentGradeInfo(bool $isForce = false)
  31. {
  32. // 当前登录的用户信息
  33. $userInfo = UserService::getCurrentLoginUser($isForce);
  34. if (empty($userInfo) || empty($userInfo['grade_id'])) {
  35. return false;
  36. }
  37. // 获取会员等级信息
  38. $gradeInfo = UserGradeModel::detail($userInfo['grade_id']);
  39. if (empty($gradeInfo) || $gradeInfo['is_delete'] || empty($gradeInfo['status'])) {
  40. return false;
  41. }
  42. return $gradeInfo;
  43. }
  44. /**
  45. * 获取和计算折扣后的价格
  46. * @param $originalPrice
  47. * @param $discountRatio
  48. * @return string
  49. */
  50. public static function getDiscountPrice($originalPrice, $discountRatio): string
  51. {
  52. // 使用高精度方法计算等级折扣; 因bcmach不支持四舍五入, 所以精确计算到3位数, 使用round四舍五入
  53. $discountPrice = helper::bcmul($originalPrice, $discountRatio / 10, 3);
  54. $discountPrice = round((float)$discountPrice, 2);
  55. return helper::number2($discountPrice, true);
  56. }
  57. }