<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\{Category,Lave as LaveModel};
use liuniu\repositories\LaveRepository;
use liuniu\UtilService;
use think\Request;

class Lave extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function lst(Request $request)
    {
        $where = UtilService::getMore(
            [
                ['page',1],
                ['limit',10],
                ['cid',$this->cid],
                ['paid',1],
                ['category_id',0],
                ['order',''],
                ['key',''],
            ],$request
        );
        $this->success('获取成功',LaveModel::lst($where));
    }
    public function ify()
    {
        $this->success('获取成功',Category::getCategoryArray('lave'));
    }
    public function create(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['cid',$this->cid],
                ['user_id',$this->auth->getUserinfo()['id']],
                ['order_name',''],
                ['category_id',0],
                ['amount',0],
                ['name',''],
                ['contact',''],
                ['tel',0],
                ['address',''],
                ['is_open','0'],
                ['is_ticket','0'],
                ['pay_type','0'],
                ['type','0'],
                ['help_id',0],
                ['from','weixin'],
            ],$request
        );
        $where1 = $where;
        unset($where1['from']);
        $where1['order_id'] = LaveModel::getNewOrderId();
        if(cache('lave_'.$where['user_id']))  $this->error('正在处理中');
        @file_put_contents("lave.txt",json_encode($where1));
        $order = LaveModel::create($where1);
        cache('lave_'.$where['user_id'],'1',10);
        if(!$order) $this->error(LaveModel::getErrorInfo());
        $orderId = $order['order_id'];
        $info = compact('orderId');
        if ($orderId) {
            $orderInfo = LaveModel::where('order_id', $orderId)->find();
            if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
            if ($orderInfo['paid'])  $this->error('支付已支付!');
            try {
                if ($where['from'] == 'routine') {
                    $jsConfig = LaveRepository::jsPay($this->cid,$orderId); //创建订单jspay
                } else if ($where['from'] == 'weixinh5') {
                    $jsConfig = LaveRepository::h5Pay($this->cid,$orderId);
                } else {
                    $jsConfig  = LaveRepository::wxPay($this->cid,$orderId);
                }
            } catch (\Exception $e) {
                return $this->error( $e->getMessage());
            }
            $info['jsConfig'] = $jsConfig;

                return $this->success('订单创建成功',$info);

        } else $this->error(LaveModel::getErrorInfo());
    }
    public function mylst(Request  $request)
    {
        $where = UtilService::getMore(
            [
                ['page',1],
                ['limit',10],
                ['cid',$this->cid],
                ['user_id',$this->auth->getUserinfo()['id']],
                ['paid',-1],
                ['category_id',0],
            ],$request
        );
        $this->success('获取成功',LaveModel::lst($where));
    }
    /**
     * 订单支付
     * @param Request $request
     * @return mixed
     */
    public function pay(Request $request)
    {
        list($uni, $paytype, $from) = UtilService::postMore([
            ['uni', ''],
            ['paytype', '0'],
            ['from', 'weixin']
        ], $request, true);
        if (!$uni)  $this->error('参数错误!');
        $order = LaveModel::where('cid',$this->cid)->where('order_id',$uni)->find();
        if (!$order)
           $this->error('订单不存在!');
        if ($order['paid'])
            $this->error('该订单已支付!');
        $order['pay_type'] = $paytype; //重新支付选择支付方式
        switch ($order['pay_type']) {
            case '0':
                try {
                    if ($from == 'routine') {
                        $jsConfig = LaveRepository::jsPay($this->cid,$order); //订单列表发起支付
                    } else if ($from == 'weixinh5') {
                        $jsConfig = LaveRepository::h5Pay($this->cid,$order);
                    } else {
                        $jsConfig = LaveRepository::wxPay($this->cid,$order);
                    }
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success('获取成功',$jsConfig);
                break;
        }
        return $this->error('支付方式错误');
    }

}
?>