<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop;

use app\admin\controller\bbfxshop\Base;
use think\Db;
use think\Cache;

class Config extends Base
{
    public function _initialize(){
        parent::_initialize();
    }
    
    public function getConfig(){
        Cache::rm("bbfx_set_".UNIACID);
        $base = \app\common\model\bbfxshop\Common::getConfig(true);
        
        $uniacid = UNIACID;
        $h5_url = request()->domain()."/mobile/#/pages/index/index?i=".$uniacid;
        $result = \app\common\model\QRcode::create($h5_url,"h5_url_".$uniacid);
        $h5_url_qrcode = "";
        if($result['code'] == 1){
            $h5_url_qrcode = cdnurl($result['data']['url'])."?t=".time();
        }
        $base['h5_url'] = $h5_url;
        $base['h5_url_qrcode'] = $h5_url_qrcode;

        $merchant_h5_url = request()->domain()."/merchant/h5";
        $result = \app\common\model\QRcode::create($merchant_h5_url,"merchant_h5_url_".$uniacid);
        $merchant_h5_url_qrcode = "";
        if($result['code'] == 1){
            $merchant_h5_url_qrcode = cdnurl($result['data']['url'])."?t=".time();
        }
        $base['merchant_h5_url'] = $merchant_h5_url;
        $base['merchant_h5_url_qrcode'] = $merchant_h5_url_qrcode;

        
        $base['admin_id'] = $this->auth->id;
        $base['admin_order_notify'] = Db::name("admin")->where(['id'=>$this->auth->id])->value("order_notify");
        unset($base['pay_plugin']);
        $wxapp_url_qrcode = "";
        $result = \app\common\model\bbfxshop\Weixin::getUnlimitedQRCode("pages/index/index","wxapp_url_".$uniacid);
        if($result['code'] == 1){
            $wxapp_url_qrcode = cdnurl($result['data']['url'])."?t=".time();
        }
        $base['wxapp_url'] = "pages/index/index";
        $base['wxapp_url_qrcode'] = $wxapp_url_qrcode;
        $base['order_express_tpl'] = request()->domain()."/tpl/订单物流单号导入模板.xlsx";
        $base['user_tpl'] = request()->domain()."/tpl/会员导入模板.xlsx";
        $base['version'] = Db::name("bbfx")->order("id asc")->value("version_name");
        if($base['chat']['status'] == 1){
            // $base['chat']['start_command1'] = "php server.php start";
            // $base['chat']['start_command2'] = "sudo /www/server/php/72/bin/php server.php start";
            // $base['chat']['stop_command1'] = "php server.php stop";
            // $base['chat']['stop_command2'] = "sudo /www/server/php/72/bin/php server.php stop";
        }
        $base['admin_username'] = $this->auth->username;
        $base['is_show_unpaid_plugin'] = 0;
        if($this->auth->id == 1){
            $base['is_show_unpaid_plugin'] = 1;
        }

        $strikeout_rewards = \app\common\model\bbfxshop\FenxiaoTongji::getRewards();

        $base['strikeout_rewards'] = $strikeout_rewards;

        if($base['contribute_vadded']['status'] == 1){
            $contribute_vadded_res = \app\common\model\bbfxshop\ContributeVadded::latestData();
            $base['contribute_vadded']['current_contribute'] = $contribute_vadded_res['current_contribute'];
            $base['contribute_vadded']['user_contribute_total'] = $contribute_vadded_res['user_contribute_total'];
            $base['contribute_vadded']['current_price'] = $contribute_vadded_res['current_price'];
        }

        // app下载二维码
        $android_download_qrcode = "";
        if(!empty($base['data']['entry']['android_download_url'])){
            $result = \app\common\model\QRcode::create($base['data']['entry']['android_download_url'],"android_download_".$uniacid);
            if($result['code'] == 1){
                $android_download_qrcode = cdnurl($result['data']['url'])."?t=".time();
            }
        }
        $base['android_download_qrcode'] = $android_download_qrcode;
        $ios_download_qrcode = "";
        if(!empty($base['data']['entry']['ios_download_url'])){
            $result = \app\common\model\QRcode::create($base['data']['entry']['ios_download_url'],"ios_download_".$uniacid);
            if($result['code'] == 1){
                $ios_download_qrcode = cdnurl($result['data']['url'])."?t=".time();
            }
        }
        $base['ios_download_qrcode'] = $ios_download_qrcode;
        
        if(!empty($base['sreturn'])){
            $base['sreturn']['process_running_directory'] = ROOT_PATH;
            $base['sreturn']['process_running_command'] = "/www/server/php/72/bin/php ".ROOT_PATH."think queue:work --queue=default --sleep=3 --daemon --memory=512";
        }

        __success("操作成功",$base);
    }

