FormController.php 14.9 KB
<?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();
    }
}