1
0

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