    public function setOrderNotify(){
        $order_notify = Db::name("admin")->where(['id'=>$this->auth->id])->value("order_notify");
        if($order_notify == 1){
            $order_notify = 0;
        }
        else{
            $order_notify = 1;
        }

        Db::name("admin")->where("id",$this->auth->id)->update(["order_notify"=>$order_notify]);
        
        __success("操作成功");
    }

    public function pluginCollect(){
        $plugin = $this->param("plugin/s");
        if(empty($plugin)){
            __error("请选址插件应用");
        }
        $plugin_collect = Db::name("bbfx_base")->where(['uniacid'=>UNIACID])->value("plugin_collect");
        if(!empty($plugin_collect)){
            $plugin_collect = explode(",",$plugin_collect);
        }
        else{
            $plugin_collect = [];
        }

        if(!in_array($plugin,$plugin_collect)){
            $plugin_collect[] = $plugin;
        }
        else{
            $plugin_collect = array_diff($plugin_collect, [$plugin]);
        }
        
        Db::name("bbfx_base")->where(['uniacid'=>UNIACID])->update(["plugin_collect"=>implode(",",$plugin_collect)]);

        __success("操作成功",$plugin_collect);
    }

    public function clearQrcode(){
        
        try {
            $path = ROOT_PATH."public/data/poster_wxapp";
            if(is_dir($path)){
                deleteDir($path);
                mkdir($path, 0777, true); 
            }
            $path = ROOT_PATH."public/data/qr";
            if(is_dir($path)){
                deleteDir($path);
                mkdir($path, 0777, true); 
            }
        } catch (\Exception $e) {
            __error($e->getMessage());
        }

        __success("操作成功");
    }
    
    protected function upload_cert($fileinput) {
        global $_W;
        $path = ROOT_PATH . "cert";
        mkdir($path);
        $f = $fileinput . '_' . UNIACID . '.pem';
        $outfilename = $path . "/" . $f;
        $filename = $_FILES[$fileinput]['name'];
        $tmp_name = $_FILES[$fileinput]['tmp_name'];
        if (!empty($filename) && !empty($tmp_name)) {
            $ext = strtolower(substr($filename, strrpos($filename, '.')));
            if ($ext != '.pem') {
                $errinput = "";
                if ($fileinput == 'cert') {
                    $errinput = "CERT文件格式错误";
                } 
                else if ($fileinput == 'cert_key') {
                    $errinput = 'KEY文件格式错误';
                }
                __error(0, $errinput . ',请重新上传!');
            }
            return file_get_contents($tmp_name);
        }
        return "";
    }

    public function backendNoticeSubmit(){
        $params = $this->param(['backend_notice/a']);
        $backend_notice = $params['backend_notice'];
        if(empty($backend_notice)){
            __error("请输入后台公告");
        }
        $backend_notice = iserializer($backend_notice);
        Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->update(["backend_notice"=>$backend_notice]);
        __success("操作成功");
    }
    
    public function hidePluginSubmit(){
        $params = $this->param();
        
        
      	$base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
      	$data = array(
        	"hide_close_plugin"=>intval($params['hide_close_plugin'])
        );
        
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
      	__success("编辑成功");
    }
    
