index.js 15 KB

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