checkout.js 14 KB

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