    public function paySubmit(){
        $params = $this->param();
        
        
      	$base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        $recharge = $params['recharge'];
        $recharge['zfb_skqrcode'] = replaceSiteroot($recharge['zfb_skqrcode']);
        $recharge['wx_skqrcode'] = replaceSiteroot($recharge['wx_skqrcode']);
        $recharge['xn_skqrcode'] = replaceSiteroot($recharge['xn_skqrcode']);
        $recharge['szr_skqrcode'] = replaceSiteroot($recharge['szr_skqrcode']);
        $recharge['ysf_skqrcode'] = replaceSiteroot($recharge['ysf_skqrcode']);
        $recharge['cb_pay_skqrcode'] = replaceSiteroot($recharge['cb_pay_skqrcode']);
        $recharge['jd_pay_skqrcode'] = replaceSiteroot($recharge['jd_pay_skqrcode']);
        $recharge['eb_pay_skqrcode'] = replaceSiteroot($recharge['eb_pay_skqrcode']);
        $recharge['ddb_pay_skqrcode'] = replaceSiteroot($recharge['ddb_pay_skqrcode']);
        $recharge['min_pay_skqrcode'] = replaceSiteroot($recharge['min_pay_skqrcode']);

      	$data = array(
        	"is_wxpay"=>intval($params['is_wxpay']),
        	"is_alipay"=>intval($params['is_alipay']),
        	"is_balancepay"=>intval($params['is_balancepay']),
        	"is_cashpay"=>intval($params['is_cashpay']),
        	"is_scorepay"=>intval($params['is_scorepay']),
        	"is_repurchase_currency_deduct"=>intval($params['is_repurchase_currency_deduct']),
        	// "is_recharge"=>intval($params['is_recharge']),
        	"is_pay_password"=>intval($params['is_pay_password']),
        	// "recharge_ratio"=>intval($params['recharge_ratio']),
        	"is_merch_transfer_new"=>intval($params['is_merch_transfer_new']),
        	"merch_transfer_scene"=>trim($params['merch_transfer_scene']),
        	"pay_tip"=>htmlspecialchars_decode($this->param('pay_tip/h')),
        	"pay_call_text"=>trim($params['pay_call_text']),
        	// "recharge"=>iserializer($recharge),
        );
        
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
        
      	__success("编辑成功");
    }

    public function rechargebalanceSubmit(){
        $params = $this->param();
        $recharge = $params['recharge'];

        $recharge['zfb_skqrcode'] = replaceSiteroot($recharge['zfb_skqrcode']);
        $recharge['wx_skqrcode'] = replaceSiteroot($recharge['wx_skqrcode']);
        $recharge['xn_skqrcode'] = replaceSiteroot($recharge['xn_skqrcode']);
        $recharge['szr_skqrcode'] = replaceSiteroot($recharge['szr_skqrcode']);
        $recharge['ysf_skqrcode'] = replaceSiteroot($recharge['ysf_skqrcode']);
        $recharge['cb_pay_skqrcode'] = replaceSiteroot($recharge['cb_pay_skqrcode']);
        $recharge['jd_pay_skqrcode'] = replaceSiteroot($recharge['jd_pay_skqrcode']);
        $recharge['eb_pay_skqrcode'] = replaceSiteroot($recharge['eb_pay_skqrcode']);
        $recharge['ddb_pay_skqrcode'] = replaceSiteroot($recharge['ddb_pay_skqrcode']);
        $recharge['min_pay_skqrcode'] = replaceSiteroot($recharge['min_pay_skqrcode']);
        $recharge['other_pay_skqrcode'] = replaceSiteroot($recharge['other_pay_skqrcode']);

        $data = array(
            "is_recharge_balance"=>intval($params['is_recharge_balance']),
        	"recharge_ratio"=>max(0,intval($params['recharge_ratio'])),
        	"recharge"=>iserializer($recharge),
        );
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
        
      	__success("编辑成功");
    }

    
    public function rechargescoreSubmit(){
        $params = $this->param();
        $recharge = $params['recharge_score'];

        $recharge['zfb_skqrcode'] = replaceSiteroot($recharge['zfb_skqrcode']);
        $recharge['wx_skqrcode'] = replaceSiteroot($recharge['wx_skqrcode']);
        $recharge['xn_skqrcode'] = replaceSiteroot($recharge['xn_skqrcode']);
        $recharge['szr_skqrcode'] = replaceSiteroot($recharge['szr_skqrcode']);
        $recharge['ysf_skqrcode'] = replaceSiteroot($recharge['ysf_skqrcode']);
        $recharge['cb_pay_skqrcode'] = replaceSiteroot($recharge['cb_pay_skqrcode']);
        $recharge['jd_pay_skqrcode'] = replaceSiteroot($recharge['jd_pay_skqrcode']);
        $recharge['eb_pay_skqrcode'] = replaceSiteroot($recharge['eb_pay_skqrcode']);
        $recharge['ddb_pay_skqrcode'] = replaceSiteroot($recharge['ddb_pay_skqrcode']);
        $recharge['min_pay_skqrcode'] = replaceSiteroot($recharge['min_pay_skqrcode']);
        $recharge['other_pay_skqrcode'] = replaceSiteroot($recharge['other_pay_skqrcode']);

        $data = array(
            "is_recharge"=>intval($params['is_recharge']),
        	"recharge_score_ratio"=>max(0,intval($params['recharge_score_ratio'])),
        	"recharge_score"=>iserializer($recharge),
        );
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
        
      	__success("编辑成功");
    }
    
