CreateController.class.php
7.23 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
206
207
208
209
210
211
<?php
/**
* 创建订单
* User: tangxingguo
* Date: 2018/3/5
* Time: 2018年3月5日10:27:31
*/
namespace Apicp\Controller\Order;
use Com\PackageValidate;
use Common\Common\Cache;
use Common\Common\Order;
use VcySDK\Member;
use VcySDK\Service;
use VcySDK\Enterprise;
class CreateController extends AbstractController
{
/**
* 商品类型:续费
*/
const PRODUCT_TYPE_RENEW = 1;
/**
* 商品类型:空间容量
*/
const PRODUCT_TYPE_DISK = 2;
/**
* 商品类型:流量
*/
const PRODUCT_TYPE_NETWORK = 3;
/**
* 商品类型:扩充人员
*/
const PRODUCT_TYPE_USER = 4;
/**
* Create
* @author tangxingguo
* @desc 创建订单接口
* @param Int product_type:true 商品类型(1=续费;2=空间容量;3=流量;4=扩充人数);当空间容量与流量同时购买的情况,product_type传2 或 3 均可
* @param Int user_num 人数
* @param Int years 年数
* @param Int disk 空间
* @param Int network 流量
* @param Int remaind_day 剩余天数(扩充人数情况下必须)
* @param Int price:true 价格
* @return array|bool 商品信息
array(
'order_id' => 123, // 订单ID
'order_desc' => '扩充人数', // 订单内容
'total_price' => 24518, // 订单总金额(单位:分)
'order_sn' => '20180117170840644', // 订单编号
'order_type' => 4, // 订单类型(1=购买「业绩比拼」应用,2=扩充「业绩比拼」应用使用人数,3=购买「员圈直播」并发人数,4=购买「员圈」应用,5=扩充「员圈」应用使用人数,6=购买空间/流量扩展包)
'created' => 1517486261770, // 创建时间
'buy_content' => array( // 购买内容
'user_num' => 100, // 购买人数(续费、扩充人数时值有效;续费时返回总人数,扩充人数时返回扩充的人数)
'years' => 1, // 购买年数(续费时值有效)
'disk' => 1, // 购买的空间容量(购买空间时值有效)
'network' => 1, // 购买的流量(购买流量时值有效)
),
)
*/
public function Index_post()
{
// 请求数据
$postData = I('post.');
// 验证规则
$rules = [
'product_type' => 'require|integer',
'price' => 'require|integer',
'user_num' => 'integer',
'years' => 'integer|in:1,2,3',
'disk' => 'integer',
'network' => 'integer',
'remaind_day' => 'integer',
];
// 验证请求数据
$validate = new PackageValidate();
$validate->postData = $postData;
$validate->validateParams($rules);
// 格式化请求参数
$params= $this->_formatPostData($postData);
// 提交订单
$orderServ = &Order::instance();
$orderDetail = $orderServ->addOrder($params);
// 购买内容
$buyContent = json_decode($orderDetail['ordContent'], true);
// 返回参数
$this->_result = [
'order_id' => $orderDetail['ordId'],
'order_desc' => $orderDetail['ordTitle'],
'total_price' => $orderDetail['ordTotalPrice'],
'order_sn' => $orderDetail['ordCode'],
'order_type' => $orderDetail['ordType'],
'created' => $orderDetail['ordCreated'],
'buy_content' => [
'user_num' => isset($buyContent['personAmount']) ? $buyContent['personAmount'] : 0,
'years' => isset($buyContent['year']) ? $buyContent['year'] : 0,
'disk' => isset($buyContent['disk']) ? $buyContent['disk'] : 0,
'network' => isset($buyContent['network']) ? $buyContent['network'] : 0,
],
];
}
/**
* 格式化请求参数
* @author tangxingguo
* @param array $postData 请求参数
* @return array
*/
private function _formatPostData($postData)
{
// 参数初始化
$params = [
'ordTotalPrice' => $postData['price'],
'eaId' => $this->_login->user['eaId'],
];
if (!empty($this->_login->user['eaMobile'])) {
$params['eaMobile'] = $this->_login->user['eaMobile'];
}
if (!empty($this->_login->user['eaRealname'])) {
$params['eaRealname'] = $this->_login->user['eaRealname'];
}
// 企业信息
$sdk = new Enterprise(Service::instance());
$epDetail = $sdk->detail();
// 实时人数信息
$memberSdk = new Member(Service::instance());
$memberStatus = $memberSdk->memberStatusCount([]);
// 企业内的人数,evUsedUserTotal是昨天的使用人数,更新为实时人数
$epDetail['evUsedUserTotal'] = $memberStatus['amount'];
// 参数校验
switch ($postData['product_type']) {
// 开通、续费员圈应用
case self::PRODUCT_TYPE_RENEW:
if (!isset($postData['user_num'], $postData['years'])) {
E('_ERR_ORDER_PARAMS_MISS');
}
// 人数检查,购买的人数 需 大于当前使用的人数
$userTotal = $postData['user_num'];
if ($userTotal < $epDetail['evUsedUserTotal']) {
E('_ERR_ORDER_USER_NUM_FAIL');
}
$params['prCostKey'] = 'yq_app_rule';
$params['year'] = $postData['years'];
$params['num'] = $postData['user_num'];
break;
// 扩充人数
case self::PRODUCT_TYPE_USER:
if (!isset($postData['user_num']) || !isset($postData['remaind_day'])) {
E('_ERR_ORDER_PARAMS_MISS');
}
// 人数检查,总付费人数 需 大于当前使用的人数
if ($epDetail['evUsedUserTotal'] > $epDetail['evAvailableUserTotal'] + $postData['user_num']) {
E('_ERR_ORDER_USER_NUM_FAIL');
}
$params['prCostKey'] = 'yq_persons_rule';
$params['num'] = $postData['user_num'];
$params['remaindDay'] = $postData['remaind_day'];
break;
// 空间容量
case self::PRODUCT_TYPE_DISK:
// 流量
case self::PRODUCT_TYPE_NETWORK:
if (!isset($postData['network']) && !isset($postData['disk'])) {
E('_ERR_ORDER_PARAMS_MISS');
}
// 值校验,以100G位单位出售
if (isset($postData['network'])) {
if ($postData['network'] % 100 > 0) {
E('_ERR_ORDER_NETWORK_FAIL');
}
$params['network'] = $postData['network'];
}
if (isset($postData['disk'])) {
$params['disk'] = $postData['disk'];
}
$params['prCostKey'] = 'space_flow_rule';
break;
}
return $params;
}
}