index.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. this.setData({
  109. list,
  110. instrumentList, // 乐器列表
  111. isOverSaled,
  112. selected,
  113. smallGoods,
  114. selectInstrumentId: '',
  115. selectedInstrument: {}
  116. }, () => {
  117. this.onFormatGoods()
  118. })
  119. } catch (e) {
  120. console.log(e, 'e')
  121. }
  122. },
  123. // 格式化类型
  124. formatPeriod(num: number, type: string) {
  125. const template: any = {
  126. DAY: "天卡",
  127. MONTH: "月卡",
  128. YEAR: "年卡"
  129. }
  130. if (type === "YEAR" && num >= 99) {
  131. return '永久卡'
  132. }
  133. return num + (template[type] || '')
  134. },
  135. // 选择
  136. onSelectGoods(e: any) {
  137. const { dataset } = e.currentTarget
  138. const item = this.data.list.find((item: any) => item.id === dataset.id)
  139. // 判断是否有库存
  140. if (item.stockNum <= 0) {
  141. return
  142. }
  143. this.setData({
  144. selected: item || {}
  145. }, () => {
  146. this.onFormatGoods()
  147. })
  148. },
  149. /** 选中乐器 */
  150. onSelectInstrument(e: any) {
  151. const { dataset } = e.currentTarget;
  152. if (dataset.id === this.data.selectInstrumentId) {
  153. this.setData({
  154. selectInstrumentId: '',
  155. selectedInstrument: {}
  156. }, () => {
  157. this.onFormatGoods()
  158. })
  159. } else {
  160. const item = this.data.instrumentList.find((item: any) => item.id === dataset.id);
  161. this.setData({
  162. selectInstrumentId: dataset.id,
  163. selectedInstrument: item || {}
  164. }, () => {
  165. this.onFormatGoods()
  166. })
  167. }
  168. },
  169. onFirstChange(e: any) {
  170. const detail = e.detail;
  171. if (detail.source === 'touch' || detail.source == 'autoplay') {
  172. this.setData({
  173. firstCurrent: detail.current
  174. })
  175. }
  176. },
  177. // 事件处理函数
  178. changeSwiper(e: any) {
  179. const detail = e.detail;
  180. if (detail.source === 'touch' || detail.source == 'autoplay') {
  181. this.setData({
  182. current: detail.current
  183. })
  184. }
  185. },
  186. /** 格式化选中的商品 */
  187. onFormatGoods() {
  188. const selected = this.data.selected;
  189. const selectedInstrument = this.data.selectedInstrument
  190. const params = {
  191. typeName: '',
  192. showSalePrice: '' as any, // 显示的现价
  193. originalPrice: 0, // 原价
  194. salePrice: 0, // 现价
  195. discountPrice: '' as any, // 已省
  196. integerPart: '',
  197. decimalPart: '',
  198. }
  199. // 选中期限
  200. if (selected.id) {
  201. params.typeName = selected.typeName
  202. params.showSalePrice = selected.showSalePrice
  203. params.originalPrice = selected.originalPrice
  204. params.salePrice = selected.salePrice
  205. params.discountPrice = selected.discountPrice
  206. const prices: any = formatPrice(params.salePrice);
  207. params.integerPart = prices.integerPart
  208. params.decimalPart = prices.decimalPart
  209. }
  210. // 选中乐器
  211. if (selectedInstrument.id) {
  212. params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
  213. params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
  214. params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
  215. params.showSalePrice = formatPrice(params.salePrice, "ALL");
  216. params.discountPrice = formatPrice(
  217. params.originalPrice - params.salePrice,
  218. "ALL"
  219. );
  220. const prices: any = formatPrice(params.salePrice);
  221. params.integerPart = prices.integerPart
  222. params.decimalPart = prices.decimalPart
  223. }
  224. this.setData({
  225. formatSelectGood: params
  226. })
  227. },
  228. isLogin() {
  229. // 判断是否登录
  230. if (!app.globalData.isLogin) {
  231. wx.navigateTo({
  232. url: '../login/login',
  233. })
  234. return false
  235. }
  236. return true
  237. },
  238. /** 我的订单 */
  239. onOrder() {
  240. // 判断是否登录
  241. if (!this.isLogin()) {
  242. return
  243. }
  244. wx.navigateTo({
  245. url: '../orders/orders',
  246. })
  247. },
  248. /** 客服 */
  249. onService() {
  250. this.setData({
  251. showService: true
  252. })
  253. },
  254. changePop(event: { detail: any }) {
  255. this.setData({
  256. showService: event.detail
  257. })
  258. },
  259. onBuyShop() {
  260. // 判断是否登录
  261. if (!this.isLogin()) {
  262. return
  263. }
  264. this.setData({
  265. popupShow: true
  266. })
  267. },
  268. onClose() {
  269. this.setData({
  270. popupShow: false
  271. })
  272. },
  273. onSubmit() {
  274. // 判断是否登录
  275. const that = this
  276. debounce(function () {
  277. if (!that.isLogin()) {
  278. return
  279. }
  280. let info = JSON.stringify({
  281. ...that.data.selected
  282. });
  283. // console.log(that.data.selected, "that.data.selected")
  284. info = encodeURIComponent(info);
  285. wx.navigateTo({
  286. url: `../orders/order-detail?orderInfo=${info}`,
  287. success: () => {
  288. that.setData({
  289. popupShow: false
  290. })
  291. }
  292. })
  293. }, 500)()
  294. },
  295. onSelectBuyer() {
  296. this.setData({
  297. showBuyer: true
  298. })
  299. },
  300. onCloseBuyer(e: {detail: any}) {
  301. this.setData({
  302. showBuyer: e.detail
  303. })
  304. },
  305. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  306. wx.previewImage({
  307. current: e.currentTarget.dataset.src,
  308. urls: this.data.imgList,
  309. success: () => {
  310. this.setData({
  311. isFromPreviewImage: true
  312. })
  313. }
  314. })
  315. },
  316. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  317. wx.previewImage({
  318. current: e.currentTarget.dataset.src,
  319. urls: this.data.goodsImgList,
  320. success: () => {
  321. this.setData({
  322. isFromPreviewImage: true
  323. })
  324. }
  325. })
  326. },
  327. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  328. wx.previewImage({
  329. current: e.currentTarget.dataset.src,
  330. urls: [e.currentTarget.dataset.src],
  331. success: () => {
  332. this.setData({
  333. isFromPreviewImage: true
  334. })
  335. }
  336. })
  337. },
  338. /**
  339. * 生命周期函数--监听页面显示
  340. */
  341. onShow() {
  342. if (!this.data.isFromPreviewImage) {
  343. this.onInit()
  344. } else {
  345. this.setData({
  346. isFromPreviewImage: false
  347. })
  348. }
  349. },
  350. onShareAppMessage() {
  351. return {
  352. title: '器乐数字AI工具',
  353. path: '/pages/index/index',
  354. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  355. }
  356. },
  357. onShareTimeline() {
  358. return {
  359. title: '器乐数字AI工具',
  360. path: '/pages/index/index',
  361. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  362. }
  363. }
  364. })