checkout.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var app = getApp();
  4. var dateArray = [];
  5. Page({
  6. data: {
  7. checkedGoodsList: [],
  8. addressVo: {},
  9. checkedCoupon: [],
  10. couponList: [],
  11. goodsBizTypeList: ['00','02','10','11'],
  12. goodsTotalPrice: 0.00, //商品总价
  13. freightPrice00: 0.00, //备货业务快递费
  14. freightPrice02: 0.00, //补货业务快递费
  15. freightPrice10: 0.00, //现场发包快递费
  16. freightPrice11: 0.00, //普通商品快递费
  17. couponPrice: 0.00, //优惠券的价格
  18. orderTotalPrice: 0.00, //订单总价
  19. actualPrice: 0.00, //实际需要支付的总价
  20. userCouponId: 0,
  21. fullCutCouponId: 0,
  22. fullCutCouponDec: 0, //满减优惠券
  23. goodsTotalByType00: 0,//备货业务商品总计
  24. goodsTotalByType02: 0,//补货业务商品总计
  25. goodsTotalByType10: 0,//现场发包商品总计
  26. goodsTotalByType11: 0, //普通商品商品总计
  27. postscript: "",
  28. postscript00: '',//备货业务备注
  29. postscript02: '',//补货业务备注
  30. postscript10: '',//现场发包备注
  31. postscript11: '',//普通商品备注
  32. checkStore: false,
  33. storeVo: {},
  34. delivery_date: '',
  35. delivery_remark: '尽快送达',
  36. multiArray: [],
  37. multiIndex: [0, 0],
  38. timeArray: util.getNowTimeArray(),
  39. idNo: '',
  40. userName: '',
  41. idNoM: '',
  42. idNoDisabled: false,
  43. checkCart: ''
  44. },
  45. bindMultiPickerColumnChange: function (e) {
  46. let that = this;
  47. // console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
  48. var data = {
  49. multiArray: this.data.multiArray,
  50. multiIndex: this.data.multiIndex
  51. };
  52. data.multiIndex[e.detail.column] = e.detail.value
  53. if (e.detail.column == 0){
  54. data.multiIndex[1] = 0
  55. }
  56. let timeArray = that.data.timeArray;
  57. if (data.multiIndex[0] == 1){
  58. timeArray = util.timeArray.slice(0);
  59. }else{
  60. timeArray = util.getNowTimeArray();
  61. }
  62. data.timeArray = timeArray;
  63. data.multiArray[1] = timeArray;
  64. this.setData(data)
  65. },
  66. bindMultiPickerChange: function (e) {
  67. let that = this;
  68. console.log('picker发送选择改变,携带值为', e.detail.value)
  69. this.setData({
  70. multiIndex: e.detail.value,
  71. delivery_date: dateArray[e.detail.value[0]],
  72. delivery_remark: e.detail.value[0] == 0 ? that.data.timeArray[e.detail.value[1]] : that.data.timeArray[e.detail.value[1]]
  73. })
  74. console.log('delivery_date', this.data.delivery_date, ',delivery_remark', this.data.delivery_remark)
  75. },
  76. initPicker(){
  77. let that = this;
  78. let date = new Date();
  79. let hours = date.getHours();
  80. let minutes = date.getMinutes();
  81. let index = 0;
  82. var today = util.getNowTime();
  83. var tomorrow = util.getDateStr(new Date(),1);
  84. dateArray = [today, tomorrow];
  85. this.setData({
  86. 'multiArray[0]': dateArray,
  87. 'multiArray[1]': that.data.timeArray,
  88. })
  89. },
  90. formatidcard(idcard) {
  91. if (idcard.length == 15) {
  92. return idcard.replace(/(\d{6})\d{6}(\d{3})/, "$1******$2");
  93. } else {
  94. return idcard.replace(/(\d{5})\d{6}(\d{6})/, "$1******$2");
  95. }
  96. },
  97. getCheckoutInfo: function () {
  98. let that = this;
  99. util.request(api.CartCheckout, { userCouponId: that.data.userCouponId, merchSn: wx.getStorageSync('merchSn'),
  100. checkCart: that.data.checkCart }).then(function (res) {
  101. if (res.errno === 0) {
  102. console.log(res.data);
  103. that.setData({
  104. checkedGoodsList: res.data.checkedGoodsList,
  105. actualPrice: res.data.actualPrice,
  106. addressVo: res.data.addressVo,
  107. checkedCoupon: res.data.checkedCoupon,
  108. couponList: res.data.couponList,
  109. fullCutCouponId: res.data.fullCutCouponId,
  110. couponPrice: res.data.couponPrice,
  111. freightPrice00: res.data.freightPrice00,
  112. freightPrice02: res.data.freightPrice02,
  113. freightPrice10: res.data.freightPrice10,
  114. freightPrice11: res.data.freightPrice11,
  115. goodsTotalPrice: res.data.goodsTotalPrice,
  116. orderTotalPrice: res.data.orderTotalPrice,
  117. fullCutCouponDec: res.data.fullCutCouponDec,
  118. userName: res.data.userName,
  119. idNo: res.data.idNo
  120. });
  121. if (that.data.idNo) {
  122. that.setData({
  123. idNoM: that.formatidcard(that.data.idNo)
  124. });
  125. }
  126. if(that.data.idNo){
  127. that.setData({
  128. idNoDisabled: true
  129. });
  130. }
  131. let num00 = 0;
  132. let num02 = 0;
  133. let num10 = 0;
  134. let num11 = 0;
  135. let goodsTotalPrice00 = 0;
  136. let goodsTotalPrice02 = 0;
  137. let goodsTotalPrice10 = 0;
  138. let goodsTotalPrice11 = 0;
  139. for (var i = 0; i < that.data.checkedGoodsList.length; i++) {
  140. if ('00' == that.data.checkedGoodsList[i].goodsBizType) {
  141. num00 = num00 + that.data.checkedGoodsList[i].number;
  142. goodsTotalPrice00 = goodsTotalPrice00 +
  143. (that.data.checkedGoodsList[i].retail_price * that.data.checkedGoodsList[i].number);
  144. }
  145. if ('02' == that.data.checkedGoodsList[i].goodsBizType) {
  146. num02 = num02 + that.data.checkedGoodsList[i].number;
  147. goodsTotalPrice02 = goodsTotalPrice02 +
  148. (that.data.checkedGoodsList[i].retail_price * that.data.checkedGoodsList[i].number);
  149. }
  150. if ('10' == that.data.checkedGoodsList[i].goodsBizType) {
  151. num10 = num10 + that.data.checkedGoodsList[i].number;
  152. goodsTotalPrice10 = goodsTotalPrice10 +
  153. (that.data.checkedGoodsList[i].retail_price * that.data.checkedGoodsList[i].number);
  154. }
  155. if ('11' == that.data.checkedGoodsList[i].goodsBizType) {
  156. num11 = num11 + that.data.checkedGoodsList[i].number;
  157. goodsTotalPrice11 = goodsTotalPrice11 +
  158. (that.data.checkedGoodsList[i].retail_price * that.data.checkedGoodsList[i].number);
  159. }
  160. }
  161. that.setData({
  162. goodsTotalByType00: num00,
  163. goodsTotalByType02: num02,
  164. goodsTotalByType10: num10,
  165. goodsTotalByType11: num11,
  166. goodsTotalPrice00: (goodsTotalPrice00 + that.data.freightPrice00).toFixed(2),
  167. goodsTotalPrice02: (goodsTotalPrice02 + that.data.freightPrice02).toFixed(2),
  168. goodsTotalPrice10: (goodsTotalPrice10 + that.data.freightPrice10).toFixed(2),
  169. goodsTotalPrice11: (goodsTotalPrice11 + that.data.freightPrice11).toFixed(2)
  170. });
  171. // console.log(that.data.goodsTotalByType00);
  172. // console.log(that.data.goodsTotalByType02);
  173. // console.log(that.data.goodsTotalByType10);
  174. // console.log(that.data.goodsTotalByType11);
  175. // 选择优惠券
  176. if (that.data.userCouponId && that.data.couponList.length > 0) {
  177. for (var i = 0; i < that.data.couponList.length; i++) {
  178. if (that.data.userCouponId == that.data.couponList[i].id) {
  179. that.setData({
  180. checkedCoupon: that.data.couponList[i],
  181. couponPrice: that.data.couponList[i].type_money
  182. });
  183. break;
  184. }
  185. }
  186. }
  187. }
  188. wx.hideLoading();
  189. that.checkStore();
  190. });
  191. },
  192. selectAddress() {
  193. this.setData({
  194. checkStore: false
  195. });
  196. wx.navigateTo({
  197. url: '/pages/shopping/address/address',
  198. })
  199. },
  200. addAddress() {
  201. wx.navigateTo({
  202. url: '/pages/shopping/addressAdd/addressAdd',
  203. })
  204. },
  205. selectCoupon() {
  206. let that = this;
  207. wx.navigateTo({
  208. url: '/pages/shopping/coupon/coupon',
  209. })
  210. },
  211. bindPostscriptBlur(e) {
  212. let goodsType = e.target.dataset.goodsType;
  213. if (goodsType == '00') {
  214. this.setData({
  215. postscript00: e.detail.value
  216. });
  217. }
  218. if (goodsType == '02') {
  219. this.setData({
  220. postscript02: e.detail.value
  221. });
  222. }
  223. if (goodsType == '10') {
  224. this.setData({
  225. postscript10: e.detail.value
  226. });
  227. }
  228. if (goodsType == '11') {
  229. this.setData({
  230. postscript11: e.detail.value
  231. });
  232. }
  233. },
  234. submitOrder: function (e) {
  235. // console.log(e.detail.formId);
  236. if (!this.data.addressVo) {
  237. util.showErrorToast('请选择收货地址');
  238. return false;
  239. }
  240. if (!e.detail.value.idNo) {
  241. util.showErrorToast('身份证号不能为空');
  242. return false;
  243. }
  244. if (!e.detail.value.userName) {
  245. util.showErrorToast('姓名不能为空');
  246. return false;
  247. }
  248. if (this.data.idNoM == '') {
  249. this.setData({
  250. idNo: e.detail.value.idNo,
  251. userName: e.detail.value.userName
  252. });
  253. }
  254. if (!this.checkIdcard(this.data.idNo)){
  255. wx.showModal({
  256. title: '提示信息',
  257. content: '请输入正确的身份证号',
  258. showCancel: false
  259. });
  260. return false;
  261. }
  262. // if (!this.data.checkStore || !this.data.checkStore) {
  263. // util.showErrorToast('超过门店配送范围');
  264. // return false;
  265. // }
  266. let that = this;
  267. // console.log(that.data.postscript00);
  268. // console.log(that.data.postscript02);
  269. // console.log(that.data.postscript10);
  270. // console.log(that.data.postscript11);
  271. wx.showLoading({
  272. title: '加载中...',
  273. });
  274. util.request(api.OrderSubmit, {
  275. checkedAddress: that.data.addressVo,
  276. userCouponId: that.data.userCouponId,
  277. fullCutCouponId: that.data.fullCutCouponId,
  278. postscript00: that.data.postscript00,
  279. postscript02: that.data.postscript02,
  280. postscript10: that.data.postscript10,
  281. postscript11: that.data.postscript11,
  282. delivery_date: that.data.delivery_date,
  283. delivery_remark: that.data.delivery_remark,
  284. idNo: that.data.idNo,
  285. userName: that.data.userName,
  286. merchSn: wx.getStorageSync("merchSn"),
  287. checkCart: that.data.checkCart
  288. }, 'POST').then(function (res) {
  289. if (res.errno === 0) {
  290. let actualPrice = that.data.actualPrice;
  291. let orderIds = [];
  292. for (let i = 0; i < res.data.orderInfo.length; i++){
  293. orderIds[i] = res.data.orderInfo[i].id;
  294. }
  295. wx.redirectTo({
  296. url: '/pages/pay/pay?orderIds=' + orderIds + '&actualPrice=' + actualPrice + '&isMergePay=1'//此处提交支付属于合并支付
  297. })
  298. } else {
  299. wx.showModal({
  300. title: '提示信息',
  301. content: res.errmsg,
  302. showCancel: false
  303. });
  304. }
  305. wx.hideLoading();
  306. });
  307. },
  308. // 校验门店
  309. checkStore: function () {
  310. let that = this;
  311. util.request(api.CheckStore, {
  312. checkedAddress: that.data.addressVo,
  313. storeId: wx.getStorageSync('storeId')
  314. }, 'POST').then(function (res) {
  315. if (res.errno === 0) {
  316. that.setData({
  317. storeVo: res.data,
  318. checkStore: true
  319. });
  320. } else{
  321. // console.log(res.errmsg);
  322. // util.showErrorToast(res.errmsg);
  323. that.setData({
  324. checkStore: false
  325. });
  326. }
  327. });
  328. },
  329. onLoad: function (options) {
  330. // 页面初始化 options为页面跳转所带来的参数
  331. // console.log(options.checkCart);
  332. this.setData({
  333. checkCart: options.checkCart
  334. });
  335. },
  336. onReady: function () {
  337. // 页面渲染完成
  338. },
  339. onShow: function () {
  340. // 页面显示
  341. wx.showLoading({
  342. title: '加载中...',
  343. })
  344. this.getCheckoutInfo();
  345. this.initPicker()
  346. },
  347. onHide: function () {
  348. // 页面隐藏
  349. },
  350. onUnload: function () {
  351. // 页面关闭
  352. },
  353. // 校验身份证号
  354. //校验码校验
  355. checkCode: function (val) {
  356. var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  357. var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  358. var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  359. var code = val.substring(17);
  360. if (p.test(val)) {
  361. var sum = 0;
  362. for (var i = 0; i < 17; i++) {
  363. sum += val[i] * factor[i];
  364. }
  365. if (parity[sum % 11] == code.toUpperCase()) {
  366. return true;
  367. }
  368. }
  369. return false;
  370. },
  371. //省份校验
  372. checkProv: function (val) {
  373. var pattern = /^[1-9][0-9]/;
  374. var provs = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门" };
  375. if (pattern.test(val)) {
  376. if (provs[val]) {
  377. return true;
  378. }
  379. }
  380. return false;
  381. },
  382. //出生日期码校验
  383. checkDate: function (val) {
  384. var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
  385. if (pattern.test(val)) {
  386. var year = val.substring(0, 4);
  387. var month = val.substring(4, 6);
  388. var date = val.substring(6, 8);
  389. var date2 = new Date(year + "-" + month + "-" + date);
  390. if (date2 && date2.getMonth() == (parseInt(month) - 1)) {
  391. return true;
  392. }
  393. }
  394. return false;
  395. },
  396. checkIdcard: function (val) {
  397. if (this.checkCode(val)) {
  398. var date = val.substring(6, 14);
  399. if (this.checkDate(date)) {
  400. if (this.checkProv(val.substring(0, 2))) {
  401. return true;
  402. }
  403. }
  404. }
  405. // this.isError("请输入正确身份证号");
  406. return false;
  407. },
  408. // 通过身份证号获取出生日期和性别
  409. getBirthAndSex: function (idCard) {
  410. var that = this;
  411. var info = {};
  412. var birth = (idCard.length === 18) ? idCard.slice(6, 14) : idCard.slice(6, 12);
  413. var order = (idCard.length === 18) ? idCard.slice(-2, -1) : idCard.slice(-1);
  414. info.birthDay = (idCard.length === 18) ? ([birth.slice(0, 4), birth.slice(4, 6), birth.slice(-2)]).join('-') : (['19' + birth.slice(0, 2), birth.slice(2, 4), birth.slice(-2)]).join('-');
  415. info.sex = (order % 2 === 0 ? 2 : 0);
  416. return info;
  417. }
  418. })