FormController.php
14.9 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/12/25
* Time: 19:57
*/
namespace app\index\controller;
use app\index\model\CollocationModel;
use app\index\model\OrderInfoModel;
use app\index\model\OrderModel;
use app\index\model\OrderRenewModel;
use app\index\model\PayMethodModel;
use app\index\model\InsuranceCompanyModel;
use app\index\model\PageHtmlModel;
use app\index\model\SubjoinInsuranceModel;
use app\index\model\UserModel;
use app\index\server\FormServer;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Validate;
//我要托管
class FormController extends WeChatBaseController
{
//授权
function _initialize()
{
//判断用户是否微信浏览器打开
$this->isWechat();
//判断是否手机端
$this->isMobile();
//微信授权
parent::_initialize();
$this->checkWeChatUserLogin();
//阻止拉黑用户
$this->ban();
}
//进入我要托管页面
public function index(){
$options = [
'app_id' => config('wechat_config.app_id'),
'secret' => config('wechat_config.secret'),
'payment' => config('wechat_config.payment'),
];
$app = new Application($options);
$js = $app->js;
$jssdk = $js->config(['chooseImage', 'uploadImage', 'previewImage'], $debug = false, $beta = false, $json = true);
//判断用户是否注册手机号
$isRegister = json_decode($this->isRegister(),true);
if(!empty($isRegister['code'])){
return $this->fetch('register_mobile/index');//注册手机号页面
}
//判断用户是否填写问卷调查
$isQuestion = json_decode($this->isQuestion(),true);
if(!empty($isQuestion)){
return $this->fetch('question/index');//问卷调查页面
}
$PageHtmlModel = new PageHtmlModel();
$page_html1 = $PageHtmlModel->findData(array('id'=>2));//顶部图片
$page_html2 = $PageHtmlModel->findData(array('id'=>3));//上传说明
$InsuranceCompanyModel = new InsuranceCompanyModel();
$insurance_company = $InsuranceCompanyModel->selectData();//保险公司
$PayMethodModel = new PayMethodModel();
$pay_method = $PayMethodModel->selectData();//缴费方式
$this->assign(
array(
'jssdk'=>$jssdk,
'page_html1'=>$page_html1,
'page_html2'=>$page_html2,
'insurance_company'=>$insurance_company,
'pay_method'=>$pay_method,
)
);
return $this->fetch();
}
//创建保险
public function add_post(){
$user_id = cmf_get_current_user_id();
$param = $this->request->param();
//主险
$validate = new Validate([
'insurance_company_id' => 'require',
'product_name' => 'require',
'insurance_type_id' => 'require',
'insurance_price' => 'require',
// 'payment_time_type' => 'require',
'payment_time' => 'require',
'pay_method_id' => 'require',
'pay_method_price' => 'require',
'guarantee_deadline_time' => 'require',
'insurance_num' => 'require',
'application' => 'require',
'insurer' => 'require',
'relation' => 'require',
'insurer_birthday' => 'require',
'insurer_time' => 'require',
'take_time' => 'require',
'insure_time' => 'require',
'remit' => 'require',
]);
$validate->message([
'insurance_company_id' => '请选择保险公司!',
'product_name' => '产品名称不能为空!',
'insurance_type_id' => '请选择保险类型!',
'insurance_price' => '保险金额不能为空!',
// 'payment_time_type' => '请选择缴费期限类型!',
'payment_time' => '请选择缴费期限!',
'pay_method_id' => '缴费方式不能为空!',
'pay_method_price' => '期缴保费不能为空!',
'guarantee_deadline_time' => '请选择保障期限!',
'insurance_num' => '保险单号不能为空!',
'application' => '投保人不能为空!',
'insurer' => '被保人不能为空!',
'relation' => '与被保人关系!',
'insurer_birthday' => '请选择被保人生日!',
'insurer_time' => '请选择投保日期!',
'take_time' => '请选择合同生效日期!',
'insure_time' => '请选择保险时间!',
'remit' => '请选择投保人豁免!',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$images = [];
if(!empty($param['imgs'])){
$images = explode(',',$param['imgs']);
}
$array1 = [
'insurance_company_id' => $param['insurance_company_id'],
'product_name' => $param['product_name'],
'insurance_type_id' => $param['insurance_type_id'],
'insurance_price' => $param['insurance_price'],
// 'payment_time_type' => $param['payment_time_type'],
// 'payment_time_id' => $param['payment_time_id'] ? $param['payment_time_id'] : null,
'payment_time' => $param['payment_time'] ? $param['payment_time'] : null,
'pay_method_id' => $param['pay_method_id'],
'pay_method_price' => $param['pay_method_price'],
'guarantee_deadline_time' => $param['guarantee_deadline_time'],
'insurance_num' => $param['insurance_num'],
'application' => $param['application'],
'insurer' => $param['insurer'],
'relation' => $param['relation'],
'insurer_birthday' => $param['insurer_birthday'],
'insurer_time' => strtotime($param['insurer_time']),
'take_time' => strtotime($param['take_time']),
'insure_time' => $param['insure_time'],
'remit' => $param['remit'],
'remark' => !empty($param['remark']) ? $param['remark'] : null,
'agent_name' => !empty($param['agent_name']) ? $param['agent_name'] : null,
'agent_phone' => !empty($param['agent_phone']) ? $param['agent_phone'] : null,
'create_time' => time(),
'expire_time' => strtotime($param['take_time'])+$param['guarantee_deadline_time']*365*24*60*60,
'user_id' => $user_id,
'image' => !empty($images) ? json_encode($images) : null,
];
//开启事务
Db::startTrans();
$result1 = Db::name('collocation')->insertGetId($array1);
if(empty($result1)){
Db::rollback();
$arr['code'] = 40000;
$arr['msg'] = "主险添加失败!";
return json_encode($arr);
}
//附加险
if(!empty($param['subjoin_product_name_arr'])){
$param['subjoin_product_name_arr'] = explode(',',$param['subjoin_product_name_arr']);
$param['subjoin_price_arr'] = explode(',',$param['subjoin_price_arr']);
$param['subjoin_time_arr'] = explode(',',$param['subjoin_time_arr']);
$param['payment_time_arr'] = explode(',',$param['payment_time_arr']);
$param['subjoin_pay_method_price_arr'] = explode(',',$param['subjoin_pay_method_price_arr']);
$param['subjoin_guarantee_deadline_time_arr'] = explode(',',$param['subjoin_guarantee_deadline_time_arr']);
$array2 = [];
foreach($param['subjoin_product_name_arr'] as $key => $value){
$data = [
'subjoin_product_name' => $value,
'insurance_type_id' => $param['subjoin_insurance_type_id_arr'][$key] ? $param['subjoin_insurance_type_id_arr'][$key] : null,
'subjoin_price' => $param['subjoin_price_arr'][$key] ? $param['subjoin_price_arr'][$key] : null,
'subjoin_time' => $param['subjoin_time_arr'][$key] ? $param['subjoin_time_arr'][$key] : null,
'payment_time' => $param['payment_time_arr'][$key] ? $param['payment_time_arr'][$key] : null,
'subjoin_pay_method_price' => $param['subjoin_pay_method_price_arr'][$key] ? $param['subjoin_pay_method_price_arr'][$key] : null,
'subjoin_guarantee_deadline_time'=>$param['subjoin_guarantee_deadline_time_arr'][$key] ? $param['subjoin_guarantee_deadline_time_arr'][$key] : null,
'user_id' => $user_id,
'collocation_id' => $result1,
'create_time' => time(),
];
array_push($array2, $data);
}
$result2 = Db::name('subjoin_insurance')->insertAll($array2);
if(empty($result2)){
Db::rollback();
$arr['code'] = 40000;
$arr['msg'] = "附加险添加失败!";
}
}
Db::commit();
$arr['code'] = 20000;
$arr['msg'] = "附加险添加成功!";
$arr['data'] = $result1;
return json_encode($arr);
}
public function edit(){
$options = [
'app_id' => config('wechat_config.app_id'),
'secret' => config('wechat_config.secret'),
'payment' => config('wechat_config.payment'),
];
$app = new Application($options);
$js = $app->js;
$jssdk = $js->config(['chooseImage', 'uploadImage', 'previewImage'], $debug = false, $beta = false, $json = true);
$id = $this->request->param('id');
$user_id = cmf_get_current_user_id();
$orderInfoModel = new CollocationModel();
$data = $orderInfoModel->findData(array('c.id'=>$id,'c.user_id'=>$user_id));
$subjoinInsuranceModel = new SubjoinInsuranceModel();
$subjoin = $subjoinInsuranceModel->selectData2(array('collocation_id'=>$data['id']));
$PageHtmlModel = new PageHtmlModel();
$page_html2 = $PageHtmlModel->findData(array('id'=>3));//上传说明
$InsuranceCompanyModel = new InsuranceCompanyModel();
$insurance_company = $InsuranceCompanyModel->selectData();//保险公司
$PayMethodModel = new PayMethodModel();
$pay_method = $PayMethodModel->selectData();//缴费方式
$this->assign(
array(
'data'=>$data,
'page_html2'=>$page_html2,
'insurance_company'=>$insurance_company,
'pay_method'=>$pay_method,
'jssdk'=>$jssdk,
'subjoin'=>$subjoin,
)
);
return $this->fetch();
}
//更改订单
public function edit_post(){
$user_id = cmf_get_current_user_id();
$param = $this->request->param();
dump($param);
}
//保单详情页
public function info(){
$param = $this->request->param();
$collocationModel = new CollocationModel();
//主险
$data = $collocationModel->findData(array('id'=>$param['id']));
$formServer = new FormServer();
//附加险
$data = $formServer->getSubjoinInsurance($data);
$this->assign(
array(
'data'=>$data,
)
);
}
//创建订单
public function create_order(){
$user_id = cmf_get_current_user_id();
$param = $this->request->param();
if($param['type'] == 1){
//免费
$data1['status'] = 1;
}else if($param['type'] == 2){
//付费
$data1['status'] = 2;
}else{
$this->error('参数错误!','');
}
$total = 10;
$data1['num'] = cmf_get_order_sn();
$data1['user_id'] = $user_id;
$data1['total'] = $total;
$data1['create_time'] = time();
if($data1['status'] == 1){
$data1['order_about_time'] = 1999999999;
}else{
$data1['order_about_time'] = null;
}
$orderModel = new OrderModel();
//开启事务
$orderModel->startTrans();
$result1 = $orderModel->insertData($data1);
if(empty($result1)){
//回滚
$orderModel->rollback();
$arr['code'] = 40000;
$arr['msg'] = '主订单表添加失败!';
return json_encode($arr);
}
$data2['order_id'] = $result1;
$data2['collocation_id'] = $param['collocation_id'];
$data2['create_time'] = time();
$orderInfoModel = new OrderInfoModel();
$result2 = $orderInfoModel->insertData($data2);
if(empty($result2)){
//回滚
$orderModel->rollback();
$arr['code'] = 40000;
$arr['msg'] = '副订单表添加失败!';
return json_encode($arr);
}
$orderModel->commit();
$arr['code'] = 20000;
$arr['msg'] = '操作成功!';
$arr['data'] = $result1;//返回主订单id
return json_encode($arr);
}
//创建续费订单
public function create_order_renew(){
$user_id = cmf_get_current_user_id();
$param = $this->request->param();
//判断当前订单是否为当收费
$orderModel = new OrderModel();
$order = $orderModel->findData(array('id'=>$param['order_id'],'user_id'=>$user_id));
if(empty($order)){
$arr['code'] = 40002;
$arr['msg'] = '我查询到当前订单!';
return json_encode($arr);
}
if($order['status'] == 1){
$arr['code'] = 40003;
$arr['msg'] = '当前订单为免费!';
return json_encode($arr);
}
$total = 10;
$data['num'] = cmf_get_order_sn();
$data['user_id'] = $user_id;
$data['total'] = $total;
$data['status'] = 1;
$data['order_id'] = $order['id'];
$data['create_time'] = time();
$orderRenewModel = new OrderRenewModel();
$result = $orderRenewModel->insertData($data);
if(empty($result)){
$arr['code'] = 40003;
$arr['msg'] = '创建续费订单失败!';
return json_encode($arr);
}
$arr['code'] = 20000;
$arr['msg'] = '创建续费订单成功';
return json_encode($arr);
}
//进入下单页面
public function cost(){
$user_id = cmf_get_current_user_id();
$userModel = new UserModel();
$user = $userModel->findUserData(array('id'=>$user_id));
$param = $this->request->param();
$param['imgs'] = str_replace('*', '/', $param['imgs']);
if($param['insurer'] == $user['name']){
//显示免费
$type = 1;
}else{
//只显示付费
$type = 2;
}
$this->assign(
array(
'type'=>$type,
'param'=>$param,
)
);
return $this->fetch();
}
}