index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. import {
  2. orderConfirm,
  3. getCouponsOrderPrice,
  4. orderCreate,
  5. postOrderComputed
  6. } from '../../api/order.js';
  7. import {
  8. getAddressDefault,
  9. getAddressDetail
  10. } from '../../api/user.js';
  11. import {
  12. openPaySubscribe
  13. } from '../../utils/SubscribeMessage.js';
  14. import {
  15. storeListApi
  16. } from '../../api/store.js';
  17. import {
  18. CACHE_LONGITUDE,
  19. CACHE_LATITUDE
  20. } from '../../config.js';
  21. const app = getApp();
  22. Page({
  23. /**
  24. * 页面的初始数据
  25. */
  26. data: {
  27. textareaStatus: true,
  28. parameter: {
  29. 'navbar': '1',
  30. 'return': '1',
  31. 'title': '提交订单',
  32. 'color': true,
  33. 'class': '0'
  34. },
  35. //支付方式
  36. cartArr: [{
  37. "name": "微信支付",
  38. "icon": "icon-weixin2",
  39. value: 'weixin',
  40. title: '微信快捷支付'
  41. },
  42. {
  43. "name": "余额支付",
  44. "icon": "icon-icon-test",
  45. value: 'yue',
  46. title: '可用余额:'
  47. },
  48. {
  49. "name": "线下支付",
  50. "icon": "icon-yinhangqia",
  51. value: 'offline',
  52. title: '线下支付'
  53. },
  54. ],
  55. payType: 'weixin', //支付方式
  56. openType: 1, //优惠券打开方式 1=使用
  57. active: 0, //支付方式切换
  58. coupon: {
  59. coupon: false,
  60. list: [],
  61. statusTile: '立即使用'
  62. }, //优惠券组件
  63. address: {
  64. address: false
  65. }, //地址组件
  66. addressInfo: {}, //地址信息
  67. pinkId: 0, //拼团id
  68. addressId: 0, //地址id
  69. couponId: 0, //优惠券id
  70. cartId: '', //购物车id
  71. userInfo: {}, //用户信息
  72. mark: '', //备注信息
  73. couponTitle: '请选择', //优惠券
  74. coupon_price: 0, //优惠券抵扣金额
  75. useIntegral: false, //是否使用积分
  76. integral_price: 0, //积分抵扣金额
  77. ChangePrice: 0, //使用积分抵扣变动后的金额
  78. formIds: [], //收集formid
  79. status: 0,
  80. is_address: false,
  81. isClose: false,
  82. toPay: false, //修复进入支付时页面隐藏从新刷新页面
  83. shippingType: 0,
  84. system_store: {},
  85. storePostage: 0,
  86. contacts: '',
  87. contactsTel: '',
  88. mydata: {},
  89. storeList: []
  90. },
  91. /**
  92. * 授权回调事件
  93. */
  94. onLoadFun: function () {
  95. this.getaddressInfo();
  96. this.getConfirm();
  97. //调用子页面方法授权后执行获取地址列表
  98. this.selectComponent('#address-window').getAddressList();
  99. },
  100. /**
  101. * 生命周期函数--监听页面显示
  102. */
  103. onShow: function () {
  104. this.setData({
  105. textareaStatus: true
  106. });
  107. if (app.globalData.isLog && this.data.isClose && this.data.toPay == false) {
  108. this.getaddressInfo();
  109. this.selectComponent('#address-window').getAddressList();
  110. }
  111. let pages = getCurrentPages();
  112. let currPage = pages[pages.length - 1]; //当前页面
  113. if (currPage.data.storeItem) {
  114. let json = currPage.data.storeItem;
  115. this.setData({
  116. system_store: json,
  117. });
  118. }
  119. },
  120. /**
  121. * 获取门店列表数据
  122. */
  123. getList: function () {
  124. let longitude = wx.getStorageSync(CACHE_LONGITUDE); //经度
  125. let latitude = wx.getStorageSync(CACHE_LATITUDE); //纬度
  126. let data = {
  127. latitude: latitude, //纬度
  128. longitude: longitude, //经度
  129. page: 1,
  130. limit: 10
  131. }
  132. storeListApi(data).then(res => {
  133. let list = res.data.list || [];
  134. this.setData({
  135. storeList: list,
  136. system_store: list[0],
  137. });
  138. }).catch(err => {
  139. })
  140. },
  141. /*
  142. * 跳转门店列表
  143. */
  144. showStoreList: function () {
  145. wx.navigateTo({
  146. url: '/pages/goods_details_store/index?go=order'
  147. })
  148. },
  149. computedPrice: function () {
  150. let shippingType = this.data.shippingType;
  151. postOrderComputed(this.data.orderKey, {
  152. addressId: this.data.addressId,
  153. useIntegral: this.data.useIntegral ? 1 : 0,
  154. couponId: this.data.couponId,
  155. shipping_type: parseInt(shippingType) + 1,
  156. payType: this.data.payType
  157. }).then(res => {
  158. let result = res.data.result;
  159. if (result) {
  160. this.setData({
  161. totalPrice: result.pay_price,
  162. integral_price: result.deduction_price,
  163. coupon_price: result.coupon_price,
  164. integral: this.data.useIntegral ? result.SurplusIntegral : this.data.userInfo.integral,
  165. 'priceGroup.storePostage': shippingType == 1 ? 0 : result.pay_postage,
  166. });
  167. }
  168. })
  169. },
  170. addressType: function (e) {
  171. let index = e.currentTarget.dataset.index;
  172. if (this.data.storeList.length > 0) {
  173. this.setData({
  174. shippingType: parseInt(index)
  175. });
  176. } else {
  177. if (index == 1) {
  178. return app.Tips({
  179. title: '暂无门店信息,你无法选择到店自提'
  180. });
  181. }
  182. }
  183. this.computedPrice();
  184. },
  185. bindPickerChange: function (e) {
  186. let value = e.detail.value;
  187. this.setData({
  188. shippingType: value
  189. })
  190. this.computedPrice();
  191. },
  192. /**
  193. * 生命周期函数--监听页面隐藏
  194. */
  195. onHide: function () {
  196. this.setData({
  197. isClose: true
  198. });
  199. },
  200. ChangCouponsClone: function () {
  201. this.setData({
  202. 'coupon.coupon': false
  203. });
  204. },
  205. changeTextareaStatus: function () {
  206. for (var i = 0, len = this.data.coupon.list.length; i < len; i++) {
  207. this.data.coupon.list[i].use_title = '';
  208. this.data.coupon.list[i].is_use = 0;
  209. }
  210. this.setData({
  211. textareaStatus: true,
  212. status: 0,
  213. "coupon.list": this.data.coupon.list
  214. });
  215. },
  216. /**
  217. * 处理点击优惠券后的事件
  218. */
  219. ChangCoupons: function (e) {
  220. var index = e.detail,
  221. list = this.data.coupon.list,
  222. couponTitle = '请选择',
  223. couponId = 0;
  224. for (var i = 0, len = list.length; i < len; i++) {
  225. if (i != index) {
  226. list[i].use_title = '';
  227. list[i].is_use = 0;
  228. }
  229. }
  230. if (list[index].is_use) {
  231. //不使用优惠券
  232. list[index].use_title = '';
  233. list[index].is_use = 0;
  234. } else {
  235. //使用优惠券
  236. list[index].use_title = '不使用';
  237. list[index].is_use = 1;
  238. couponTitle = list[index].coupon_title;
  239. couponId = list[index].id;
  240. }
  241. this.setData({
  242. couponTitle: couponTitle,
  243. couponId: couponId,
  244. 'coupon.coupon': false,
  245. "coupon.list": list,
  246. });
  247. this.computedPrice();
  248. },
  249. /**
  250. * 使用积分抵扣
  251. */
  252. ChangeIntegral: function () {
  253. this.setData({
  254. useIntegral: !this.data.useIntegral
  255. });
  256. this.computedPrice();
  257. },
  258. /**
  259. * 选择地址后改变事件
  260. * @param object e
  261. */
  262. OnChangeAddress: function (e) {
  263. this.setData({
  264. textareaStatus: true,
  265. addressId: e.detail,
  266. 'address.address': false
  267. });
  268. this.getaddressInfo();
  269. this.computedPrice();
  270. },
  271. /**
  272. * 生命周期函数--监听页面加载
  273. */
  274. onLoad: function (options) {
  275. if (!options.cartId) return app.Tips({
  276. title: '请选择要购买的商品'
  277. }, {
  278. tab: 3,
  279. url: 1
  280. });
  281. // if (options.shippingType) this.setData({ shippingType: options.shippingType, storeIndex: options.storeIndex });
  282. this.setData({
  283. couponId: options.couponId || 0,
  284. pinkId: options.pinkId ? parseInt(options.pinkId) : 0,
  285. addressId: options.addressId || 0,
  286. cartId: options.cartId,
  287. is_address: options.is_address ? true : false
  288. });
  289. },
  290. bindHideKeyboard: function (e) {
  291. this.setData({
  292. mark: e.detail.value
  293. });
  294. },
  295. /**
  296. * 获取当前订单详细信息
  297. */
  298. getConfirm: function () {
  299. var that = this;
  300. orderConfirm(this.data.cartId).then(res => {
  301. that.setData({
  302. userInfo: res.data.userInfo,
  303. integral: res.data.userInfo.integral,
  304. cartInfo: res.data.cartInfo,
  305. integralRatio: res.data.integralRatio,
  306. offlinePostage: res.data.offlinePostage,
  307. orderKey: res.data.orderKey,
  308. priceGroup: res.data.priceGroup,
  309. totalPrice: app.help().Add(parseFloat(res.data.priceGroup.totalPrice), parseFloat(res.data.priceGroup.storePostage)),
  310. seckillId: parseInt(res.data.seckill_id),
  311. usableCoupon: res.data.usableCoupon,
  312. // system_store: res.data.system_store,
  313. store_self_mention: res.data.store_self_mention
  314. });
  315. that.data.cartArr[1].title = '可用余额:' + res.data.userInfo.now_money;
  316. if (res.data.offline_pay_status == 2) that.data.cartArr.pop();
  317. that.setData({
  318. cartArr: that.data.cartArr,
  319. ChangePrice: that.data.totalPrice
  320. });
  321. that.getBargainId();
  322. that.getCouponList();
  323. that.getList();
  324. }).catch(err => {
  325. return app.Tips({
  326. title: err
  327. }, {
  328. tab: 3,
  329. url: 1
  330. });
  331. });
  332. },
  333. /*
  334. * 提取砍价和拼团id
  335. */
  336. getBargainId: function () {
  337. var that = this;
  338. var cartINfo = that.data.cartInfo;
  339. var BargainId = 0;
  340. var combinationId = 0;
  341. cartINfo.forEach(function (value, index, cartINfo) {
  342. BargainId = cartINfo[index].bargain_id,
  343. combinationId = cartINfo[index].combination_id
  344. })
  345. that.setData({
  346. BargainId: parseInt(BargainId),
  347. combinationId: parseInt(combinationId)
  348. });
  349. if (that.data.cartArr.length == 3 && (BargainId || combinationId || that.data.seckillId)) {
  350. that.data.cartArr.pop();
  351. that.setData({
  352. cartArr: that.data.cartArr
  353. });
  354. }
  355. },
  356. /**
  357. * 获取当前金额可用优惠券
  358. */
  359. getCouponList: function () {
  360. var that = this;
  361. let data = {
  362. cartId: this.data.cartId
  363. }
  364. getCouponsOrderPrice(this.data.totalPrice, data).then(res => {
  365. that.setData({
  366. 'coupon.list': res.data,
  367. openType: 1
  368. });
  369. });
  370. },
  371. /*
  372. * 获取默认收货地址或者获取某条地址信息
  373. */
  374. getaddressInfo: function () {
  375. var that = this;
  376. if (that.data.addressId) {
  377. getAddressDetail(that.data.addressId).then(res => {
  378. res.data.is_default = parseInt(res.data.is_default);
  379. that.setData({
  380. addressInfo: res.data || {},
  381. addressId: res.data.id || 0,
  382. 'address.addressId': res.data.id || 0
  383. });
  384. })
  385. } else {
  386. getAddressDefault().then(res => {
  387. res.data.is_default = parseInt(res.data.is_default);
  388. that.setData({
  389. addressInfo: res.data || {},
  390. addressId: res.data.id || 0,
  391. 'address.addressId': res.data.id || 0
  392. });
  393. })
  394. }
  395. },
  396. payItem: function (e) {
  397. var that = this;
  398. var active = e.currentTarget.dataset.index;
  399. that.setData({
  400. active: active,
  401. animated: true,
  402. payType: that.data.cartArr[active].value,
  403. });
  404. that.computedPrice();
  405. setTimeout(function () {
  406. that.car();
  407. }, 500);
  408. },
  409. coupon: function () {
  410. this.setData({
  411. 'coupon.coupon': true
  412. })
  413. },
  414. car: function () {
  415. var that = this;
  416. that.setData({
  417. animated: false
  418. });
  419. },
  420. onAddress: function () {
  421. this.setData({
  422. textareaStatus: false,
  423. 'address.address': true,
  424. pagesUrl: '/pages/user_address_list/index?cartId=' + this.data.cartId + '&pinkId=' + this.data.pinkId + '&couponId=' + this.data.couponId
  425. });
  426. },
  427. realName: function (e) {
  428. this.setData({
  429. contacts: e.detail.value
  430. })
  431. },
  432. phone: function (e) {
  433. this.setData({
  434. contactsTel: e.detail.value
  435. })
  436. },
  437. SubOrder: function (e) {
  438. var that = this,
  439. data = {};
  440. if (!this.data.payType) return app.Tips({
  441. title: '请选择支付方式'
  442. });
  443. if (!this.data.addressId && !this.data.shippingType) return app.Tips({
  444. title: '请选择收货地址'
  445. });
  446. if (this.data.shippingType == 1) {
  447. if (this.data.contacts == "" || this.data.contactsTel == "")
  448. return app.Tips({
  449. title: '请填写联系人或联系人电话'
  450. });
  451. if (!/^1(3|4|5|7|8|9|6)\d{9}$/.test(this.data.contactsTel)) {
  452. return app.Tips({
  453. title: '请填写正确的手机号'
  454. });
  455. }
  456. if (!/^[\u4e00-\u9fa5\w]{2,16}$/.test(this.data.contacts)) {
  457. return app.Tips({
  458. title: '请填写您的真实姓名'
  459. });
  460. }
  461. }
  462. data = {
  463. real_name: that.data.contacts,
  464. phone: that.data.contactsTel,
  465. addressId: that.data.addressId,
  466. formId: '',
  467. couponId: that.data.couponId,
  468. payType: that.data.payType,
  469. useIntegral: that.data.useIntegral,
  470. bargainId: that.data.BargainId,
  471. combinationId: that.data.combinationId,
  472. pinkId: that.data.pinkId,
  473. seckill_id: that.data.seckillId,
  474. mark: that.data.mark,
  475. store_id: that.data.system_store ? that.data.system_store.id : 0,
  476. 'from': 'routine',
  477. shipping_type: app.help().Add(that.data.shippingType, 1)
  478. };
  479. if (data.payType == 'yue' && parseFloat(that.data.userInfo.now_money) < parseFloat(that.data.totalPrice)) return app.Tips({
  480. title: '余额不足!'
  481. });
  482. wx.showLoading({
  483. title: '订单支付中'
  484. });
  485. openPaySubscribe().then(() => {
  486. orderCreate(this.data.orderKey, data).then(res => {
  487. var status = res.data.status,
  488. orderId = res.data.result.orderId,
  489. jsConfig = res.data.result.jsConfig,
  490. goPages = '/pages/order_pay_status/index?order_id=' + orderId + '&msg=' + res.msg;
  491. switch (status) {
  492. case 'ORDER_EXIST':
  493. case 'EXTEND_ORDER':
  494. case 'PAY_ERROR':
  495. wx.hideLoading();
  496. return app.Tips({
  497. title: res.msg
  498. }, {
  499. tab: 5,
  500. url: goPages
  501. });
  502. break;
  503. case 'SUCCESS':
  504. wx.hideLoading();
  505. if (that.data.BargainId || that.data.combinationId || that.data.pinkId || that.data.seckillId)
  506. return app.Tips({
  507. title: res.msg,
  508. icon: 'success'
  509. }, {
  510. tab: 4,
  511. url: goPages
  512. });
  513. return app.Tips({
  514. title: res.msg,
  515. icon: 'success'
  516. }, {
  517. tab: 5,
  518. url: goPages
  519. });
  520. break;
  521. case 'WECHAT_PAY':
  522. that.setData({
  523. toPay: true
  524. });
  525. wx.requestPayment({
  526. timeStamp: jsConfig.timestamp,
  527. nonceStr: jsConfig.nonceStr,
  528. package: jsConfig.package,
  529. signType: jsConfig.signType,
  530. paySign: jsConfig.paySign,
  531. success: function (res) {
  532. wx.hideLoading();
  533. if (that.data.BargainId || that.data.combinationId || that.data.pinkId || that.data.seckillId)
  534. return app.Tips({
  535. title: '支付成功',
  536. icon: 'success'
  537. }, {
  538. tab: 4,
  539. url: goPages
  540. });
  541. return app.Tips({
  542. title: '支付成功',
  543. icon: 'success'
  544. }, {
  545. tab: 5,
  546. url: goPages
  547. });
  548. },
  549. fail: function (e) {
  550. wx.hideLoading();
  551. return app.Tips({
  552. title: '取消支付'
  553. }, {
  554. tab: 5,
  555. url: goPages + '&status=2'
  556. });
  557. },
  558. complete: function (e) {
  559. wx.hideLoading();
  560. //关闭当前页面跳转至订单状态
  561. if (res.errMsg == 'requestPayment:cancel') return app.Tips({
  562. title: '取消支付'
  563. }, {
  564. tab: 5,
  565. url: goPages + '&status=2'
  566. });
  567. },
  568. })
  569. break;
  570. case 'PAY_DEFICIENCY':
  571. wx.hideLoading();
  572. //余额不足
  573. return app.Tips({
  574. title: res.msg
  575. }, {
  576. tab: 5,
  577. url: goPages + '&status=1'
  578. });
  579. break;
  580. }
  581. }).catch(err => {
  582. wx.hideLoading();
  583. return app.Tips({
  584. title: err
  585. });
  586. });
  587. });
  588. }
  589. })