product-add.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. BannerJson = {}
  2. ColoeJson = {}
  3. pattr = []
  4. PageType = true;
  5. ProductId = "";
  6. window.onload = function () {
  7. var uploader = new plupload.Uploader({ //创建实例的构造方法
  8. runtimes: 'html5,flash,silverlight,html4', //上传插件初始化选用那种方式的优先级顺序
  9. // browse_button: 'bannerbtn', // 上传按钮
  10. browse_button: $("#bannerbtn")[0], // 上传按钮
  11. url: "upload.php", //远程上传地址
  12. flash_swf_url: 'plupload/Moxie.swf', //flash文件地址
  13. silverlight_xap_url: 'plupload/Moxie.xap', //silverlight文件地址
  14. filters: {
  15. max_file_size: '200mb', //最大上传文件大小(格式100b, 10kb, 10mb, 1gb)
  16. mime_types: [ //允许文件上传类型
  17. {
  18. title: "files",
  19. extensions: "jpg,png,gif,ico"
  20. }
  21. ]
  22. },
  23. multi_selection: true, //true:ctrl多文件上传, false 单文件上传
  24. init: {
  25. FilesAdded: function (up, files) { //文件上传前
  26. if ($("#ul_pics").children("li").length > 30) {
  27. alert("您上传的图片太多了!");
  28. uploader.destroy();
  29. } else {
  30. var li = '';
  31. plupload.each(files, function (file) { //遍历文件
  32. li += "<li id='" + file['id'] + "'><div class='progress'><span class='bar'></span><span class='percent'>0%</span></div></li>";
  33. });
  34. $("#ul_pics").append(li);
  35. uploader.start();
  36. }
  37. },
  38. UploadProgress: function (up, file) { //上传中,显示进度条
  39. var percent = file.percent;
  40. $("#" + file.id).find('.bar').css({
  41. "width": percent + "%"
  42. });
  43. $("#" + file.id).find(".percent").text(percent + "%");
  44. },
  45. FileUploaded: function (up, file, info) { //文件上传成功的时候触发
  46. var data = eval("(" + info.response + ")");
  47. // $("#" + file.id).html("<div class='img'><img src='" + data.pic + "'/></div><input type='' value='' class='input'><span class='span'>删除</span>");
  48. // $("#" + file.id).html("<div class='img'><img src='" + data.pic + "'/></div><p>" + file.name + "</p><span class='span' onclick='DeleteBanner(" + file.id + ")'>删除</span>");
  49. $("#" + file.id).html(CreateBanner(data.pic, file.name, file.id));
  50. BannerJson[file.id] = data;
  51. },
  52. Error: function (up, err) { //上传出错的时候触发
  53. // alert(err.message);
  54. $.Huimodalalert('服务器错误!请联系管理员', 3000);
  55. }
  56. },
  57. });
  58. var uploader2 = new plupload.Uploader({ //创建实例的构造方法
  59. runtimes: 'html5,flash,silverlight,html4', //上传插件初始化选用那种方式的优先级顺序
  60. browse_button: 'colorbtn', // 上传按钮
  61. url: "upload.php", //远程上传地址
  62. flash_swf_url: 'plupload/Moxie.swf', //flash文件地址
  63. silverlight_xap_url: 'plupload/Moxie.xap', //silverlight文件地址
  64. filters: {
  65. max_file_size: '200mb', //最大上传文件大小(格式100b, 10kb, 10mb, 1gb)
  66. mime_types: [ //允许文件上传类型
  67. {
  68. title: "files",
  69. extensions: "jpg,png,gif,ico"
  70. }
  71. ]
  72. },
  73. multi_selection: true, //true:ctrl多文件上传, false 单文件上传
  74. init: {
  75. FilesAdded: function (up, files) { //文件上传前
  76. if ($("#ul_pics2").children("li").length > 30) {
  77. alert("您上传的图片太多了!");
  78. uploader2.destroy();
  79. } else {
  80. var li = '';
  81. plupload.each(files, function (file) { //遍历文件
  82. li += "<li id='" + file['id'] + "'><div class='progress'><span class='bar'></span><span class='percent'>0%</span></div></li>";
  83. });
  84. $("#ul_pics2").append(li);
  85. uploader2.start();
  86. }
  87. },
  88. UploadProgress: function (up, file) { //上传中,显示进度条
  89. var percent = file.percent;
  90. $("#" + file.id).find('.bar').css({
  91. "width": percent + "%"
  92. });
  93. $("#" + file.id).find(".percent").text(percent + "%");
  94. },
  95. FileUploaded: function (up, file, info) { //文件上传成功的时候触发
  96. var data = eval("(" + info.response + ")");
  97. // $("#" + file.id).html("<div class='img'><img src='" + data.pic + "'/></div><input type='' value='' class='input' placeholder='请输入颜色分类或其他说明'><span class='span' onclick='DeleteColor(" + file.id + ")'>删除</span>");
  98. $("#" + file.id).html(CreateColor(data.pic, "", file.id));
  99. ColoeJson[file.id] = data;
  100. },
  101. Error: function (up, err) { //上传出错的时候触发
  102. // alert(err.message);
  103. $.Huimodalalert('服务器错误!请联系管理员', 3000);
  104. }
  105. },
  106. });
  107. uploader.init();
  108. uploader2.init();
  109. //日历控件
  110. laydate.render({
  111. elem: '#CountDown', //指定元素
  112. type: 'datetime',
  113. // format: 'y/M/d',
  114. // format: 'd/M/y',
  115. });
  116. GetProductInfo();
  117. }
  118. //获取产品信息
  119. var GetProductInfo = function () {
  120. var pid = GetUrlParam("pid");
  121. ProductId = pid;
  122. var parameter = pid ? urlEncode(["SelectProduct", pid]) : urlEncode(["SelectProductNumber"]);
  123. $.ajax({
  124. type: "post",
  125. url: "/malai/php/index.php",
  126. data: {mode: parameter},
  127. dataType: "json",
  128. success: function (data) {
  129. if (!pid) {
  130. // if (data[0].number == null) {
  131. // data[0].number += 1;
  132. // }
  133. // $("#ProductRemarks").val(data[0].number);
  134. PageType = true;
  135. }
  136. else {
  137. PrintProductInfo(data);
  138. PageType = false;
  139. }
  140. }
  141. });
  142. }
  143. //打印产品信息
  144. var PrintProductInfo = function (data) {
  145. var input = $('input[type="text"]'), id, value = data[0][0];
  146. for (var i = 0; i < input.length; i++) {
  147. id = input.eq(i).attr("id");
  148. input.eq(i).attr("value", value[id]);
  149. }
  150. $("#ProductIntroduce").html(value.ProductIntroduce);
  151. // $("#ueditor_0").contents().find("body").html($("#ueditor_0").contents().find("body").html() + value.ProductInfo);
  152. //$("#ueditor_0").contents().find("body").html(value.ProductInfo);
  153. $("#ueditor_0").contents().find("body").html(unescape(value.ProductInfo));
  154. var banner = JSON.parse(value.ProductAttr)["7b824b9c-738b-11e8-a479-0610c52349b6"],
  155. color = JSON.parse(value.ProductAttr)["69d4e108-738a-11e8-a479-0610c52349b6"],
  156. size = JSON.parse(value.ProductAttr)['d6d6361b-acea-11e8-bbc6-0a4b7569e232'],
  157. classs = JSON.parse(value.ProductAttr)["cfb738c4-bff6-11e8-bbc6-0a4b7569e232"],
  158. comment = JSON.parse(value.ProductAttr)["d5cca263-c224-11e8-bbc6-0a4b7569e232"];
  159. for (var i in banner) {
  160. for (var j in banner[i]) {
  161. var id = NewGuid(),
  162. li = "<li id='" + id + "'></li>";
  163. $("#ul_pics").append(li);
  164. $("#" + id).html(CreateBanner(banner[i][j], "", id));
  165. // BannerJson[id] = banner[i][j];
  166. }
  167. }
  168. for (var i in color) {
  169. for (var j in color[i]) {
  170. var id = NewGuid(),
  171. li = "<li id='" + id + "'></li>";
  172. $("#ul_pics2").append(li);
  173. $("#" + id).html(CreateColor(color[i][j], j, id));
  174. // ColoeJson[id] = color[i][j];
  175. }
  176. }
  177. for (var i in size) {
  178. for (var j in size[i]) {
  179. AddSize(false, size[i][j]);
  180. }
  181. }
  182. $.each(classs, function (i, val) {
  183. AddClass(false, i, val);
  184. })
  185. for (var i in comment) {
  186. CreateComment(false, comment[i]);
  187. }
  188. $(".Submission").html("修改产品信息");
  189. }
  190. //创建banner、打印banner
  191. var CreateBanner = function (src, name, id) {
  192. if (src.split("/malai").length > 1) {
  193. src = src;
  194. }
  195. else {
  196. src = "/malai/" + src;
  197. }
  198. var str = "<div class='img'><img src='" + src + "'/></div><p>" + name + "</p><span class='span' onclick='DeleteBanner(\"" + id + "\")'>删除</span>";
  199. return str;
  200. }
  201. //删除banner图
  202. var DeleteBanner = function (el) {
  203. var flag = confirm("是否确定删除banner图?");
  204. if (flag) {
  205. var id = el.id;
  206. // delete BannerJson[id];
  207. $("#" + el).remove();
  208. }
  209. }
  210. //创建颜色分类、打印颜色分类
  211. var CreateColor = function (src, name, id) {
  212. if (src.split("/malai").length > 1) {
  213. src = src;
  214. }
  215. else {
  216. src = "/malai/" + src;
  217. }
  218. name == "" ? "" : name;
  219. var str = "<div class='img'><img src='" + src + "'/></div><input type='' value='" + name + "' class='input' placeholder='请输入颜色分类或其他说明'><span class='span' onclick='DeleteColor(\"" + id + "\")'>删除</span>";
  220. return str;
  221. }
  222. //删除颜色分类图片
  223. var DeleteColor = function (el) {
  224. var flag = confirm("是否确定删除颜色分类?");
  225. if (flag) {
  226. var id = el.id;
  227. delete ColoeJson[id];
  228. $("#" + el).remove();
  229. }
  230. }
  231. //添加尺寸
  232. var AddSize = function (flag, size) {
  233. if (flag) {
  234. var li = '<li><input type="" value="" class="input" placeholder="请输入尺寸"><span class="span" onclick="DeleteSize()">删除</span></li>';
  235. }
  236. else {
  237. var li = '<li><input type="" value="' + size + '" class="input" placeholder="请输入尺寸"><span class="span" onclick="DeleteSize()">删除</span></li>';
  238. }
  239. $("#ul_pics3").append(li);
  240. }
  241. //删除尺寸
  242. var DeleteSize = function () {
  243. var flag = confirm("是否确定删除尺寸?");
  244. if (flag) {
  245. event.target.parentNode.remove();
  246. }
  247. }
  248. //添加套餐
  249. var AddClass = function (flag, indexof, obj) {
  250. var li, ul, ul_length;
  251. li = '<li>' +
  252. '<div class="number_select">' +
  253. '<span>销售数量选择</span>' +
  254. '<select>' +
  255. '<option>0</option>' +
  256. '<option>1</option>' +
  257. '<option>2</option>' +
  258. '<option>3</option>' +
  259. '<option>4</option>' +
  260. '<option>5</option>' +
  261. '</select>' +
  262. '</div>' +
  263. '<input type="" value="" class="input" placeholder="请输入套餐说明">' +
  264. '<input type="" value="" class="input" placeholder="请输入套餐价格">' +
  265. '<span class="span" onclick="DeleteClass()">删除</span>' +
  266. '</li>';
  267. ul = $("#ul_pics4").append($(li));
  268. if (flag) {
  269. ul_length = ul.find("li").length;
  270. ul.find("select").eq(ul_length - 1).val(ul_length);
  271. if (ul_length == 1) {
  272. ul.find("input").eq(1).val($("#ProductPrice").val())
  273. }
  274. }
  275. else {
  276. $.each(obj, function (i, val) {
  277. if (i == "type") {
  278. ul.find("select").eq(indexof).val(val);
  279. }
  280. else {
  281. ul.find("li").eq(indexof).find("input").eq(0).val(val);
  282. ul.find("li").eq(indexof).find("input").eq(1).val(i);
  283. }
  284. });
  285. }
  286. }
  287. //删除套餐
  288. var DeleteClass = function () {
  289. var flag = confirm("是否确定删除套餐?");
  290. if (flag) {
  291. event.target.parentNode.remove();
  292. }
  293. }
  294. //添加评价
  295. var CreateComment = function (flag, obj) {
  296. var id = NewGuid();
  297. var li = '<li>' +
  298. '<div class="comment_header">' +
  299. '<input class="comment_username" placeholder="请输入用户名" type="text"/>' +
  300. '<span class="comment_delete" onclick="DeleteComment()">删除</span>' +
  301. '</div>' +
  302. '<div class="comment_content">' +
  303. '<textarea placeholder="请输入评价"></textarea>' +
  304. '</div>' +
  305. '<div class="comment_picture_warp">' +
  306. '<div class="comment_picture" id="' + id + '" style="cursor: pointer">' +
  307. '<img src="../images/addphoto.png" style="cursor: pointer"/>' +
  308. '</div>' +
  309. '</div>' +
  310. '<div class="comment_footer">' +
  311. '<input type="text" class="comment_footer_input" placeholder="请输入评价日期">' +
  312. '</div>' +
  313. '</li>';
  314. $(".ul_pics_comment").append(li);
  315. var el = $("#" + id)[0];
  316. FileUpload(el, function (r) {
  317. CreateCommentImg(el, r.pic, true);
  318. console.log(r);
  319. });
  320. //日历控件
  321. laydate.render({
  322. elem: $(el).parents().eq(1).find(".comment_footer_input").eq(0)[0], //指定元素
  323. type: 'datetime',
  324. format: 'd/M/y'
  325. });
  326. if (!flag) {
  327. $(el).parents().eq(1).find("input[type=text]").eq(0).val(obj['name']);
  328. $(el).parents().eq(1).find("textarea").val(obj['text']);
  329. $(el).parents().eq(1).find("input[type=text]").eq(1).val(obj['time']);
  330. var img = obj['imgurl'].split(",");
  331. img.forEach(function (val) {
  332. CreateCommentImg(el, val, false);
  333. });
  334. }
  335. }
  336. var CreateCommentImg = function (el, img, flag) {
  337. if (img == "" && !flag) {
  338. return;
  339. }
  340. img = flag ? '/malai/' + img : img;
  341. var div = '<div class="comment_picture" ><img src="' + img + '"><img src="../images/close.png" class="closeimg" onclick="CreateCommentImgDelete()"/></div>';
  342. $(el).parents().eq(0).append(div);
  343. }
  344. var CreateCommentImgDelete = function () {
  345. var flag = confirm("是否确定此图片?");
  346. if (flag) {
  347. event.target.parentNode.remove();
  348. }
  349. }
  350. //删除评价
  351. var DeleteComment = function () {
  352. var flag = confirm("是否确定删除评价?");
  353. if (flag) {
  354. event.target.parentNode.parentNode.remove();
  355. }
  356. }
  357. //上传产品
  358. var UploadNewProduct = function () {
  359. var pn = $("#ProductName").val(),
  360. pt1 = $("#ProductIntroduce").val(),
  361. pt2 = $("#ueditor_0").contents().find("body")[0].innerHTML,
  362. pr1 = $("#ProductPrice").val(),
  363. pr2 = $("#ProductPrimeCost").val(),
  364. num = $("#ProductRemarks").val(),
  365. dis = $("#Discount").val(),
  366. so = $("#Sold").val(),
  367. prom = $("#Promotion").val(),
  368. cd = $("#CountDown").val(),
  369. meal = $("#ul_pics4 li");
  370. if (pn.trim() == "") {
  371. $.Huimodalalert('请输入产品名称', 3000);
  372. }
  373. // else if (pt1.trim() == "") {
  374. // $.Huimodalalert('产品介绍', 3000);
  375. // return;
  376. // }
  377. else if (pr1.trim() == "") {
  378. $.Huimodalalert('请输入产品原价', 3000);
  379. }
  380. else if (pr2.trim() == "") {
  381. $.Huimodalalert('请输入产品售价', 3000);
  382. }
  383. else if (dis.trim() == "") {
  384. $.Huimodalalert('请输入产品折扣', 3000);
  385. }
  386. else if (so.trim() == "") {
  387. $.Huimodalalert('请输入产品已售', 3000);
  388. }
  389. else if (prom.trim() == "") {
  390. $.Huimodalalert('请输入产品促销信息', 3000);
  391. }
  392. else if (cd.trim() == "") {
  393. $.Huimodalalert('请输入产品倒计时', 3000);
  394. }
  395. // else if ($.isEmptyObject(BannerJson)) {
  396. // $.Huimodalalert('请上传banner图', 3000);
  397. // }
  398. // else if ($.isEmptyObject(ColoeJson)) {
  399. // $.Huimodalalert('请上传颜色分类图片', 3000);
  400. // }
  401. else if (pr2 == "<p><br></p>") {
  402. $.Huimodalalert('请填写详细内容', 3000);
  403. }
  404. else if (meal.length == 0) {
  405. $.Huimodalalert('请填写产品套餐', 3000);
  406. }
  407. else {
  408. //公用变量
  409. var flag,
  410. src,
  411. obj = {},
  412. colorarray = [],
  413. bannerarray = [],
  414. sizearray = [],
  415. classsarray = [],
  416. commentarray = [];
  417. /************************************************************************************banner整合(必填)************************************************************************************/
  418. var banner = $("#ul_pics li img");
  419. if (banner.length == 0) {
  420. $.Huimodalalert('请上传banner图', 3000);
  421. return;
  422. }
  423. else {
  424. for (var i = 0; i < banner.length; i++) {
  425. flag = i;
  426. src = banner.eq(i).attr("src");
  427. obj = {};
  428. obj[flag] = src;
  429. bannerarray.push(obj);
  430. }
  431. pattr["7b824b9c-738b-11e8-a479-0610c52349b6"] = bannerarray;
  432. }
  433. /************************************************************************************产品颜色分类整合(必填)*******************************************************************************/
  434. var color = $("#ul_pics2 li"),
  435. color_input = $("#ul_pics2 li input");
  436. if (color.length == 0) {
  437. $.Huimodalalert('请上传颜色分类图', 3000);
  438. return;
  439. }
  440. else {
  441. for (var i = 0; i < color_input.length; i++) {
  442. if (color_input.eq(i).val().trim() == "") {
  443. $.Huimodalalert('请输入第' + (i + 1) + '个颜色或者说明', 3000);
  444. return;
  445. }
  446. flag = color_input.eq(i).val();
  447. src = $($("#ul_pics2 li input")[i]).parent().find("img").attr("src");
  448. obj = {};
  449. obj[flag] = src;
  450. colorarray.push(obj);
  451. pattr["69d4e108-738a-11e8-a479-0610c52349b6"] = colorarray;
  452. }
  453. }
  454. /************************************************************************************尺寸整合(可填)***************************************************************************************/
  455. var size = $("#ul_pics3 .input");
  456. for (var i = 0; i < size.length; i++) {
  457. if (size.eq(i).val().trim() == "") {
  458. $.Huimodalalert('请输入尺寸', 3000);
  459. return;
  460. }
  461. flag = i;
  462. src = size.eq(i).val();
  463. obj = {};
  464. obj[flag] = src;
  465. sizearray.push(obj);
  466. }
  467. pattr["d6d6361b-acea-11e8-bbc6-0a4b7569e232"] = sizearray;
  468. /************************************************************************************套餐整合(必填)***************************************************************************************/
  469. var classs = $("#ul_pics4 li"),
  470. classs_select, classs_input;
  471. for (var i = 0; i < classs.length; i++) {
  472. obj = {};
  473. classs_select = classs.eq(i).find("select");
  474. if (classs_select == 0) {
  475. $.Huimodalalert('请选择第' + (i + 1) + "个套餐框中的销售数量", 3000);
  476. return;
  477. }
  478. obj["type"] = classs_select.eq(0).val();
  479. classs_input = classs.eq(i).find("input");
  480. for (var j = 0; j < classs_input.length; j++) {
  481. if (classs_input.eq(j).val() == "") {
  482. $.Huimodalalert('请填写第' + (i + 1) + '个套餐框中的第' + (j + 1) + '个输入框说明', 3000);
  483. return;
  484. }
  485. }
  486. flag = classs_input.eq(1).val();
  487. src = classs_input.eq(0).val();
  488. obj[flag] = src;
  489. classsarray.push(obj);
  490. }
  491. pattr['cfb738c4-bff6-11e8-bbc6-0a4b7569e232'] = classsarray;
  492. /************************************************************************************评价整合(可填)***************************************************************************************/
  493. var comment = $(".ul_pics_comment li");
  494. for (var i = 0; i < comment.length; i++) {
  495. if (comment.eq(i).find('input[type=text]').eq(0).val().trim() == "") {
  496. $.Huimodalalert('请输入第' + (i + 1) + "个评价框中的用户名", 3000);
  497. return;
  498. }
  499. if (comment.eq(i).find('textarea').val().trim() == "") {
  500. $.Huimodalalert('请输入第' + (i + 1) + "个评价框中的评价内容", 3000);
  501. return;
  502. }
  503. if (comment.eq(i).find('input[type=text]').eq(1).val().trim() == "") {
  504. $.Huimodalalert('请输入第' + (i + 1) + "个评价框中的评价时间", 3000);
  505. return;
  506. }
  507. obj = {};
  508. obj['name'] = comment.eq(i).children().eq(0).children('input').val();
  509. obj['text'] = comment.eq(i).children().eq(1).children().val();
  510. obj['time'] = comment.eq(i).find("input[type=text]").eq(1).val();
  511. flag = comment.eq(i).children().eq(2).children(".comment_picture");
  512. src = "";
  513. for (var j = 1; j < flag.length; j++) {
  514. j == 1 ? "" : src += ',';
  515. src += flag.eq(j).children("img").eq(0).attr("src");
  516. }
  517. obj['imgurl'] = src;
  518. commentarray.push(obj);
  519. }
  520. pattr["d5cca263-c224-11e8-bbc6-0a4b7569e232"] = commentarray;
  521. /************************************************************************************所有整合结束***************************************************************************************/
  522. var str = JsonToString(pattr);
  523. var parameter = PageType ? urlEncode(["UploadNewProduct", pn, pt1, escape(pt2), pr1, pr2, num, str, dis, so, prom, cd]) : urlEncode(["UpdateProductInfo", ProductId, pn, pt1, escape(pt2), pr1, pr2, str, dis, so, prom, cd]);
  524. /* $(".Submission")[0].onclick = function () {
  525. }*/
  526. $.ajax({
  527. type: "post",
  528. url: "/malai/php/index.php",
  529. data: {mode: parameter},
  530. dataType: "json",
  531. success: function (data) {
  532. if (PageType) {
  533. if (data.length == 1 && data != -1) {
  534. alert("上传成功!");
  535. window.location.href = "product.html?pid=" + data[0].ProductId;
  536. }
  537. }
  538. else {
  539. if (data.length == 1 && data != -1) {
  540. alert("修改成功!");
  541. window.location.href = "product-add.html?pid=" + data[0].ProductId;
  542. }
  543. }
  544. }
  545. });
  546. }
  547. }
  548. //文件上传接口
  549. var FileUpload = function (el, cb) {
  550. var file = new plupload.Uploader({ //创建实例的构造方法
  551. runtimes: 'html5,flash,silverlight,html4', //上传插件初始化选用那种方式的优先级顺序
  552. browse_button: el, // 上传按钮
  553. url: "upload.php", //远程上传地址
  554. flash_swf_url: 'plupload/Moxie.swf', //flash文件地址
  555. silverlight_xap_url: 'plupload/Moxie.xap', //silverlight文件地址
  556. filters: {
  557. max_file_size: '200mb', //最大上传文件大小(格式100b, 10kb, 10mb, 1gb)
  558. mime_types: [ //允许文件上传类型
  559. {
  560. title: "files",
  561. extensions: "jpg,png,gif,ico"
  562. }
  563. ]
  564. },
  565. multi_selection: true, //true:ctrl多文件上传, false 单文件上传
  566. init: {
  567. FilesAdded: function (up, files) { //文件上传前
  568. file.start();
  569. },
  570. UploadProgress: function (up, file) { //上传中,显示进度条
  571. },
  572. FileUploaded: function (up, file, info) { //文件上传成功的时候触发
  573. var data = eval("(" + info.response + ")");
  574. cb && cb(data);
  575. },
  576. Error: function (up, err) { //上传出错的时候触发
  577. // alert(err.message);
  578. alert();
  579. $.Huimodalalert('服务器错误!请联系管理员', 3000);
  580. }
  581. },
  582. });
  583. file.init();
  584. }
  585. //连接字符串
  586. var urlEncode = function (arr) {
  587. var i; //循环初始化
  588. for (i = 0; i < arr.length; i++) {
  589. arr[i] = encodeURIComponent(encodeURIComponent(arr[i]));
  590. }
  591. return arr.join();
  592. }
  593. //获取网址
  594. var GetUrlParam = function (name) {
  595. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  596. var r = window.location.search.substr(1).match(reg); //匹配目标参数
  597. if (r != null) return unescape(r[2]);
  598. return null; //返回参数值
  599. }
  600. //json转换为string
  601. var JsonToString = function (obj) {
  602. var i,
  603. _type = false,
  604. _str = "",
  605. _type = typeof (obj); //获取类型
  606. //开始序列化
  607. if (obj == null) {
  608. }
  609. else {
  610. obj = obj.valueOf(); //获取值 同时类型也为对应
  611. if ("boolean,number".indexOf(_type) > -1) {//bool和number类型转化
  612. _str = obj.toString();
  613. }
  614. else if (_type == "string") {//json处理字符串时需要加上 "\""符号
  615. _str = "\"" + obj + "\"";
  616. }
  617. else if (typeof obj == "array") { //数组处理
  618. _str = "[";
  619. for (i = 0; i < obj.length; i++) {
  620. _str += JsonToString(obj[i]) + ((i < obj.length - 1) ? "," : "")
  621. }
  622. ;_str += "]";
  623. return _str
  624. }
  625. else {//object处理
  626. _str = "{";
  627. for (i in obj) {
  628. if (obj.hasOwnProperty(i)) {
  629. if (_str.length > 1) {
  630. _str += ","
  631. }
  632. ;_str += "\"" + i + "\":" + JsonToString(obj[i]);
  633. }
  634. }
  635. _str += "}";
  636. }
  637. return _str;
  638. }
  639. }
  640. //生成GUID
  641. var NewGuid = function () {
  642. var _UN, i, _UGID = "";
  643. for (i = 0; i < 32; i++) {
  644. _UGID += Math.floor(Math.random() * 16).toString(16); //随机0 - 16 的数字 转变为16进制的字符串
  645. _UN = Math.floor((i - 7) / 4); //计算 (i-7)除4
  646. if (_UN > -1 && _UN < 4 && (i == (7 + 4 * _UN))) { //会使guid中间加 "-" 形式为xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  647. _UGID += "-";
  648. }
  649. }
  650. return _UGID;
  651. }