checkout.js 13 KB

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