checkout.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. import Util from '../../utils/util.js';
  2. import Verify from '../../utils/verify.js';
  3. import DeliveryTypeEnum from '../../utils/enum/DeliveryType.js';
  4. import PayTypeEnum from '../../utils/enum/order/PayType';
  5. import Dialog from '../../components/dialog/dialog';
  6. import Toast from '../../components/toast/toast';
  7. const App = getApp()
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. // 当前页面参数
  14. options: {},
  15. // // 系统设置:配送方式
  16. // deliverySetting: [],
  17. // 系统设置
  18. setting: {
  19. delivery: [], // 支持的配送方式
  20. },
  21. // 配送方式
  22. isShowTab: false,
  23. DeliveryTypeEnum,
  24. curDelivery: null,
  25. // 支付方式
  26. PayTypeEnum,
  27. curPayType: PayTypeEnum.WECHAT.value,
  28. address: null, // 默认收货地址
  29. exist_address: false, // 是否存在收货地址
  30. selectedShopId: 0, // 选择的自提门店id
  31. linkman: '', // 自提联系人
  32. phone: '', // 自提联系电话
  33. // 商品信息
  34. goods: {},
  35. // 选择的优惠券
  36. selectCouponId: 0,
  37. // 是否使用积分抵扣
  38. isUsePoints: false,
  39. // 买家留言
  40. remark: '',
  41. // 禁用submit按钮
  42. disabled: false,
  43. has_error: false,
  44. error_msg: '',
  45. notRefresh: false, // 不允许刷新
  46. },
  47. /**
  48. * 生命周期函数--监听页面加载
  49. */
  50. onLoad(options) {
  51. let _this = this;
  52. // 当前页面参数
  53. _this.setData({
  54. options
  55. });
  56. console.log(options);
  57. },
  58. /**
  59. * 生命周期函数--监听页面显示
  60. */
  61. onShow() {
  62. let _this = this;
  63. // 获取当前订单信息
  64. !_this.data.notRefresh && _this.getOrderData();
  65. },
  66. /**
  67. * 获取当前订单信息
  68. */
  69. getOrderData() {
  70. let _this = this,
  71. options = _this.data.options;
  72. // 获取订单信息回调方法
  73. let callback = result => {
  74. let resData = result.data;
  75. // 请求错误
  76. if (result.code !== 1) {
  77. App.showError(result.msg);
  78. return false;
  79. }
  80. // 显示错误信息
  81. if (resData.has_error) {
  82. App.showError(resData.error_msg);
  83. }
  84. let data = {};
  85. // 当前选择的配送方式
  86. data.curDelivery = resData.delivery;
  87. // 如果只有一种配送方式则不显示选项卡
  88. data.isShowTab = resData.setting.delivery.length > 1;
  89. // 上门自提联系信息
  90. if (_this.data.linkman === '') {
  91. data.linkman = resData.last_extract.linkman;
  92. }
  93. if (_this.data.phone === '') {
  94. data.phone = resData.last_extract.phone;
  95. }
  96. // 设置页面数据
  97. _this.setData(Object.assign({}, resData, data));
  98. wx.hideLoading();
  99. };
  100. wx.showLoading({
  101. title: '加载中...',
  102. });
  103. // 请求的参数
  104. let params = {
  105. delivery: _this.data.curDelivery || 0,
  106. shop_id: _this.data.selectedShopId || 0,
  107. coupon_id: _this.data.selectCouponId || 0,
  108. is_use_points: _this.data.isUsePoints ? 1 : 0,
  109. };
  110. // 立即购买
  111. if (options.order_type === 'buyNow') {
  112. App._get('order/buyNow', Object.assign({}, params, {
  113. goods_id: options.goods_id,
  114. goods_num: options.goods_num,
  115. goods_sku_id: options.goods_sku_id,
  116. }), result => {
  117. callback(result);
  118. });
  119. }
  120. // 砍价活动结算
  121. else if (options.order_type === 'bargain') {
  122. App._get('bargain.order/checkout', Object.assign({}, params, {
  123. task_id: options.task_id,
  124. goods_sku_id: options.goods_sku_id,
  125. }), result => {
  126. callback(result);
  127. });
  128. }
  129. // 秒杀活动结算
  130. else if (options.order_type === 'sharp') {
  131. App._get('sharp.order/checkout', Object.assign({}, params, {
  132. active_time_id: options.active_time_id,
  133. sharp_goods_id: options.sharp_goods_id,
  134. goods_sku_id: options.goods_sku_id,
  135. goods_num: options.goods_num,
  136. }), result => {
  137. callback(result);
  138. });
  139. }
  140. // 购物车结算
  141. else if (options.order_type === 'cart') {
  142. App._get('order/cart', Object.assign({}, params, {
  143. cart_ids: options.cart_ids || 0,
  144. }), result => {
  145. callback(result);
  146. });
  147. }
  148. },
  149. /**
  150. * 切换配送方式
  151. */
  152. onSwichDelivery(e) {
  153. // 设置当前配送方式
  154. let _this = this;
  155. _this.setData({
  156. curDelivery: e.currentTarget.dataset.current
  157. });
  158. // 重新获取订单信息
  159. _this.getOrderData();
  160. },
  161. /**
  162. * 快递配送:选择收货地址
  163. */
  164. onSelectAddress() {
  165. let _this = this;
  166. // 允许刷新
  167. _this.setData({
  168. notRefresh: false
  169. });
  170. // 跳转到选择自提点
  171. wx.navigateTo({
  172. url: '../address/' + (_this.data.exist_address ? 'index?from=flow' : 'create')
  173. });
  174. },
  175. /**
  176. * 上门自提:选择自提点
  177. */
  178. onSelectExtractPoint() {
  179. let _this = this,
  180. selectedId = _this.data.selectedShopId;
  181. // 允许刷新
  182. _this.setData({
  183. notRefresh: false
  184. });
  185. // 跳转到选择自提点
  186. wx.navigateTo({
  187. url: '../_select/extract_point/index?selected_id=' + selectedId
  188. });
  189. },
  190. /**
  191. * 跳转到商品详情页
  192. */
  193. onTargetGoods(e) {
  194. wx.navigateTo({
  195. url: `../goods/index?goods_id=${e.currentTarget.dataset.id}`,
  196. })
  197. },
  198. /**
  199. * 订单提交
  200. */
  201. onSubmitOrder() {
  202. let _this = this,
  203. options = _this.data.options;
  204. if (_this.data.disabled) {
  205. return false;
  206. }
  207. // 表单验证
  208. if (!_this._onVerify()) {
  209. return false;
  210. }
  211. // 按钮禁用, 防止二次提交
  212. _this.data.disabled = true;
  213. let url = '';
  214. // 表单提交的数据
  215. let postData = {
  216. delivery: _this.data.curDelivery,
  217. pay_type: _this.data.curPayType,
  218. shop_id: _this.data.selectedShopId || 0,
  219. linkman: _this.data.linkman,
  220. phone: _this.data.phone,
  221. coupon_id: _this.data.selectCouponId || 0,
  222. is_use_points: _this.data.isUsePoints ? 1 : 0,
  223. remark: _this.data.remark || '',
  224. };
  225. // 创建订单-立即购买
  226. if (options.order_type === 'buyNow') {
  227. url = 'order/buyNow';
  228. postData = Object.assign(postData, {
  229. goods_id: options.goods_id,
  230. goods_num: options.goods_num,
  231. goods_sku_id: options.goods_sku_id,
  232. });
  233. }
  234. // 创建订单-购物车结算
  235. if (options.order_type === 'cart') {
  236. url = 'order/cart';
  237. postData = Object.assign(postData, {
  238. cart_ids: options.cart_ids || 0,
  239. });
  240. }
  241. // 创建订单-砍价活动
  242. if (options.order_type === 'bargain') {
  243. url = 'bargain.order/checkout';
  244. postData = Object.assign(postData, {
  245. task_id: options.task_id,
  246. goods_sku_id: options.goods_sku_id,
  247. });
  248. }
  249. // 创建订单-秒杀商品
  250. if (options.order_type === 'sharp') {
  251. url = 'sharp.order/checkout';
  252. postData = Object.assign(postData, {
  253. active_time_id: options.active_time_id,
  254. sharp_goods_id: options.sharp_goods_id,
  255. goods_sku_id: options.goods_sku_id,
  256. goods_num: options.goods_num,
  257. });
  258. }
  259. // 提交到后端
  260. const onCommitCallback = () => {
  261. // 显示loading
  262. wx.showLoading({
  263. title: '正在处理...'
  264. });
  265. // 订单提交
  266. App._post_form(url, postData, result => {
  267. _this._onSubmitCallback(result);
  268. }, result => {}, () => {
  269. wx.hideLoading();
  270. // 解除按钮禁用
  271. _this.data.disabled = false;
  272. });
  273. // 不允许刷新
  274. _this.setData({
  275. notRefresh: true
  276. });
  277. };
  278. // 请求用户订阅消息
  279. _this._onRequestSubscribeMessage(onCommitCallback);
  280. },
  281. /**
  282. * 请求用户订阅消息
  283. */
  284. _onRequestSubscribeMessage(onCommitCallback) {
  285. let _this = this,
  286. tmplIds = _this.data.setting.order_submsg;
  287. if (tmplIds.length == 0) {
  288. onCommitCallback();
  289. return;
  290. }
  291. wx.requestSubscribeMessage({
  292. tmplIds,
  293. success(res) {},
  294. fail(res) {},
  295. complete(res) {
  296. onCommitCallback();
  297. },
  298. });
  299. },
  300. /**
  301. * 订单提交成功后回调
  302. */
  303. _onSubmitCallback(result) {
  304. let _this = this;
  305. // 订单创建成功后回调--微信支付
  306. if (result.code === -10) {
  307. App.showError(result.msg, () => {
  308. _this.redirectToOrderIndex();
  309. });
  310. return false;
  311. }
  312. // 发起微信支付
  313. if (result.data.pay_type == PayTypeEnum.WECHAT.value) {
  314. App.wxPayment({
  315. payment: result.data.payment,
  316. success: res => {
  317. _this.redirectToOrderIndex();
  318. },
  319. fail: res => {
  320. App.showError(result.msg.error, () => {
  321. _this.redirectToOrderIndex();
  322. });
  323. },
  324. });
  325. }
  326. // 余额支付
  327. if (result.data.pay_type == PayTypeEnum.BALANCE.value) {
  328. App.showSuccess(result.msg.success, () => {
  329. _this.redirectToOrderIndex();
  330. });
  331. }
  332. },
  333. /**
  334. * 表单验证
  335. */
  336. _onVerify() {
  337. let _this = this;
  338. if (_this.data.has_error) {
  339. App.showError(_this.data.error_msg);
  340. return false;
  341. }
  342. // 验证自提填写的联系方式
  343. if (_this.data.curDelivery == DeliveryTypeEnum.EXTRACT.value) {
  344. _this.setData({
  345. linkman: _this.data.linkman.trim(),
  346. phone: _this.data.phone.trim(),
  347. });
  348. if (_this.data.selectedShopId <= 0) {
  349. App.showError('请选择自提的门店');
  350. return false;
  351. }
  352. if (Verify.isEmpty(_this.data.linkman)) {
  353. App.showError('请填写自提联系人');
  354. return false;
  355. }
  356. if (Verify.isEmpty(_this.data.phone)) {
  357. App.showError('请填写自提联系电话');
  358. return false;
  359. }
  360. if (!Verify.isPhone(_this.data.phone)) {
  361. App.showError('请输入正确的联系电话');
  362. return false;
  363. }
  364. }
  365. return true;
  366. },
  367. /**
  368. * 买家留言
  369. */
  370. bindRemark(e) {
  371. let _this = this;
  372. _this.setData({
  373. remark: e.detail.value
  374. })
  375. },
  376. /**
  377. * 选择优惠券(弹出/隐藏)
  378. */
  379. onTogglePopupCoupon() {
  380. let _this = this;
  381. if (_this.data.coupon_list.length > 0) {
  382. _this.setData({
  383. showBottomPopup: !_this.data.showBottomPopup
  384. });
  385. }
  386. },
  387. /**
  388. * 选择优惠券
  389. */
  390. onSelectCoupon(e) {
  391. const app = this;
  392. // 当前选择的优惠券
  393. const index = e.currentTarget.dataset.index
  394. const couponItem = app.data.coupon_list[index]
  395. // 判断是否在适用范围
  396. if (!couponItem['is_apply']) {
  397. Toast({
  398. selector: '#zan-toast',
  399. message: couponItem.not_apply_info,
  400. timeout: 1800
  401. });
  402. return
  403. }
  404. // 记录选中的优惠券id
  405. app.setData({
  406. selectCouponId: couponItem.user_coupon_id
  407. });
  408. // 重新获取订单信息
  409. app.getOrderData();
  410. // 隐藏优惠券弹层
  411. app.onTogglePopupCoupon();
  412. },
  413. /**
  414. * 不使用优惠券
  415. */
  416. onNotUseCoupon() {
  417. let _this = this;
  418. _this.setData({
  419. selectCouponId: 0
  420. });
  421. // 重新获取订单信息
  422. _this.getOrderData();
  423. // 隐藏优惠券弹层
  424. _this.onTogglePopupCoupon();
  425. },
  426. /**
  427. * 选择支付方式
  428. */
  429. onSelectPayType(e) {
  430. let _this = this;
  431. // 设置当前支付方式
  432. _this.setData({
  433. curPayType: e.currentTarget.dataset.value
  434. });
  435. },
  436. /**
  437. * 跳转到未付款订单
  438. */
  439. redirectToOrderIndex() {
  440. wx.redirectTo({
  441. url: '../order/index',
  442. });
  443. },
  444. /**
  445. * input绑定:联系人
  446. */
  447. onInputLinkman(e) {
  448. let _this = this;
  449. _this.setData({
  450. linkman: e.detail.value
  451. });
  452. },
  453. /**
  454. * input绑定:联系电话
  455. */
  456. onInputPhone(e) {
  457. let _this = this;
  458. _this.setData({
  459. phone: e.detail.value
  460. });
  461. },
  462. /**
  463. * 选择积分抵扣
  464. */
  465. onTriggerPoints({
  466. detail
  467. }) {
  468. let _this = this;
  469. _this.setData({
  470. isUsePoints: detail
  471. });
  472. // 重新获取订单信息
  473. _this.getOrderData();
  474. },
  475. /**
  476. * 显示积分说明
  477. */
  478. onShowPoints(e) {
  479. let _this = this;
  480. // 显示dialog
  481. let setting = _this.data.setting;
  482. Dialog({
  483. title: `${setting.points_name}说明`,
  484. message: setting.points_describe,
  485. selector: '#zan-base-dialog',
  486. isScroll: true, // 滚动
  487. buttons: [{
  488. text: '关闭',
  489. color: 'red',
  490. type: 'cash'
  491. }]
  492. });
  493. },
  494. });