<?php
/**
 * 智慧快递模块定义
 *
 * @author Min晓天
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');

class Evzg_expressModule extends WeModule {
	public function fieldsFormDisplay($rid = 0) {
		//要嵌入规则编辑页的自定义内容，这里 $rid 为对应的规则编号，新增时为 0
	}

	public function fieldsFormValidate($rid = 0) {
		//规则编辑保存时，要进行的数据验证，返回空串表示验证无误，返回其他字符串将呈现为错误提示。这里 $rid 为对应的规则编号，新增时为 0
		return '';
	}

	public function fieldsFormSubmit($rid) {
		//规则验证无误保存入库时执行，这里应该进行自定义字段的保存。这里 $rid 为对应的规则编号
	}

	public function ruleDeleted($rid) {
		//删除规则时调用，这里 $rid 为对应的规则编号
	}

	public function settingsDisplay($settings) {
		global $_W, $_GPC;
		//点击模块设置时将调用此方法呈现模块设置页面，$settings 为模块设置参数, 结构为数组。这个参数系统针对不同公众账号独立保存。
		//在此呈现页面中自行处理post请求并保存设置参数（通过使用$this->saveSettings()来实现）
		if(checksubmit()) {
		    load()->func("file");
		    $certroot = MODULE_ROOT . '/template/certdata/' . $_W['uniacid'] . '/' . $_GPC['certkey'];
		    if ($this->module["config"]["certkey"] != $_GPC["certkey"] && !empty($this->module["config"]["certkey"])) {
		        $oldcertroot = MODULE_ROOT . '/template/certdata/' . $_W['uniacid'] . '/' . $this->module["config"]["certkey"];
		        rename($oldcertroot, $certroot);
		    }
		    $r = mkdirs($certroot);
		    if (!empty($_GPC['apiclient_cert'])) {
		        $ret = file_put_contents($certroot . '/apiclient_cert.pem', trim($_GPC['apiclient_cert']));
		        $r = $r && $ret;
		    }
		    if (!empty($_GPC['apiclient_key'])) {
		        $ret = file_put_contents($certroot . '/apiclient_key.pem', trim($_GPC['apiclient_key']));
		        $r = $r && $ret;
		    }
		    if (!$r) {
		        message('证书保存失败, 请保证 /addons/evzg_express/template/certdata/ 目录可写');
		    }
		    $dat=array();
			//字段验证, 并获得正确的数据$dat
			$dat['msg'] = $_GPC['template_message'];
			//var_dump($_GPC['template_message']);die();
			$dat['dayu'] = array('dayu_appkey'=>$_GPC['dayu_appkey'],'dayu_secretkey'=>$_GPC['dayu_secretkey'],'dayu_sign'=>$_GPC['dayu_sign'],'dayu_sign'=>$_GPC['dayu_sign'],'dayu_temp'=>$_GPC['dayu_temp'],'dayu_temp2'=>$_GPC['dayu_temp2'],'dayu_temp3'=>$_GPC['dayu_temp3'],'dayutpl'=>$_GPC['dayutpl']);
			$dat['juhe'] = array('juhe_appkey'=>$_GPC['juhe_appkey'],'juhe_temp'=>$_GPC['juhe_temp'],'juhe_temp2'=>$_GPC['juhe_temp2']);
			$dat['status'] =$_GPC['status'];
			$dat['dayutpl'] =$_GPC['dayutpl'];
			$dat['enabled'] =$_GPC['enabled'];//短信模块选择
			$dat['apikey'] = $_GPC['apikey'];
			$dat['baiduak'] = $_GPC['baiduak'];
			$dat['uid'] = $_GPC['uid'];
			//百度接口
			$dat['baiduappid'] =$_GPC['baiduappid'];
			$dat['baiduapikey'] = $_GPC['baiduapikey'];
			$dat['baidusecretkey'] = $_GPC['baidusecretkey'];

			$dat['key'] = $_GPC['key'];
			$dat['mchid'] = $_GPC['mchid'];
			$dat['certkey'] = $_GPC['certkey'];
			$dat['mchidapikey'] = $_GPC['mchidapikey'];
			//腾讯优图
			$dat['youtuappid'] = trim($_GPC['youtuappid']);
			$dat['youtukey'] = trim($_GPC['youtukey']);
			$dat['youtusecret'] = trim($_GPC['youtusecret']);
			$dat['youtuserid'] = trim($_GPC['youtuserid']);
 			$dat['type_discernment'] = $_GPC['type_discernment'];
 			$this->saveSettings($dat);
			if($this->saveSettings($dat)){
			    message("设置成功","referer","success");
			}
		}
		//这里来展示设置项表单
		include $this->template('setting');
	}

}