Util.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2019/3/14
  6. * Time: 17:47
  7. */
  8. namespace App\Modules;
  9. use App\Modules\Book\Services\BookConfigService;
  10. class Util
  11. {
  12. /**
  13. * 计算成本率
  14. * @param $amount 充值金额
  15. * @param $cost 成本
  16. * @return int|string
  17. */
  18. static function getPersentAmount($amount, $cost)
  19. {
  20. $percentResult = '0%';
  21. if (is_numeric($amount) && $amount > 0) {
  22. if (abs($cost) < 0.01) {
  23. $percentResult = '100%';
  24. } else {
  25. $percentResult = round(($amount / $cost) * 100, 2) . '%';
  26. }
  27. }
  28. return $percentResult;
  29. }
  30. static function getPersentAmountInteger($amount, $cost)
  31. {
  32. $percentResult = '0';
  33. if (is_numeric($amount) && $amount > 0) {
  34. if (abs($cost) < 0.01) {
  35. $percentResult = '1';
  36. } else {
  37. $percentResult = round(($amount / $cost), 4);
  38. }
  39. }
  40. return $percentResult;
  41. }
  42. /**
  43. * 加密site id
  44. */
  45. static function encodeDistributionChannelId($id)
  46. {
  47. $hashids = new \Hashids\Hashids('', 16, 'abcdefghjklmnopqrstuvwxyz1234567890');
  48. return $hashids->encode($id);
  49. }
  50. /**
  51. * 根据图书id获取域名
  52. * @param string $bid
  53. * @return string
  54. */
  55. static function getDomainByBid($bid = '')
  56. {
  57. $domain = 'leyuee.com';
  58. //如果图书id为空,则返回默认的域名
  59. if (!empty($bid)) {
  60. $bookConfig = BookConfigService::getBookById($bid);
  61. if ($bookConfig) {
  62. $domain = $bookConfig->promotion_domain;
  63. }
  64. }
  65. return $domain;
  66. }
  67. static function getFormatDate($date, $created_at = '')
  68. {
  69. $count = strlen($date);
  70. if ($created_at) {
  71. $year = date('Y', strtotime($created_at));
  72. } else {
  73. $year = date('Y');
  74. }
  75. if (2 == $count) {
  76. $date = $year . '0' . $date[0] . '0' . $date[1];
  77. } elseif (3 == $count) {
  78. if (0 === strpos($date, '0')) {
  79. $date = $year . substr($date, 0, 2) . '0' . $date[2];
  80. } else {
  81. $date = $year . '0' . $date;
  82. }
  83. } elseif (4 == $count) {
  84. $date = $year . $date;
  85. }
  86. return $date;
  87. }
  88. static function getFullDate($year, $month, $day, $created_at = '')
  89. {
  90. if (strlen($year) != 4) {
  91. if ($created_at) {
  92. $year = date('Y', strtotime($created_at));
  93. } else {
  94. $year = date('Y');
  95. }
  96. }
  97. $day = (strlen($day) == 1) ? ('0' . $day) : $day;
  98. $month = (strlen($month) == 1) ? ('0' . $month) : $month;
  99. $date = $year . $month . $day;
  100. $date = date('Y/m/d', strtotime($date));
  101. return $date;
  102. }
  103. }