ShopController.php
3.2 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
<?php
/**
* Created by PhpStorm.
* auther: sgj
* Date: 2018/12/17
* Time: 16:18
*/
namespace app\portal\controller;
use app\portal\model\GoodsModel;
use app\user\model\UserModel;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Request;
class ShopController extends WeChatBaseController
{
protected $GoodsModel;
public function __construct(GoodsModel $GoodsModel)
{
parent::__construct();
$this->GoodsModel = $GoodsModel;
}
public function index()
{
$goods=$this->GoodsModel->getList()->toArray();
$this->assign('goods',$goods);
return $this->fetch();
}
public function goodinfo(){
$id=input('id');
$good=$this->GoodsModel->getGoodInfo($id);
$this->assign('good',$good);
// dump($good);
return $this->fetch();
}
/*
* 确认订单
*/
public function confirmOrder(){
if(\request()->isAjax()) {
//消耗的总积分量
if (empty(cmf_get_current_user_id())){
$this->error();
}
$data=input('');
$good=$this->GoodsModel->getGoodInfo($data['id']);
$sum=$data['num']*$good['fee'];
$User=new UserModel();
$user=$User->getMyself(cmf_get_current_user_id())->toArray();
if ($user['score']<$sum){
$this->error('积分不足,下单失败!');
}else{
/*
* 开启事务 处理订单
*/
$insert['order_sn']='JYH-'.uniqid();
$insert['num']=$data['num'];
$insert['good_id']=$good['id'];
$insert['fee']=$sum;
$insert['pay_type']=1;
$insert['delivery_tel']=$data['tel'];
$insert['delivery_name']=$data['name'];
$insert['delivery_address']=$data['address'];
$insert['remark']=$data['content'];
$insert['user_id']=cmf_get_current_user_id();
$insert['add_time']=time();
Db::startTrans();
try{
Db::name('order')->insert($insert);
Db::name('user')->where('id',cmf_get_current_user_id())->setDec('score',$sum);
// 提交事务
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
$this->error();
}
$this->success('下单成功!');
}
}else{
/*jsSDK加入*/
$config=config('wechat_config');
$Wechat=new Application($config);
$js = $Wechat->js;
$api[]='openAddress';
$sdk=$js->config($api, true);
$good_id=input('id');
$num=input('num');
$good=$this->GoodsModel->getGoodInfo($good_id);
$good['num']=$num;
$total_fee=$good['fee']*$num;
/*
* 获取上次的地址
*
*/
$this->assign('good',$good);
$this->assign('sdk',$sdk);
$this->assign('total_fee',$total_fee);
return $this->fetch();
}
}
}