<?php
namespace Tests\src;

use Dever;

class Api
{
    # 用户注册
    public function user()
    {
        $data['avatar'] = '';
        $data['sex'] = rand(1,2);
        $data['city'] = '';
        $data['province'] = '';
        $data['country'] = '';
        $data['openid'] = 'openid_test_' . rand(1, 10000000);
        $data['unionid'] = 'unionid_test_' . rand(1, 100000);
        $data['username'] = $data['openid'];
        $data['session_key'] = $data['openid'];
        Dever::load('passport/applet')->create($data);

        return 'ok';
    }

    # 购买电子刊
    public function buy()
    {
        $id = Dever::input('journal_id', 1);
        $buy_id = Dever::input('buy_id', 1);
        $journal = Dever::load('act/order')->getJournal($id);
        $buy = Dever::load('act/order')->getBuy($buy_id);

    	//$order_id = Dever::load('act/order')->createOrder('JP');
        $order_data['cate_id'] = $journal['cate_id'];
        //$order_data['order_id'] = $order_id;
        $order_data['product_id'] = $journal['id'];
        $order_data['buy_id'] = $buy['id'];
        $order_data['uid'] = 1; 
        $order_data['type'] = 3;
        $order_data['status'] = 1;

        $order_data['name'] = $journal['name'] . '-' . $buy['name'];
        $order_data['buy_num'] = $buy['num'];

        $order_data['cash'] = 100;

        $source = Dever::input('source');
        $system = Dever::input('system', 1);
        if (!$source) {
            $source = $system;
        }
        if ($source) {
            $order_data['source'] = $source;
        }
        $seller = Dever::input('seller');
        if ($seller) {
            $order_data['seller_id'] = $seller;
        }

        $id = Dever::db('act/order_temp')->insert($order_data);

        $send['pay_product_id'] = $order_data['product_id'];
        $send['pay_uid'] = $order_data['uid'];
        $send['pay_cash'] = $order_data['cash'];
        $send['pay_order_id'] = Dever::load('act/order')->createOrderId($order_data, $id);
        $send['pay_status'] = 2;
        $send['pay_msg'] = '';


        Dever::load('act/lib/pay')->act_action($send);
        return 'ok';
    }

    private function uid()
    {
        $uid = rand(1, 10);
        $uid = Dever::login($uid);
        Dever::setInput('signature', $uid);

        $id = rand(1, 10);
        Dever::setInput('id', $id);
    }

    # 查看电子刊列表
    public function getList()
    {
        $this->uid();
        $data = Dever::load('main/journal_v1')->getList();

        return $data;
    }

    # 查看电子刊列表
    public function getView()
    {
        $this->uid();
        $data = Dever::load('main/journal_v1')->view();

        return $data;
    }

    # 获取电子刊权限
    public function getRead()
    {
        $this->uid();
        $data = Dever::load('main/journal_v1')->read();

        return $data;
    }
}
