index.ts 4.9 KB

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