index.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/1732101023155.png',
  14. 'https://oss.dayaedu.com/ktyq/1732101058157.png',
  15. 'https://oss.dayaedu.com/ktyq/1732101072881.png',
  16. 'https://oss.dayaedu.com/ktyq/1732101091093.png',
  17. 'https://oss.dayaedu.com/ktyq/1732101102921.png',
  18. ],
  19. goodsImgList: [
  20. "https://oss.dayaedu.com/ktyq/1732101129195.png",
  21. "https://oss.dayaedu.com/ktyq/1732101146406.png",
  22. "https://oss.dayaedu.com/ktyq/1732101158356.png",
  23. "https://oss.dayaedu.com/ktyq/1732101169900.png"
  24. ],
  25. current: 0,
  26. autoplay: false,
  27. interval: 5000,
  28. duration: 500,
  29. popupShow: false,
  30. list: [] as any,
  31. isOverSaled: false, // 是否所有商品都没有库存
  32. selected: {} as any,
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad() {
  38. this.onInit()
  39. },
  40. /**
  41. * 获取基础信息
  42. */
  43. async onInit() {
  44. try {
  45. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  46. const list = data.data || []
  47. let selected: any = {}
  48. let isOverSaled = true // 是否销售完
  49. list.forEach((item: any) => {
  50. item.originalPrice = this.formatPrice(item.originalPrice, 'ALL');
  51. item.typeName = this.formatPeriod(item.num, item.period);
  52. const prices: any = this.formatPrice(item.salePrice)
  53. item.integerPart = prices.integerPart
  54. item.decimalPart = prices.decimalPart
  55. if(item.stockNum > 0) {
  56. isOverSaled = false
  57. if( !selected.id) {
  58. selected = item
  59. }
  60. }
  61. });
  62. if(isOverSaled) {
  63. // 没有可购买商品则默认选中第一个商品
  64. selected = list[0]
  65. }
  66. this.setData({
  67. list,
  68. isOverSaled,
  69. selected
  70. })
  71. } catch(e) {
  72. console.log(e, 'e')
  73. }
  74. },
  75. // 格式化价格
  76. formatPrice(price: number, type?: string) {
  77. const amountStr = price.toFixed(2)
  78. const [integerPart, decimalPart] = amountStr.split('.');
  79. if(type === 'ALL') {
  80. return amountStr
  81. }
  82. return {
  83. integerPart,
  84. decimalPart
  85. }
  86. },
  87. // 格式化类型
  88. formatPeriod(num: number, type: string) {
  89. const template: any = {
  90. DAY: "天卡",
  91. MONTH: "月卡",
  92. YEAR: "年卡"
  93. }
  94. if(type === "YEAR" && num >= 99) {
  95. return '永久卡'
  96. }
  97. return num + template[type]
  98. },
  99. // 选择
  100. onSelectGoods(e: any) {
  101. const { dataset } = e.currentTarget
  102. const item = this.data.list.find((item: any) => item.id === dataset.id)
  103. // 判断是否有库存
  104. if(item.stockNum <= 0) {
  105. return
  106. }
  107. this.setData({
  108. selected: item || {}
  109. })
  110. },
  111. // 事件处理函数
  112. changeSwiper(e: any) {
  113. const detail = e.detail;
  114. if(detail.source === 'touch' || detail.source == 'autoplay') {
  115. this.setData({
  116. current: detail.current
  117. })
  118. }
  119. },
  120. isLogin() {
  121. // 判断是否登录
  122. if(!app.globalData.isLogin) {
  123. wx.navigateTo({
  124. url: '../login/login',
  125. })
  126. return false
  127. }
  128. return true
  129. },
  130. /** 我的订单 */
  131. onOrder() {
  132. // 判断是否登录
  133. if(!this.isLogin()) {
  134. return
  135. }
  136. wx.navigateTo({
  137. url: '../orders/orders',
  138. })
  139. },
  140. onBuyShop() {
  141. // 判断是否登录
  142. if(!this.isLogin()) {
  143. return
  144. }
  145. this.setData({
  146. popupShow: true
  147. })
  148. },
  149. onClose() {
  150. this.setData({
  151. popupShow: false
  152. })
  153. },
  154. onSubmit() {
  155. // 判断是否登录
  156. const that = this
  157. debounce(function () {
  158. if(!that.isLogin()) {
  159. return
  160. }
  161. let info = JSON.stringify({
  162. ...that.data.selected
  163. });
  164. console.log(that.data.selected, "that.data.selected")
  165. info = encodeURIComponent(info);
  166. wx.navigateTo({
  167. url: `../orders/order-detail?orderInfo=${info}`,
  168. })
  169. that.setData({
  170. popupShow: false
  171. })
  172. }, 500)()
  173. },
  174. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  175. wx.previewImage({
  176. current: e.currentTarget.dataset.src,
  177. urls: this.data.imgList
  178. })
  179. },
  180. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  181. wx.previewImage({
  182. current: e.currentTarget.dataset.src,
  183. urls: this.data.goodsImgList
  184. })
  185. },
  186. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  187. wx.previewImage({
  188. current: e.currentTarget.dataset.src,
  189. urls: [e.currentTarget.dataset.src]
  190. })
  191. },
  192. /**
  193. * 生命周期函数--监听页面显示
  194. */
  195. onShow() {
  196. this.onInit()
  197. },
  198. openService() {
  199. wx.navigateTo({
  200. url: '../service/service'
  201. })
  202. }
  203. })