<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test2.aspx.cs" Inherits="eFrameWork.AppWebAPI.Test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="../Scripts/jquery-1.12.1.js"></script>
</head>
<script>
    var token = "anonymous";
    var eServer = "http://192.168.1.9/eframework/AppWebAPI/";
	eServer = "http://demo.eketeam.com/AppWebAPI/";

    function gettoken(usr, pwd) {
        $.ajax({
            type: "POST",
            url: eServer + "getToKen.aspx",
            data: { username: usr, password: pwd },
            dataType: "json",
            success: function (data, textStatus, request) {
                //alert(JSON.stringify(data));
                if (data.errcode == 0) {
                    token = data.token;
                    $("#txttoken").val(token);
                }
                else {
                    alert(data.message);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("error:" + XMLHttpRequest.responseText + "::" + XMLHttpRequest.status + "::" + XMLHttpRequest.readyState + "::" + XMLHttpRequest.statusText);
            }
        });
    };
    var eModel = function (appitem,modelid) {
		this.appitem=appitem;	
        this.modelid = modelid;
        this.list = function () {
            var result = {};
            $.ajax({
                type: "GET",
                cache: false,
                url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : "") + "&page=1",//+ "&debug=1",
                headers: { "auth": token },
                dataType: "json",
                async: false,
                success: function (data, textStatus, request) {
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    result = { "errcode": "0", "message": XMLHttpRequest.status + ":" + errorThrown };
                }
            });
            return result;
        };
        this.insert = function (data) {
            var result = {};
            data["act"] = "apisave";
            data["id"] = "";
            $.ajax({
                type: "POST",
                url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : ""),// + "&debug=1",
                headers: { "auth": token },
                contentType: "application/json",
                data: JSON.stringify(data),
                dataType: "json",
                async: false,
                success: function (data, textStatus, request) {
                    //alert(JSON.stringify(data));
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    result = { "errcode": "0", "message": XMLHttpRequest.status + ":" + errorThrown };
                    //alert("error:" + XMLHttpRequest.responseText + "::" + XMLHttpRequest.status + "::" + XMLHttpRequest.readyState + "::" + XMLHttpRequest.statusText);
                }
            });
            return result;
        };
        this.update = function (id, data) {
            var result = {};
            data["act"] = "apisave";
            data["id"] = id;
            $.ajax({
                type: "POST",
                url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : ""),//+ "&debug=1",
                headers: { "auth": token },
                contentType: "application/json",
                data: JSON.stringify(data),
                dataType: "json",
                async: false,
                success: function (data, textStatus, request) {
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    result = { "errcode": "0", "message": XMLHttpRequest.status + ":" + errorThrown };
                }
            });
            return result;
        };
        this.read = function (id) {
            var result = {};
            $.ajax({
                type: "GET",
                cache: false,
                url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : "") + "&act=apiedit&id=" + id,//+ "&debug=1",
                headers: { "auth": token },
                dataType: "json",
                async: false,
                success: function (data, textStatus, request) {
                    //alert(JSON.stringify(data));
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    result = { "errcode": "0", "message": XMLHttpRequest.status + ":" + errorThrown };
                }
            });
            return result;
        };
        this.view = function (id) {
            var result = {};
            $.ajax({
                type: "GET",
                cache: false,
                url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : "") + "&act=apiview&id=" + id,//+ "&debug=1",
                headers: { "auth": token },
                dataType: "json",
                async: false,
                success: function (data, textStatus, request) {
                    //alert(JSON.stringify(data));
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    result = { "errcode": "0", "message": XMLHttpRequest.status + ":" + errorThrown };
                }
            });
            return result;
        };
        this.delete = function (id) {
            var result = {};
            var data = { "act": "apidel", "id": id };
            $.ajax({
                type: "POST",
                url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : ""),//+ "&debug=1",
                headers: { "auth": token },
                data: data,
                dataType: "json",
                async: false,
                success: function (data, textStatus, request) {
                    result = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    result = { "errcode": "0", "message": XMLHttpRequest.status + ":" + errorThrown };
                }
            });
            return result;
        };
        return this;
    };

    var model = new eModel("cc89067d-3ec8-4f28-9fdd-507f0dc3df58","e1e951c9-a076-45b8-b551-24a0ddbe6291");
    function model_list() {
        var data = model.list();
        $("#txttoken").val(JSON.stringify(data));
        //alert(JSON.stringify(data));	
    };
    function model_insert() {
        var  data = {  "XM": "东东", "XB": "2" ,"PID":"101" };
		data = { "XM": "东东", "XB": "2" ,"PID_name":"101" };
        var result = model.insert(data);
        $("#txttoken").val(JSON.stringify(result));;
    };

    function model_update() {
        var data = {  "XM": "东233东", "XB": "2" };
        var result = model.update("19", data);
        $("#txttoken").val(JSON.stringify(result));;
    };
    function model_delete() {
        var result = model.delete("19");
        $("#txttoken").val(JSON.stringify(result));;
    };
    function model_read() {
        var data = model.read("19");
        $("#txttoken").val(JSON.stringify(data));
    };
    function model_view() {
        var data = model.view("19");
        $("#txttoken").val(JSON.stringify(data));
    };

</script>
<body>
<textarea id="txttoken" style="width:800px;height:130px;padding:6px;"></textarea><br>
 <button onClick="gettoken('admin','123456');">获取令牌</button><br>
<button onClick="model_list();">Model列表</button><br>

<button onClick="model_insert();">Model添加</button>
<button onClick="model_update();">Model修改</button>
<button onClick="model_delete();">Model删除</button>
<button onClick="model_read();">Model读取编辑数据</button>
<button onClick="model_view();">Model查看数据</button>



<script>

    function headimg_change(obj) {
        var url = "http://demo.eketeam.com/Plugins/ProUpload.aspx?type=file";        
        if (obj.files) {
            var formData = new FormData();
            for (var i = 0; i < obj.files.length; i++) {
                formData.append('files', obj.files[i], obj.files[i].name);
            }

            $.ajax({
                type: "POST", async: true,
                data:formData,
                url: url,
				headers: { "auth": token },
                dataType: "json",
                contentType: false,
                //dataType: "formData",
                cache: false,//上传文件无需缓存
                processData: false,//用于对data参数进行序列化处理 这里必须false
                success: function (data) 
                {
                    alert(data.files.length);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) { }
            });
            obj.value = "";
        }
        else {
            alert("您的浏览器不支持此功能!");
        }
    };
</script>
<input type="file" name="file"  onchange="headimg_change(this);">
</body>
</html>