index.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/1732613795513.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/1732697791582.png",
  21. "https://oss.dayaedu.com/ktyq/1732697810826.png",
  22. "https://oss.dayaedu.com/ktyq/1732706055542.png",
  23. "https://oss.dayaedu.com/ktyq/1732706066094.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. isFromPreviewImage: 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. onBuyShop() {
  142. // 判断是否登录
  143. if(!this.isLogin()) {
  144. return
  145. }
  146. this.setData({
  147. popupShow: true
  148. })
  149. },
  150. onClose() {
  151. this.setData({
  152. popupShow: false
  153. })
  154. },
  155. onSubmit() {
  156. // 判断是否登录
  157. const that = this
  158. debounce(function () {
  159. if(!that.isLogin()) {
  160. return
  161. }
  162. let info = JSON.stringify({
  163. ...that.data.selected
  164. });
  165. info = encodeURIComponent(info);
  166. wx.navigateTo({
  167. url: `../orders/order-detail?orderInfo=${info}`,
  168. })
  169. that.setData({
  170. popupShow: false
  171. })
  172. }, 300)()
  173. },
  174. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  175. wx.previewImage({
  176. current: e.currentTarget.dataset.src,
  177. urls: this.data.imgList,
  178. success: () => {
  179. this.setData({
  180. isFromPreviewImage: true
  181. })
  182. }
  183. })
  184. },
  185. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  186. wx.previewImage({
  187. current: e.currentTarget.dataset.src,
  188. urls: this.data.goodsImgList,
  189. success: () => {
  190. this.setData({
  191. isFromPreviewImage: true
  192. })
  193. }
  194. })
  195. },
  196. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  197. wx.previewImage({
  198. current: e.currentTarget.dataset.src,
  199. urls: [e.currentTarget.dataset.src],
  200. success: () => {
  201. this.setData({
  202. isFromPreviewImage: true
  203. })
  204. }
  205. })
  206. },
  207. /**
  208. * 生命周期函数--监听页面显示
  209. */
  210. onShow() {
  211. if(!this.data.isFromPreviewImage) {
  212. this.onInit()
  213. } else {
  214. this.setData({
  215. isFromPreviewImage: false
  216. })
  217. }
  218. },
  219. openService() {
  220. wx.navigateTo({
  221. url: '../service/service'
  222. })
  223. }
  224. })