wangtengyu
2018-11-28 b5d693294fc71eaf5c758fdaa0f215c0cc55ee6a
对外接口工具类 by 王腾宇
2 files added
187 ■■■■■ changed files
api/userApi.php 79 ●●●●● patch | view | raw | blame | history
web/common/interface.func.php 108 ●●●●● patch | view | raw | blame | history
api/userApi.php
New file
@@ -0,0 +1,79 @@
<?php
/**
 * 获取用户数据相关 Api
 *  王腾宇
 */
require '../framework/bootstrap.inc.php';
require '../web/common/interface.func.php';
if($_SERVER['REQUEST_METHOD'] != 'POST'){
    http_response_code(405);
} else {
    $retData=checkParam($_GET);
    switch ($retData["regMsg"]){
        case "e00000":
            $userApi = new UserApi();
            switch (getUrl($_SERVER['REQUEST_URI']))
            {
                case "getUserList":
                    exit($userApi->getUserList($retData["regData"]));
                    break;
                case "getUserOrderList":
                    exit($userApi->getUserOrderList($retData["regData"]));
                    break;
                default: http_response_code(404);
            }
        case "e00001":
            exit(array(
                "rspCode"=>"e00001",
                "rspMsg"=>"接入平台不存在"
            ));
        case "e00002":
        exit(array(
            "rspCode"=>"e00002",
            "rspMsg"=>"鉴权失败"
        ));
    }
}
class UserApi
{
    static $db;
    function _initialize()
    {
        $db = $this-> pdo();
    }
    function pdo() {
        static $db;
        global $_W;
        if(empty($db)) {
            if($_W['config']['db']['slave_status'] == true && !empty($_W['config']['db']['slave'])) {
                load()->classs('slave.db');
                $db = new SlaveDb('master');
            } else {
                load()->classs('db');
                if(empty($_W['config']['db']['master'])) {
                    $_W['config']['db']['master'] = $GLOBALS['_W']['config']['db'];
                    $db = new DB($_W['config']['db']);
                } else {
                    $db = new DB('master');
                }
            }
        }
        return $db;
    }
    public function getUserList($param)
    {
        return json_encode($param,JSON_UNESCAPED_UNICODE);
    }
    public function getUserOrderList($param)
    {
        return json_encode($param,JSON_UNESCAPED_UNICODE);
    }
}
web/common/interface.func.php
New file
@@ -0,0 +1,108 @@
<?php
/**
 * 对外接口工具类 Api
 * 王腾宇
 */
// 1000001 微擎
// 1000002 商城
// 1000003 管理系统
static $systemId = array(
    "1000001"=>"3a03c2e6f2f911e8b9245254009bf9ba",
    "1000002"=>"466a50d8f2f911e8b9245254009bf9ba",
    "1000003"=>"53cfae5df2f911e8b9245254009bf9ba",
);
/**
 * 接口数据校验
 * e00000 检验成功
 * e00001 接入平台不存在
 * e00002 鉴权失败
 */
function checkParam ($param)
{
    global $systemId;
    $retData = array();
    $sysKey = $systemId[substr($param["id"],0,7)];
    if ($sysKey == null){
        $retData["regMsg"] = "e00001";
    } else {
        if (checkSign($param,$sysKey)){
            $retData["regMsg"] = "e00000";
            $retData["regData"] = json_decode($param["data"],true);
        } else {
            $retData["regMsg"] = "e00002";
        }
    }
    return $retData;
}
/**
 * 校验签名
 */
function checkSign($data,$sysKey)
{
    $str = "";
    foreach ($data as $key => $value)
    {
        if ($key != "sign") {
            $str = $str.$key."=".$value."&";
        }
    }
    return $data["sign"] == md5($str."key=".$sysKey);
}
/**
 * 解析请求接口地址
 */
function getUrl($url)
{
    $arr = explode('/', $url);
    return substr($arr[count($arr)-1],0,strpos($arr[count($arr)-1], '?'));
}
/**
 * 生成对外接口字符串
 * 例子: id=100000120181128223494512&data=%7B%22code%22%3A%221%22%2C%22id%22%3A%22100000120181128223494512%22%7D&sign=a59336edda9fd6785fac6720d02c7d61
 */
function makeInterfaceParam($param, $appId)
{
    $param["id"]=$appId.date("Ymd").getRandomNumber(9);
    $paramJson = json_encode($param,JSON_UNESCAPED_UNICODE);
    return
        "id=".$param["id"].
        "&data=".urlencode($paramJson).
        "&sign=".getSign($paramJson,$appId,$param["id"]);
}
/**
 * 生成签名
 */
function getSign($param,$appId,$id){
    global $systemId;
    return md5(
        "id=".$id.
        "&data=".$param.
        "&key=".$systemId["$appId"]
    );
}
/**
 * 生成随机数字
 */
function getRandomNumber($length){
    $arr = [0,1,2,3,4,5,6,7,8,9];
    $str = '';
    for ($i = 0; $i < $length; $i++)
    {
        $str .= $arr[rand(0, 9)];
    }
    return $str;
}