index.ts 6.2 KB

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