Value.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php namespace Content\Lib;
  2. use Dever;
  3. class Value
  4. {
  5. public function getInfo($member)
  6. {
  7. $id = Dever::input('id');
  8. $where = array('status' => 1);
  9. $where['id'] = $id;
  10. $info = Dever::db('info', 'content')->find($where);
  11. if (!$info) {
  12. Dever::alert('内容不存在');
  13. }
  14. $password = Dever::input('password');
  15. if ($password) {
  16. if ($info['password'] && $password == $info['password']) {
  17. $result['status'] = 1;
  18. } else {
  19. Dever::error('密码输入错误');
  20. }
  21. } else {
  22. $result = $this->getPrice($member, $info);
  23. }
  24. if ($result['status'] == 1) {
  25. $result['value'] = Dever::db('value', 'content')->select(array('info_id' => $info['id']));
  26. }
  27. return $result;
  28. }
  29. # 获取价格
  30. public function getPrice($member, $info)
  31. {
  32. $result = array();
  33. $result['id'] = $info['id'];
  34. $result['name'] = $info['name'];
  35. $result['status'] = 2;
  36. $order = Dever::db('order', 'content')->find(array('info_id' => $info['id'], 'uid' => $member['id'], 'status' => 2));
  37. if ($order) {
  38. $result['status'] = 1;
  39. } else {
  40. if ($info['price'] > 0) {
  41. if ($member['vip_id'] > 0) {
  42. $vip_price = 0;
  43. $price = Dever::db('price', 'content')->select(array('type' => 1, 'info_id' => $info['id']));
  44. if ($price) {
  45. foreach ($price as &$v) {
  46. if ($v['type_id'] == $member['vip_id']) {
  47. $vip_price = $v['price'];
  48. break;
  49. } elseif ($v['type_id'] <= 0) {
  50. $vip_price = $v['price'];
  51. }
  52. }
  53. if ($vip_price < $info['price']) {
  54. $info['price'] = $vip_price;
  55. }
  56. }
  57. }
  58. if ($member['agent_id'] > 0) {
  59. $agent_price = 0;
  60. $price = Dever::db('price', 'content')->select(array('type' => 2, 'info_id' => $info['id']));
  61. if ($price) {
  62. foreach ($price as &$v) {
  63. if ($v['type_id'] == $member['agent_id']) {
  64. $agent_price = $v['price'];
  65. break;
  66. } elseif ($v['type_id'] <= 0) {
  67. $agent_price = $v['price'];
  68. }
  69. }
  70. if ($agent_price < $info['price']) {
  71. $info['price'] = $agent_price;
  72. }
  73. }
  74. }
  75. }
  76. if ($info['price'] <= 0) {
  77. $result['status'] = 1;
  78. } else {
  79. $result['password'] = 2;
  80. if ($info['password']) {
  81. $result['password'] = 1;
  82. }
  83. $result['status'] = 2;
  84. $result['price'] = $info['price'];
  85. $result['price_text'] = Dever::load('info', 'content')->getPrice($info['price'], $member['score']);
  86. $result['button'] = '购买';
  87. }
  88. }
  89. return $result;
  90. }
  91. # 下单
  92. public function getPay($member, $info, $account = 'wechat', $env = 3)
  93. {
  94. # 支付账户
  95. $account = 'pay_' . $account;
  96. # 下单
  97. $order = array
  98. (
  99. 'uid' => $member['id'],
  100. 'username' => $member['name'],
  101. 'openid' => $member['openid'] ?? '',
  102. 'order_num' => Dever::load('util', 'api')->createNumber('C', 'content/order'),
  103. 'cash' => ($info['price']/$member['score_per'])*100,#每个人的积分换算公式可能不一样
  104. 'name' => $info['name'],
  105. 'time_expire' => time() + 3600,
  106. );
  107. # 设置支付成功的回调信息,多个参数用|隔开
  108. $param['notify'] = 'content/value.paySuccess|' . Dever::input('p') . '|' . $order['order_num'];
  109. $data = Dever::load('account', 'api')->run($account, 'order', $order, $env);
  110. if ($data) {
  111. $order['info_id'] = $info['id'];
  112. $order['account_id'] = $data['account_id'];
  113. $order['api_id'] = $data['api_id'];
  114. $data['order_id'] = Dever::db('order', 'content')->insert($order);
  115. }
  116. return $data;
  117. }
  118. # 支付成功
  119. public function paySuccess($place, $order_num, $status, $body)
  120. {
  121. Dever::input('authorization', 'is_string', '入口码', $place);
  122. $order = Dever::db('order', 'content')->find(array('order_num' => $order_num));
  123. if ($order && $order['status'] == 1) {
  124. if ($status == 1) {
  125. $update['status'] = 2;
  126. } else {
  127. $update['status'] = 3;
  128. }
  129. $update['pdate'] = time();
  130. $state = Dever::db('order', 'content')->update($order['id'], $update);
  131. if (!$state && $order['project_id']) {
  132. return '订单更新失败';
  133. }
  134. }
  135. }
  136. }