<?php
declare (strict_types = 1);

namespace app\model\api;

use library\basic\BaseModel;
use library\traits\JwtAuthModelTrait;
use library\traits\ModelTrait;
use think\Model;

/**
 * @mixin \think\Model
 */
class Sys extends Model
{
    use ModelTrait;
    use JwtAuthModelTrait;
    /**
     * 保存数据
     * @param $save
     */
    public function saveSys($save) {
        $this->where('id',1)->save($save);
    }
    /**
     * 微信配置信息
     * @param type $data
     */
    public function saveWxConfig($data){
        $save=[
            "appid"          => empty($data["appid"])?"":$data["appid"],
            "secret"         => empty($data["secret"])?"":$data["secret"],
            "mchid"          => empty($data["mchid"])?"":$data["mchid"],
            "apiv2key"       => empty($data["apiv2key"])?"":$data["apiv2key"],
            "apiclient_key"  => empty($data["apiclient_key"])?"":$data["apiclient_key"],
            "apiclient_cert" => empty($data["apiclient_cert"])?"":$data["apiclient_cert"],
            "notify_url"     => empty($data["notify_url"])?"":$data["notify_url"],
        ];
        $this->where("id",1)->update(["wxconfig"=> serialize($save)]);
    }
    /**
     * 分享配置信息
     * @param type $data
     */
    public function saveShareConfig($data){
        $save=[
            "img"     => empty($data["img"])  ?"":$data["img"],
            "title"   => empty($data["title"])?"":$data["title"],
            "query"   => empty($data["query"])?"":$data["query"],
            "content" => empty($data["content"])?"":$data["content"],
        ];
        $this->where("id",1)->update(["shareconfig"=> serialize($save)]);
    }
    /**
     * 邀请配置
     * @param type $data
     */
    public function saveInviteConfig($data){
        $save=[
            "img"     => empty($data["img"]) ? "" : $data["img"],
        ];
        $this->where("id",1)->update(["inviteconfig"=> serialize($save)]);
    }
    /**
     * 获取配置详情
     * @return type
     */
    public function getDataInfo($code=""){
        $sys = $this->where("id", 1)->find();
        //微信配置
        $wxconfig=[
            "appid"=>"",
            "secret"=>"",
            "mchid"=>"",
            "apiv2key"=>"",
            "apiclient_key"=>"",
            "apiclient_cert"=>"",
            "notify_url"=>""
        ];
        if(!empty($sys->wxconfig)){
            $wxconfig = unserialize($sys->wxconfig);
        }
        //分享配置
        $shareconfig=[
            "img"=>"",
            "title"=>"",
            "query"=>"",
            "content"=>"",
        ];
        if(!empty($sys->shareconfig)){
            $shareconfig = unserialize($sys->shareconfig);
        }
        //邀请配置
        $inviteconfig=[
            "img"=>"",
        ];
        if(!empty($sys->inviteconfig)){
            $inviteconfig = unserialize($sys->inviteconfig);
        }
        if($code=="weixin"){
            return $wxconfig;
        }
        if($code=="share"){
            return $shareconfig;
        }
        if($code=="invite"){
            return $inviteconfig;
        }
        $data = $sys->toArray();
        $data["wxconfig"] = $wxconfig;
        $data["shareconfig"] = $shareconfig;
        $data["inviteconfig"] = $inviteconfig;
        return $data;
    }
    
    /**
     * 获取微信配置
     * @return bool
     */
    public function getWeixinConfig(){
        $data = $this->getDataInfo("weixin");
        foreach($data as $k=>$v){
            if(empty($v)){
                return false;
            }
        }
        $return=[
            "APPID"         => $data["appid"],
            "APPSECRET"     => $data["secret"],
            "MCHID"         => $data["mchid"],
            "ApiV2Key"      => $data["apiv2key"],
            "ApiclientKey"  => "file://".app()->getRootPath()."/".trim($data["apiclient_key"],"/"),
            "ApiclientCert" => "file://".app()->getRootPath()."/".trim($data["apiclient_cert"],"/"),
            "NOTIFY_URL"    => $data["notify_url"],
        ];
        return $return;
    }
}
