userOrderDetail.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. var id=mobile.getUrlVars("id");
  2. var order;
  3. var money = 0;
  4. var product;
  5. var orderDate=[];
  6. var addrlist=[];
  7. mui.init();
  8. mui.ready(function(){
  9. getById();
  10. $("#product").on("tap","li",function(e){
  11. location.href=base+"/mobile/productDetail.jsp?id="+$(this).attr("data-id");
  12. });
  13. $("#pay").on("tap",".mui-input-row",function(e){
  14. var list = $(this).find("input[type='radio']");
  15. list.each(function(){
  16. this.checked = true;
  17. });
  18. });
  19. $("#pay-btn").on("tap",function(e){
  20. var i=-1;
  21. $('input[type="radio"]').each(function(){
  22. if(this.checked){
  23. i = $(this).parent().index();
  24. }
  25. });
  26. if(i==1){
  27. if(parseFloat(money)<parseFloat(order.order_amt)){
  28. layer.open({content: "余额不足",time: 3});
  29. return;
  30. }
  31. mobile.ajaxPost(base+'/userOrder/moneyPay',{id:id},function(json){
  32. if(json.success){
  33. layer.open({content: '支付成功',time: 3});
  34. setTimeout(function() {
  35. window.location.reload();
  36. }, 1000);
  37. }else{
  38. layer.open({content: json.msg,time: 3});
  39. }
  40. });
  41. }else{
  42. var addressId = $('#addr').find('li').attr('data-id');
  43. if(!addressId&&product.is_delivery=="1"){
  44. layer.open({content:'请选择收货地址',time:3});
  45. return;
  46. }
  47. mobile.ajaxPost(base+'/userOrder/pay',{id:id,addressId:addressId},function(json){
  48. if(json.success){
  49. var data=json.data;
  50. if (typeof WeixinJSBridge == "undefined"){
  51. if( document.addEventListener ){
  52. document.addEventListener('WeixinJSBridgeReady', onBridgeReady(data), false);
  53. }else if (document.attachEvent){
  54. document.attachEvent('WeixinJSBridgeReady', onBridgeReady(data));
  55. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(data));
  56. }
  57. }else{
  58. onBridgeReady(data);
  59. }
  60. }else{
  61. layer.open({content: json.msg,time: 3});
  62. }
  63. });
  64. }
  65. });
  66. $("#use-btn").on("tap",function(e){
  67. if($("#password").val()==""){
  68. layer.open({content:'请输入核销密码',time:3});
  69. return;
  70. }
  71. mobile.ajaxPost(base+'/userOrder/useOrder',{id:id,password:$("#password").val(),useNum:$('.mui-input-numbox').val()},function(json){
  72. if(json.success){
  73. layer.open({content:'核销成功',time:1});
  74. setTimeout(function() {
  75. location.reload();
  76. }, 1000);
  77. }else{
  78. layer.open({content: json.msg,time: 3});
  79. }
  80. });
  81. });
  82. $("#order-btn").on("tap",function(e){
  83. if($("#order_date").val()==""){
  84. layer.open({content:'请选择预约日期',time:3});
  85. return;
  86. }
  87. mobile.ajaxPost(base+'/userOrder/orderDate',{id:id,productId:product.id,orderDate:$("#order_date").val()},function(json){
  88. if(json.success){
  89. layer.open({content: "预约成功,请在"+$("#order_date").val()+"当天使用",time: 2});
  90. setTimeout(function() {
  91. location.reload();
  92. }, 2000);
  93. }else{
  94. layer.open({content: json.msg,time: 3});
  95. }
  96. });
  97. });
  98. $("#comment .icon-chucuo1").on("click",function(){
  99. $("#comment").remove();
  100. });
  101. $("#comment .main").on("tap",function(){
  102. location.href=base+"/mobile/index.jsp";
  103. });
  104. $("#comment .comment").on("tap",function(){
  105. location.href=base+"/mobile/userOrderComment.jsp?id="+order.id;
  106. });
  107. $("#reorder-btn").on("tap",function(e){
  108. var html=[];
  109. html.push('<select id="orderDate" style="border: 1px solid rgba(0, 0, 0, .2)!important;">');
  110. $.each(orderDate, function (id,item) {
  111. html.push('<option value="'+item+'">'+item+'</option>');
  112. });
  113. html.push('</select>');
  114. mui.alert(html.join(""), '选择预约日期', function() {
  115. if($("#orderDate").val()==""){
  116. layer.open({content:'请选择预约日期',time:3});
  117. return;
  118. }
  119. mobile.ajaxPost(base+'/userOrder/orderDate',{id:id,productId:product.id,orderDate:$("#orderDate").val()},function(json){
  120. if(json.success){
  121. layer.open({content: "预约成功,请在"+$("#orderDate").val()+"当天使用",time: 2});
  122. setTimeout(function() {
  123. location.reload();
  124. }, 2000);
  125. }else{
  126. layer.open({content: json.msg,time: 3});
  127. }
  128. });
  129. });
  130. });
  131. $("#addr").on("tap",function(){
  132. $('#addrList').addClass('mui-active');
  133. });
  134. $("#addrList").on("tap",'.mui-icon-left-nav',function(){
  135. $('#addrList').removeClass('mui-active');
  136. });
  137. $("#addrList").on("tap",'.mui-btn-link',function(){
  138. location.href=base+'/mobile/userAddressAdd.jsp';
  139. });
  140. });
  141. function getById(){
  142. mobile.ajaxPost(base+'/userOrder/getById',{id:id},function(json){
  143. order = json.data;
  144. product = order.list[0];
  145. if (product.is_delivery=="1") {
  146. getAddressList();
  147. $("#addr").show();
  148. }
  149. var html= [];
  150. $.each(order.list,function(id,item){
  151. html.push('<li class="mui-table-view-cell mui-media" data-id="'+item.id+'">');
  152. html.push('<div class="product-img mui-pull-left">');
  153. html.push('<img src="'+imgUrl+item.product_main_img+'">');
  154. if(item.order_hour<=0){
  155. html.push('<span class="img-icon">免预约</span>')
  156. }
  157. html.push('</div>');
  158. html.push('<div class="mui-media-body pl10">');
  159. html.push('<p class="product-merchant mui-ellipsis">'+item.merchant_name+'</p>');
  160. html.push('<p class="product-title mui-ellipsis-2">'+item.product_name+'</p>');
  161. html.push('<p class="product-price items">');
  162. html.push('<em class="item"><b>¥</b>'+item.product_price+'<span class="del-line color3 ml5">'+item.product_old_price+'元</span></em>');
  163. html.push('<span class="color2 fs14">x'+item.product_qty+'</span></p>');
  164. html.push('</div>');
  165. html.push('</li>');
  166. });
  167. if(order.order_status=="10"){
  168. $('#pay_amt').prev().html('需付款');
  169. $('#title').html('待付款');
  170. $('#pay_amt').html('<em class="fs16"><b>¥</b>'+order.order_amt+'</em>');
  171. $("#pay").removeClass("mui-hidden");
  172. var interval= setInterval(function() {
  173. var leftTime = (new Date(order.create_time.replace(/-/g, "/"))) - (new Date()) + 5*60*1000;
  174. var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数
  175. var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
  176. var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
  177. var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
  178. hours = checkTime(hours);
  179. minutes = checkTime(minutes);
  180. seconds = checkTime(seconds);
  181. if(leftTime<0){
  182. $(".left-time").html("订单失效,请重新下单");
  183. location.href=base+"/mobile/productDetail.jsp?id="+order.list[0].id;
  184. }else{
  185. $(".left-time").html("请在" + minutes+"分"+seconds+"秒内付款,否则订单会失效");
  186. }
  187. }, 1000);
  188. getMoney();
  189. }else if(order.order_status=="20"){
  190. if (product.order_num&&!order.order_date){
  191. getOrderDate();
  192. $("#order").removeClass("mui-hidden");
  193. $('#pay_amt').prev().html('已付款');
  194. $('#title').html('待预约');
  195. $('#pay_amt').html('<em class="fs16"><b>¥</b>'+mobile.nullToZore(order.pay_amt)+'</em>');
  196. if (product.product_type!='30'){
  197. if(new Date(product.use_time.replace(/-/g, "/"))>new Date()){
  198. $(".use_time").html("请在"+product.use_time+"前使用");
  199. }else{
  200. $(".use_time").html("使用时间已截止");
  201. }
  202. }
  203. }else{
  204. $("#tip").removeClass("mui-hidden");
  205. $("#use").removeClass("mui-hidden");
  206. $('#pay_amt').prev().html('已付款');
  207. $('#title').html('待使用');
  208. $('#pay_amt').html('<em class="fs16"><b>¥</b>'+mobile.nullToZore(order.pay_amt)+'</em>');
  209. if (product.product_type!='30'){
  210. if(new Date(product.use_time.replace(/-/g, "/"))>new Date()){
  211. if (order.order_date){
  212. $(".use_time").html("请在"+order.order_date+"当天使用");
  213. $("#reorder-btn").removeClass("mui-hidden");
  214. getOrderDate();
  215. }else{
  216. $(".use_time").html("请在"+product.use_time+"前使用");
  217. }
  218. }else{
  219. $(".use_time").html("使用时间已截止");
  220. }
  221. }
  222. var nbox = mui('.mui-numbox').numbox();
  223. nbox.options["max"]=product.surplus_qty;
  224. $('#surplus-qty').html("<span class='color-red'>剩余"+product.surplus_qty+"份</span>");
  225. if (product.product_qty>1){
  226. $('#use-num').show();
  227. $('#surplus-qty').show();
  228. }
  229. }
  230. }else if(order.order_status=="30"){
  231. $('#pay_amt').prev().html('已付款');
  232. $('#title').html('交易成功');
  233. $('#pay_amt').html('<em class="fs16"><b>¥</b>'+mobile.nullToZore(order.pay_amt)+'</em>');
  234. if(order.comment_flag!="1"){
  235. $(".integral").html(parseInt(order.pay_amt)+"积分");
  236. $("#comment").removeClass("mui-hidden");
  237. }
  238. }else{
  239. $('#title').html('订单失效');
  240. }
  241. if(order.order_type=="10"){
  242. $("#sumPrice").removeClass("mui-hidden");
  243. }else if(order.order_type=="20"){
  244. $("#sumIntegral").removeClass("mui-hidden");
  245. }
  246. $('.mui-content').removeClass('mui-hidden');
  247. $('#product').html(html.join(''));
  248. });
  249. }
  250. //获取余额
  251. function getMoney() {
  252. mobile.ajaxPost(base + '/userMoney/getMoney', {}, function(json) {
  253. money = json.data;
  254. $('#money').html("¥"+json.data);
  255. },"");
  256. }
  257. //获取余额
  258. function getOrderDate() {
  259. mobile.ajaxPost(base + '/userOrder/getOrderDate', {productId: product.id}, function(json) {
  260. if (json.data&&json.data.length>0){
  261. orderDate = json.data;
  262. $.each(json.data, function (id,item) {
  263. $("#order_date").append('<option value="'+item+'">'+item+'</option>');
  264. })
  265. }
  266. },"");
  267. }
  268. function onBridgeReady(json){
  269. WeixinJSBridge.invoke(
  270. 'getBrandWCPayRequest',
  271. json,
  272. function(res){
  273. if(res.err_msg == "get_brand_wcpay_request:ok" ) {
  274. queryOrder();
  275. }else{
  276. layer.open({content: '支付失败',time: 3});
  277. }
  278. }
  279. );
  280. }
  281. function queryOrder(){
  282. mobile.ajaxPost(base+'/userOrder/queryOrder',{id:id},function(json){
  283. if(json.success){
  284. var data=json.data;
  285. if(data.trade_state=="SUCCESS"){
  286. layer.open({content: '支付成功',time: 3});
  287. setTimeout(function() {
  288. window.location.reload();
  289. }, 1000);
  290. }else{
  291. layer.open({content: '支付失败',time: 3});
  292. }
  293. }else{
  294. layer.open({content: json.msg,time: 3});
  295. }
  296. });
  297. }
  298. function checkTime(i){ // 将0-9的数字前面加上0,例1变为01
  299. if(i<10) {
  300. i = "0" + i;
  301. }
  302. return i;
  303. }
  304. //获取地址列表
  305. function getAddressList() {
  306. mobile.ajaxPost(base + '/userAddress/getList', {}, function(json) {
  307. if(json.data&&json.data.length){
  308. $.each(json.data,function(id,item){
  309. var html = [];
  310. html.push('<li class="mui-table-view-cell">');
  311. html.push('<a class="mui-navigate-right">');
  312. html.push('<p class="color1">');
  313. html.push('<span class="mr15">'+item.consignee+'</span>');
  314. html.push('<span class="phone">'+item.mobile+'</span>');
  315. html.push('</p>');
  316. if(item.is_default=='1'){
  317. html.push('<p class="color2 mui-ellipsis-2 pr15"><span class="main-color">[默认] </span>');
  318. }else{
  319. html.push('<p class="color2 mui-ellipsis-2 pr15">');
  320. }
  321. html.push(''+mobile.nullToSpace(item.district)+mobile.nullToSpace(item.address)+mobile.nullToSpace(item.house_num)+'</p>');
  322. html.push('</a>');
  323. html.push('</li>');
  324. var map = new BMap.Map("allmap");
  325. var pointA = new BMap.Point(item.longitude,item.latitude);
  326. var pointB = new BMap.Point(117.950, 28.463);
  327. var distance = parseInt(map.getDistance(pointA,pointB));
  328. if(distance<=10000*1000){
  329. addrlist.push(item);
  330. $("#inrange").append(html.join(""));
  331. }else{
  332. $("#outrange").append(html.join(""));
  333. }
  334. });
  335. }else{
  336. $('#addr').html('<li class="mui-table-view-cell"><a class="mui-navigate-right">'+
  337. '<p class="color1">选择收货地址</p></a></li>');
  338. $('#addrList .mui-table-view').html('');
  339. mobile.tip('#addrList .mui-content','暂无收货地址');
  340. }
  341. if($("#inrange li").length!=0){
  342. $("#inrange").removeClass("mui-hidden");
  343. var item = addrlist[0];
  344. $('#addr').html('<li class="mui-table-view-cell" data-id="'+item.id+'"><a class="mui-navigate-right">'+
  345. '<p class="color1"><span class="mr15">'+item.consignee+'</span><span class="phone">'+item.mobile+'</span></p>'+
  346. '<p class="color2 mui-ellipsis-2">'+mobile.nullToSpace(item.district)+mobile.nullToSpace(item.address)+mobile.nullToSpace(item.house_num)+'</p></a></li>');
  347. }
  348. if($("#outrange li").length!=0){
  349. $("#outrange").removeClass("mui-hidden");
  350. $("#outrangelbl").removeClass("mui-hidden");
  351. }
  352. });
  353. }