123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php namespace Content\Lib;
- use Dever;
- class Value
- {
- public function getInfo($member)
- {
- $id = Dever::input('id');
- $where = array('status' => 1);
- $where['id'] = $id;
- $info = Dever::db('info', 'content')->find($where);
- if (!$info) {
- Dever::alert('内容不存在');
- }
- $password = Dever::input('password');
- if ($password) {
- if ($info['password'] && $password == $info['password']) {
- $result['status'] = 1;
- } else {
- Dever::error('密码输入错误');
- }
- } else {
- $result = $this->getPrice($member, $info);
- }
-
- if ($result['status'] == 1) {
- $result['value'] = Dever::db('value', 'content')->select(array('info_id' => $info['id']));
- }
- return $result;
- }
- # 获取价格
- public function getPrice($member, $info)
- {
- $result = array();
- $result['id'] = $info['id'];
- $result['name'] = $info['name'];
- $result['status'] = 2;
- $order = Dever::db('order', 'content')->find(array('info_id' => $info['id'], 'uid' => $member['id'], 'status' => 2));
- if ($order) {
- $result['status'] = 1;
- } else {
- if ($info['price'] > 0) {
- if ($member['vip_id'] > 0) {
- $vip_price = 0;
- $price = Dever::db('price', 'content')->select(array('type' => 1, 'info_id' => $info['id']));
- if ($price) {
- foreach ($price as &$v) {
- if ($v['type_id'] == $member['vip_id']) {
- $vip_price = $v['price'];
- break;
- } elseif ($v['type_id'] <= 0) {
- $vip_price = $v['price'];
- }
- }
- if ($vip_price < $info['price']) {
- $info['price'] = $vip_price;
- }
- }
- }
- if ($member['agent_id'] > 0) {
- $agent_price = 0;
- $price = Dever::db('price', 'content')->select(array('type' => 2, 'info_id' => $info['id']));
- if ($price) {
- foreach ($price as &$v) {
- if ($v['type_id'] == $member['agent_id']) {
- $agent_price = $v['price'];
- break;
- } elseif ($v['type_id'] <= 0) {
- $agent_price = $v['price'];
- }
- }
- if ($agent_price < $info['price']) {
- $info['price'] = $agent_price;
- }
- }
- }
- }
- if ($info['price'] <= 0) {
- $result['status'] = 1;
- } else {
- $result['password'] = 2;
- if ($info['password']) {
- $result['password'] = 1;
- }
- $result['status'] = 2;
- $result['price'] = $info['price'];
- $result['price_text'] = Dever::load('info', 'content')->getPrice($info['price'], $member['score']);
- $result['button'] = '购买';
- }
- }
- return $result;
- }
- # 下单
- public function getPay($member, $info, $account = 'wechat', $env = 3)
- {
- # 支付账户
- $account = 'pay_' . $account;
- # 下单
- $order = array
- (
- 'uid' => $member['id'],
- 'username' => $member['name'],
- 'openid' => $member['openid'] ?? '',
- 'order_num' => Dever::load('util', 'api')->createNumber('C', 'content/order'),
- 'cash' => ($info['price']/$member['score_per'])*100,#每个人的积分换算公式可能不一样
- 'name' => $info['name'],
- 'time_expire' => time() + 3600,
- );
- # 设置支付成功的回调信息,多个参数用|隔开
- $param['notify'] = 'content/value.paySuccess|' . Dever::input('p') . '|' . $order['order_num'];
- $data = Dever::load('account', 'api')->run($account, 'order', $order, $env);
- if ($data) {
- $order['info_id'] = $info['id'];
- $order['account_id'] = $data['account_id'];
- $order['api_id'] = $data['api_id'];
- $data['order_id'] = Dever::db('order', 'content')->insert($order);
- }
- return $data;
- }
- # 支付成功
- public function paySuccess($place, $order_num, $status, $body)
- {
- Dever::input('authorization', 'is_string', '入口码', $place);
- $order = Dever::db('order', 'content')->find(array('order_num' => $order_num));
- if ($order && $order['status'] == 1) {
- if ($status == 1) {
- $update['status'] = 2;
- } else {
- $update['status'] = 3;
- }
- $update['pdate'] = time();
- $state = Dever::db('order', 'content')->update($order['id'], $update);
- if (!$state && $order['project_id']) {
- return '订单更新失败';
- }
- }
- }
- }
|