Skip to content

orders #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion application/controllers/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public function index()
$product = $this->admin_model->getProductData();
$catgory = $this->admin_model->getCatgoryData();
$user = $this->admin_model->getUserData();
$orders = $this->admin_model->getOrdersData();
$data['products'] = $product;
$data['catgory'] = $catgory;
$data['user'] = $user;
// var_dump($product,$catgory)
$data['orders'] = $orders;
// var_dump($user);
// $this->load->view('home',['products' => $products]);

$this->layout->view('admin',['data' => $data]);
Expand Down
1 change: 1 addition & 0 deletions application/controllers/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function Login() {

if($user['status'] == 2){
// 登录成功构造JWT, 加上当前时间戳。
$token['userid'] = $user['data']->userid;
$token['email'] = $user['data']->email;
$token['role'] = $user['data']->role;
$token['time'] = time();
Expand Down
29 changes: 29 additions & 0 deletions application/controllers/MyOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/ImplementJwt.php';
class MyOrder extends CI_Controller {

public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->objOfJwt = new ImplementJwt();
}

public function index()
{
if(isset($_COOKIE['auth'])){
$decodeToken = $this->objOfJwt->DecodeToken($_COOKIE['auth']);
$user = $decodeToken;

$this->load->library('layout');
$this->load->model('Orders_model');// 都是获取所有数据接口,可复用
$orders = $this->Orders_model->getOrdersData($user['userid']);
$data['orders'] = $orders;

$this->layout->view('myOrders',['data' => $data]);
} else {
echo '请<a href="login">登录</a> <a href="home">Home</a>';
}

}
}
44 changes: 44 additions & 0 deletions application/controllers/Orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH . '/libraries/ImplementJwt.php';
class Orders extends CI_Controller {


public function __construct(){
parent::__construct();
$this->objOfJwt = new ImplementJwt();
}

public function index()
{
}

public function CreateOrder() {
$response = array('status'=>'0','msg'=>'failed','data'=>'');
$this->load->model('Orders_model');

if(isset($_COOKIE['auth'])){
$decodeToken = $this->objOfJwt->DecodeToken($_COOKIE['auth']);
$user = $decodeToken;
$userid = $user['userid'];

$post = $this->input->post(NULL, TRUE);
$data = array(
'pid' => $post['pid'],
'qty' => $post['qty'],
'userid' => $userid,
'hash' => md5($post['pid'] . ':' . $post['qty'] . ':' . time()),//
'status' => 0
);
$res = $this->Orders_model->create_orders($data);

$response = array('status'=>'2','msg'=>'success','data'=>$res[0]);
echo json_encode($response);
}else {
$response = array('status'=>'0','msg'=>'failed','data'=>'登录过期了,请重新登录');
echo json_encode($response);
}
}
}

7 changes: 7 additions & 0 deletions application/models/Admin_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public function getUserData(){
return $query->result();

}

public function getOrdersData(){
// return array("Volvo","BMW","SAAB");
$query = $this->db->query('select * from orders;');
return $query->result();

}
}
54 changes: 54 additions & 0 deletions application/models/Orders_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
class Orders_model extends CI_Model {

public function __construct()
{
// $this->load->database(); // 配置文件中已经自动加载
}


public function get_orders_detail($orderid){
try{
$sql = "SELECT * FROM orders WHERE orderid = ?";
// use query bindings to prevent against injection.
$query = $this->db->query($sql, array($orderid));
return $query->result();
}catch(PDOEXCEPTION $e){
echo $e->getMessage();
}
}

public function create_orders($data){
// session_start();
try{
$query = $this->db->insert( 'orders' , $data );
if($query){
$orderid = $this->db->insert_id();
try{
$sql = "SELECT * FROM orders WHERE orderid = ?";
// use query bindings to prevent against injection.
$res= $this->db->query($sql, array($orderid));
return $res->result();

}catch(PDOEXCEPTION $e){
echo $e->getMessage();
}
}
}catch(PDOEXCEPTION $e){
echo $e->getMessage();
}
}

public function getOrdersData($userId){
try{
$sql = "SELECT * FROM orders WHERE userid = ?";
// use query bindings to prevent against injection.
$query = $this->db->query($sql, array($userId));
return $query->result();
}catch(PDOEXCEPTION $e){
echo $e->getMessage();
}
}


}
44 changes: 40 additions & 4 deletions application/views/admin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<div class="content-wrapper">
<!-- <div class="alert alert-success alert-dismissible" style="position:fixed;top:0;right:0;z-index:99" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Successful operation !</strong>
</div> -->
<div class="container-fluid">
<div class="row">
<div class="col-12">
Expand Down Expand Up @@ -177,6 +173,46 @@