    public function printerSubmit(){
        $printer = $this->param(['status/d','printer_id/d','printer_type/d','show_delivery/d','show_intact_mobile/d','note/s','qrcode/s']);
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        
        $data = array("printer"=>iserializer($printer));
        if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
      	__success("编辑成功");
    }
    
    // 数据概览显示设置
    public function tongjiSubmit(){
        global $_W;
        $params = $this->param(['show_module/a']);
        $tongji = $_W['base']['data']['tongji'];
        $res = \app\common\model\bbfxshop\Common::setSet('tongji',$params);
        
        if($res){
            
        }
        
        \app\admin\model\AdminLog::record("","",$params,"bbfx_base");
        
        __success("编辑成功");
    }
     
    // 电子面单API设置
    public function electronicWaybillSubmit(){
        global $_W;
        $params = $this->param(['status/d','AppCode/s','fahuo_username/s','fahuo_mobile/s','fahuo_address/s','fahuo_region/a','express_id/d','TemplateSize/s','api_params/a']);
        if($params['status'] == 1){
            if(empty($params['AppCode'])){
                __error("请输入阿里云电子面单AppCode");
            }
            if(empty($params['fahuo_username'])){
                __error("请输入寄件人姓名");
            }
            if(empty($params['fahuo_mobile'])){
                __error("请输入寄件人手机号");
            }
            if(empty($params['fahuo_region'][2])){
                __error("请选择发货区域");
            }
            if(empty($params['fahuo_address'])){
                __error("请输入寄件人地址");
            }
            if(empty($params['express_id'])){
                __error("请选择快递公司");
            }
        }
        if(!empty($params['fahuo_region'][2])){
            $params['fahuo_province'] = $params['fahuo_region'][0];
            $params['fahuo_city'] = $params['fahuo_region'][1];
            $params['fahuo_area'] = $params['fahuo_region'][2];
        }
        $res = \app\common\model\bbfxshop\Common::setSet('electronic_waybill',$params);
        
        if($res){
            
        }
        
        \app\admin\model\AdminLog::record("","",$params,"bbfx_base");
        
        __success("编辑成功");
    }
    
    // 阿里云OSS配置
    public function ossSubmit(){
        global $_W;
        $params = $this->param(['status/d','type/d','AccessKeyID/s','AccessKeySecret/s','Endpoint/s','BucketName/s','qiniu_AccessKey/s','qiniu_SecretKey/s','qiniu_BucketName/s','qiniu_Endpoint/s','qiniu_domain_xy/s']);
        $aliyun_oss = $_W['base']['data']['aliyun_oss'];
        $res = \app\common\model\bbfxshop\Common::setSet('aliyun_oss',$params);
        
        if($res){
            if(!empty($params['BucketName']) && $params['status'] == 1){
                /*
                $is_createBucket = Cache::get($params['BucketName']."_".UNIACID);
                
                if(empty($is_createBucket)){
                    $result = \app\common\model\bbfxshop\AliyunOss::createBucket();
                    print_r($result);die;
                    if($result['code'] != 1){
                        __error($result['message']);
                    }
                    Cache::set($params['BucketName']."_".UNIACID,1);
                }
                */
            }
        }

        if($params['status'] == 1 && $params['type'] == 1){
            if($params['qiniu_domain_xy'] == 'http://'){
                $domain1 = 'http://'.$params['qiniu_Endpoint'];
                $domain2 = 'https://'.$params['qiniu_Endpoint'];
            }
            else{
                $domain1 = 'https://'.$params['qiniu_Endpoint'];
                $domain2 = 'http://'.$params['qiniu_Endpoint'];
            }
            $list = Db::name("attachment")->where(['url'=>['like',$domain2.'%']])->field("id,url")->select();
            if(!empty($list)){
                foreach($list as $val){
                    $url = str_replace($domain2,$domain1,$val['url']);
                    Db::name("attachment")->where(['id'=>$val['id']])->update(['url'=>$url]);
                }
            }
        }
        
        \app\admin\model\AdminLog::record("","",$params,"bbfx_base");

        // 更新附件列表缓存
        $attachments = Db::name("attachment")->where(['oss_domain'=>['neq',''],'url'=>['not like','%http%']])->field("url,oss_domain")->select();
         if(!empty($attachments)){
            $attachment_list = array_skey($attachments,"url");
            Cache::set("attachment_list",$attachment_list);
        }
        
        __success("编辑成功");
    }
    
