confirm.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. var shoppingConfirm = new Vue({
  2. el: "#shoppingConfirm",
  3. data: {
  4. userId: 0,
  5. userIdentity: '',
  6. userToken: '',
  7. type: '',
  8. productIds: '',
  9. listData: [],
  10. kindCount: 0,
  11. totalCount: 0,
  12. totalPrice: 0,
  13. reducedPrice: 0,
  14. address: {
  15. id: '',
  16. townId: '',
  17. cityId: '',
  18. provinceId: '',
  19. name: '',
  20. mobile: '',
  21. desc: '',
  22. town: '',
  23. city: '',
  24. province: '',
  25. default: '',
  26. selectedId: ''
  27. },
  28. addressList: [],
  29. isShowAddressList: false,
  30. isShowAddressForm: false,
  31. addressForm: {
  32. id: '',
  33. name: '',
  34. mobile: '',
  35. townId: '',
  36. cityId: '',
  37. provinceId: '',
  38. desc: '',
  39. default: ''
  40. },
  41. provinceOptions:[],
  42. cityOptions:[],
  43. townOptions:[],
  44. saveLoading: false,
  45. rule:{
  46. name: '^[a-zA-Z\\u4e00-\\u9fa5]{2,}$',
  47. mobile: '^\\d{6,12}$'
  48. },
  49. balance: {
  50. isFreight:false, //使用采美豆抵扣运费
  51. flag: false, //使用余额抵扣
  52. originUserMoney: 0, //初始化后不能改变,用于还原余额抵扣
  53. userMoney: 0, //显示可使用余额
  54. deductMoney: 0, //显示已使用的余额
  55. surplusMoney: 0 //显示勾选后的剩余抵扣
  56. },
  57. payInfo: {
  58. clauseId: 0, //条款(暂时保留)
  59. orderShouldPayFee: 0.00,//提交的总价
  60. balancePayFlag: 0, //余额抵扣的状态 0不使用,1使用
  61. freight: 0.00, //邮费
  62. freePostFlag:-1 // 0包邮 -1到付 1 有运费
  63. },
  64. invoice: {
  65. id:'',
  66. type: 0,
  67. invoiceTitleType: 0, //发票抬头类型 1企业 0个人
  68. invoiceTitle: '', //单位名称
  69. corporationTaxNum:'',//纳税人识别号
  70. registeredAddress: '',//单位地址
  71. registeredPhone:'', //注册电话
  72. openBank:'', //开户银行
  73. bankAccountNo: '', //银行账号
  74. },
  75. isBeansShow:false,
  76. userBeans:0,
  77. freightBeansMoney:0,
  78. freightMoney:0,
  79. hanldUserBeans:0,
  80. submitLoading: false,
  81. },
  82. computed: {
  83. },
  84. filters: {
  85. NumFormat:function(num) {
  86. var temp = String(num).split('.');
  87. if (temp.length === 1 || temp[1].length < 2) {
  88. return Number(num).toFixed(2);
  89. }else{
  90. return Number(num);
  91. }
  92. }
  93. },
  94. methods: {
  95. blurHandle: function(event) {
  96. var el = event.currentTarget;
  97. verifyHandle(el);
  98. },
  99. getConfirmList: function (productCount) {
  100. var _self = this;
  101. if(this.userId ===0){return;}
  102. OrderApi.GetOrderConfirmInfo({
  103. userId: _self.userId,
  104. count: productCount,
  105. productIds: this.productIds
  106. },function(r){
  107. if (r.code === 0 && r.data) {
  108. _self.listData = r.data.list;
  109. _self.totalPrice = r.data.totalPrice;
  110. _self.reducedPrice = r.data.reducedPrice;
  111. _self.totalCount = r.data.totalCount;
  112. _self.kindCount = r.data.kindCount;
  113. _self.balance.userMoney = r.data.userMoney;
  114. _self.balance.originUserMoney = r.data.userMoney;
  115. _self.payInfo.orderShouldPayFee = r.data.totalPrice;
  116. if(r.data.invoice){
  117. r.data.invoice.type = 0;
  118. _self.invoice = r.data.invoice;
  119. }
  120. _self.getAddressList(_self.userId,20);
  121. }else{
  122. CAIMEI.Alert(r.msg, '确定', false);
  123. }
  124. });
  125. },
  126. // 获取邮费
  127. getFreight: function(townId){
  128. var _self = this;
  129. OrderApi.GetOrderPostage({
  130. userId: this.userId,
  131. productIds: this.productIds,
  132. totalPrice: this.totalPrice,
  133. townId: townId
  134. },function (r) {
  135. if(r.code === 0 && r.data){
  136. _self.payInfo.freePostFlag = r.data.freePostFlag;
  137. _self.userBeans = r.data.userBeans;
  138. _self.payInfo.freight = (r.data.freePostFlag===1 ? r.data.freight : 0);
  139. _self.payInfo.orderShouldPayFee = _self.totalPrice + _self.payInfo.freight;
  140. switch (_self.payInfo.freePostFlag) {
  141. case 1:
  142. if( _self.userBeans >= r.data.freight*100 ){
  143. _self.freightBeansMoney = r.data.freight;
  144. _self.freightMoney = r.data.freight;
  145. _self.isBeansShow = true;
  146. }else{
  147. _self.isBeansShow = false;
  148. }
  149. break;
  150. case -1:
  151. if( _self.userBeans >= 3000 ){
  152. _self.freightMoney = 30;
  153. _self.freightBeansMoney = 30;
  154. _self.isBeansShow = true;
  155. }else{
  156. _self.isBeansShow = false;
  157. }
  158. break;
  159. }
  160. }else{
  161. CAIMEI.Alert(r.msg, '确定', false);
  162. }
  163. })
  164. },
  165. // 获取收货地址
  166. getAddressList: function(userId,pageSize){
  167. var _self = this;
  168. OrderApi.GetAddressList({
  169. userID:userId,
  170. pageNum:1,
  171. pageSize:pageSize
  172. },function (r) {
  173. if(r.code === 0 && r.data){
  174. if(r.data.results){
  175. _self.addressList = r.data.results;
  176. _self.address.id = r.data.results[0].addressID;
  177. _self.address.townId = r.data.results[0].townID;
  178. _self.address.cityId = r.data.results[0].cityID;
  179. _self.address.provinceId = r.data.results[0].provinceID;
  180. _self.address.name = r.data.results[0].shouHuoRen;
  181. _self.address.mobile = r.data.results[0].mobile;
  182. _self.address.desc = r.data.results[0].address;
  183. _self.address.town = r.data.results[0].town;
  184. _self.address.city = r.data.results[0].city;
  185. _self.address.province = r.data.results[0].province;
  186. _self.address.default = true;
  187. _self.address.selectedId = r.data.results[0].addressID;
  188. _self.getFreight(_self.address.townId);
  189. }else{
  190. _self.createAddress();
  191. }
  192. _self.getProvinceOptions();
  193. }else {
  194. CAIMEI.Alert(r.msg, '确定', false);
  195. _self.createAddress();
  196. }
  197. })
  198. },
  199. // 获取全部省份
  200. getProvinceOptions: function(){
  201. var _self = this;
  202. PublicApi.GetProvince({},function(r){
  203. if(r.code === 0 && r.data){
  204. _self.provinceOptions =r.data;
  205. }else{
  206. CAIMEI.Alert(r.msg,'确定',false);
  207. }
  208. });
  209. },
  210. getCityOptions: function(provinceId,callback){
  211. var _self = this;
  212. if(provinceId && provinceId>0){
  213. _self.cityOptions = [];
  214. _self.townOptions = [];
  215. _self.addressForm.cityId = '';
  216. _self.addressForm.townId = '';
  217. PublicApi.GetCity({ provinceId: provinceId },function(r){
  218. if(r.code === 0 && r.data){
  219. _self.cityOptions = r.data;
  220. if(callback){callback();}
  221. }else{
  222. CAIMEI.Alert(r.msg,'确定',false);
  223. }
  224. });
  225. }
  226. },
  227. getTownOptions: function(cityId, callback){
  228. var _self = this;
  229. if(cityId && cityId>0){
  230. _self.townOptions = [];
  231. _self.addressForm.townId = '';
  232. PublicApi.GetTown({cityId: cityId},function(r){
  233. if(r.code === 0 && r.data){
  234. _self.townOptions = r.data;
  235. if(callback){callback();}
  236. }else{
  237. CAIMEI.Alert(r.msg,'确定',false);
  238. }
  239. })
  240. }
  241. },
  242. showAddressBox: function(){
  243. this.isShowAddressList = true;
  244. if(!isPC){fixedBody();}
  245. },
  246. closeAddressBox: function(){
  247. this.isShowAddressList = false;
  248. if(!isPC){looseBody();}
  249. },
  250. hideAddressForm: function(){
  251. this.isShowAddressForm = false;
  252. if(!isPC){looseBody();}
  253. },
  254. createAddress: function(){
  255. this.isShowAddressForm = true;
  256. this.addressForm.name= '';
  257. this.addressForm.mobile= '';
  258. this.addressForm.townId= '';
  259. this.addressForm.cityId= '';
  260. this.addressForm.provinceId= '';
  261. this.addressForm.desc= '';
  262. this.addressForm.default= '';
  263. this.getProvinceOptions();
  264. if(!isPC){fixedBody();}
  265. },
  266. updateAddress: function(){
  267. this.isShowAddressForm = true;
  268. this.addressForm.id = this.address.id;
  269. this.addressForm.name = this.address.name;
  270. this.addressForm.mobile = this.address.mobile;
  271. this.addressForm.default = this.address.default;
  272. this.addressForm.desc = this.address.desc;
  273. this.addressForm.provinceId = this.address.provinceId;
  274. var _self = this;
  275. this.getCityOptions(this.address.provinceId, function(){
  276. _self.addressForm.cityId = _self.address.cityId;
  277. _self.getTownOptions(_self.address.cityId,function(){
  278. _self.addressForm.townId = _self.address.townId;
  279. });
  280. });
  281. if(!isPC){fixedBody();}
  282. },
  283. saveAddressForm: function(){
  284. var _self = this;
  285. var pass = verifyForm();
  286. if (_self.saveLoading) { return false; }
  287. this.$nextTick(function() {
  288. if (!pass) {return false;}
  289. if(!_self.addressForm.townId){
  290. CAIMEI.dialog('请选择所在地区');
  291. return false;
  292. };
  293. _self.saveLoading = true;
  294. var params = {
  295. userID:_self.userId, //用户id ,只在新增收货地址时传
  296. shouHuoRen:_self.addressForm.name, //收货人
  297. mobile:_self.addressForm.mobile, //手机
  298. townID:_self.addressForm.townId, //区ID
  299. address:_self.addressForm.desc, //地址
  300. defaultFlag:_self.addressForm.default?1:0 //是否默认收货地址(0 不是默认,1 默认)
  301. };
  302. if(_self.addressForm.id){
  303. // 编辑
  304. params.addressID = _self.addressForm.id;//编辑保存地址ID
  305. }
  306. _self.saveAddress(params);
  307. });
  308. },
  309. saveAddress: function(params){
  310. var _self = this;
  311. OrderApi.AddSaveAddress(params,function(r){
  312. if(r.code === 0){
  313. CAIMEI.dialog('保存成功');
  314. _self.saveLoading = false;
  315. _self.isShowAddressForm = false;
  316. // 重新获取地址列表
  317. _self.getAddressList(_self.userId,20);
  318. }else{
  319. CAIMEI.Alert(r.msg,'确定',false);
  320. }
  321. });
  322. },
  323. // 列表直接设为默认地址
  324. setDefaultAddress: function(addressId){
  325. var _self = this;
  326. OrderApi.DefaultAddress({addressId:addressId,userId:_self.userId},function (r) {
  327. if(r.code === 0 ){
  328. // 重新获取地址列表
  329. _self.getAddressList(_self.userId,20);
  330. }else{
  331. CAIMEI.Alert(r.msg,'确定',false);
  332. }
  333. });
  334. },
  335. // 列表删除单个地址
  336. deleteAddress: function(addressId){
  337. var _self = this;
  338. CAIMEI.Modal('确定要删除该地址?','取消','确定',function () {
  339. OrderApi.DeleteAddress({addressID:addressId,userID:_self.userId},function (r) {
  340. if(r.code === 0 ){
  341. CAIMEI.dialog('删除成功');
  342. // 重新获取地址列表
  343. _self.getAddressList(_self.userId,20);
  344. }else{
  345. CAIMEI.Alert(r.msg,'确定',false);
  346. }
  347. })
  348. });
  349. },
  350. chooseAddress: function(selectedId){
  351. this.address.selectedId = selectedId;
  352. },
  353. confirmAddress: function(){
  354. var _self = this;
  355. this.addressList.forEach(function(item){
  356. if (_self.address.selectedId==item.addressID){
  357. _self.address.id = item.addressID;
  358. _self.address.townId = item.townID;
  359. _self.address.cityId = item.cityID;
  360. _self.address.provinceId = item.provinceID;
  361. _self.address.name = item.shouHuoRen;
  362. _self.address.mobile = item.mobile;
  363. _self.address.desc = item.address;
  364. _self.address.town = item.town;
  365. _self.address.city = item.city;
  366. _self.address.province = item.province;
  367. _self.address.default = (item.defaultFlag > 0);
  368. _self.getFreight(_self.address.townId);
  369. }
  370. });
  371. this.closeAddressBox();
  372. },
  373. toggleThisLadder: function(event){
  374. var el = event.currentTarget;
  375. if($(el).hasClass("on")){
  376. $(el).removeClass("on").siblings('.mFixed').hide();
  377. if(!isPC){looseBody();}
  378. }else{
  379. $(el).addClass("on").siblings('.mFixed').show();
  380. if(!isPC){fixedBody();}
  381. }
  382. },
  383. hideThisLadder: function(event){
  384. var el = event.currentTarget;
  385. $(el).parents('.priceTag').find('.tag').removeClass("on").siblings('.mFixed').hide();
  386. if(!isPC){looseBody();}
  387. },
  388. useUserMoney: function(){
  389. var _self = this;
  390. if(this.balance.userMoney>0){
  391. this.$nextTick(function() {
  392. var total = _self.totalPrice + _self.payInfo.freight;
  393. if(_self.balance.flag){
  394. _self.payInfo.balancePayFlag = 1;
  395. if(_self.balance.userMoney>=total){
  396. // 全部抵扣
  397. _self.payInfo.orderShouldPayFee = 0;
  398. _self.balance.deductMoney = total;
  399. _self.balance.surplusMoney = _self.balance.userMoney-total;
  400. }else{
  401. // 部分抵扣
  402. _self.payInfo.orderShouldPayFee = total - _self.balance.userMoney;
  403. _self.balance.deductMoney = _self.balance.userMoney;
  404. _self.balance.surplusMoney = 0;
  405. }
  406. }else{
  407. // 取消抵扣
  408. _self.payInfo.balancePayFlag = 0;
  409. _self.payInfo.orderShouldPayFee = total;
  410. _self.balance.userMoney = _self.balance.originUserMoney;
  411. _self.balance.deductMoney = 0;
  412. _self.balance.surplusMoney = _self.balance.originUserMoney;
  413. }
  414. });
  415. }
  416. },
  417. changeDeductibleFreight: function(){//选择采美豆抵扣邮费
  418. var _self = this;
  419. if(_self.balance.isFreight){
  420. _self.hanldUserBeans = _self.freightBeansMoney*100;
  421. if(_self.payInfo.freePostFlag == 1){
  422. console.log('商品总价-------->',_self.totalPrice);
  423. console.log('邮费------------>',_self.payInfo.freight);
  424. console.log('采美豆换算金额--->',_self.freightBeansMoney);
  425. _self.payInfo.orderShouldPayFee = (_self.totalPrice*10 + _self.payInfo.freight*10 - _self.freightBeansMoney*10)/10;
  426. console.log('最终支付金额---->',_self.payInfo.orderShouldPayFee)
  427. }else{
  428. _self.payInfo.orderShouldPayFee = _self.totalPrice ;
  429. }
  430. }else{
  431. _self.payInfo.orderShouldPayFee = _self.totalPrice + _self.payInfo.freight;
  432. _self.hanldUserBeans = 0
  433. }
  434. },
  435. submitOrder: function(){
  436. var _self = this;
  437. if(this.submitLoading){return false;}
  438. if(!this.address.id){
  439. CAIMEI.dialog('请先添加收货地址~');
  440. return false;
  441. }
  442. if(this.invoice.type*1 === 1){
  443. // 普通发票
  444. if(!this.invoice.invoiceTitle){
  445. CAIMEI.dialog('请输入个人抬头');return false;
  446. }
  447. if(this.invoice.invoiceTitleType===1 && !this.invoice.corporationTaxNum){
  448. CAIMEI.dialog('请输入纳税人识别号');return false;
  449. }
  450. }else if(this.invoice.type*1 === 2){
  451. // 增值税发票
  452. if(!this.invoice.invoiceTitle){
  453. CAIMEI.dialog('请输入个人抬头');return false;
  454. }
  455. if(!this.invoice.corporationTaxNum){
  456. CAIMEI.dialog('请输入纳税人识别号');return false;
  457. }
  458. if(!this.invoice.registeredAddress){
  459. CAIMEI.dialog('请输入单位地址');return false;
  460. }
  461. if(!this.invoice.registeredPhone){
  462. CAIMEI.dialog('请输入注册电话');return false;
  463. }
  464. if(!this.invoice.openBank){
  465. CAIMEI.dialog('请输入开户银行');return false;
  466. }
  467. if(!this.invoice.bankAccountNo){
  468. CAIMEI.dialog('请输入银行账号');return false;
  469. }
  470. }
  471. // 禁用按钮
  472. this.submitLoading = true;
  473. // 发票信息
  474. if(this.invoice.type*1 === 0){
  475. this.invoice = Object.assign(this.invoice,'',{type:0});
  476. }else if(this.invoice.type*1 === 1){
  477. this.invoice = Object.assign(this.invoice,'',{type:1, invoiceContent: '商品明细'});
  478. }else if(this.invoice.type*1 === 2){
  479. this.invoice = Object.assign(this.invoice,'',{type:2});
  480. }
  481. //商品信息
  482. var _orderInfo = this.listData.map(function(supplier){
  483. var productInfo = [];
  484. supplier.cartList.forEach(function(item){
  485. productInfo.push({
  486. productId:item.productId,
  487. productNum:item.number,
  488. presentNum:0,
  489. productType:item.productType
  490. })
  491. });
  492. return {
  493. shopId: supplier.id,
  494. note: supplier.note ? supplier.note : '',
  495. productInfo: productInfo
  496. }
  497. });
  498. var _payInfo = {
  499. clauseId: 0,
  500. orderShouldPayFee: toFloat(this.payInfo.orderShouldPayFee),
  501. balancePayFlag: this.payInfo.balancePayFlag,
  502. freight: toFloat(this.payInfo.freight),
  503. freePostFlag: this.payInfo.freePostFlag,
  504. userBeans:this.hanldUserBeans
  505. };
  506. var params = {
  507. cartType: this.type,
  508. orderSource: 1, // 购买类型:(1购物车提交[对应表cm_cart],2直接购买提交, 3协销下单)
  509. serviceProviderId: '', // 协销ID(小程序忽略)
  510. clubUserId: this.userId, // 机构用户ID
  511. addressId: this.address.id,// 地址ID
  512. orderInfo: _orderInfo, // 商品信息
  513. payInfo: _payInfo, // 订单信息
  514. orderInvoice: this.invoice // 发票信息
  515. };
  516. OrderApi.ConfirmOrder({'params':JSON.stringify(params)},function (r) {
  517. if(r.code === 0){
  518. var _data = r.data;
  519. if(Number(_data.code) === 1){//余额抵扣全部订单金额
  520. var data = { payableAmount: _self.deductMoney };
  521. CAIMEI.Storage.removeItem('confirmOrderInfo');
  522. CAIMEI.Storage.setItem('confirmOrderInfo',JSON.stringify({data:data}));
  523. window.location.href = '/pay/success.html?pageType=www&type=success&payAmount='+_data.payTotalFee;
  524. }else{//余额抵扣部分订单金额或者未支付的
  525. var data = {
  526. orderID:_data.orderID,
  527. orderNo:_data.orderNo,
  528. orderMark:_data.orderMark,
  529. payableAmount:_data.payableAmount
  530. };
  531. CAIMEI.Storage.removeItem('confirmOrderInfo');
  532. CAIMEI.Storage.setItem('confirmOrderInfo',JSON.stringify({data:data}));
  533. window.location.href = '/pay/caimei-paycash.html?type=confirm&orderID='+_data.orderID;
  534. }
  535. }else{
  536. CAIMEI.Alert(r.msg,'确定',true, function(){
  537. setTimeout(function(){
  538. _self.submitLoading = false;
  539. },500);
  540. });
  541. }
  542. });
  543. }
  544. },
  545. created: function () {
  546. this.type = getUrlParam("type") ? getUrlParam("type")*1 : "";
  547. if(globalUserData){
  548. this.userId = globalUserData.userId;
  549. this.userIdentity = globalUserData.identity;
  550. this.userToken = globalUserData.token;
  551. // type:(1购物车提交[对应表cm_cart],2直接购买提交, 3协销下单)
  552. if(this.type === 1) {
  553. this.productIds = window.localStorage.getItem("shoppingProductIds");
  554. // 获取列表数据
  555. this.getConfirmList(null);
  556. }else if(this.type === 2){
  557. this.productIds = getUrlParam("id");
  558. var count = getUrlParam("count");
  559. if(this.productIds && count) {
  560. // 获取列表数据
  561. this.getConfirmList(count);
  562. }else{
  563. alertInfo("参数错误,请重新提交订单!");
  564. }
  565. }
  566. }
  567. },
  568. mounted: function () {
  569. var _self = this;
  570. var minAwayBtm = $("footer").height();
  571. //处理滚动条控制底部提交fixed
  572. $(window).scroll(function() {
  573. var awayBtm = $(document).height() - $(window).scrollTop() - $(window).height();
  574. if(minAwayBtm <= awayBtm){
  575. $(".summaryWrap").addClass('fixed');
  576. }else{
  577. $(".summaryWrap").removeClass("fixed");
  578. }
  579. });
  580. }
  581. });