Price.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php namespace Psource\Lib;
  2. use Dever;
  3. use Place;
  4. class Price
  5. {
  6. private $place;
  7. public function __construct(Place $place)
  8. {
  9. $this->place = $place;
  10. }
  11. # 获取价格
  12. public function get($info, $set, $buy_num = 1, $rebate = [])
  13. {
  14. # 生成范围
  15. $info['scope'] = $set['scope'] . ',' . $info['id'];
  16. # 查看库存
  17. if (isset($info['sku_id'])) {
  18. # 这种是资源传过来的
  19. $info['stock'] = Dever::load(\Pstock\Lib\Info::class)->yue($info['id'], $info['sku_id']);
  20. # 检测scope是否包含有parent_id
  21. if (isset($set['parent_id']) && $set['parent_id'] <= 0 && $info['cate_child_id']) {
  22. $info['scope'] = $set['scope'] . ',' . $info['cate_child_id'] . ',' . $info['id'];
  23. } else {
  24. $info['scope'] = $set['scope'] . ',' . $info['id'];
  25. }
  26. } elseif (isset($info['info_id'])) {
  27. # 这种是sku传过来的
  28. $info['stock'] = Dever::load(\Pstock\Lib\Info::class)->yue($info['info_id'], $info['id']);
  29. $info['scope'] = $set['scope'] . ',' . $info['info_id'];
  30. }
  31. # 原价
  32. $info['m_price'] = Dever::number($info['price']);
  33. # 促销 必须登录才有
  34. $info['promotion'] = $info['rebate'] = [];
  35. if ($this->place->uid) {
  36. $info['promotion'] = Dever::load(\Pbenefit\Lib\Item::class)->load('promotion')->get($this->place->uid, $info['scope'], $info['price'], $set['score']['exp']);
  37. if ($info['promotion']) {
  38. $info['price'] = $info['promotion']['price'];
  39. }
  40. }
  41. if ($info['price'] <= 0) {
  42. $info['price'] = 0;
  43. }
  44. if ($info['price'] == $info['m_price']) {
  45. $info['m_price'] = 0;
  46. }
  47. list($info['price_text'], $info['m_price_text']) = Dever::load(\Pscore\Lib\Info::class)->getText([$info['price'], $info['m_price']], $set['score']);
  48. # 获取促销描述
  49. $info['promotion_text'] = '';
  50. if ($info['promotion']) {
  51. $info['promotion_text'] = '<table class="rich-table" style="border-collapse: collapse; text-align: center; width: 100%;">';
  52. $info['promotion_text'] .= '<tr><td>原价</td><td></td><td>'.$info['m_price_text'].'</td></tr>';
  53. $promotion = [$info['promotion']];
  54. foreach ($promotion as $k => $v) {
  55. $text = Dever::load(\Pscore\Lib\Info::class)->getText($v['price'], $set['score']);
  56. $type = '<td></td>';
  57. if ($v['type'] != 3) {
  58. $type = '<td>'.$v['value'].'</td>';
  59. }
  60. $info['promotion_text'] .= '<tr><td>'.$v['name'].'</td>'.$type.'<td>'.$text.'</td></tr>';
  61. }
  62. $info['promotion_text'] .= '</table>';
  63. }
  64. # 购买数量
  65. $info['total_price'] = $info['price'];
  66. if ($buy_num >= 0) {
  67. if ($info['stock']['num'] >= 0 && $info['stock']['num'] <= $buy_num) {
  68. $buy_num = $info['stock']['num'];
  69. }
  70. $info['buy_num'] = Dever::number($buy_num);
  71. $info['total_price'] = Dever::math('mul', $info['price'], $buy_num);
  72. /*
  73. if (isset($info['unum'])) {
  74. $info['total_unum'] = Dever::math('mul', $info['unum'], $buy_num);
  75. }
  76. */
  77. } else {
  78. # 返利
  79. if ($this->place->uid) {
  80. $info['money'] = Dever::math('mul', $info['total_price'], $set['score']['exp']);
  81. $info['rebate'] = Dever::load(\Pbenefit\Lib\Item::class)->load('rebate')->get($this->place->uid, $info['scope'], $info['money'], $rebate);
  82. }
  83. # 获取促销标签
  84. $info['promotion_tag'] = Dever::load(\Pbenefit\Lib\Item::class)->load('promotion')->getInfoByTag($info['scope']);
  85. }
  86. return $info;
  87. }
  88. # 获取专享
  89. public function getHave($info)
  90. {
  91. $result['status'] = 1;
  92. if ($info['have_vip']) {
  93. $result['status'] = 2;
  94. $result['list'] = [];
  95. $where['id'] = ['in', $info['have_vip']];
  96. $level = Dever::db('prole/level')->select($where, ['col' => 'id,name,info_id']);
  97. if ($level) {
  98. if ($this->place->uid && !isset($this->place->user['role_level'])) {
  99. $this->place->user['role_level'] = Dever::db('puser/role')->getLevel($this->place->uid);
  100. }
  101. $role = [];
  102. foreach ($level as $v) {
  103. if (empty($role[$v['info_id']])) {
  104. $role[$v['info_id']] = Dever::db('prole/info')->find(['id' => $v['info_id']], ['col' => 'id,name']);
  105. $role[$v['info_id']]['list'] = [];
  106. }
  107. $role[$v['info_id']]['list'][] = $v;
  108. if ($this->place->user['role_level'] && in_array($v['id'], $this->place->user['role_level'])) {
  109. $result['status'] = 1;
  110. }
  111. }
  112. $result['list'] = array_values($role);
  113. }
  114. }
  115. return $result;
  116. }
  117. }