    // 阿里云物流信息查询配置
    public function deliverySubmit(){
        global $_W;
        $params = $this->param(['status/d','AppKey/s','AppSecret/s','AppCode/s']);
        $aliyun_delivery = $_W['base']['data']['aliyun_delivery'];
        $res = \app\common\model\bbfxshop\Common::setSet('aliyun_delivery',$params);
        
        \app\admin\model\AdminLog::record("","",$params,"bbfx_base");
        
        __success("编辑成功");
    }

    public function entrySubmit(){
        global $_W;
        $params = $this->param(['android_download_url/s','ios_download_url/s']);
        $entry = $_W['base']['data']['entry'];
        $res = \app\common\model\bbfxshop\Common::setSet('entry',$params);
        
        \app\admin\model\AdminLog::record("","",$params,"bbfx_base");
        
        __success("编辑成功");
    }

    // 地图配置
    public function mapSubmit(){
        global $_W;
        $params = $this->param(['admin_map/a','h5_map/a']);
        $map = $_W['base']['data']['map'];


        if(empty($params['h5_map']['key'])){
            __error("请填写H5端地图apikey");
        }
        if(empty($params['h5_map']['securityJsCode'])){
            __error("请填写H5端地图安全密钥");
        }
        
        
        $search1 = $map['h5_map']['key'];
        $replace1 = $params['h5_map']['key'];
        $search2 = $map['h5_map']['securityJsCode'];
        $replace2 = $params['h5_map']['securityJsCode'];

        
        $dir = ROOT_PATH . 'public/mobile/static/js';      
        $pattern = $dir . '/index.*.js';

        $files = glob($pattern);
        if (empty($files)) {
            __error("H5端地图文件错误");
        }

        // 如果只有一个，直接取第一个；有多个可按需要再筛选
        $filepath = $files[0];
        // print_r($filepath);die;
        // 2. 读文件内容
        $content = file_get_contents($filepath);
        if ($content === false) {
            __error("读取H5端地图文件失败");
        }

        if($search1 != $replace1 || $search2 != $replace2){
            if(strstr($content, '29a12b6691bbddc8b78117101eb50dcc')){
                $search1 = '29a12b6691bbddc8b78117101eb50dcc';
                $search2 = '939070896252c3b96cefc36f5902c25c';
            }

            $newContent = $content;
            if($search1 != $replace1){
                $newContent = str_replace($search1, $replace1, $newContent);
            }
            if($search2 != $replace2){
                $newContent = str_replace($search2, $replace2, $newContent);
            }
            // 4. 写回文件
            if (file_put_contents($filepath, $newContent) === false) {
                __error("写入H5端地图文件失败");
            }
        }
            
        $res = \app\common\model\bbfxshop\Common::setSet('map',$params);

        \app\admin\model\AdminLog::record("","",$params,"bbfx_base");
        
        __success("编辑成功");
    }
    
    public function withdrawalSubmit(){
        $params = $this->param(['balance/d','balance_integer/d','max_balance/d','min_balance/d','balance_service_charge/f','balance_fixed_service_charge/f','balance_wx_check/d','balance_wx_check_min/d','balance_wx_check_max/d','bank/s','balance_type/a','tx_date_limit/d','tx_date_type/d','tx_day/a','tx_week/a']);
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        $withdrawal = iunserializer($base['withdrawal']);

        if(empty($withdrawal)){
            $withdrawal = [];
        }
        $withdrawal['balance_tx_tip'] = htmlspecialchars_decode($this->param('balance_tx_tip/h'));
        $withdrawal['balance'] = $params['balance'];
        $withdrawal['balance_integer'] = $params['balance_integer'];
        $withdrawal['max_balance'] = $params['max_balance'];
        $withdrawal['min_balance'] = $params['min_balance'];
        $withdrawal['balance_service_charge'] = $params['balance_service_charge'];
        $withdrawal['balance_fixed_service_charge'] = $params['balance_fixed_service_charge'];
        $withdrawal['bank'] = $params['bank'];
        $withdrawal['balance_type'] = $params['balance_type'];
        $withdrawal['tx_date_limit'] = $params['tx_date_limit'];
        $withdrawal['tx_date_type'] = $params['tx_date_type'];
        $withdrawal['tx_day'] = $params['tx_day'];
        $withdrawal['tx_week'] = $params['tx_week'];
        $withdrawal['balance_wx_check'] = $params['balance_wx_check'];
        $withdrawal['balance_wx_check_min'] = $params['balance_wx_check_min'];
        $withdrawal['balance_wx_check_max'] = $params['balance_wx_check_max'];

        $data = ["withdrawal"=>iserializer($withdrawal)];
        if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
      	__success("编辑成功");
    }
    