</table>
</div>
<div class="orders-table col-md-6">
<table class="table table-hover">
<thead>
<tr>
<th>orderid</th>
<th>pid</th>
<th>qty</th>
<th>userid</th>
<th>hash</th>
<th>status</th>
</tr>
</thead>
<tbody class="orders-tbody">
<?php
if($data['orders']){
foreach($data['orders'] as $row) {
$orders_orderid = $row->orderid;
$orders_pid = $row->pid;
$orders_qty = $row->qty;
$orders_userid = $row->userid;
$orders_hash = $row->hash;
$orders_status = $row->status;
echo
'
<tr>
<td>' . $orders_orderid . '</td>
<td>' . $orders_pid . '</td>
<td>' . $orders_qty . '</td>
<td>' . $orders_userid . '</td>
<td>' . $orders_hash . '</td>
<td>' . $orders_status . '</td>
</tr>
';
}
}
?>
</tbody>

</table>
</div>
</div>


Expand Down
88 changes: 85 additions & 3 deletions application/views/header.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="pay-loading">
<div class="loading-spinner"></div>
</div>

<div class="row page-header" >
<h1 class="col-xs-12 col-sm-4 col-md-3 header-title">SALE</h1>
Expand All @@ -14,7 +17,9 @@
if($data){
echo
'
<p>' . $data['email'] . '</p>
<p>
<a href="myOrder" class="user-info" data-id=' . $data['userid'] . '>' . $data['email'] . '</a>
</p>
<button type="button" class="logOut btn-info">signOut</button>
';
} else {
Expand All @@ -35,11 +40,26 @@
<!-- 渲染 -->
<div class="shopping-car"></div>
<!-- end -->
<button class="btn-info checkout col-sm-6">Checkout</button>
<?php
if($data){
echo
'
<button class="btn-info checkout col-sm-6">Checkout</button>
';
} else {
echo
'
<button class="btn-info signIn-checkout col-sm-6">Checkout</button>
';
}
?>
<p class="col-sm-2">total:$<span class="total-product">0</span></p>
</div>
</div>
</div>

<div class="paypal-form" style="display:none">
</div>
</div>
<script>
var header = {
Expand All @@ -48,6 +68,7 @@
this.changeProductNum();
this.logOut();
this.logIn();
this.checkout();
},
getShoppingCarData:function(){
var shopingList = JSON.parse(localStorage.getItem("shopCar")) || [];
Expand All @@ -64,7 +85,7 @@
data.forEach(function(item){
total += Number(item.price)*(item.num);
var shopHtml = `<p data-id="${item.pid}">
<a href="item">${item.name}</a>
<a href="item?id=${item.pid}">${item.name}</a>
<input style="min-width:50px" class="product-num" value="${item.num}" type="number" min="1" max="100"></input>
<span class="price"> $${item.price}</span>
</p>`
Expand All @@ -74,6 +95,7 @@
},

changeProductNum(){//改变写到locastorage中
var that = this;
$('.shopping-car').on('input','.product-num',function(){
var id = $(this).parent().attr('data-id');
var num = $(this).val() || 0;
Expand Down Expand Up @@ -128,6 +150,66 @@ function findProd(prod) {
})

},

checkout(){
var that = this;
$('.header-shopping').on('click', '.signIn-checkout', function(){
alert('暂未登录')
})

$('.header-shopping').on('click', '.checkout', function(){
var shopingList = JSON.parse(localStorage.getItem("shopCar")) || [];
// var userId = $('.header-user-info').find('.user-info').attr('data-id');
var pidArr = [];
var qtyArr = [];
var total = 0;
shopingList.forEach(function(e,i){
pidArr.push(e.pid);
qtyArr.push(e.num);
total += Number(e.price)*(e.num);
})
$.ajax({
type: "post",
data: {
pid: pidArr.join('-'),
qty: qtyArr.join('-')
},
url: "./Orders/CreateOrder",
dataType: 'json',
beforeSend: function() {
$('.pay-loading').show();
// console.log(123123)
},
success: function(data) {
if(data.status == 2){
$('.paypal-form').html(
`
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="[email protected]">

<input type="hidden" name="item_name_1" value=${data.data.pid}>
<input type="hidden" name="amount_1" value="${total.toFixed(2)}">

<input class="submit" type="submit" value="PayPal">
<input type="hidden" name="return" value="http://47.98.195.42/php/myOrder">
</form>
`)

setTimeout(function(){
$('.paypal-form').find('.submit').click();
}, 50);

}

},
error: function() {
alert("ajax error");
}
});
})
}

}
header.init();
Expand Down
Loading