index.js 17 KB

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