    public function withdrawalscoreSubmit(){
        $params = $this->param(['score/d','','score_integer/d','max_score/d','min_score/d','score_service_charge/f','score_fixed_service_charge/f','score_wx_check/d','score_wx_check_min/d','withdrawal_score_ratio/f','score_wx_check_max/d','score_withdrawal_balance_check/d','score_bank/s','score_type/a','score_tx_date_limit/d','score_tx_date_type/d','score_tx_day/a','score_tx_week/a']);
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        $withdrawal = iunserializer($base['withdrawal']);

        if(empty($withdrawal)){
            $withdrawal = [];
        }
        $withdrawal['score_tx_tip'] = htmlspecialchars_decode($this->param('score_tx_tip/h'));
        $withdrawal['score'] = $params['score'];
        $withdrawal['score_integer'] = $params['score_integer'];
        $withdrawal['max_score'] = $params['max_score'];
        $withdrawal['min_score'] = $params['min_score'];
        $withdrawal['score_service_charge'] = $params['score_service_charge'];
        $withdrawal['score_fixed_service_charge'] = $params['score_fixed_service_charge'];
        $withdrawal['score_withdrawal_balance_check'] = $params['score_withdrawal_balance_check'];
        $withdrawal['withdrawal_score_ratio'] = $params['withdrawal_score_ratio'];
        $withdrawal['score_bank'] = $params['score_bank'];
        $withdrawal['score_type'] = $params['score_type'];
        $withdrawal['score_tx_date_limit'] = $params['score_tx_date_limit'];
        $withdrawal['score_tx_date_type'] = $params['score_tx_date_type'];
        $withdrawal['score_tx_day'] = $params['score_tx_day'];
        $withdrawal['score_tx_week'] = $params['score_tx_week'];
        $withdrawal['score_wx_check'] = $params['score_wx_check'];
        $withdrawal['score_wx_check_min'] = $params['score_wx_check_min'];
        $withdrawal['score_wx_check_max'] = $params['score_wx_check_max'];

        $data = ["withdrawal"=>iserializer($withdrawal)];
        if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
      	__success("编辑成功");
    }
    
    public function baseSubmit(){
        $params = $this->param();
        
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        
        $msg = "";
      	if(!$params['title']){
        	$msg = "请填写平台名称";
        }
        if(!$params['logo']){
        	$msg = "请上传logo";
        }
        
      	if($msg){
        	__error($msg);
        }
      	$params['goodsset']['sellout_icon'] = replaceSiteroot($params['goodsset']['sellout_icon']);

        $yuyue_time_period = $params['goodsset']['yuyue_time_period'];
        foreach($yuyue_time_period as $key=>$val){
            $stime = $val['stime'];
            $etime = $val['etime'];
            if(empty($stime) || empty($etime)){
                __error("预约时间不能为空");
            }

            $st = strtotime($stime);
            $et = strtotime($etime);
            if($st >= $et){
                __error("预约开始时间不得大于预约结束时间");
            }
        }

      	$data = array(
        	"title"=>$params['title'],
        	"is_open"=>intval($params['is_open']),
        	"logo"=>$params['logo'],
        	"is_pian"=>intval($params['is_pian']),
        	"is_forcemobile"=>intval($params['is_forcemobile']),
        	"wxapp_login"=>implode(',',$params['wxapp_login']),
        	"service_agreement"=>htmlspecialchars_decode($this->param('service_agreement/h')),
        	"privacy_agreement"=>htmlspecialchars_decode($this->param('privacy_agreement/h')),
        	"block_tips"=>$params['block_tips'],
        	"platform_close_tips"=>$params['platform_close_tips'],
        	"buylevel_tips"=>$params['buylevel_tips'],
        	"stock_remind_num"=>intval($params['stock_remind_num']),
        	"is_gzh_login"=>intval($params['is_gzh_login']),
        	"h5_login"=>implode(',',$params['h5_login']),
        	"app_login"=>implode(',',$params['app_login']),
        	"kefu_link"=>$params['kefu_link'],
        	"uid_prefix"=>$params['uid_prefix'],
        	"orderset"=>iserializer($params['orderset']),
        	"goodsset"=>iserializer($params['goodsset']),
        	"store"=>iserializer($params['store']),
        	// "withdrawal"=>iserializer($params['withdrawal']),
        	"telephone"=>trim($params['telephone']),
        // 	"login_bg"=>$params['login_bg'],
        // 	"font_color"=>$params['font_color'],
        // 	"theme_color"=>$params['theme_color'],
        // 	"btn_color"=>$params['btn_color'],
        // 	"login_template"=>intval($params['login_template']),
        // 	"category_template"=>intval($params['category_template']),
        // 	"fenxiao_template"=>intval($params['fenxiao_template']),
        // 	"texts"=>iserializer($params['texts']),
        	"login_password_update_type"=>intval($params['login_password_update_type']),
        	"register_required_sup_uid"=>intval($params['register_required_sup_uid']),
        );

        if(!empty($data['uid_prefix'])){
            $uid_prefix = intval($data['uid_prefix']);
            if($uid_prefix <= 0 || $uid_prefix > 999){
                __error("用户ID前缀必须是1-3位有效数字组成");
            }
        }
        
        $data['share_title'] = $params['share_title'];
        $data['share_desc'] = $params['share_desc'];
        $data['share_img'] = $params['share_img'];
        
        $data['logo'] = replaceSiteroot($data['logo']);
        $data['share_img'] = replaceSiteroot($data['share_img']);
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
      	__success("编辑成功");
    }
    
