index.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // index.ts
  2. import { api_shopProduct } from "../../api/login";
  3. import { debounce } from '../../utils/util'
  4. // 获取应用实例
  5. const app = getApp<IAppOption>()
  6. // pages/orders/orders.ts
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. imgList: [
  13. 'https://oss.dayaedu.com/ktyq/1732672861312.png',
  14. 'https://oss.dayaedu.com/ktyq/1732613781003.png',
  15. 'https://oss.dayaedu.com/ktyq/1733118132694.png',
  16. 'https://oss.dayaedu.com/ktyq/1732613807112.png',
  17. // 'https://oss.dayaedu.com/ktyq/1732101102921.png',
  18. ],
  19. goodsImgList: [
  20. "https://oss.dayaedu.com/ktyq/1733130441242.png",
  21. "https://oss.dayaedu.com/ktyq/1733118166964.png",
  22. "https://oss.dayaedu.com/ktyq/1732706055542.png",
  23. "https://oss.dayaedu.com/ktyq/1732706066094.png"
  24. ],
  25. serviceShow: true,
  26. current: 0,
  27. // autoplay: false,
  28. // interval: 5000,
  29. // duration: 500,
  30. popupShow: false,
  31. list: [] as any,
  32. isOverSaled: false, // 是否所有商品都没有库存
  33. selected: {} as any,
  34. isFromPreviewImage: false,
  35. isShowOperation: false, // 是否显示操作按钮
  36. },
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad() {
  41. // this.onInit()
  42. },
  43. /**
  44. * 获取基础信息
  45. */
  46. async onInit() {
  47. try {
  48. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  49. const list = data.data || []
  50. let selected: any = {}
  51. let isOverSaled = true // 是否销售完
  52. list.forEach((item: any) => {
  53. item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
  54. item.typeName = this.formatPeriod(item.num, item.period);
  55. const prices: any = this.formatPrice(item.salePrice)
  56. item.integerPart = prices.integerPart
  57. item.decimalPart = prices.decimalPart
  58. if(item.stockNum > 0) {
  59. isOverSaled = false
  60. if( !selected.id) {
  61. selected = item
  62. }
  63. }
  64. });
  65. if(isOverSaled) {
  66. // 没有可购买商品则默认选中第一个商品
  67. selected = list[0]
  68. }
  69. this.setData({
  70. list,
  71. isOverSaled,
  72. selected
  73. })
  74. } catch(e) {
  75. console.log(e, 'e')
  76. }
  77. },
  78. // 格式化价格
  79. formatPrice(price: number, type?: string) {
  80. const amountStr = price.toFixed(2)
  81. const [integerPart, decimalPart] = amountStr.split('.');
  82. if(type === 'ALL') {
  83. return amountStr
  84. }
  85. return {
  86. integerPart,
  87. decimalPart
  88. }
  89. },
  90. // 格式化类型
  91. formatPeriod(num: number, type: string) {
  92. const template: any = {
  93. DAY: "天卡",
  94. MONTH: "月卡",
  95. YEAR: "年卡"
  96. }
  97. if(type === "YEAR" && num >= 99) {
  98. return '永久卡'
  99. }
  100. return num + template[type]
  101. },
  102. // 选择
  103. onSelectGoods(e: any) {
  104. const { dataset } = e.currentTarget
  105. const item = this.data.list.find((item: any) => item.id === dataset.id)
  106. // 判断是否有库存
  107. if(item.stockNum <= 0) {
  108. return
  109. }
  110. this.setData({
  111. selected: item || {}
  112. })
  113. },
  114. // 事件处理函数
  115. changeSwiper(e: any) {
  116. const detail = e.detail;
  117. if(detail.source === 'touch' || detail.source == 'autoplay') {
  118. this.setData({
  119. current: detail.current
  120. })
  121. }
  122. },
  123. isLogin() {
  124. // 判断是否登录
  125. if(!app.globalData.isLogin) {
  126. wx.navigateTo({
  127. url: '../login/login',
  128. })
  129. return false
  130. }
  131. return true
  132. },
  133. /** 我的订单 */
  134. onOrder() {
  135. // 判断是否登录
  136. if(!this.isLogin()) {
  137. return
  138. }
  139. wx.navigateTo({
  140. url: '../orders/orders',
  141. })
  142. },
  143. onBuyShop() {
  144. // 判断是否登录
  145. if(!this.isLogin()) {
  146. return
  147. }
  148. this.setData({
  149. popupShow: true
  150. })
  151. },
  152. onClose() {
  153. this.setData({
  154. popupShow: false
  155. })
  156. },
  157. onSubmit() {
  158. // 判断是否登录
  159. const that = this
  160. debounce(function () {
  161. if(!that.isLogin()) {
  162. return
  163. }
  164. let info = JSON.stringify({
  165. ...that.data.selected
  166. });
  167. info = encodeURIComponent(info);
  168. wx.navigateTo({
  169. url: `../orders/order-detail?orderInfo=${info}`,
  170. success: () => {
  171. that.setData({
  172. popupShow: false
  173. })
  174. }
  175. })
  176. }, 300)()
  177. },
  178. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  179. wx.previewImage({
  180. current: e.currentTarget.dataset.src,
  181. urls: this.data.imgList,
  182. success: () => {
  183. this.setData({
  184. isFromPreviewImage: true
  185. })
  186. }
  187. })
  188. },
  189. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  190. wx.previewImage({
  191. current: e.currentTarget.dataset.src,
  192. urls: this.data.goodsImgList,
  193. success: () => {
  194. this.setData({
  195. isFromPreviewImage: true
  196. })
  197. }
  198. })
  199. },
  200. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  201. wx.previewImage({
  202. current: e.currentTarget.dataset.src,
  203. urls: [e.currentTarget.dataset.src],
  204. success: () => {
  205. this.setData({
  206. isFromPreviewImage: true
  207. })
  208. }
  209. })
  210. },
  211. /**
  212. * 生命周期函数--监听页面显示
  213. */
  214. onShow() {
  215. if(!this.data.isFromPreviewImage) {
  216. this.onInit()
  217. } else {
  218. this.setData({
  219. isFromPreviewImage: false
  220. })
  221. }
  222. this.setData({
  223. serviceShow: true
  224. })
  225. },
  226. onHide() {
  227. this.setData({
  228. serviceShow: false
  229. })
  230. },
  231. onScroll(e: { detail: any }) {
  232. // console.log(e, 'any')
  233. const scrollTop = e.detail.scrollTop || 0
  234. if(scrollTop > 40) {
  235. this.setData({
  236. isShowOperation: true
  237. })
  238. } else {
  239. this.setData({
  240. isShowOperation: false
  241. })
  242. }
  243. },
  244. onShareAppMessage() {
  245. return {
  246. title: '音乐数字AI器乐工具',
  247. path: '/pages/index/index',
  248. imageUrl: 'https://oss.dayaedu.com/ktyq/1733311074676.png'
  249. }
  250. },
  251. onShareTimeline() {
  252. return {
  253. title: '音乐数字AI器乐工具',
  254. path: '/pages/index/index',
  255. imageUrl: 'https://oss.dayaedu.com/ktyq/1733311074676.png'
  256. }
  257. }
  258. })