index.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // index.ts
  2. import { api_shopInstruments, api_shopProduct } from "../../api/login";
  3. import { debounce, formatPrice } from '../../utils/util'
  4. // 获取应用实例
  5. const app = getApp<IAppOption>()
  6. // pages/orders/orders.ts
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. firstImgList: [
  13. 'https://oss.dayaedu.com/ktyq/1739181226082.png',
  14. 'https://oss.dayaedu.com/ktyq/1739181253580.png',
  15. 'https://oss.dayaedu.com/ktyq/1739181268714.png',
  16. 'https://oss.dayaedu.com/ktyq/1739181284457.png',
  17. 'https://oss.dayaedu.com/ktyq/1739181316779.png',
  18. 'https://oss.dayaedu.com/ktyq/1739181329436.png'
  19. ],
  20. firstCurrent: 0,
  21. imgList: [
  22. 'https://oss.dayaedu.com/ktyq/1732610921517.png',
  23. 'https://oss.dayaedu.com/ktyq/1732610940095.png',
  24. 'https://oss.dayaedu.com/ktyq/1732610952376.png',
  25. 'https://oss.dayaedu.com/ktyq/1732610965625.png',
  26. // 'https://oss.dayaedu.com/ktyq/1731664269098.png',
  27. ],
  28. goodsImgList: [
  29. 'https://oss.dayaedu.com/ktyq/1739182504757.png',
  30. 'https://oss.dayaedu.com/ktyq/1739182521080.png',
  31. 'https://oss.dayaedu.com/ktyq/1739182535430.png',
  32. 'https://oss.dayaedu.com/ktyq/1739182557976.png',
  33. 'https://oss.dayaedu.com/ktyq/1739182545221.png'
  34. ],
  35. current: 0,
  36. autoplay: false,
  37. interval: 5000,
  38. duration: 500,
  39. popupShow: false,
  40. instrumentList: [] as any,
  41. list: [] as any,
  42. isOverSaled: false, // 是否所有商品都没有库存
  43. smallGoods: {}, // 最小金额商品
  44. selected: {} as any,
  45. selectInstrumentId: '', // 选中的乐器
  46. selectedInstrument: {} as any,
  47. formatSelectGood: {
  48. typeName: '',
  49. showSalePrice: '', // 显示的现价
  50. originalPrice: 0, // 原价
  51. salePrice: 0, // 现价
  52. discountPrice: '' // 已省
  53. } as any, // 格式化所有选中的数据
  54. showService: false,
  55. isFromPreviewImage: false,
  56. showBuyer: true, // 收益人
  57. },
  58. /**
  59. * 生命周期函数--监听页面加载
  60. */
  61. onLoad() {
  62. this.onInit()
  63. },
  64. /**
  65. * 获取基础信息
  66. */
  67. async onInit() {
  68. try {
  69. const result = await api_shopInstruments({ appId: app.globalData.appId })
  70. const instrumentList = result.data.data || []
  71. instrumentList.forEach((item: any) => {
  72. item.showSalePrice = formatPrice(item.salePrice || 0, 'ALL')
  73. })
  74. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  75. const list = data.data || []
  76. let selected: any = {}
  77. let isOverSaled = true // 是否销售完
  78. // 最少金额商品
  79. let smallGoods: any = {}
  80. list.forEach((item: any) => {
  81. item.originalPrice = formatPrice(item.originalPrice, "ALL");
  82. item.showSalePrice = formatPrice(item.salePrice, "ALL");
  83. item.typeName = this.formatPeriod(item.num, item.period);
  84. item.discountPrice = formatPrice(
  85. item.originalPrice - item.salePrice,
  86. "ALL"
  87. );
  88. const prices: any = formatPrice(item.salePrice)
  89. item.integerPart = prices.integerPart
  90. item.decimalPart = prices.decimalPart
  91. if (item.stockNum > 0) {
  92. isOverSaled = false
  93. if (!selected.id) {
  94. selected = item
  95. }
  96. }
  97. // 获取最小金额
  98. if(smallGoods?.salePrice) {
  99. smallGoods = smallGoods.salePrice <= item.salePrice ? smallGoods : item
  100. } else {
  101. smallGoods = item
  102. }
  103. });
  104. if (isOverSaled) {
  105. // 没有可购买商品则默认选中第一个商品
  106. selected = list[0]
  107. }
  108. console.log(smallGoods, 'smallGoods')
  109. this.setData({
  110. list,
  111. instrumentList, // 乐器列表
  112. isOverSaled,
  113. selected,
  114. smallGoods,
  115. selectInstrumentId: '',
  116. selectedInstrument: {}
  117. }, () => {
  118. this.onFormatGoods()
  119. })
  120. } catch (e) {
  121. console.log(e, 'e')
  122. }
  123. },
  124. // 格式化类型
  125. formatPeriod(num: number, type: string) {
  126. const template: any = {
  127. DAY: "天卡",
  128. MONTH: "月卡",
  129. YEAR: "年卡"
  130. }
  131. if (type === "YEAR" && num >= 99) {
  132. return '永久卡'
  133. }
  134. return num + (template[type] || '')
  135. },
  136. // 选择
  137. onSelectGoods(e: any) {
  138. const { dataset } = e.currentTarget
  139. const item = this.data.list.find((item: any) => item.id === dataset.id)
  140. // 判断是否有库存
  141. if (item.stockNum <= 0) {
  142. return
  143. }
  144. this.setData({
  145. selected: item || {}
  146. }, () => {
  147. this.onFormatGoods()
  148. })
  149. },
  150. /** 选中乐器 */
  151. onSelectInstrument(e: any) {
  152. const { dataset } = e.currentTarget;
  153. if (dataset.id === this.data.selectInstrumentId) {
  154. this.setData({
  155. selectInstrumentId: '',
  156. selectedInstrument: {}
  157. }, () => {
  158. this.onFormatGoods()
  159. })
  160. } else {
  161. const item = this.data.instrumentList.find((item: any) => item.id === dataset.id);
  162. this.setData({
  163. selectInstrumentId: dataset.id,
  164. selectedInstrument: item || {}
  165. }, () => {
  166. this.onFormatGoods()
  167. })
  168. }
  169. },
  170. onFirstChange(e: any) {
  171. const detail = e.detail;
  172. if (detail.source === 'touch' || detail.source == 'autoplay') {
  173. this.setData({
  174. firstCurrent: detail.current
  175. })
  176. }
  177. },
  178. // 事件处理函数
  179. changeSwiper(e: any) {
  180. const detail = e.detail;
  181. if (detail.source === 'touch' || detail.source == 'autoplay') {
  182. this.setData({
  183. current: detail.current
  184. })
  185. }
  186. },
  187. /** 格式化选中的商品 */
  188. onFormatGoods() {
  189. const selected = this.data.selected;
  190. const selectedInstrument = this.data.selectedInstrument
  191. const params = {
  192. typeName: '',
  193. showSalePrice: '' as any, // 显示的现价
  194. originalPrice: 0, // 原价
  195. salePrice: 0, // 现价
  196. discountPrice: '' as any, // 已省
  197. integerPart: '',
  198. decimalPart: '',
  199. }
  200. // 选中期限
  201. if (selected.id) {
  202. params.typeName = selected.typeName
  203. params.showSalePrice = selected.showSalePrice
  204. params.originalPrice = selected.originalPrice
  205. params.salePrice = selected.salePrice
  206. params.discountPrice = selected.discountPrice
  207. const prices: any = formatPrice(params.salePrice);
  208. params.integerPart = prices.integerPart
  209. params.decimalPart = prices.decimalPart
  210. }
  211. // 选中乐器
  212. if (selectedInstrument.id) {
  213. params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
  214. params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
  215. params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
  216. params.showSalePrice = formatPrice(params.salePrice, "ALL");
  217. params.discountPrice = formatPrice(
  218. params.originalPrice - params.salePrice,
  219. "ALL"
  220. );
  221. const prices: any = formatPrice(params.salePrice);
  222. params.integerPart = prices.integerPart
  223. params.decimalPart = prices.decimalPart
  224. }
  225. console.log(params, "params")
  226. this.setData({
  227. formatSelectGood: params
  228. })
  229. },
  230. isLogin() {
  231. // 判断是否登录
  232. if (!app.globalData.isLogin) {
  233. wx.navigateTo({
  234. url: '../login/login',
  235. })
  236. return false
  237. }
  238. return true
  239. },
  240. /** 我的订单 */
  241. onOrder() {
  242. // 判断是否登录
  243. if (!this.isLogin()) {
  244. return
  245. }
  246. wx.navigateTo({
  247. url: '../orders/orders',
  248. })
  249. },
  250. /** 客服 */
  251. onService() {
  252. this.setData({
  253. showService: true
  254. })
  255. },
  256. changePop(event: { detail: any }) {
  257. this.setData({
  258. showService: event.detail
  259. })
  260. },
  261. onBuyShop() {
  262. // 判断是否登录
  263. if (!this.isLogin()) {
  264. return
  265. }
  266. this.setData({
  267. popupShow: true
  268. })
  269. },
  270. onClose() {
  271. this.setData({
  272. popupShow: false
  273. })
  274. },
  275. onSubmit() {
  276. // 判断是否登录
  277. const that = this
  278. debounce(function () {
  279. if (!that.isLogin()) {
  280. return
  281. }
  282. let info = JSON.stringify({
  283. ...that.data.selected
  284. });
  285. console.log(that.data.selected, "that.data.selected")
  286. info = encodeURIComponent(info);
  287. wx.navigateTo({
  288. url: `../orders/order-detail?orderInfo=${info}`,
  289. success: () => {
  290. that.setData({
  291. popupShow: false
  292. })
  293. }
  294. })
  295. }, 500)()
  296. },
  297. onSelectBuyer() {
  298. this.setData({
  299. showBuyer: true
  300. })
  301. },
  302. onCloseBuyer(e: {detail: any}) {
  303. this.setData({
  304. showBuyer: e.detail
  305. })
  306. },
  307. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  308. wx.previewImage({
  309. current: e.currentTarget.dataset.src,
  310. urls: this.data.imgList,
  311. success: () => {
  312. this.setData({
  313. isFromPreviewImage: true
  314. })
  315. }
  316. })
  317. },
  318. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  319. wx.previewImage({
  320. current: e.currentTarget.dataset.src,
  321. urls: this.data.goodsImgList,
  322. success: () => {
  323. this.setData({
  324. isFromPreviewImage: true
  325. })
  326. }
  327. })
  328. },
  329. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  330. wx.previewImage({
  331. current: e.currentTarget.dataset.src,
  332. urls: [e.currentTarget.dataset.src],
  333. success: () => {
  334. this.setData({
  335. isFromPreviewImage: true
  336. })
  337. }
  338. })
  339. },
  340. /**
  341. * 生命周期函数--监听页面显示
  342. */
  343. onShow() {
  344. if (!this.data.isFromPreviewImage) {
  345. this.onInit()
  346. } else {
  347. this.setData({
  348. isFromPreviewImage: false
  349. })
  350. }
  351. },
  352. onShareAppMessage() {
  353. return {
  354. title: '器乐数字AI工具',
  355. path: '/pages/index/index',
  356. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  357. }
  358. },
  359. onShareTimeline() {
  360. return {
  361. title: '器乐数字AI工具',
  362. path: '/pages/index/index',
  363. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  364. }
  365. }
  366. })