clearing.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. var userIntegral = 0; // 用户可用积分
  2. var meonyProportion = 0; // 兑换比例:1个积分=?分钱
  3. var payTotal = 0.0; // 支付总金额
  4. var userInputIntegral = 0; // 抵扣积分
  5. var isPay = true; // 是否在请求支付,false表示正在请求,true可以请求
  6. var cartIdStr = ""; // 购物车ID
  7. var usePayMoney = 0;
  8. var ipt_balance = 0;
  9. var yhq_val = 0;
  10. var used_balance = 0;
  11. var yhq_type = 1;
  12. mui.ready(function() {
  13. mui.ajax('/iamberry/wechat/order/payBefore', {
  14. dataType: 'json',
  15. type: 'POST',
  16. timeout: 15000,
  17. success: function(dt) {
  18. if(dt.status == true) {
  19. // 购物项遍历
  20. //console.log(dt)
  21. var liNote = "";
  22. if(dt.data.balance == 0) {
  23. $(".pay_ipt_box").hide();
  24. } else {
  25. used_balance = accDiv(dt.data.balance, 100);
  26. $('#used_balance').html("¥ " + accDiv(dt.data.balance, 100));
  27. }
  28. var yhq_str = "";
  29. if(dt.data.couponItems.length == 0) {
  30. $(".forward").hide();
  31. } else {
  32. $("#yhq_count").html(dt.data.couponItems.length);
  33. mui(dt.data.couponItems).each(function(index) {
  34. yhq_str += '<div class="mui-input-row mui-radio"><label>' + this.couponName + '</label><input name="radio1" value="' + accDiv(this.couponReduce, 100) + '" type="radio" id="' + this.couponItemId + '" yhq_type="' + this.couponType + '"></div>';
  35. });
  36. $(".my-popover").html(yhq_str + '<div class="mui-input-row mui-radio"><label>不使用优惠券</label><input name="radio1" value="1" type="radio"></div>');
  37. }
  38. for(var i = 0; i < dt.data.cartItems.length; i++) {
  39. var total = dt.data.cartItems[i].cartNum * dt.data.cartItems[i].productPrice / 100; // 购物车小计,分-->元
  40. var pro_color = switchcolor(dt.data.cartItems[i].productColor)
  41. total = total.toFixed(2); // 保留两位小数
  42. var productId = dt.data.cartItems[i].cartProductId;
  43. var price = dt.data.cartItems[i].productPrice;
  44. var num = dt.data.cartItems[i].cartNum;
  45. liNote = liNote +
  46. "<li class='mui-table-view-cell mui-media'>" +
  47. "<a>" +
  48. "<span class='mui-media-object mui-pull-left wh-100'> <img src='" + dt.data.cartItems[i].productIntroduceImg + "'></span>" +
  49. "<div class='mui-media-body'>" +
  50. "<div class='wt-space gwc_tit'>" + dt.data.cartItems[i].productName + "<span class='price_sp cl-gwc mui-pull-right' id='product-" + productId + "' price='" + price + "'>¥" + total + "</span></div>" +
  51. "<small class='gwc_pp'>颜色:" + pro_color + "</small>" +
  52. "<div class='mui-numbox'><button class='mui-btn mui-btn-numbox-minus' onclick='shoppSubEvent(" + productId + ")' type='button'>-</button>" +
  53. "<input class='mui-input-numbox' readonly='readonly' type='tel' id='shoppNum-" + productId + "' value='" + num + "'/>" +
  54. // "<span class='price cl-6'>x" + num + "</span>" +
  55. "<button class='mui-btn mui-btn-numbox-plus' onclick='shoppAddEvent(" + productId + ")' type='button'>+</button></div>" +
  56. "</div>" +
  57. "</a>" +
  58. "</li>";
  59. }
  60. var $productListUl = $("#products-list-ul");
  61. $productListUl.empty();
  62. $productListUl.append(liNote);
  63. dt.data.userIntegral = dt.data.userIntegral - 1;
  64. // 保存数据
  65. meonyProportion = dt.data.meonyProportion; // 兑换比例
  66. payTotal = dt.data.payTotal; // 支付金额,单位分
  67. userIntegral = dt.data.userIntegral; // 用户可用积分
  68. cartIdStr = dt.data.cartId; // 购物车ID
  69. // 替换数据
  70. usePayMoney = accDiv(dt.data.payTotal, 100);
  71. $("#user-integral-span").html(dt.data.userIntegral);
  72. $("#user-pay-money-span").html(usePayMoney + " 元");
  73. //默认自己拉出地址
  74. if(dt.data.addressInfo != undefined && dt.data.addressInfo != null) {
  75. $("#user-address-div").empty().append(
  76. "收货人:" + dt.data.addressInfo.salesAddressName + "<span class='ml-5'>" + dt.data.addressInfo.salesAddressTel + "</span>" +
  77. "<a>" +
  78. "<p class='shipping_address wt-space'>" + dt.data.addressInfo.salesAddressInfo + "<span class='mui-icon mui-icon-arrowright icon-r-c'></span></p>" +
  79. "</a>");
  80. $("#user-address-div").show();
  81. $("#open-wechat-addres-a").hide();
  82. //给微信参数赋值
  83. wechatAddr.status = true;
  84. wechatAddr.detailInfo = dt.data.addressInfo.salesAddressInfo;
  85. wechatAddr.userName = dt.data.addressInfo.salesAddressName;
  86. wechatAddr.userTel = dt.data.addressInfo.salesAddressTel;
  87. wechatAddr.postalCode = dt.data.addressInfo.salesAddressPostnum;
  88. }
  89. } else {
  90. mui.alert(dt.message);
  91. }
  92. },
  93. error: function(xhr, type, errorThrown) {
  94. errorfn(xhr);
  95. }
  96. });
  97. // 获取微信共享地址
  98. mui('body').on('tap', '#open-wechat-addres-a,#user-address-div', function() {
  99. editAddrV1(
  100. function(res) {
  101. // 用户成功拉出地址
  102. var address = res.provinceName + "-" + res.cityName + "-" + res.countryName + "-" + res.detailInfo;
  103. wechatAddr.status = true;
  104. wechatAddr.detailInfo = address;
  105. wechatAddr.postalCode = res.postalCode;
  106. wechatAddr.userName = res.userName;
  107. wechatAddr.userTel = res.telNumber;
  108. wechatAddr.nationalCode = res.nationalCode;
  109. $("#user-address-div").empty().append(
  110. "收货人:" + wechatAddr.userName + "<span class='ml-5'>" + wechatAddr.userTel + "</span>" +
  111. "<a>" +
  112. "<p class='shipping_address wt-space'>" + wechatAddr.detailInfo + "<span class='mui-icon mui-icon-arrowright icon-r-c'></span></p>" +
  113. "</a>");
  114. $("#user-address-div").show();
  115. $("#open-wechat-addres-a").hide();
  116. },
  117. function() {
  118. if($('#user-address-div').html() != null && $('#user-address-div').html() != '') {
  119. wechatAddr.status = true;
  120. } else {
  121. wechatAddr.status = false;
  122. // 用户取消拉出地址
  123. mui.alert("您没有选择收货地址!");
  124. }
  125. }
  126. );
  127. });
  128. // 积分数量改变时
  129. mui('body').on('change', '#input-user-integral', function() {
  130. var inputUserIntegral = $("#input-user-integral").val();
  131. $.trim(inputUserIntegral);
  132. var r = /^[1-9]+\d*$/;
  133. if(!r.test(inputUserIntegral)) {
  134. $("#input-user-integral").val(userInputIntegral);
  135. return false;
  136. }
  137. inputUserIntegral = parseInt(inputUserIntegral);
  138. if(inputUserIntegral > userIntegral) {
  139. $("#input-user-integral").val(userInputIntegral);
  140. return false;
  141. }
  142. if((inputUserIntegral * meonyProportion) > payTotal) {
  143. $("#input-user-integral").val(userInputIntegral);
  144. return false;
  145. }
  146. // 置换数据
  147. userInputIntegral = inputUserIntegral;
  148. var temp = (userInputIntegral * meonyProportion / 100).toFixed(2);
  149. $("#integral-money-span").html(temp);
  150. temp = ((payTotal - (userInputIntegral * meonyProportion)) / 100).toFixed(2);
  151. $("#user-pay-money-span").html(temp);
  152. });
  153. // 去支付
  154. mui('body').on('tap', '.btn-weixin-pay', function() {
  155. var yhq_id = $("input[name=radio1]:checked").attr('id');
  156. if(yhq_id == undefined) {
  157. yhq_id = "";
  158. }
  159. var remark = $("#textarea").val();
  160. var pattern = new RegExp("[ `~!@#$^&*()=|{}':;'\\[\\]<>/?~!@#¥……&*()——|{}【】‘;:”“'、?]")
  161. if(remark != "") {
  162. $.trim(remark);
  163. if(pattern.test(remark)) {
  164. mui.alert("请不要输入中文、字母、数字以外的特殊字符!");
  165. return;
  166. }
  167. if(!(remark.length >= 0 && remark.length <= 100)) {
  168. mui.alert("备注字数长度在0-100之间!");
  169. return;
  170. }
  171. }
  172. if(wechatAddr.status) {
  173. if(isPay) {
  174. isPay = false; // 锁定请求按钮
  175. layer.open({
  176. type: 2,
  177. shadeClose: false,
  178. content: "正在请求加载订单信息..."
  179. }); // 调用加载弹出层
  180. /* 组装数据 */
  181. var dataArgs = {
  182. "orderAddressInfo": wechatAddr.detailInfo,
  183. "orderAddressPostNum": wechatAddr.postalCode,
  184. "orderAddressMoblie": wechatAddr.userTel,
  185. "orderAddressName": wechatAddr.userName,
  186. "cartIdStr": cartIdStr,
  187. "orderIntegralNum": userInputIntegral,
  188. "orderUserRemark": remark,
  189. "couponId": yhq_id,
  190. "balancePayAmount": accMul(ipt_balance, 100)
  191. };
  192. mui.ajax('/iamberry/wechat/order/pay', { // 提交订单,准备支付
  193. data: dataArgs,
  194. dataType: 'json',
  195. type: 'POST',
  196. timeout: 15000,
  197. success: function(dt) {
  198. layer.closeAll();
  199. //mui.alert(dt.resultCode=="500");
  200. if(dt.resultCode == "500") {
  201. mui.alert(dt.message);
  202. isPay = true;
  203. return;
  204. };
  205. if(dt.data.info == "isOK") {
  206. mui.openWindow({
  207. url: domanUrl + "/iamberry/wechat/order/paySuccess?orderId=" + dt.data.orderId + "&dates=" + new Date().getTime()
  208. });
  209. return;
  210. };
  211. if(dt.status) {
  212. var str = dt.data.info;
  213. var obj = JSON.parse("{" + str + "}");
  214. WeixinJSBridge.invoke('getBrandWCPayRequest', obj, function(res) {
  215. if(res.err_msg == 'get_brand_wcpay_request:ok') {
  216. var orderIds = {
  217. "orderId": dt.data.orderId
  218. };
  219. layer.open({
  220. type: 2,
  221. shadeClose: false,
  222. content: "正在请求加载订单信息..."
  223. }); // 调用加载弹出层
  224. var url = "/iamberry/wechat/order/checkOrderPaySuccess";
  225. $.getJSON(url, {
  226. "s": new Date().getTime(),
  227. "orderId": dt.data.orderId
  228. }, function(data) {
  229. if(data.status == true) { // 支付成功
  230. window.location.href = "/iamberry/wechat/order/paySuccess?orderId=" + orderIds.orderId + "&dates=" + new Date().getTime();
  231. } else {
  232. setTimeout("checkOrderStatus('" + dt.data.orderId + "')", 1000);
  233. }
  234. });
  235. } else {
  236. isPay = true;
  237. // mui.alert("对不起,支付失败!请稍后重试!" + dt.data.orderId + "支付失败!");
  238. window.location.href = "/iamberry/pay/goOrderInfo?orderId=" + dt.data.orderId + "&dates=" + new Date().getTime(); // 前往订单详情
  239. }
  240. });
  241. } else {
  242. isPay = true;
  243. mui.alert(dt.message);
  244. window.location.href = "/iamberry/pay/goOrderInfo?orderId=" + dt.data.orderId + "&dates=" + new Date().getTime(); // 前往订单详情
  245. }
  246. },
  247. error: function(xhr, type, errorThrown) {
  248. isPay = true;
  249. layer.closeAll();
  250. mui.alert("您的网络异常,请刷新重试!");
  251. return;
  252. }
  253. });
  254. } else {
  255. mui.alert("正在请求支付中~~请稍后!");
  256. return;
  257. }
  258. } else {
  259. mui.alert("请选择收货地址!");
  260. return;
  261. }
  262. });
  263. mui('body').on('tap', '.mui-popover-action li>a', function() {
  264. var a = this,
  265. parent;
  266. //根据点击按钮,反推当前是哪个actionsheet
  267. for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
  268. if(parent.classList.contains('mui-popover-action')) {
  269. break;
  270. }
  271. }
  272. //关闭actionsheet
  273. mui('#' + parent.id).popover('toggle');
  274. yhq_val = $("input[name=radio1]:checked").val();
  275. yhq_type = $("input[name=radio1]:checked").attr("yhq_type");
  276. if(yhq_val == undefined) {
  277. yhq_val = 0
  278. } else {
  279. if(yhq_type == 1) {
  280. if(yhq_val > (usePayMoney - ipt_balance)) {
  281. $("input[name=radio1]:checked").removeAttr("checked");
  282. yhq_val = 0;
  283. mui.alert(' ', '当前优惠券不可用', function() {});
  284. }
  285. $("#yhq_price").html(yhq_val);
  286. $("#accSub,#accSub>div:first-child").show();
  287. $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
  288. } else {
  289. yhq_val = accSub(usePayMoney, Math.floor(accMul($("input[name=radio1]:checked").val(), usePayMoney) * 100) / 100);
  290. $("#yhq_price").html(yhq_val);
  291. $("#accSub,#accSub>div:first-child").show();
  292. $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2));
  293. }
  294. }
  295. });
  296. mui.os.ios ? listener_ipt() : keyup_fn();
  297. var reg = /^(([1-9]\d*))$/;
  298. function keyup_fn() {
  299. $('body').on('blur', '#pay_ipt', function(e) {
  300. ipt_balance = $(this).val();
  301. if(reg.test(ipt_balance)) {
  302. if(ipt_balance > (usePayMoney - yhq_val)) {
  303. ipt_balance = (usePayMoney - yhq_val).toFixed(2);
  304. if(ipt_balance < 0) {
  305. ipt_balance = 0;
  306. $(this).val("");
  307. } else {
  308. $(this).val((usePayMoney - yhq_val).toFixed(2));
  309. }
  310. }
  311. if(ipt_balance > used_balance) {
  312. ipt_balance = used_balance;
  313. $(this).val(used_balance);
  314. }
  315. $('#used_buy').html(ipt_balance);
  316. var money = (usePayMoney - yhq_val).toFixed(2);
  317. money <= 0 ? money = 0 : money;
  318. $("#accSub,#accSub>div:last-child").show();
  319. $("#user-pay-money-span").html((money - ipt_balance).toFixed(2) + " 元");
  320. } else {
  321. mui.alert(' ', '您输入的数字有误,请重新输入', function() {
  322. $(this).val("");
  323. ipt_balance = 0;
  324. $('#used_buy').html(ipt_balance);
  325. $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2) + " 元");
  326. });
  327. }
  328. });
  329. }
  330. function listener_ipt() {
  331. document.getElementById('pay_ipt').onblur = function() {
  332. var th = this;
  333. ipt_balance = th.value;
  334. if(reg.test(ipt_balance)) {
  335. if(ipt_balance > (usePayMoney - yhq_val)) {
  336. ipt_balance = (usePayMoney - yhq_val).toFixed(2);
  337. if(ipt_balance < 0) {
  338. ipt_balance = 0;
  339. th.value = "";
  340. } else {
  341. th.value = (usePayMoney - yhq_val).toFixed(2);
  342. }
  343. }
  344. if(ipt_balance > used_balance) {
  345. th.value = used_balance;
  346. ipt_balance = used_balance;
  347. }
  348. $('#used_buy').html(ipt_balance);
  349. var money = (usePayMoney - yhq_val).toFixed(2);
  350. money <= 0 ? money = 0 : money;
  351. $("#accSub,#accSub>div:last-child").show();
  352. $("#user-pay-money-span").html((money - ipt_balance).toFixed(2) + " 元");
  353. } else {
  354. mui.alert(' ', '您输入的数字有误<br>请重新输入', function() {
  355. th.value = "";
  356. ipt_balance = 0;
  357. $('#used_buy').html(ipt_balance);
  358. $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2) + " 元");
  359. });
  360. }
  361. }
  362. }
  363. document.querySelector('.loading-bg').style.display = 'none';
  364. });
  365. // 校验订单状态
  366. function checkOrderStatus(orderId) {
  367. var url = "/iamberry/wechat/order/checkOrderPaySuccess";
  368. var order = {
  369. "s": new Date().getTime(),
  370. "orderId": orderId
  371. };
  372. $.getJSON(url, order, function(data) {
  373. if(data.status == true) { // 支付成功
  374. window.location.href = "/iamberry/wechat/order/paySuccess?orderId=" + orderId + "&dates=" + new Date().getTime();
  375. } else {
  376. window.location.href = '/iamberry/pay/goOrderInfo?orderId=' + orderId + "&dates=" + new Date().getTime();;
  377. }
  378. });
  379. }
  380. // 点击加号的事件
  381. function shoppAddEvent(productId) {
  382. mui.ajax('/iamberry/wechat/cart/addCart', {
  383. data: {
  384. "productId": productId,
  385. "carNum": 1
  386. },
  387. dataType: 'json',
  388. type: 'GET',
  389. timeout: 15000,
  390. success: function(dt) {
  391. if(dt.status == true) {
  392. // 销售价
  393. var productObj = $("#product-" + productId);
  394. var price = parseInt(productObj.attr("price"));
  395. // total
  396. var total = payTotal + price;
  397. payTotal = total;
  398. // 修改数量
  399. var shoppNumObj = $("#shoppNum-" + productId);
  400. var num = parseInt(shoppNumObj.val());
  401. shoppNumObj.val(num + 1);
  402. /* $("#user-pay-money-span").html((payTotal / 100).toFixed(2)); */
  403. yhq_val = $("input[name=radio1]:checked").val();
  404. yhq_type = $("input[name=radio1]:checked").attr("yhq_type");
  405. usePayMoney = accDiv(payTotal, 100);
  406. if(yhq_val == undefined) {
  407. yhq_val = 0
  408. }
  409. if(yhq_type == 2) {
  410. yhq_val = accSub(usePayMoney, (Math.floor(accMul(yhq_val, usePayMoney) * 100) / 100));
  411. $("#yhq_price").html(accSub(usePayMoney, Math.floor(accMul($("input[name=radio1]:checked").val(), usePayMoney) * 100) / 100));
  412. }
  413. $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2) + " 元");
  414. // 修改小计
  415. productObj.html("¥" + ((num + 1) * price / 100).toFixed(2));
  416. } else {
  417. // 失败之后的事件
  418. mui.alert(dt.message);
  419. }
  420. },
  421. error: function(xhr, type, errorThrown) {}
  422. });
  423. }
  424. // 购物车减
  425. function shoppSubEvent(productId) {
  426. var shoppNumObj = $("#shoppNum-" + productId);
  427. var num = parseInt(shoppNumObj.val());
  428. if(num <= 1) {
  429. mui.alert("不能继续减少了哦!");
  430. return;
  431. }
  432. mui.ajax('/iamberry/wechat/cart/deleteCart', {
  433. data: {
  434. "productId": productId,
  435. "cartNum": 1
  436. },
  437. dataType: 'json',
  438. type: 'GET',
  439. timeout: 15000,
  440. success: function(dt) {
  441. if(dt.status == true) {
  442. // 销售价
  443. var productObj = $("#product-" + productId);
  444. var price = parseInt(productObj.attr("price"));
  445. // total
  446. var total = payTotal - price;
  447. payTotal = total;
  448. // 修改数量
  449. var shoppNumObj = $("#shoppNum-" + productId);
  450. var num = parseInt(shoppNumObj.val());
  451. shoppNumObj.val(num - 1);
  452. /*$("#user-pay-money-span").html((payTotal / 100).toFixed(2));*/
  453. yhq_val = $("input[name=radio1]:checked").val();
  454. yhq_type = $("input[name=radio1]:checked").attr("yhq_type");
  455. usePayMoney = accDiv(payTotal, 100);
  456. if(yhq_val == undefined) {
  457. yhq_val = 0
  458. }
  459. if(yhq_type == 2) {
  460. yhq_val = accSub(usePayMoney, Math.floor(accMul(yhq_val, usePayMoney) * 100) / 100);
  461. $("#yhq_price").html(accSub(usePayMoney, Math.floor(accMul($("input[name=radio1]:checked").val(), usePayMoney) * 100) / 100));
  462. if(ipt_balance > (usePayMoney - yhq_val)) {
  463. if((usePayMoney - yhq_val) <= 0) {
  464. ipt_balance = 0;
  465. $('#used_buy').html(ipt_balance);
  466. $('#pay_ipt').val("");
  467. } else {
  468. ipt_balance = (usePayMoney - yhq_val).toFixed(2);
  469. $('#used_buy').html(ipt_balance);
  470. $('#pay_ipt').val(ipt_balance);
  471. }
  472. }
  473. $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2) + " 元");
  474. } else {
  475. if((usePayMoney - yhq_val) < ipt_balance) {
  476. if((usePayMoney - yhq_val) <= 0) {
  477. ipt_balance = 0;
  478. $('#used_buy').html(ipt_balance);
  479. $('#pay_ipt').val("");
  480. } else {
  481. ipt_balance = (usePayMoney - yhq_val).toFixed(2);
  482. $('#used_buy').html(ipt_balance);
  483. $('#pay_ipt').val(ipt_balance);
  484. }
  485. }
  486. if(yhq_val > (usePayMoney - ipt_balance)) {
  487. mui.alert(' ', '当前优惠券不可用', function() {
  488. $("input[name=radio1]:checked").removeAttr("checked");
  489. yhq_val = 0;
  490. $("#yhq_price").html(yhq_val);
  491. $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2) + " 元");
  492. });
  493. }
  494. $("#user-pay-money-span").html((usePayMoney - yhq_val - ipt_balance).toFixed(2) + " 元");
  495. }
  496. productObj.html("¥" + ((num - 1) * price / 100).toFixed(2));
  497. } else {
  498. mui.alert(dt.message);
  499. }
  500. },
  501. error: function(xhr, type, errorThrown) {
  502. errorfn(xhr);
  503. }
  504. });
  505. }