    public function styleSubmit(){
        $params = $this->param();
        
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
      	
      	$data = array(
        	"font_color"=>$params['font_color'],
        	"theme_color"=>$params['theme_color'],
        	"btn_color"=>$params['btn_color'],
        	"bg_color"=>$params['bg_color'],
        	"login_template"=>intval($params['login_template']),
        	"category_template"=>intval($params['category_template']),
        	"fenxiao_template"=>intval($params['fenxiao_template']),
        	"login_bg"=>replaceSiteroot($params['login_bg']),
        	"top_bg"=>replaceSiteroot($params['top_bg']),
        	"goods_detail_style"=>intval($params['goods_detail_style']),
        	"goods_detail_fontsize"=>intval($params['goods_detail_fontsize']),
        	// "texts"=>iserializer($params['texts']),
        );
        
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
      	__success("编辑成功");
    }

    public function textSubmit(){
        $params = $this->param();
        
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
      	$data = array(
        	"texts"=>iserializer($params['base_texts']),
        );
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");

        $fenxiao = Db::name("bbfx_fenxiao")->where(["uniacid"=>UNIACID])->find();
      	$data = array(
        	"texts"=>iserializer($params['fenxiao_texts']),
        );
    	if(!empty($fenxiao)){
            $res = Db::name("bbfx_fenxiao")->where(["uniacid"=>UNIACID,"id"=>$fenxiao['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_fenxiao")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_fenxiao");

      	__success("编辑成功");
    }
    
    public function advertSubmit(){
        $params = $this->param();
        
        
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        
        $msg = "";
        
        if($params['is_adunitId'] == '1'){
            if(!intval($params['integral'])){
            	$msg = "请填写激励视频广告奖励积分";
            }
        }
      
      	if($msg){
        	__error($msg);
        }
        
        $task_num = intval($params['task_num']);
        $task_num = $task_num<-1?-1:$task_num;
      	
      	$data = array(
      	    "task_num"=>$task_num,
      	    "is_task"=>intval($params['is_task']),
        	"ad_unitId"=>$params['ad_unitId'],
        	"is_adunitId"=>$params['is_adunitId'],
        	"ad_bannar_unitId"=>$params['ad_bannar_unitId'],
        	"ad_interstitialAd_unitId"=>$params['ad_interstitialAd_unitId'],
        	"integral"=>intval($params['integral']),
        );
        
      
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
        
      	__success("编辑成功");
    }

    public function sendTestSms(){
        $mobile = $this->param("mobile/s");
        $type = 'login';
        $result = \app\common\model\bbfxshop\Sms::sendCode($mobile,$type);
        if($result['code'] === 1){
            __success("验证码发送成功，请注意查收");
        }
        else{
            __error($result['message']);
        }
    }
    
    public function messageSubmit(){
        $params = $this->param();
        
        
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        
        $data = array(
      	    "is_subscribemsg"=>intval($params['is_subscribemsg']),
      	    "updatenotice_status"=>intval($params['updatenotice_status']),
      	    "updatenotice_tips"=>$params['updatenotice_tips'],
      	    "subscribemsg"=>iserializer($params['subscribemsg']),
      	    "gzh_template_msg"=>iserializer($params['gzh_template_msg']),
      	    "sms"=>iserializer($params['sms']),
      	    "admin"=>$params['admin'],
      	    "wx_appid"=>$params['wx_appid'],
      	    "wx_secret"=>$params['wx_secret'],
      	    "wx_domain"=>$params['wx_domain'],
      	    "notice_type"=>intval($params['notice_type']),
        );
      
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
      	__success("编辑成功");
    }
    
    public function msgVariableList(){
        $params = $this->param();
        
        $variableList = \app\common\model\bbfxshop\Message::getVariableList();
        
        __success("操作成功",['variableList'=>$variableList]);
    }
    
    
    public function textSubmit1(){
        $params = $this->param();
        
        
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        
        $text = $params['text'];
        
        $text = iserializer($text);
        $data = array("text_replace"=>$text);
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
      	__success("编辑成功");
    }
    
    public function signSubmit(){
        $params = $this->param();
        
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        
        $msg = "";
      	if($params['is_sign'] == "1"){
        	if(!$params['first_reward']){
            	$msg = "请填写首次签到奖励";
            }
        	if(!$params['daily_reward']){
            	$msg = "请填写日常签到奖励";
            }
        }
        
      	if($msg){
        	__error($msg);
        }
      	
      	$data = array(
        	"is_sign"=>$params['is_sign'],
        	"first_reward"=>$params['first_reward'],
        	"daily_reward"=>$params['daily_reward']
        );
      
    	if(!empty($base)){
            $res = Db::name("bbfx_base")->where(["uniacid"=>UNIACID,"id"=>$base['id']])->update($data);
        }
      	else{
          	$data['uniacid'] = UNIACID;
            $res = Db::name("bbfx_base")->insertGetId($data);
        }
        \app\admin\model\AdminLog::record("","",$data,"bbfx_base");
      	__success("编辑成功");
    }
    
    public function getSignrule(){
        $params = $this->param();
        
        $sign_rule = Db::name("bbfx_sign_rule")->where(["uniacid"=>UNIACID])->order('day asc')->select();
        
        __success("",$sign_rule);
    }
    
    public function sign_del(){
        $params = $this->param();
        
        
        $base = Db::name("bbfx_base")->where(["uniacid"=>UNIACID])->find();
        
        $id = trim($params['id']);
        if(!$id){
            __error("id错误");  
        }
        $result = Db::name("bbfx_sign_rule")->where(['uniacid' => UNIACID,"id"=>$id])->find();
        if (!empty($result)) {
    	    __success('删除成功');
        }
        else{
            __error("删除失败"); 
        }
    }
    
    public function signPost(){
        $params = $this->param();
        
        
        $id = intval($params['id']);
        
        $type = intval($params['type']);
        $day = intval($params['day']);
        $integral = intval($params['integral']);
        $sign_it = Db::name("bbfx_sign_rule")->where(["uniacid"=>UNIACID,"type"=>$type,"day"=>$day])->find();
        
        $msg = "";
        
        if($day <= 0){
            if($type == "1"){
                $msg = "请填写连续签到天数";
            }
            else{
                $msg = "请填写总签到天数";
            }
        }
        if($integral <= 0){
            if($type == "2"){
                $msg = "请填写连续签到奖励";
            }
            else{
                $msg = "请填写总签到奖励".$integral;
            }
        }
        
        if(!empty($sign_it) && $sign_it['id'] != $id){
            if($type == "1"){
                $msg = "已有相同连续签到天数";
            }
            else{
                $msg = "已有相同连续签到天数总签到天数";
            }
        }
        
        
        if($msg){
            __error($msg);
        }
        $data = array("day"=>$day,"type"=>$type,"integral"=>$integral,"uniacid"=>UNIACID);
        if(!empty($id)){
            $res = Db::name("bbfx_sign_rule")->where(['id'=>$id])->update($data);
        }
        else{
            $res = Db::name("bbfx_sign_rule")->insertGetId($data);
        }
        
        if($res){
            __success("操作成功");
        }
        else{
            __error("操作失败");
        }
    }
    
}