index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // nova-tourism/pages/collect/collect-detail/index.js
  2. let Parse = getApp().Parse;
  3. const company = getApp().globalData.company
  4. const uid = Parse.User.current()?.id
  5. let sev = require('../../../service/request')
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. statusBarHeight: 0,
  12. screenHeight: 0,
  13. customHeight: 0,
  14. bottomNavHeight: 0,
  15. contentHeight: 0,
  16. gid: null, //商品id
  17. good: null, //商品详情
  18. spec: null, //商品规格
  19. checkSpec: null, //选中的规格
  20. totalPrice: 0, //订单总价
  21. address: null, //收货地址
  22. tradeNo: null, //订单编号
  23. order: null, //当前订单-parse
  24. distributeType: 'delivery', //配送方式-默认物流配送
  25. count: 1, //单独购买商品总数
  26. specMap: {}, //save到订单的数据
  27. specBox: false, //规格弹框
  28. moreBox: false, //选择多款弹框
  29. listBox: false, //多款列表弹框
  30. specGoodList: [], //选择的多款商品-多款
  31. checkSpec_more: null, //选中的规格-多款
  32. count_more: 1, //购买商品总数-多款
  33. totalPrice_more: 0, //订单总价-多款
  34. },
  35. /**
  36. * 生命周期函数--监听页面加载
  37. */
  38. onLoad: function (options) {
  39. const systemInfo = wx.getSystemInfoSync();
  40. const statusBarHeight = systemInfo.statusBarHeight || 0;
  41. const screenHeight = systemInfo.screenHeight || 0;
  42. const custom = wx.getMenuButtonBoundingClientRect();
  43. const customHeight = custom.height + 10 + 2 || 0;
  44. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  45. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  46. this.setData({
  47. statusBarHeight,
  48. screenHeight,
  49. customHeight,
  50. bottomNavHeight,
  51. contentHeight
  52. });
  53. this.setData({
  54. gid: options?.gid
  55. })
  56. this.refersh()
  57. },
  58. async refersh() {
  59. this.getGood()
  60. },
  61. /**获取商品 */
  62. async getGood() {
  63. let {
  64. gid
  65. } = this.data
  66. let query = new Parse.Query('ShopGoods')
  67. query.include('shopStore')
  68. let d = await query.get(gid)
  69. let good = d?.toJSON()
  70. if (!good?.images || good?.images?.length <= 0) good.images = [good.image || 'https://file-cloud.fmode.cn/EbxZUK5lBI/20250321/vdq1j1110711455.jpg']
  71. if (!good?.specMap || !good?.specMap?.specList || good?.specMap?.specList?.length <= 0) {
  72. good.specMap = {
  73. specList: [good?.name || '暂无规格']
  74. }
  75. good.specMap[good?.name || '暂无规格'] = [{
  76. price: good?.price || 0,
  77. }]
  78. }
  79. console.log(good)
  80. let spec = good?.specMap?.specList
  81. let checkSpec = {}
  82. if (spec && spec?.length > 0) {
  83. checkSpec = good?.specMap[spec[0]][0]
  84. checkSpec.title = spec[0]
  85. }
  86. let totalPrice = checkSpec?.price || good?.price
  87. console.log(spec, checkSpec)
  88. this.setData({
  89. good,
  90. store: good.shopStore,
  91. spec,
  92. checkSpec,
  93. totalPrice
  94. })
  95. this.getTotalPrice()
  96. },
  97. /**切换配送方式 */
  98. chickDistributeType(e) {
  99. let {
  100. type
  101. } = e.currentTarget.dataset
  102. this.setData({
  103. distributeType: type
  104. })
  105. },
  106. /** 打开规格弹框*/
  107. openSpec() {
  108. let {
  109. spec
  110. } = this.data
  111. if (spec?.length <= 0) return
  112. this.setData({
  113. specBox: true
  114. })
  115. },
  116. /**关闭规格弹框 */
  117. closeSpec() {
  118. this.setData({
  119. specBox: false
  120. })
  121. },
  122. /**选择规格 */
  123. getCheckSpec(e) {
  124. let {
  125. good,
  126. spec,
  127. checkSpec,
  128. totalPrice,
  129. } = this.data
  130. let {
  131. title
  132. } = e.currentTarget.dataset
  133. checkSpec = good?.specMap[title][0]
  134. checkSpec.title = title
  135. totalPrice = checkSpec?.price || good?.price
  136. console.log(spec, checkSpec)
  137. this.setData({
  138. spec,
  139. checkSpec,
  140. totalPrice,
  141. })
  142. this.getTotalPrice()
  143. },
  144. /**单个物品-步进器改变 */
  145. changeCount(e) {
  146. this.setData({
  147. count: e.detail
  148. })
  149. this.getTotalPrice()
  150. },
  151. /**计算单个物品总价/获取specMap */
  152. getTotalPrice() {
  153. let {
  154. good,
  155. count,
  156. checkSpec
  157. } = this.data
  158. let specMap = {}
  159. specMap[good.objectId] = {
  160. count,
  161. price: checkSpec.price,
  162. spec: checkSpec?.title || ''
  163. }
  164. let totalPrice = parseFloat((count * checkSpec.price).toFixed(2))
  165. console.log(specMap, totalPrice)
  166. this.setData({
  167. totalPrice,
  168. specMap
  169. })
  170. },
  171. /** 打开多款弹框*/
  172. openMoreBox() {
  173. let {
  174. spec,
  175. checkSpec,
  176. good
  177. } = this.data
  178. console.log(spec, checkSpec, good)
  179. let price = good.specMap[spec[0]][0].price || good.price
  180. this.setData({
  181. moreBox: true,
  182. count_more: 1,
  183. checkSpec_more: {
  184. price: price,
  185. title: spec[0],
  186. totalPrice: price
  187. }
  188. })
  189. console.log(spec, checkSpec, good)
  190. },
  191. /**关闭多款弹框 */
  192. closeMoreBox() {
  193. this.setData({
  194. moreBox: false
  195. })
  196. },
  197. /**多款-步进器改变 */
  198. changeCount_more(e) {
  199. this.setData({
  200. count_more: e.detail
  201. })
  202. this.getCurrentSpec()
  203. },
  204. /**选择规格-多款 */
  205. getCheckSpec_more(e) {
  206. let {
  207. good,
  208. spec,
  209. checkSpec_more,
  210. } = this.data
  211. let {
  212. title
  213. } = e.currentTarget.dataset
  214. checkSpec_more = good?.specMap[title][0]
  215. checkSpec_more.title = title
  216. console.log(spec, checkSpec_more)
  217. this.setData({
  218. checkSpec_more,
  219. })
  220. this.getCurrentSpec()
  221. },
  222. /**计算当前款式价格/specMap - 多款 */
  223. getCurrentSpec() {
  224. let {
  225. checkSpec_more,
  226. count_more
  227. } = this.data
  228. let totalPrice = (checkSpec_more.price * count_more || 0).toFixed(2)
  229. checkSpec_more.totalPrice = totalPrice
  230. console.log(checkSpec_more, count_more)
  231. this.setData({
  232. checkSpec_more
  233. })
  234. },
  235. /**确定当前款 */
  236. confirmCurrent() {
  237. let {
  238. specGoodList,
  239. count_more,
  240. checkSpec_more,
  241. } = this.data
  242. let index = specGoodList?.findIndex(item => item.spec == checkSpec_more.title)
  243. if (index < 0) {
  244. specGoodList.push({
  245. count: count_more,
  246. price: checkSpec_more.price,
  247. spec: checkSpec_more.title,
  248. checked: true
  249. })
  250. } else {
  251. specGoodList[index] = {
  252. count: count_more,
  253. price: checkSpec_more.price,
  254. spec: checkSpec_more.title,
  255. checked: true
  256. }
  257. wx.showToast({
  258. title: `已勾选并修改 ${checkSpec_more.title} 款式数量为${count_more}`,
  259. icon: 'none'
  260. })
  261. }
  262. this.setData({
  263. specGoodList
  264. })
  265. this.getTotalPrice_more()
  266. this.closeMoreBox()
  267. this.openListBox()
  268. },
  269. /**计算物品总价 - 多款*/
  270. getTotalPrice_more() {
  271. let {
  272. good,
  273. specGoodList,
  274. totalPrice_more,
  275. } = this.data
  276. totalPrice_more = 0
  277. let list = specGoodList.filter(item => item.checked)
  278. for (let i in list) {
  279. let item = list[i]
  280. if (item.checked) {
  281. totalPrice_more = parseFloat((totalPrice_more + (item.price * item.count)).toFixed(2))
  282. }
  283. }
  284. let specMap = {}
  285. specMap[good.objectId] = {
  286. price: totalPrice_more || 0,
  287. list: list || []
  288. }
  289. console.log(specMap, specGoodList, totalPrice_more)
  290. this.setData({
  291. totalPrice_more: parseFloat(totalPrice_more),
  292. specMap,
  293. specGoodList
  294. })
  295. },
  296. /**列表-多款-步进器改变 */
  297. changeLMCount(e) {
  298. let {
  299. specGoodList
  300. } = this.data
  301. let {
  302. index
  303. } = e.currentTarget.dataset
  304. specGoodList[index].count = parseInt(e.detail || 1)
  305. this.setData({
  306. specGoodList
  307. })
  308. this.getTotalPrice_more()
  309. },
  310. /**列表-是否选中 */
  311. changeChecked(e) {
  312. console.log(e)
  313. let {
  314. specGoodList
  315. } = this.data
  316. let {
  317. index
  318. } = e.currentTarget.dataset
  319. specGoodList[index].checked = e.detail
  320. this.setData({
  321. specGoodList
  322. })
  323. this.getTotalPrice_more()
  324. },
  325. /**多款款式弹框-去结算 */
  326. settlement() {
  327. this.closeMoreBox()
  328. this.openListBox()
  329. },
  330. /** 打开多款列表弹框*/
  331. openListBox() {
  332. this.setData({
  333. listBox: true,
  334. })
  335. },
  336. /**关闭多款列表弹框 */
  337. closeListBox() {
  338. this.setData({
  339. listBox: false
  340. })
  341. },
  342. /** 判断是否可创建订单*/
  343. judgeOrder() {
  344. let {
  345. address,
  346. distributeType
  347. } = this.data
  348. if (distributeType == 'delivery' && !address?.objectId) {
  349. wx.showToast({
  350. title: '请确认收货地址',
  351. icon: 'none',
  352. });
  353. return false
  354. }
  355. return true
  356. },
  357. /** 判断是否可创建订单-多款*/
  358. judgeOrder_more() {
  359. let addCheck = this.judgeOrder()
  360. let {specGoodList} = this.data
  361. let index = specGoodList.findIndex(item=>item.checked)
  362. if(addCheck&&index!=-1) return true
  363. return false
  364. },
  365. /**创建订单-单个商品 */
  366. async submitOrder() {
  367. this.getTotalPrice()
  368. let isPass = this.judgeOrder()
  369. if (!isPass) return
  370. let {
  371. good,
  372. totalPrice,
  373. address,
  374. order,
  375. tradeNo,
  376. specMap,
  377. distributeType
  378. } = this.data
  379. let o = order
  380. if (!order?.id) {
  381. let Order = Parse.Object.extend("Order");
  382. o = new Order()
  383. o.set("company", {
  384. __type: 'Pointer',
  385. className: 'Company',
  386. objectId: company
  387. })
  388. o.set("store", {
  389. __type: 'Pointer',
  390. className: 'ShopStore',
  391. objectId: good?.shopStore?.objectId
  392. })
  393. o.set("user", {
  394. __type: 'Pointer',
  395. className: '_User',
  396. objectId: uid
  397. })
  398. o.set('status', '100') //待支付
  399. o.set('type', 'scenery')
  400. o.set("targetObject", [{
  401. "__type": "Pointer",
  402. "className": "ShopGoods",
  403. "objectId": good?.objectId
  404. }])
  405. }
  406. let now = new Date()
  407. tradeNo = "C" +
  408. String(now.getFullYear()) +
  409. (now.getMonth() + 1) +
  410. now.getDate() +
  411. now.getHours() +
  412. now.getMinutes() +
  413. now.getSeconds() +
  414. Math.random().toString().slice(-6);
  415. o.set("orderNum", tradeNo);
  416. o.set("totalPrice", totalPrice || 0);
  417. o.set("distributeType", distributeType || 'verify');
  418. if (distributeType == 'verify') {
  419. o.set("info", {});
  420. } else {
  421. o.set("address", {
  422. __type: 'Pointer',
  423. className: 'ShopAddress',
  424. objectId: address?.objectId
  425. });
  426. o.set("info", {
  427. name: address?.name,
  428. mobile: address?.mobile,
  429. address: address?.full_region + address?.address
  430. });
  431. }
  432. o.set("specMap", specMap);
  433. try {
  434. order = await o.save()
  435. this.setData({
  436. tradeNo,
  437. order: o,
  438. showPay: true,
  439. showPay_more:false,
  440. })
  441. wx.showToast({
  442. title: '已创建订单',
  443. icon: 'none'
  444. })
  445. } catch (error) {
  446. wx.showToast({
  447. title: '出错了请检查后重试',
  448. icon: 'error'
  449. })
  450. }
  451. },
  452. /**创建订单-多款规格 */
  453. async submitOrder_more() {
  454. this.getTotalPrice_more()
  455. let isPass = this.judgeOrder_more()
  456. if (!isPass) return
  457. let {
  458. good,
  459. totalPrice_more,
  460. address,
  461. order,
  462. tradeNo,
  463. specMap,
  464. distributeType
  465. } = this.data
  466. let o = order
  467. if (!order?.id) {
  468. let Order = Parse.Object.extend("Order");
  469. o = new Order()
  470. o.set("company", {
  471. __type: 'Pointer',
  472. className: 'Company',
  473. objectId: company
  474. })
  475. o.set("store", {
  476. __type: 'Pointer',
  477. className: 'ShopStore',
  478. objectId: good?.shopStore?.objectId
  479. })
  480. o.set("user", {
  481. __type: 'Pointer',
  482. className: '_User',
  483. objectId: uid
  484. })
  485. o.set('status', '100') //待支付
  486. o.set('type', 'scenery')
  487. o.set("targetObject", [{
  488. "__type": "Pointer",
  489. "className": "ShopGoods",
  490. "objectId": good?.objectId
  491. }])
  492. }
  493. let now = new Date()
  494. tradeNo = "C" +
  495. String(now.getFullYear()) +
  496. (now.getMonth() + 1) +
  497. now.getDate() +
  498. now.getHours() +
  499. now.getMinutes() +
  500. now.getSeconds() +
  501. Math.random().toString().slice(-6);
  502. o.set("orderNum", tradeNo);
  503. o.set("totalPrice", totalPrice_more || 0);
  504. o.set("distributeType", distributeType || 'verify');
  505. if (distributeType == 'verify') {
  506. o.set("info", {});
  507. } else {
  508. o.set("address", {
  509. __type: 'Pointer',
  510. className: 'ShopAddress',
  511. objectId: address?.objectId
  512. });
  513. o.set("info", {
  514. name: address?.name,
  515. mobile: address?.mobile,
  516. address: address?.full_region + address?.address
  517. });
  518. }
  519. o.set("specMap", specMap);
  520. try {
  521. order = await o.save()
  522. this.setData({
  523. tradeNo,
  524. order: o,
  525. showPay: false,
  526. showPay_more:true
  527. })
  528. wx.showToast({
  529. title: '已创建订单',
  530. icon: 'none'
  531. })
  532. } catch (error) {
  533. wx.showToast({
  534. title: '出错了请检查后重试',
  535. icon: 'error'
  536. })
  537. }
  538. },
  539. /**支付回调 */
  540. async acceptResult(e) {
  541. let {
  542. order
  543. } = this.data
  544. let {
  545. params
  546. } = e.detail
  547. if (params == 'ok') {
  548. wx.showLoading({
  549. title: '加载中',
  550. })
  551. order.set('status', '200')
  552. order.set('payTime',new Date())
  553. order.set('payType', 'wx')
  554. order.set('isPay', true)
  555. try {
  556. await order.save()
  557. setTimeout(() => {
  558. wx.hideLoading()
  559. wx.redirectTo({
  560. url: '/nova-tourism/pages/my/my-order/index?active=0',
  561. })
  562. }, 1000);
  563. } catch (error) {
  564. wx.showToast({
  565. title: '保存订单出错,请联系客服核对订单',
  566. icon: 'none'
  567. })
  568. }
  569. } else {
  570. wx.showToast({
  571. title: '支付失败',
  572. icon: 'none'
  573. })
  574. }
  575. },
  576. /** 跳转*/
  577. tourl(e) {
  578. const url = e.currentTarget.dataset.url
  579. wx.navigateTo({
  580. url: `${url}` // 目标页面的路径
  581. });
  582. },
  583. /**
  584. * 生命周期函数--监听页面初次渲染完成
  585. */
  586. onReady: function () {
  587. },
  588. /**
  589. * 生命周期函数--监听页面显示
  590. */
  591. async onShow() {
  592. let address = await sev.getAddress()
  593. console.log(address)
  594. this.setData({
  595. address
  596. })
  597. },
  598. /**
  599. * 生命周期函数--监听页面隐藏
  600. */
  601. onHide: function () {
  602. },
  603. /**
  604. * 生命周期函数--监听页面卸载
  605. */
  606. onUnload: function () {
  607. },
  608. /**
  609. * 页面相关事件处理函数--监听用户下拉动作
  610. */
  611. onPullDownRefresh: function () {
  612. },
  613. /**
  614. * 页面上拉触底事件的处理函数
  615. */
  616. onReachBottom: function () {
  617. },
  618. /**
  619. * 用户点击右上角分享
  620. */
  621. onShareAppMessage: function () {
  622. }
  623. })