Vip.php
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/4/30
* Time: 15:23
*/
namespace app\index\controller;
use app\common\controller\Frontend;
use app\index\controller\WechatPay;
use app\index\model\Firmstores;
use app\index\model\Minestore;
use app\index\model\Price;
use app\index\model\Province;
use app\index\model\Store;
use app\index\model\Viporder;
class Vip extends Frontend
{
protected $noNeedLogin = [''];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
$this->view->assign('is_search', 0);
$this->view->assign('is_active', 0);
}
/**
* vip购买页面
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function index(){
$user_id = $this->auth->id;
$storeModel = new Store();
$store = $storeModel->findData(['user_id'=>$user_id]);
if(!empty($store)){
$this->redirect('index/member/order');
}
// 查询是否有已支付、待审核的订单
$viporder_model = new Viporder();
$viporder = $viporder_model->where(['user_id'=>$user_id,'status'=>2,'audit'=>['in',['1','2']]])->find();
if($viporder) {
return $this->fetch('pay_success');
}
//个人店铺
$minestoreModel = new Minestore();
$minestore = $minestoreModel->findData(['user_id'=>$user_id]);
//企业店铺
$firmstoresModel = new Firmstores();
$firmstores = $firmstoresModel->findData(['user_id'=>$user_id]);
//判断是否提交申请
if(empty($minestore) && empty($firmstores)){
$this->redirect('index/enter/index');
}
//省份
$provinceModel = new Province();
$province = $provinceModel->selectData([]);
//价格
$priceModel = new Price();
$price = $priceModel->findData(['id'=>1]);
$this->assign('province',$province);
$this->assign('price',$price);
$this->assign('title',"入驻缴费");
return $this->fetch();
}
/**
* 创建订单
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function create_order(){
$user_id = $this->auth->id;
$param = $this->request->param();
$validate = new \think\Validate([
'pay_type' => 'require',
'vip_type' => 'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
//价格
$priceModel = new Price();
$price = $priceModel->findData(['id'=>1]);
if($param['vip_type'] == 1){
if(empty($param['province_ids'])){
$this->error('请至少选择一个城市');
}
$number = explode(',',$param['province_ids']);
$money = $price['vipprice'] * count($number);
}else{
$money = $price['svipprice'];
}
$storeModel = new Store();
$store = $storeModel->findData(['user_id'=>$user_id]);
if(!empty($store)){
$this->error('请前往续费~','index/member/order');
}
//个人店铺
$minestoreModel = new Minestore();
$minestore = $minestoreModel->findData(['user_id'=>$user_id]);
//企业店铺
$firmstoresModel = new Firmstores();
$firmstores = $firmstoresModel->findData(['user_id'=>$user_id]);
//判断是否提交申请
if(empty($minestore) && empty($firmstores)){
$this->error('请先提交入驻申请','index/enter/index');
}
$type = !empty($minestore) ? 1 : (!empty($firmstores) ? 2 : 0);
$arr['pay_type'] = $param['pay_type'];
$arr['user_id'] = $user_id;
$arr['type'] = $type;
$arr['vip_type'] = $param['vip_type'];
$arr['province_ids'] = !empty($param['province_ids']) ? $param['province_ids'] : null;
$arr['num'] = get_order_sn();
$arr['money'] = $money;
$arr['status'] = '1';
$arr['audit'] = '1';
$arr['createtime'] = time();
$viporderModel = new Viporder();
$result = $viporderModel->insertData($arr);
if(empty($result)){
$this->error('sql执行失败');
}
$this->success('SUCCESS','',['viporder_id'=>$result,'url'=>url("index/vip/pay",array('viporder_id'=>$result),false,true)]);
}
/**
* 支付接口
*/
public function pay(){
$viporder_id = $this->request->param('viporder_id',0,'intval');
if(empty($viporder_id)){
$this->error('缺少必要参数');
}
$viporderModel = new Viporder();
$data = $viporderModel->findData(['id'=>$viporder_id]);
if(empty($data)){
$this->error('未查询到该订单');
}
if($data['status'] != '1'){
$this->error('订单状态不合法');
}
if($data['pay_type'] == '1'){
//微信支付
$pay = new WechatPay();
$code_url = $pay->index($data);
if(empty($code_url)){
$this->error('生成二维码失败','','','');
}
\PHPQRCode\QRcode::png($code_url);
}else if($data['pay_type'] == '2'){
//支付宝支付
$params['subject'] = "购买商品";
$params['out_trade_no'] = $data['num'];
$params['total_amount'] = $data['money'];
$result = \alipay\Pagepay::pay($params);
echo $result;
}else{
$this->error('未知的支付类型');
}
}
/**
* 检测订单支付状态
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function order_pay(){
$user_id = $this->auth->id;
$order_id = $this->request->param('order_id',0,'intval');
if(empty($order_id)){
$this->error('缺少必要参数');
}
$viporderModel = new Viporder();
$data = $viporderModel->findData(['id'=>$order_id]);
if(empty($data)){
$this->error('查询为空');
}
if($data['status'] != '1'){
$url = url('index/vip/pay_success');
$storeModel = new Store();
$store = $storeModel->findData(['user_id'=>$data['user_id']]);
if($store){
$url = url("index/member/member");
}
$this->success('SUCCESS',$url);
}
$this->error('尚未支付');
}
public function pay_success() {
return $this->view->fetch();
}
}