Demo entry 6645850

test

   

Submitted by anonymous on Oct 12, 2017 at 01:36
Language: PHP. Code size: 7.4 kB.

<?php
namespace Index\controller;

use Nette\Mail\Message;
use service\View;
use App\model\Goods;
use App\model\User;
use App\model\Order;
use App\model\Finance;
use App\model\Address;
// 开启事务使用
// use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Support\Facades\DB;

use App\controller\BaseController as BaseController;

/**
 * 前台控制器
 * Created by Bruce.
 * User: Bruce
 * Date: 2017/10/5
 * Time: 20:22
 */
class IndexController extends BaseController
{
    public $user_id;

    public function __construct()
    {
        parent::__construct();
        View::setViewPath();

        // $this->auth();
        $this->user_id = $_SESSION['user_id'];
    }

    /**
     * 检查用户是否登录
     */
    public function isLogin()
    {
        if (isset($_SESSION['user_id'])) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查用户,如果未登录,则展示登录框
     */
    public function auth()
    {
        if ( ! $this->isLogin()) {
            // echo "<script>history.go(-1);</script>";
            // $this->view = View::make('login');
            // echo "<script>document.getElementById('login').click();</script>";
            echo "<script>alert('请登录后操作!');history.go(-1);</script>";
            exit;
        }
    }

    /**
     * 商城首页
     */
    public function index()
    {
        // View::get();
        $this->view = View::make('index')->with('goods', Goods::all());
    }

    /**
     * 商品详情页
     */
    public function detail()
    {
        $id         = $_GET['id'];
        $this->view = View::make('detail')->with('goods', Goods::find($id));
    }

    /**
     * 商品下单页面,接受从详情页面传过来的数据
     */
    public function preOrder()
    {
        // 否则展示订单页面
        // var_dump($_POST);
        $good_id = $_POST['goods_id'];
        $this->view = View::make('order')
                ->with('goods', Goods::find($good_id))
                ->with('data',$_POST);
                // ->with('address',Address::where('user_id', $this->user_id)->first());
    }

    /**
     * 商品下单页面及订单处理逻辑
     */
    public function order()
    {
        // 订单页面需要检查用户是否登录
        $this->auth();
        // 如果是POST请求则为确认订单
        if (isPost()) {
            if (isset($_POST['id']) && isset($_POST['amount'])) {
                $data = Goods::find($_POST['id']);
                if ( ! $data) {
                    exit('商品不存在!');
                }
                $order = array(
                        'user_id'     => $_SESSION['user_id'],
                        'goods_id'    => $_POST['id'],
                        'amount'      => $_POST['amount'],
                        'price'       => $data['price'],
                        'order_sn'    => date("Ymd") . (substr(time(), -3)),
                        'address'     => $_POST['address'],
                        'address_name'     => $_POST['name'],
                        'address_mobile'     => $_POST['mobile'],
                        'total_price' => $data['price'] * $_POST['amount']

                );
                $res = Order::create($order);
                if ($res) {
                    echo json(1,$res['id']);
                }
                // var_dump($order);
            }
        }
        // 否则展示订单页面
        // $good_id = $_GET['goods_id'];
        // $this->view = View::make('order')->with('goods', Goods::find($good_id));

    }

    /**
     * 订单支付
     */
    public function pay()
    {
        try {
            // 取得用户 ID
            $user_id = $_SESSION['user_id'];

            // 检查订单是否已支付
            $order = Order::find($_POST['order_id']);
            // if ($order['status'] == 1) throw new \Exception('该订单已支付!');

            // 检查库存是否充足
            $goods = Goods::find($order['goods_id']);
            $amount = $goods['amount'] - $order['amount'];
            if ($amount < 0) throw new \Exception('库存不足!');

            // 先查用户选择的支付方式,目前只有余额支付
            if ($_POST['pay_kind'] != 1) throw new \Exception('您选择的支付方式有误!');

            // 校验用户的支付密码是否正确,必须带上用户名一起查询,防止用户篡改数据
            $res = User::where('username', $_SESSION['username'])->where('password', md5($_POST['password']))->first();
            if (!$res) throw new \Exception('支付密码不正确!');

            // 再查用户余额够不够支付本笔订单
            $finance = Finance::where('user_id',$user_id)->first();
            $price = $finance['balance'] - $order['total_price'];
            if ($price < 0) throw new \Exception('余额不足!');

            // 开启事务 扣用户的钱 减库存 改订单状态

            Order::where('user_id', $user_id)->update(['status' => 1]);
            // Goods::where('id', $order['goods_id'])->update(['amount' => 'amount'-1]);
            Goods::where('id', $order['goods_id'])->update(['amount' => $amount]);
            Finance::where('user_id', $user_id)->update(['balance' => $price]);

            // DB::transaction(function ($balance) {
            //     DB::table('orders')->update(['status' => 1]);
            //     DB::table('goods')->update(['amount' => 10000]);
            //     DB::table('finance')->update(['balance' => $balance]);
            // });

            echo json(1,'支付成功!');
        } catch (\Exception $e) {
            echo json(0,$e->getMessage());
        }

    }

    /**
     * 注册页面
     */
    public function register()
    {
        // 走注册逻辑
        if (isPost()) {
            // var_dump($_POST);
            if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['password2'])) {
                if ($_POST['password'] != $_POST['password2']) {
                    echo json(0, '两次密码输入不一致!');

                    return;
                }

                // 查询是否注册
                $res = User::where('username', $_POST['username'])->first();
                if ($res) {
                    echo json(0, '此用户名已存在!');
                } else {
                    $_POST['password'] = md5($_POST['password']);
                    $res               = User::create($_POST);
                    if ($res) {
                        echo json(1, '恭喜你,注册成功!');
                    } else {
                        echo json(0, '注册不成功,请稍后再试!');
                    }

                }

            }
        }
        // 否则展示相关页面
    }

    /**
     * 登录页
     */
    public function login()
    {
        // 走登录逻辑
        if (isPost()) {
            // 有两种方案可供选择
            // 1:用ajax方法
            // 2:根据来源页面,登录成功再跳转回去
            if (isset($_POST['username']) && isset($_POST['password'])) {
                $res = User::where('username', $_POST['username'])->where('password', md5($_POST['password']))->first();
                if ($res) {
                    $_SESSION['username'] = $res['username'];
                    $_SESSION['user_id']  = $res['id'];
                    echo json(1, '登录成功!');
                } else {
                    echo json(0, '用户或密码不正确!');
                }

            }
            exit;
        }
        // 否则展示相关页面
        // var_dump(isPost());
        $this->view = View::make('login');
    }

    /**
     * 用户退出登录状态
     */
    public function logout()
    {
        // unset($_SESSION['username']);
        session_destroy();
        echo json();
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).