/**
*
* Ajax请求封装,使用Layer的Loading层和弹出层提示,外层页面需要引用 layui 前端框架
*
* Layui具体使用见文档 @see https://www.layui.com
*
* @date    2019-08-15
* @author  zhangkuan
* @email   guitu18@qq.com
* @blog    https://www.guitu18.com
*/
window.Service = {
 /**
  * GET请求封装
  * @param url               请求地址
  * @param data              请求参数
  * @param successCallback   回调函数
  */
 get: function (url, data, successCallback) {
     var loading = layer.open({type: 3});
     var time = Date.parse(new Date());
     var _url = url + "?time=" + time;
     if (data) _url += "&" + $.param(data);
     $.ajax({
         url: _url,
         type: "GET",
         dataType: "json",
         complete: function () {

         },
         success: function (result) {
             layer.close(loading);
             if (successCallback) {
                 successCallback(result);
             }
         },
         error: function (e) {
             layer.close(loading);
             layer.msg('请求失败', {icon: 5});
         },
     });
 },
 /**
  * POST请求封装
  * @param url               请求地址
  * @param data              请求参数
  * @param successCallback   回调函数
  */
 post: function (url, data, successCallback) {
     var loading = layer.open({type: 3});
     var time = Date.parse(new Date());
     var _url = url + "?time=" + time;
     $.ajax({
         url: _url,
         type: "POST",
         cache: false,
         dataType: "json",
         data: data,
         complete: function () {

         },
         success: function (result) {
             layer.close(loading);
             if (successCallback) {
                 successCallback(result);
             }
         },
         error: function (e) {
             layer.close(loading);
             layer.msg('请求失败', {icon: 5});
         },
     });
 },
 /**
  * POST请求封装,ContentType为 JSON 方式提交,后台用 @RequestBody 接收参数
  * @param url               请求地址
  * @param data              请求参数
  * @param successCallback   回调函数
  */
 postBody: function (url, data, successCallback) {
     var loading = layer.open({type: 3});
     var time = Date.parse(new Date());
     var _url = url + "?time=" + time;
     $.ajax({
         url: _url,
         type: "POST",
         cache: false,
         dataType: "json",
         contentType: "application/json",
         data: JSON.stringify(data),
         complete: function () {

         },
         success: function (result) {
             layer.close(loading);
             if (successCallback) {
                 successCallback(result);
             }
         },
         error: function (e) {
             layer.close(loading);
             layer.msg('请求失败', {icon: 5});
         },
     });
 },
};

/**
* 获取路径中的参数
* @param name  参数名
* @returns {string|null}
*/
window.getParam = function (name) {
 //构造一个含有目标参数的正则表达式对象
 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
 //匹配目标参数
 var r = window.location.search.substr(1).match(reg);
 if (r != null) return unescape(r[2]);
 //返回参数值
 return null;
};

/**
* 图片预览
* @param obj
*/
function photos() {
 layui.use(["layer"], function () {
     layer.photos({
         photos: "#layer-photos",
         shadeClose: true,
         closeBtn: true,
         anim: 5,
         shade: 0.6,
     });
 });
}

/**
* 图片预览
* @param obj
*/
function previewImg(obj, height, weight) {
 debugger
 var img = new Image();
 img.src = obj.src;
 var imgHtml = "<div class='previewImg'><img src='" + obj.src + "' width='500px' height='500px'/></div>";
 //弹出层
 layui.use(["layer"], function () {
     layer.open({
         type: 1,
         shade: 0.8,
         offset: "auto",
         area: [height ? height : 1280 + "px", weight ? weight : 720 + "px"],
         shadeClose: true,
         scrollbar: false,
         title: "图片预览",
         content: imgHtml,
         // 关闭窗口
         cancel: function () {
             // layer.msg('捕获就是从页面已经存在的元素上,包裹layer的结构', {time: 5000, icon: 6});
         },
     });
 });
}