Skip to content

Commit 13f8f00

Browse files
authored
Merge pull request #15 from hanfengmi/master
orders
2 parents 91cff4c + 793d915 commit 13f8f00

File tree

10 files changed

+461
-8
lines changed

10 files changed

+461
-8
lines changed

application/controllers/Admin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public function index()
2121
$product = $this->admin_model->getProductData();
2222
$catgory = $this->admin_model->getCatgoryData();
2323
$user = $this->admin_model->getUserData();
24+
$orders = $this->admin_model->getOrdersData();
2425
$data['products'] = $product;
2526
$data['catgory'] = $catgory;
2627
$data['user'] = $user;
27-
// var_dump($product,$catgory)
28+
$data['orders'] = $orders;
29+
// var_dump($user);
2830
// $this->load->view('home',['products' => $products]);
2931

3032
$this->layout->view('admin',['data' => $data]);

application/controllers/Login.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function Login() {
3333

3434
if($user['status'] == 2){
3535
// 登录成功构造JWT, 加上当前时间戳。
36+
$token['userid'] = $user['data']->userid;
3637
$token['email'] = $user['data']->email;
3738
$token['role'] = $user['data']->role;
3839
$token['time'] = time();

application/controllers/MyOrder.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
require APPPATH . '/libraries/ImplementJwt.php';
4+
class MyOrder extends CI_Controller {
5+
6+
public function __construct(){
7+
parent::__construct();
8+
$this->load->helper('url');
9+
$this->objOfJwt = new ImplementJwt();
10+
}
11+
12+
public function index()
13+
{
14+
if(isset($_COOKIE['auth'])){
15+
$decodeToken = $this->objOfJwt->DecodeToken($_COOKIE['auth']);
16+
$user = $decodeToken;
17+
18+
$this->load->library('layout');
19+
$this->load->model('Orders_model');// 都是获取所有数据接口,可复用
20+
$orders = $this->Orders_model->getOrdersData($user['userid']);
21+
$data['orders'] = $orders;
22+
23+
$this->layout->view('myOrders',['data' => $data]);
24+
} else {
25+
echo '请<a href="login">登录</a> <a href="home">Home</a>';
26+
}
27+
28+
}
29+
}

application/controllers/Orders.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
require APPPATH . '/libraries/ImplementJwt.php';
5+
class Orders extends CI_Controller {
6+
7+
8+
public function __construct(){
9+
parent::__construct();
10+
$this->objOfJwt = new ImplementJwt();
11+
}
12+
13+
public function index()
14+
{
15+
}
16+
17+
public function CreateOrder() {
18+
$response = array('status'=>'0','msg'=>'failed','data'=>'');
19+
$this->load->model('Orders_model');
20+
21+
if(isset($_COOKIE['auth'])){
22+
$decodeToken = $this->objOfJwt->DecodeToken($_COOKIE['auth']);
23+
$user = $decodeToken;
24+
$userid = $user['userid'];
25+
26+
$post = $this->input->post(NULL, TRUE);
27+
$data = array(
28+
'pid' => $post['pid'],
29+
'qty' => $post['qty'],
30+
'userid' => $userid,
31+
'hash' => md5($post['pid'] . ':' . $post['qty'] . ':' . time()),//
32+
'status' => 0
33+
);
34+
$res = $this->Orders_model->create_orders($data);
35+
36+
$response = array('status'=>'2','msg'=>'success','data'=>$res[0]);
37+
echo json_encode($response);
38+
}else {
39+
$response = array('status'=>'0','msg'=>'failed','data'=>'登录过期了,请重新登录');
40+
echo json_encode($response);
41+
}
42+
}
43+
}
44+

application/models/Admin_model.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ public function getUserData(){
2929
return $query->result();
3030

3131
}
32+
33+
public function getOrdersData(){
34+
// return array("Volvo","BMW","SAAB");
35+
$query = $this->db->query('select * from orders;');
36+
return $query->result();
37+
38+
}
3239
}

application/models/Orders_model.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
class Orders_model extends CI_Model {
3+
4+
public function __construct()
5+
{
6+
// $this->load->database(); // 配置文件中已经自动加载
7+
}
8+
9+
10+
public function get_orders_detail($orderid){
11+
try{
12+
$sql = "SELECT * FROM orders WHERE orderid = ?";
13+
// use query bindings to prevent against injection.
14+
$query = $this->db->query($sql, array($orderid));
15+
return $query->result();
16+
}catch(PDOEXCEPTION $e){
17+
echo $e->getMessage();
18+
}
19+
}
20+
21+
public function create_orders($data){
22+
// session_start();
23+
try{
24+
$query = $this->db->insert( 'orders' , $data );
25+
if($query){
26+
$orderid = $this->db->insert_id();
27+
try{
28+
$sql = "SELECT * FROM orders WHERE orderid = ?";
29+
// use query bindings to prevent against injection.
30+
$res= $this->db->query($sql, array($orderid));
31+
return $res->result();
32+
33+
}catch(PDOEXCEPTION $e){
34+
echo $e->getMessage();
35+
}
36+
}
37+
}catch(PDOEXCEPTION $e){
38+
echo $e->getMessage();
39+
}
40+
}
41+
42+
public function getOrdersData($userId){
43+
try{
44+
$sql = "SELECT * FROM orders WHERE userid = ?";
45+
// use query bindings to prevent against injection.
46+
$query = $this->db->query($sql, array($userId));
47+
return $query->result();
48+
}catch(PDOEXCEPTION $e){
49+
echo $e->getMessage();
50+
}
51+
}
52+
53+
54+
}

application/views/admin.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
<div class="content-wrapper">
2-
<!-- <div class="alert alert-success alert-dismissible" style="position:fixed;top:0;right:0;z-index:99" role="alert">
3-
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
4-
<strong>Successful operation !</strong>
5-
</div> -->
62
<div class="container-fluid">
73
<div class="row">
84
<div class="col-12">
@@ -177,6 +173,46 @@
177173

178174
</table>
179175
</div>
176+
<div class="orders-table col-md-6">
177+
<table class="table table-hover">
178+
<thead>
179+
<tr>
180+
<th>orderid</th>
181+
<th>pid</th>
182+
<th>qty</th>
183+
<th>userid</th>
184+
<th>hash</th>
185+
<th>status</th>
186+
</tr>
187+
</thead>
188+
<tbody class="orders-tbody">
189+
<?php
190+
if($data['orders']){
191+
foreach($data['orders'] as $row) {
192+
$orders_orderid = $row->orderid;
193+
$orders_pid = $row->pid;
194+
$orders_qty = $row->qty;
195+
$orders_userid = $row->userid;
196+
$orders_hash = $row->hash;
197+
$orders_status = $row->status;
198+
echo
199+
'
200+
<tr>
201+
<td>' . $orders_orderid . '</td>
202+
<td>' . $orders_pid . '</td>
203+
<td>' . $orders_qty . '</td>
204+
<td>' . $orders_userid . '</td>
205+
<td>' . $orders_hash . '</td>
206+
<td>' . $orders_status . '</td>
207+
</tr>
208+
';
209+
}
210+
}
211+
?>
212+
</tbody>
213+
214+
</table>
215+
</div>
180216
</div>
181217

182218

application/views/header.php

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
defined('BASEPATH') OR exit('No direct script access allowed');
33
?>
4+
<div class="pay-loading">
5+
<div class="loading-spinner"></div>
6+
</div>
47

58
<div class="row page-header" >
69
<h1 class="col-xs-12 col-sm-4 col-md-3 header-title">SALE</h1>
@@ -14,7 +17,9 @@
1417
if($data){
1518
echo
1619
'
17-
<p>' . $data['email'] . '</p>
20+
<p>
21+
<a href="myOrder" class="user-info" data-id=' . $data['userid'] . '>' . $data['email'] . '</a>
22+
</p>
1823
<button type="button" class="logOut btn-info">signOut</button>
1924
';
2025
} else {
@@ -35,11 +40,26 @@
3540
<!-- 渲染 -->
3641
<div class="shopping-car"></div>
3742
<!-- end -->
38-
<button class="btn-info checkout col-sm-6">Checkout</button>
43+
<?php
44+
if($data){
45+
echo
46+
'
47+
<button class="btn-info checkout col-sm-6">Checkout</button>
48+
';
49+
} else {
50+
echo
51+
'
52+
<button class="btn-info signIn-checkout col-sm-6">Checkout</button>
53+
';
54+
}
55+
?>
3956
<p class="col-sm-2">total:$<span class="total-product">0</span></p>
4057
</div>
4158
</div>
4259
</div>
60+
61+
<div class="paypal-form" style="display:none">
62+
</div>
4363
</div>
4464
<script>
4565
var header = {
@@ -48,6 +68,7 @@
4868
this.changeProductNum();
4969
this.logOut();
5070
this.logIn();
71+
this.checkout();
5172
},
5273
getShoppingCarData:function(){
5374
var shopingList = JSON.parse(localStorage.getItem("shopCar")) || [];
@@ -64,7 +85,7 @@
6485
data.forEach(function(item){
6586
total += Number(item.price)*(item.num);
6687
var shopHtml = `<p data-id="${item.pid}">
67-
<a href="http://github.com/item">${item.name}</a>
88+
<a href="http://github.com/item%3Cspan%20class="x x-first x-last">?id=${item.pid}">${item.name}</a>
6889
<input style="min-width:50px" class="product-num" value="${item.num}" type="number" min="1" max="100"></input>
6990
<span class="price"> $${item.price}</span>
7091
</p>`
@@ -74,6 +95,7 @@
7495
},
7596

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

130152
},
153+
154+
checkout(){
155+
var that = this;
156+
$('.header-shopping').on('click', '.signIn-checkout', function(){
157+
alert('暂未登录')
158+
})
159+
160+
$('.header-shopping').on('click', '.checkout', function(){
161+
var shopingList = JSON.parse(localStorage.getItem("shopCar")) || [];
162+
// var userId = $('.header-user-info').find('.user-info').attr('data-id');
163+
var pidArr = [];
164+
var qtyArr = [];
165+
var total = 0;
166+
shopingList.forEach(function(e,i){
167+
pidArr.push(e.pid);
168+
qtyArr.push(e.num);
169+
total += Number(e.price)*(e.num);
170+
})
171+
$.ajax({
172+
type: "post",
173+
data: {
174+
pid: pidArr.join('-'),
175+
qty: qtyArr.join('-')
176+
},
177+
url: "./Orders/CreateOrder",
178+
dataType: 'json',
179+
beforeSend: function() {
180+
$('.pay-loading').show();
181+
// console.log(123123)
182+
},
183+
success: function(data) {
184+
if(data.status == 2){
185+
$('.paypal-form').html(
186+
`
187+
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
188+
<input type="hidden" name="cmd" value="_cart">
189+
<input type="hidden" name="upload" value="1">
190+
<input type="hidden" name="business" value="[email protected]">
191+
192+
<input type="hidden" name="item_name_1" value=${data.data.pid}>
193+
<input type="hidden" name="amount_1" value="${total.toFixed(2)}">
194+
195+
<input class="submit" type="submit" value="PayPal">
196+
<input type="hidden" name="return" value="http://47.98.195.42/php/myOrder">
197+
</form>
198+
`)
199+
200+
setTimeout(function(){
201+
$('.paypal-form').find('.submit').click();
202+
}, 50);
203+
204+
}
205+
206+
},
207+
error: function() {
208+
alert("ajax error");
209+
}
210+
});
211+
})
212+
}
131213

132214
}
133215
header.init();

0 commit comments

Comments
 (0)