1
0

checkout.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. that.setData({
  119. idNoM: that.formatidcard(that.data.idNo)
  120. });
  121. if(that.data.idNo){
  122. that.setData({
  123. idNoDisabled: true
  124. });
  125. }
  126. let num00 = 0;
  127. let num02 = 0;
  128. let num10 = 0;
  129. let num11 = 0;
  130. let goodsTotalPrice00 = 0;
  131. let goodsTotalPrice02 = 0;
  132. let goodsTotalPrice10 = 0;
  133. let goodsTotalPrice11 = 0;
  134. for (var i = 0; i < that.data.checkedGoodsList.length; i++) {
  135. if ('00' == that.data.checkedGoodsList[i].goodsBizType) {
  136. num00 = num00 + that.data.checkedGoodsList[i].number;
  137. goodsTotalPrice00 = goodsTotalPrice00 +
  138. (that.data.checkedGoodsList[i].retail_price * that.data.checkedGoodsList[i].number);
  139. }
  140. if ('02' == that.data.checkedGoodsList[i].goodsBizType) {
  141. num02 = num02 + that.data.checkedGoodsList[i].number;
  142. goodsTotalPrice02 = goodsTotalPrice02 +
  143. (that.data.checkedGoodsList[i].retail_price * that.data.checkedGoodsList[i].number);
  144. }
  145. if ('10' == that.data.checkedGoodsList[i].goodsBizType) {
  146. num10 = num10 + that.data.checkedGoodsList[i].number;
  147. goodsTotalPrice10 = goodsTotalPrice10 +
  148. (that.data.checkedGoodsList[i].retail_price * that.data.checkedGoodsList[i].number);
  149. }
  150. if ('11' == that.data.checkedGoodsList[i].goodsBizType) {
  151. num11 = num11 + that.data.checkedGoodsList[i].number;
  152. goodsTotalPrice11 = goodsTotalPrice11 +
  153. (that.data.checkedGoodsList[i].retail_price * that.data.checkedGoodsList[i].number);
  154. }
  155. }
  156. that.setData({
  157. goodsTotalByType00: num00,
  158. goodsTotalByType02: num02,
  159. goodsTotalByType10: num10,
  160. goodsTotalByType11: num11,
  161. goodsTotalPrice00: goodsTotalPrice00,
  162. goodsTotalPrice02: goodsTotalPrice02,
  163. goodsTotalPrice10: goodsTotalPrice10,
  164. goodsTotalPrice11: goodsTotalPrice11
  165. });
  166. // console.log(that.data.goodsTotalByType00);
  167. // console.log(that.data.goodsTotalByType02);
  168. // console.log(that.data.goodsTotalByType10);
  169. // console.log(that.data.goodsTotalByType11);
  170. // 选择优惠券
  171. if (that.data.userCouponId && that.data.couponList.length > 0) {
  172. for (var i = 0; i < that.data.couponList.length; i++) {
  173. if (that.data.userCouponId == that.data.couponList[i].id) {
  174. that.setData({
  175. checkedCoupon: that.data.couponList[i],
  176. couponPrice: that.data.couponList[i].type_money
  177. });
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. wx.hideLoading();
  184. that.checkStore();
  185. });
  186. },
  187. selectAddress() {
  188. this.setData({
  189. checkStore: false
  190. });
  191. wx.navigateTo({
  192. url: '/pages/shopping/address/address',
  193. })
  194. },
  195. addAddress() {
  196. wx.navigateTo({
  197. url: '/pages/shopping/addressAdd/addressAdd',
  198. })
  199. },
  200. selectCoupon() {
  201. let that = this;
  202. wx.navigateTo({
  203. url: '/pages/shopping/coupon/coupon',
  204. })
  205. },
  206. bindPostscriptBlur(e) {
  207. let goodsType = e.target.dataset.goodsType;
  208. if (goodsType == '00') {
  209. this.setData({
  210. postscript00: e.detail.value
  211. });
  212. }
  213. if (goodsType == '02') {
  214. this.setData({
  215. postscript02: e.detail.value
  216. });
  217. }
  218. if (goodsType == '10') {
  219. this.setData({
  220. postscript10: e.detail.value
  221. });
  222. }
  223. if (goodsType == '11') {
  224. this.setData({
  225. postscript11: e.detail.value
  226. });
  227. }
  228. },
  229. submitOrder: function (e) {
  230. // console.log(e.detail.formId);
  231. if (!this.data.addressVo) {
  232. util.showErrorToast('请选择收货地址');
  233. return false;
  234. }
  235. if (!e.detail.value.idNo) {
  236. util.showErrorToast('身份证号不能为空');
  237. return false;
  238. }
  239. if (!e.detail.value.userName) {
  240. util.showErrorToast('姓名不能为空');
  241. return false;
  242. }
  243. if (this.data.idNoM == '') {
  244. this.setData({
  245. idNo: e.detail.value.idNo,
  246. userName: e.detail.value.userName
  247. });
  248. }
  249. if (!this.checkIdcard(this.data.idNo)){
  250. wx.showModal({
  251. title: '提示信息',
  252. content: '请输入正确的身份证号',
  253. showCancel: false
  254. });
  255. return false;
  256. }
  257. // if (!this.data.checkStore || !this.data.checkStore) {
  258. // util.showErrorToast('超过门店配送范围');
  259. // return false;
  260. // }
  261. let that = this;
  262. // console.log(that.data.postscript00);
  263. // console.log(that.data.postscript02);
  264. // console.log(that.data.postscript10);
  265. // console.log(that.data.postscript11);
  266. util.request(api.OrderSubmit, {
  267. checkedAddress: that.data.addressVo,
  268. userCouponId: that.data.userCouponId,
  269. fullCutCouponId: that.data.fullCutCouponId,
  270. postscript00: that.data.postscript00,
  271. postscript02: that.data.postscript02,
  272. postscript10: that.data.postscript10,
  273. postscript11: that.data.postscript11,
  274. delivery_date: that.data.delivery_date,
  275. delivery_remark: that.data.delivery_remark,
  276. idNo: that.data.idNo,
  277. userName: that.data.userName
  278. }, 'POST').then(function (res) {
  279. if (res.errno === 0) {
  280. let actualPrice = that.data.actualPrice;
  281. let orderIds = [];
  282. for (let i = 0; i < res.data.orderInfo.length; i++){
  283. orderIds[i] = res.data.orderInfo[i].id;
  284. }
  285. wx.redirectTo({
  286. url: '/pages/pay/pay?orderIds=' + orderIds + '&actualPrice=' + actualPrice
  287. })
  288. } else {
  289. wx.showModal({
  290. title: '提示信息',
  291. content: res.errmsg,
  292. showCancel: false
  293. });
  294. }
  295. });
  296. },
  297. // 校验门店
  298. checkStore: function () {
  299. let that = this;
  300. util.request(api.CheckStore, {
  301. checkedAddress: that.data.addressVo,
  302. storeId: wx.getStorageSync('storeId')
  303. }, 'POST').then(function (res) {
  304. if (res.errno === 0) {
  305. that.setData({
  306. storeVo: res.data,
  307. checkStore: true
  308. });
  309. } else{
  310. console.log(res.errmsg);
  311. // util.showErrorToast(res.errmsg);
  312. that.setData({
  313. checkStore: false
  314. });
  315. }
  316. });
  317. },
  318. onLoad: function (options) {
  319. // 页面初始化 options为页面跳转所带来的参数
  320. },
  321. onReady: function () {
  322. // 页面渲染完成
  323. },
  324. onShow: function () {
  325. // 页面显示
  326. wx.showLoading({
  327. title: '加载中...',
  328. })
  329. this.getCheckoutInfo();
  330. this.initPicker()
  331. },
  332. onHide: function () {
  333. // 页面隐藏
  334. },
  335. onUnload: function () {
  336. // 页面关闭
  337. },
  338. // 校验身份证号
  339. //校验码校验
  340. checkCode: function (val) {
  341. 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]$/;
  342. var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  343. var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  344. var code = val.substring(17);
  345. if (p.test(val)) {
  346. var sum = 0;
  347. for (var i = 0; i < 17; i++) {
  348. sum += val[i] * factor[i];
  349. }
  350. if (parity[sum % 11] == code.toUpperCase()) {
  351. return true;
  352. }
  353. }
  354. return false;
  355. },
  356. //省份校验
  357. checkProv: function (val) {
  358. var pattern = /^[1-9][0-9]/;
  359. 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: "澳门" };
  360. if (pattern.test(val)) {
  361. if (provs[val]) {
  362. return true;
  363. }
  364. }
  365. return false;
  366. },
  367. //出生日期码校验
  368. checkDate: function (val) {
  369. var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
  370. if (pattern.test(val)) {
  371. var year = val.substring(0, 4);
  372. var month = val.substring(4, 6);
  373. var date = val.substring(6, 8);
  374. var date2 = new Date(year + "-" + month + "-" + date);
  375. if (date2 && date2.getMonth() == (parseInt(month) - 1)) {
  376. return true;
  377. }
  378. }
  379. return false;
  380. },
  381. checkIdcard: function (val) {
  382. if (this.checkCode(val)) {
  383. var date = val.substring(6, 14);
  384. if (this.checkDate(date)) {
  385. if (this.checkProv(val.substring(0, 2))) {
  386. return true;
  387. }
  388. }
  389. }
  390. // this.isError("请输入正确身份证号");
  391. return false;
  392. },
  393. // 通过身份证号获取出生日期和性别
  394. getBirthAndSex: function (idCard) {
  395. var that = this;
  396. var info = {};
  397. var birth = (idCard.length === 18) ? idCard.slice(6, 14) : idCard.slice(6, 12);
  398. var order = (idCard.length === 18) ? idCard.slice(-2, -1) : idCard.slice(-1);
  399. 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('-');
  400. info.sex = (order % 2 === 0 ? 2 : 0);
  401. return info;
  402. }
  403. })