<?php

    /**
     * 状态码说明
     * 200 成功
     * 201 未登录
     * 202 失败
     * 203 空值
     * 204 无结果
     */

    // 页面编码
    header("Content-type:application/json");
    
    // 判断登录状态
    session_start();
    if(isset($_SESSION["yinliubao"])){
        
        // 已登录
        $payapi_domain = trim($_POST['payapi_domain']);
        $payapi_merchant_id = trim($_POST['payapi_merchant_id']);
        $payapi_merchant_key = trim($_POST['payapi_merchant_key']);
        $qywx_AgentId = trim($_POST['qywx_AgentId']);
        $qywx_Secret = trim($_POST['qywx_Secret']);
        $qywx_Appid = trim($_POST['qywx_Appid']);
        $qywx_auth_domain  = trim($_POST['qywx_auth_domain']);
        
        // 过滤参数
        if(empty($payapi_domain) || !isset($payapi_domain)){
            
            $result = array(
                'code' => 203,
                'msg' => '易支付的接口域名未填写'
            );
        }else if(is_url($payapi_domain) === false) {
            
            $result = array(
                'code' => 203,
                'msg' => '易支付的接口域名不符合域名规则，要求http://或https://开头'
            );
        }else if(empty($payapi_merchant_id) || !isset($payapi_merchant_id)){
            
            $result = array(
                'code' => 203,
                'msg' => '易支付的商户id未填写'
            );
        }else if(empty($payapi_merchant_key) || !isset($payapi_merchant_key)){
            
            $result = array(
                'code' => 203,
                'msg' => '易支付的商户密钥未填写'
            );
        }else if(empty($qywx_AgentId) || !isset($qywx_AgentId)){
            
            $result = array(
                'code' => 203,
                'msg' => '企业微信应用id未填写'
            );
        }else if(empty($qywx_Secret) || !isset($qywx_Secret)){
            
            $result = array(
                'code' => 203,
                'msg' => '企业微信应用Secret'
            );
        }else if(empty($qywx_Appid) || !isset($qywx_Appid)){
            
            $result = array(
                'code' => 203,
                'msg' => '企业微信企业id未填写'
            );
        }else if(empty($qywx_auth_domain) || !isset($qywx_auth_domain)){
            
            $result = array(
                'code' => 203,
                'msg' => '企业微信应用网页授权域名未填写'
            );
        }else if(is_url($qywx_auth_domain) === false) {
            
            $result = array(
                'code' => 203,
                'msg' => '企业微信应用网页授权域名不符合域名规则，要求http://或https://开头'
            );
        }else{
            
            // 当前登录的用户
            $LoginUser = $_SESSION["yinliubao"];
            
            // 数据库配置
            include '../../../../Db.php';
        
            // 实例化类
            $db = new DB_API($config);
            
            // 验证操作权限
            $checkAdmin = $db->set_table('huoma_user')->find(['user_name' => $_SESSION["yinliubao"]]);
            $user_admin = $checkAdmin['user_admin'];
            
            if($user_admin == 2) {
                
                $result = array(
                    'code' => 202,
                    'msg' => '无操作权限'
                );
                echo json_encode($result,JSON_UNESCAPED_UNICODE);
                exit;
            }
            
            // 验证当前要操作的用户是否为当前登录的用户
            $getCurrentUser = $db->set_table('ylb_ChatroomPayConfig')->find(['id' => 1]);
            $Action_User = $getCurrentUser['Action_User'];
            
            // 用户一致：允许操作
            if($Action_User == $LoginUser){
                
                // 需更新的字段
                $updateData = [
                    'payapi_domain' => removeTrailingSlash($payapi_domain),
                    'payapi_merchant_id' => $payapi_merchant_id,
                    'payapi_merchant_key' => $payapi_merchant_key,
                    'qywx_AgentId' => $qywx_AgentId,
                    'qywx_Secret' => $qywx_Secret,
                    'qywx_Appid' => $qywx_Appid,
                    'qywx_auth_domain' => $qywx_auth_domain
                ];
                
                // 更新条件
                $updateCondition = [
                    'id' => 1,
                    'Action_User' => $LoginUser
                ];
                
                // 执行更新
                $updateSQL = $db->set_table('ylb_ChatroomPayConfig')->update($updateCondition,$updateData);
                if($updateSQL){
                    
                    // 更新成功
                    $result = array(
                        'code' => 200,
                        'msg' => '已保存'
                    );
                }else{
                    
                    // 更新失败
                    $result = array(
                        'code' => 202,
                        'msg' => '保存失败'
                    );
                }
                
            }else{
                
                // 用户不一致
                $result = array(
                    'code' => 202,
                    'msg' => '无操作权限'
                );
            }
            
        }
        
    }else{
        
        // 未登录
        $result = array(
            'code' => 201,
            'msg' => '未登录'
        );
    }
    
    // 检查字符串的最后一个字符是否是/
    function removeTrailingSlash($string) {

        // 如果是，去掉最后一个字符
        if (substr($string, -1) === '/') {
            
            $string = substr($string, 0, -1);
        }
        return $string;
    }
    
    // 验证域名的规范性
    function is_url($url) {
        
        $r = "/^http[s]?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/\S*)?$/i";
        return preg_match($r, $url) ? true : false;
    }
    
    // 输出JSON
    echo json_encode($result,JSON_UNESCAPED_UNICODE);
    
?>