Ticket.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace User\Lib;
  3. use Dever;
  4. class Ticket
  5. {
  6. # 获取门票订单列表
  7. public function getList($uid, $info_id)
  8. {
  9. $where['uid'] = $uid;
  10. $where['info_id'] = $info_id;
  11. $data = Dever::db('user/ticket_order')->getList($where);
  12. $result = array();
  13. if ($data) {
  14. foreach ($data as $k => $v) {
  15. $result[$k]['title'] = $v['name'];
  16. $result[$k]['total'] = -1;
  17. $result[$k]['status'] = 1;
  18. $result[$k]['num'] = $v['buy_num'];
  19. $result[$k]['price'] = $v['cash'] . '元';
  20. $result[$k]['cdate'] = date('Y-m-d H:i:s', $v['cdate']);
  21. }
  22. }
  23. return $result;
  24. }
  25. # 保存信息
  26. public function up($uid, $info_id, $num = 1)
  27. {
  28. $where['uid'] = $uid;
  29. $where['info_id'] = $info_id;
  30. $info = Dever::db('user/ticket')->one($where);
  31. if (!$info) {
  32. $where['num'] = $num;
  33. $where['total'] = $num;
  34. return Dever::db('user/ticket')->insert($where);
  35. } else {
  36. $update['where_id'] = $info['id'];
  37. $update['num'] = $num;
  38. $update['total'] = $num;
  39. Dever::db('user/ticket')->add($update);
  40. return $info['id'];
  41. }
  42. }
  43. # 保存说明信息
  44. public function save($uid, $id, $desc)
  45. {
  46. $where['uid'] = $uid;
  47. $where['id'] = $id;
  48. $info = Dever::db('user/ticket')->one($where);
  49. if ($info) {
  50. $update['where_id'] = $id;
  51. $update['desc'] = $desc;
  52. Dever::db('user/ticket')->update($update);
  53. }
  54. }
  55. # 获取数量
  56. public function num($uid, $info_id = false)
  57. {
  58. $where['uid'] = $uid;
  59. if ($info_id) {
  60. $where['info_id'] = $info_id;
  61. }
  62. $num = Dever::db('user/ticket')->num($where);
  63. if ($num) {
  64. return $num['num'] ? $num['num'] : 0;
  65. }
  66. return 0;
  67. }
  68. # 验证是否购买
  69. public function check($uid, $info_id)
  70. {
  71. $where['uid'] = $uid;
  72. $where['info_id'] = $info_id;
  73. $info = Dever::db('user/ticket')->one($where);
  74. return $info;
  75. }
  76. # 发起支付
  77. public function action($parent_uid, $uid, $buy_id, $object_id, $system_source, $refer = '')
  78. {
  79. if (!$uid) {
  80. Dever::alert('错误的用户信息');
  81. }
  82. $buy = Dever::db('collection/buy')->one($buy_id);
  83. if (!$buy) {
  84. Dever::alert('购买失败');
  85. }
  86. $info = Dever::db('collection/info')->one($buy['info_id']);
  87. if (!$info) {
  88. Dever::alert('购买失败');
  89. }
  90. $cate = Dever::db('collection/cate')->one($info['cate_id']);
  91. if (!$cate) {
  92. Dever::alert('购买失败');
  93. }
  94. $order_data['uid'] = $uid;
  95. $order_data['status'] = 1;
  96. $order_data['cate_id'] = $cate['id'];
  97. $order_data['info_id'] = $info['id'];
  98. $order_data['buy_id'] = $buy['id'];
  99. $order_data['buy_num'] = $buy['num'];
  100. $order_data['name'] = Dever::load("collection/lib/buy.getName", $buy);
  101. $order_data['cash'] = Dever::load("collection/lib/buy.getPrice", $buy);
  102. $order_data['system_source'] = $system_source;
  103. $order_data['order_id'] = $this->getOrderId();
  104. if (!$parent_uid && Dever::project('invite')) {
  105. $parent = Dever::load('invite/api')->getParent($uid, 1);
  106. if ($parent) {
  107. $parent_uid = $parent['uid'];
  108. }
  109. }
  110. if ($parent_uid) {
  111. $order_data['parent_uid'] = $parent_uid;
  112. }
  113. if ($object_id) {
  114. $order_data['object_id'] = $object_id;
  115. }
  116. $id = Dever::db('user/ticket_order')->insert($order_data);
  117. if (!$id) {
  118. Dever::alert('支付失败');
  119. }
  120. $param = array
  121. (
  122. 'project_id' => 2,
  123. 'channel_id' => $cate['pay_channel'],
  124. 'system_source' => $system_source,
  125. 'uid' => $uid,
  126. 'name' => $order_data['name'],
  127. 'cash' => $order_data['cash'],
  128. 'product_id' => $info['id'],
  129. 'order_id' => $order_data['order_id'],
  130. 'refer' => $refer,
  131. );
  132. $receipt = Dever::input('receipt');
  133. if ($receipt) {
  134. $param['receipt'] = $receipt;
  135. }
  136. $result = Dever::load('pay/api.pay', $param);
  137. return $result;
  138. }
  139. # 支付成功回调 安全加密 设置token
  140. public function success_secure_api_token()
  141. {
  142. $project_id = Dever::input('pay_project_id');
  143. $info = Dever::db('pay/project')->one($project_id);
  144. if ($info) {
  145. return $info['key'];
  146. }
  147. return 'ticket_dever_2020';
  148. }
  149. # 支付成功回调 安全加密
  150. public function success_secure_api($param = array())
  151. {
  152. $this->success($param);
  153. }
  154. # 支付成功回调
  155. public function success($param = array())
  156. {
  157. $send = $param ? $param : Dever::preInput('pay_');
  158. $order_id = $send['pay_order_id'];
  159. $status = $send['pay_status'];
  160. $msg = $send['pay_msg'];
  161. $order = Dever::db('user/ticket_order')->one(array('order_id' => $order_id, 'time' => time()));
  162. if ($order && $order['status'] == 1) {
  163. if ($status == 2) {
  164. # 更新持有门票数量
  165. $ticket_id = $this->up($order['uid'], $order['info_id'], $order['buy_num']);
  166. if ($ticket_id) {
  167. # 更新拥有合集数量
  168. Dever::load('user/lib/collection')->up($ticket_id, $order['uid'], $order['uid'], $order['info_id'], $order['cate_id'], 1);
  169. # 加入门票榜单
  170. Dever::load('collection/lib/ranking')->up($order['uid'], $order['info_id'], 1, $order['buy_num']);
  171. }
  172. # 更新购买数
  173. $total = Dever::db('user/ticket')->total(array('info_id' => $order['info_id']));
  174. Dever::db('collection/info')->update(array('num_buy' => $total['total'], 'where_id' => $order['info_id']));
  175. # 增加积分
  176. if ($order['parent_uid'] > 0) {
  177. $uid = $order['parent_uid'] . '_' . $order['uid'];
  178. } else {
  179. $uid = $order['uid'];
  180. }
  181. Dever::score($uid, 'buy_ticket', '购买门票', false, false, false, 'collection/info', $order['info_id']);
  182. # 发消息
  183. if (Dever::project('message')) {
  184. Dever::load('message/lib/data')->push(-1, $order['uid'], '购买提醒', '购买门票成功', 1);
  185. }
  186. }
  187. $update['status'] = $status;
  188. $update['where_id'] = $order['id'];
  189. Dever::db('user/ticket_order')->update($update);
  190. }
  191. return 'ok';
  192. }
  193. # 生成订单号
  194. public function getOrderId()
  195. {
  196. $where['order_id'] = Dever::order('T');
  197. $state = Dever::db('user/ticket_order')->one($where);
  198. if (!$state) {
  199. return $where['order_id'];
  200. } else {
  201. return $this->getOrderId();
  202. }
  203. }
  204. public function getOrderIdAndId($order_id, $id)
  205. {
  206. if ($order_id) {
  207. return $order_id . '/' . $id;
  208. } else {
  209. return '无' . '/' . $id;
  210. }
  211. }
  212. }