index.ts 5.5 KB

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