index.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // index.ts
  2. import { 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. formatSelectGood: {
  36. typeName: '',
  37. showSalePrice: '', // 显示的现价
  38. originalPrice: 0, // 原价
  39. salePrice: 0, // 现价
  40. discountPrice: '' // 已省
  41. } as any, // 格式化所有选中的数据
  42. current: 0,
  43. autoplay: false,
  44. interval: 5000,
  45. duration: 500,
  46. popupShow: false,
  47. list: [] as any,
  48. isOverSaled: false, // 是否所有商品都没有库存
  49. selected: {} as any,
  50. showService: false,
  51. isFromPreviewImage: false
  52. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad() {
  57. this.onInit()
  58. },
  59. /**
  60. * 获取基础信息
  61. */
  62. async onInit() {
  63. try {
  64. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  65. const list = data.data || []
  66. let selected: any = {}
  67. let isOverSaled = true // 是否销售完
  68. list.forEach((item: any) => {
  69. item.originalPrice = formatPrice(item.originalPrice, 'ALL');
  70. item.typeName = this.formatPeriod(item.num, item.period);
  71. const prices: any = formatPrice(item.salePrice)
  72. item.integerPart = prices.integerPart
  73. item.decimalPart = prices.decimalPart
  74. if (item.stockNum > 0) {
  75. isOverSaled = false
  76. if (!selected.id) {
  77. selected = item
  78. }
  79. }
  80. });
  81. if (isOverSaled) {
  82. // 没有可购买商品则默认选中第一个商品
  83. selected = list[0]
  84. }
  85. this.setData({
  86. list,
  87. isOverSaled,
  88. selected
  89. })
  90. } catch (e) {
  91. console.log(e, 'e')
  92. }
  93. },
  94. // 格式化类型
  95. formatPeriod(num: number, type: string) {
  96. const template: any = {
  97. DAY: "天卡",
  98. MONTH: "月卡",
  99. YEAR: "年卡"
  100. }
  101. if (type === "YEAR" && num >= 99) {
  102. return '永久卡'
  103. }
  104. return num + (template[type] || '')
  105. },
  106. // 选择
  107. onSelectGoods(e: any) {
  108. const { dataset } = e.currentTarget
  109. const item = this.data.list.find((item: any) => item.id === dataset.id)
  110. // 判断是否有库存
  111. if (item.stockNum <= 0) {
  112. return
  113. }
  114. this.setData({
  115. selected: item || {}
  116. })
  117. },
  118. onFirstChange(e: any) {
  119. const detail = e.detail;
  120. if (detail.source === 'touch' || detail.source == 'autoplay') {
  121. this.setData({
  122. firstCurrent: detail.current
  123. })
  124. }
  125. },
  126. // 事件处理函数
  127. changeSwiper(e: any) {
  128. const detail = e.detail;
  129. if (detail.source === 'touch' || detail.source == 'autoplay') {
  130. this.setData({
  131. current: detail.current
  132. })
  133. }
  134. },
  135. /** 格式化选中的商品 */
  136. onFormatGoods() {
  137. const selected = this.data.selected;
  138. const selectedInstrument = this.data.selectedInstrument
  139. const params = {
  140. typeName: '',
  141. showSalePrice: '' as any, // 显示的现价
  142. originalPrice: 0, // 原价
  143. salePrice: 0, // 现价
  144. discountPrice: '' as any, // 已省
  145. integerPart: '',
  146. decimalPart: '',
  147. }
  148. // 选中期限
  149. if (selected.id) {
  150. params.typeName = selected.typeName
  151. params.showSalePrice = selected.showSalePrice
  152. params.originalPrice = selected.originalPrice
  153. params.salePrice = selected.salePrice
  154. params.discountPrice = selected.discountPrice
  155. const prices: any = formatPrice(params.salePrice);
  156. params.integerPart = prices.integerPart
  157. params.decimalPart = prices.decimalPart
  158. }
  159. // 选中乐器
  160. if (selectedInstrument.id) {
  161. params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
  162. params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
  163. params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
  164. params.showSalePrice = formatPrice(params.salePrice, "ALL");
  165. params.discountPrice = formatPrice(
  166. params.originalPrice - params.salePrice,
  167. "ALL"
  168. );
  169. const prices: any = formatPrice(params.salePrice);
  170. params.integerPart = prices.integerPart
  171. params.decimalPart = prices.decimalPart
  172. }
  173. this.setData({
  174. formatSelectGood: params
  175. })
  176. },
  177. isLogin() {
  178. // 判断是否登录
  179. if (!app.globalData.isLogin) {
  180. wx.navigateTo({
  181. url: '../login/login',
  182. })
  183. return false
  184. }
  185. return true
  186. },
  187. /** 我的订单 */
  188. onOrder() {
  189. // 判断是否登录
  190. if (!this.isLogin()) {
  191. return
  192. }
  193. wx.navigateTo({
  194. url: '../orders/orders',
  195. })
  196. },
  197. /** 客服 */
  198. onService() {
  199. this.setData({
  200. showService: true
  201. })
  202. },
  203. changePop(event: { detail: any }) {
  204. this.setData({
  205. showService: event.detail
  206. })
  207. },
  208. onBuyShop() {
  209. // 判断是否登录
  210. if (!this.isLogin()) {
  211. return
  212. }
  213. this.setData({
  214. popupShow: true
  215. })
  216. },
  217. onClose() {
  218. this.setData({
  219. popupShow: false
  220. })
  221. },
  222. onSubmit() {
  223. // 判断是否登录
  224. const that = this
  225. debounce(function () {
  226. if (!that.isLogin()) {
  227. return
  228. }
  229. let info = JSON.stringify({
  230. ...that.data.selected
  231. });
  232. console.log(that.data.selected, "that.data.selected")
  233. info = encodeURIComponent(info);
  234. wx.navigateTo({
  235. url: `../orders/order-detail?orderInfo=${info}`,
  236. success: () => {
  237. that.setData({
  238. popupShow: false
  239. })
  240. }
  241. })
  242. }, 500)()
  243. },
  244. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  245. wx.previewImage({
  246. current: e.currentTarget.dataset.src,
  247. urls: this.data.imgList,
  248. success: () => {
  249. this.setData({
  250. isFromPreviewImage: true
  251. })
  252. }
  253. })
  254. },
  255. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  256. wx.previewImage({
  257. current: e.currentTarget.dataset.src,
  258. urls: this.data.goodsImgList,
  259. success: () => {
  260. this.setData({
  261. isFromPreviewImage: true
  262. })
  263. }
  264. })
  265. },
  266. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  267. wx.previewImage({
  268. current: e.currentTarget.dataset.src,
  269. urls: [e.currentTarget.dataset.src],
  270. success: () => {
  271. this.setData({
  272. isFromPreviewImage: true
  273. })
  274. }
  275. })
  276. },
  277. /**
  278. * 生命周期函数--监听页面显示
  279. */
  280. onShow() {
  281. if (!this.data.isFromPreviewImage) {
  282. this.onInit()
  283. } else {
  284. this.setData({
  285. isFromPreviewImage: false
  286. })
  287. }
  288. },
  289. onShareAppMessage() {
  290. return {
  291. title: '器乐数字AI工具',
  292. path: '/pages/index/index',
  293. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  294. }
  295. },
  296. onShareTimeline() {
  297. return {
  298. title: '器乐数字AI工具',
  299. path: '/pages/index/index',
  300. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  301. }
  302. }
  303. })