index.js 9.9 KB

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