| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php namespace Service\Src;
- use Dever;
- class Data
- {
- public function checkHomeLogin()
- {
- $cate = Dever::input('cate', 1);
- $user = Dever::load('passport/user')->data();
- if ($user) {
- $url = Dever::url('list?cate=' . $cate, 'service');
- Dever::location($url);
- return;
- }
- }
- public function checkLogin()
- {
- $cate = Dever::input('cate', 1);
- $user = Dever::load('passport/user')->data();
- if (!$user) {
- $url = Dever::url('home?cate=' . $cate, 'service');
- Dever::location($url);
- return;
- }
- }
- # 获取基本配置
- public function config()
- {
- $cate = Dever::input('cate', 1);
- $cate = Dever::db('journal/cate')->one($cate);
- $cate['login_title'] = '登录后才可以使用' . Dever::config('base')->name . '服务';
- $cate['bglogo'] = ':url('.$cate['logo'].') no-repeat;';
- $url = urlencode(Dever::url('list?cate=' . $cate['id'], 'service'));
- $cate['login'] = 'location.href=\'' . Dever::url('request.auth?account=1&refer=' . $url, 'oauth') . '\'';
- return $cate;
- }
-
- # 列表页
- public function lists()
- {
- $where['cate_id'] = Dever::input('cate', 1);
- $where['buy'] = 1;
- $data = Dever::db('journal/info')->getAll($where);
- if ($data) {
- foreach ($data as $k => $v) {
- if ($v['cash'] > 0) {
- $data[$k]['price'] = '¥' . number_format($v['cash'], 2);
- } else {
- $data[$k]['price'] = '免费';
- }
- $data[$k]['ding'] = '销量:' . $v['num_ding'];
- }
- }
- return $data;
- }
- public function info()
- {
- $id = Dever::input('id');
- $info = Dever::db('journal/info')->one($id);
- $info['buy'] = '正在购买《' . $info['name'] . '》';
- $buy = Dever::db('journal/buy')->one(array('id' => $id));
- if ($buy) {
- $info['buy_desc'] = $buy['buy_content'];
- } else {
- $info['buy_desc'] = '';
- }
-
- return $info;
- }
- public function code()
- {
- # 生成二维码
- $id = Dever::input('id');
- $cate = Dever::input('cate', 1);
- $this->data = Dever::load('passport/user')->data();
- if (!$this->data) {
- $url = Dever::url('home?cate=' . $cate, 'service');
- Dever::location($url);
- return;
- } else {
- $this->data['uid'] = $this->data['id'];
- }
- $type = 4;
- $scene = Dever::login($this->data['uid']) . ',' . $type . ',' . $id;
- $path = '';
- //echo $scene;die;
- $send['project'] = Dever::input('project', 1);
- $send['send'] = $scene;
- $send['width'] = 152;
- $send['path'] = $path;
- //$logo = Dever::load('wechat_applet/code.get', $send);
- $code = 'https://cm.5dev.cn/applet_on/data/upload/1/2018/06/06/239fa207778aa923f4ff7594dfbdfcca.png';
- return $code;
- }
- public function success()
- {
- $config = Dever::db('main/config')->one(1);
- $id = Dever::input('id');
- $cate = Dever::input('cate', 1);
- $config['list'] = Dever::url('list?cate=' . $cate, 'service');
- $config['buy'] = Dever::url('pay?cate='.$cate.'&id=' . $id, 'service');
- return $config;
- }
- public function buy()
- {
- $id = Dever::input('id');
- $cate = Dever::input('cate', 1);
- $data = Dever::db('journal/buy_num')->getAll(array('info_id' => $id));
- $info = Dever::db('journal/info')->one($id);
- if ($data) {
- foreach ($data as $k => $v) {
- if ($info['cash'] > 0) {
- $data[$k]['price'] = '¥' . number_format($info['cash'] * $v['num'], 2);
- } else {
- $data[$k]['price'] = '免费';
- }
- if (!$v['name']) {
- $data[$k]['name'] = '购买' . $v['num'] . '本';
- }
- $data[$k]['cate'] = $info['cate_id'];
-
- }
- }
- return $data;
- }
- public function pay()
- {
- $this->data = Dever::load('passport/user')->data();
- if (!$this->data) {
- $cate = Dever::input('cate', 1);
- $url = Dever::url('home?cate=' . $cate, 'service');
- Dever::location($url);
- return;
- } else {
- $this->data['uid'] = $this->data['id'];
- }
- $buy_id = Dever::input('buy_id');
- $journal_id = Dever::input('journal_id');
- if (!$buy_id) {
- Dever::alert('请传入正确的支付ID');
- }
- if (!$journal_id) {
- Dever::alert('请传入正确的小刊ID');
- }
- $info = Dever::db('journal/info')->one($journal_id);
- $buy = Dever::db('journal/buy_num')->one($buy_id);
- if (!$buy || !$info) {
- Dever::alert('请传入正确的小刊ID');
- }
- if (!$buy['name']) {
- $buy['name'] = '购买' . $buy['num'] . '本';
- }
- $user = Dever::db('passport/user')->one($this->data['uid']);
- $wechat = Dever::db('passport/wechat')->one(array('uid' => $this->data['uid'], 'type' => 2));
- $source_type = Dever::input('source_type');
- if ($source_type == 'ios' || $source_type == 'android') {
- $method = 'app';
- } else {
- $method = 'applet';
- }
- $order_id = $this->createOrder('JP');
- $order_data['order_id'] = $order_id;
- $order_data['product_id'] = $journal_id;
- $order_data['buy_id'] = $buy_id;
- $order_data['uid'] = $this->data['uid'];
- $order_data['type'] = 1;
- $order_data['status'] = 1;
- $order_data['name'] = $info['name'] . '-' . $buy['name'];
- //$order_data['num'] = Dever::input('num', 1);
- $order_data['num'] = $buy['num'];
- $order_data['cash'] = $info['cash'] * $order_data['num'];
- $id = Dever::db('journal/order')->insert($order_data);
- if (!$id) {
- Dever::alert('支付失败');
- }
- $cate = Dever::input('cate', 1);
- //$param参数
- $param = array
- (
- 'account_id' => 2,
- 'project_id' => 1,
- 'uid' => $this->data['uid'],
- 'username' => $user['username'],
- 'name' => $order_data['name'],
- 'cash' => $order_data['cash'],
- 'cash' => '0.01',
- 'openid' => $wechat['openid'],
- 'product_id' => $journal_id . '-' . $buy_id,
- 'order_id' => $order_id,
- 'refer' => Dever::url('success?cate='.$cate.'&id=' . $journal_id . '&buy=' . $buy_id)
- );
- return Dever::load('pay/api.page', $param);
- }
- private function createOrder($prefix = 'JC')
- {
- //$order = Dever::order($prefix);
- $order = Dever::rand(2, 2) . Dever::rand(6, 0);
- $info = Dever::db('journal/order')->one(array('order_id' => $order));
- if ($info) {
- return $this->createOrder();
- }
- return $order;
- }
- }
|