FormController.php
24.4 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?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 = $this->isRegister();
if(empty($isRegister)){
return $this->redirect('user_register_mobile/index');//注册手机号页面
}
//判断用户是否填写问卷调查
$isQuestion = $this->isQuestion();
if(empty($isQuestion)){
return $this->redirect('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',
'recommend' => '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' => '请选择投保人豁免!',
'recommend' => '请选择平台是否推荐!'
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$where['insurance_num'] = ['eq',$param['insurance_num']];
$collocation = Db::name('collocation')->where($where)->find();
if(!empty($collocation)){
$arr['code'] = 40000;
$arr['msg'] = "重复的保险单号";
return json_encode($arr);
}
$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['payment_time']*365*24*60*60,
'user_id' => $user_id,
'image' => !empty($images) ? json_encode($images) : null,
'recommend' => $param['recommend'],
];
//开启事务
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_insurance_type_id_arr'] = explode(',',$param['subjoin_insurance_type_id_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();
$collocationModel = new CollocationModel();
$data = $collocationModel->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();
//清空所有该主险下的附加险
$subjoinInsuranceModel = new SubjoinInsuranceModel();
$subjoinInsuranceModel->deleteData(array('collocation_id'=>$param['id'],'user_id'=>$user_id));
$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' => $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,
'expire_time' => strtotime($param['take_time'])+$param['payment_time']*365*24*60*60,
'image' => !empty($param['imgs']) ? json_encode($param['imgs']) : null,
];
//更新主险
$result1 = Db::name('collocation')->where(array('user_id'=>$user_id,'id'=>$param['id']))->update($array1);
//新增附加险
if(!empty($param['subjoin_product_name_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' => $param['id'],
'create_time' => time(),
];
array_push($array2, $data);
$result2 = Db::name('subjoin_insurance')->insertAll($array2);
}
}
$send = Db::name('send')->where(array('user_id'=>$user_id,'collocation_id'=>$param['id']))->find();
Db::name('send')->where(array('user_id'=>$user_id,'collocation_id'=>$param['id']))->delete();
$data4 = [];
$payment_time = $param['payment_time'];
for($i=0;$i<$payment_time;$i++){
if($array1['expire_time']-($i*365*24*60*60)-(30*24*60*60)>time()){
$data3['user_id'] = $user_id;
$data3['order_id'] = $send['order_id'];
$data3['collocation_id'] = $param['id'];
$data3['time'] = $array1['expire_time']-($i*365*24*60*60)-(30*24*60*60);
$data4[] = $data3;
}else{
$data3['time'] = $param['payment_time'];
$data4[] = $data3;
}
}
$array3 = array('order_expire_time'=>$array1['expire_time'],'order_about_time'=>$data4[count($data4)-1]['time']);
$a = Db::name('order')->where(array('collocation_id'=>$param['id'],'user_id'=>$user_id))->update($array3);
$arr['code'] = 20000;
$arr['msg'] = '更新成功!';
return json_encode($arr);
}
//保单详情页
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{
$data1['status'] = [];
$this->error('参数错误!','');
}
$collocationModel = new CollocationModel();
if($data1['status'] == 2){
//判断是否需要再付费
//支付逻辑:支付一次可以添加20张保单,10个不相同的被保险人
$order_num = Db::name('order_num')->where('user_id',$user_id)->count();
$count_order = $this->count_order();
$insurer = $collocationModel->insurerData(array('user_id'=>$user_id),"insurer");
if(empty($order_num)){
$data1['status'] = 2;
}else{
if($count_order >= $order_num*20 || count($insurer) > $order_num*10){
$data1['status'] = 2;
}else{
//不需付费
$data1['status'] = 3;
}
}
}
$total = Db::name('total')->where('id',1)->find();
$collocation = $collocationModel->findData(array('c.id'=>$param['collocation_id']));
$data1['num'] = cmf_get_order_sn();
$data1['user_id'] = $user_id;
$data1['collocation_id'] = $param['collocation_id'];
$data1['total'] = $total['total'];
$data1['create_time'] = time();
if($data1['status'] == 1 || $data1['status'] == 3){
$data1['order_expire_time'] = $collocation['expire_time'];
$data1['order_about_time'] = $collocation['expire_time']-30*24*60*60;
}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);
}
//添加消息发送
if($data1['status'] == 1 || $data1['status'] == 3) {
$data4 = [];
$payment_time = $collocation['payment_time'];
for($i=0;$i<$payment_time;$i++){
if($collocation['expire_time']-($i*365*24*60*60)-(30*24*60*60)>time()){
$data3['user_id'] = $user_id;
$data3['order_id'] = $result1;
$data3['collocation_id'] = $collocation['id'];
$data3['time'] = $collocation['expire_time']-($i*365*24*60*60)-(30*24*60*60);
$data4[] = $data3;
}else{
$data3['time'] = $collocation['expire_time']-(30*24*60*60);
$data4[] = $data3;
}
}
Db::name('send')->insertAll($data4);
$update['order_expire_time']=$collocation['expire_time'];
$update['order_about_time']=$data4[count($data4)-1]['time'];
$update['order_expire_time2']=time()+(365*24*60*60);
Db::name('order')->where('id',$result1)->update($update);
}
$orderModel->commit();
//推送模板消息
if($data1['status'] == 1 || $data1['status'] == 3){
$templateId = 'rQSM5XqM4cJEua6nsJC4IGuLfIOYvkNOVKVABILc8Ro';
$data2 = array(
'first'=>"尊敬的用户,您的保单即将生效",
'keyword1'=>$collocation['insurance_num'],
'keyword2'=>$collocation['product_name'],
'keyword3'=>$collocation['insurer'],
'keyword4'=>date('Y-m-d',$collocation['take_time'])."至".date('Y-m-d',$collocation['expire_time']),
'remark'=>"立即查看保单详情",
);
$url = url('me_guarantee/guarantee_info',array('id'=>$result2),'',true);
$user1 = Db::name('third_party_user')->where('user_id',$collocation['user_id'])->find();
$user = Db::name('user')->where('id',$user_id)->find();
$this->template($templateId,$data2,$url,$user1['openid']);
//短信通知
$data = array(
'content' => "【橙象保单托管】尊敬的用户您的保单即将生效,保单号为$collocation[insurance_num]。",//短信内容
'mobile' => $user['mobile'],//手机号码
'productid' => '887361',//产品id
'xh' => ''//小号
);
$result = send_sms($data);
if(substr($result,0,strpos($result,',')) != "1"){
$arr['code'] = 40000;
$arr['msg'] = '短信接口出错';
return json_encode($arr);
}
}
$arr['code'] = 20000;
$arr['msg'] = '操作成功!';
$arr['data'] = $result1;//返回主订单id
$arr['status'] = $data1['status'];
return json_encode($arr);
}
//创建续费订单
public function create_order_renew(){
$user_id = cmf_get_current_user_id();
$param = $this->request->param();
//查询当前订单号
$orderInfoModel = new OrderInfoModel();
$order_info = $orderInfoModel->findData2(array('collocation_id'=>$param['id']));
if(empty($order_info)){
$arr['code'] = 40000;
$arr['msg'] = "未查询到订单";
return json_encode($arr);
}
//判断当前订单是否为当收费
$orderModel = new OrderModel();
$order = $orderModel->findData(array('id'=>$order_info['order_id'],'user_id'=>$user_id));
if(empty($order)){
$arr['code'] = 40000;
$arr['msg'] = '未查询到当前订单!';
return json_encode($arr);
}
if($order['status'] == 1){
$arr['code'] = 20001;
$arr['msg'] = '当前订单为免费!';
return json_encode($arr);
}
$total = Db::name('total')->where('id',2)->find();
$data['num'] = cmf_get_order_sn();
$data['user_id'] = $user_id;
$data['total'] = $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'] = 40000;
$arr['msg'] = '创建续费订单失败!';
return json_encode($arr);
}
$arr['code'] = 20000;
$arr['msg'] = '创建续费订单成功';
$arr['data'] = $result;
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;
}
$pageHtmlModel = new PageHtmlModel();
$page_html1 = $pageHtmlModel->findData(array('id'=>5));
$page_html2 = $pageHtmlModel->findData(array('id'=>6));
$this->assign(
array(
'type'=>$type,
'param'=>$param,
'page_html1'=>$page_html1,
'page_html2'=>$page_html2,
)
);
return $this->fetch();
}
//获取一个用户有多少个保单
public function count_order(){
$user_id = cmf_get_current_user_id();
$where['user_id'] = ['eq',$user_id];
$where['status'] = ['eq',3];
$result = Db::name('order')->where($where)->count();
return $result;
}
}