index.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { api_shopInstruments, api_shopProduct } from "../../api/login"
  2. import { debounce, formatPrice } from "../../utils/util"
  3. // 获取应用实例
  4. const app = getApp<IAppOption>()
  5. // pages/select-goods/index.ts
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. instrumentList: [] as any,
  12. list: [] as any,
  13. isOverSaled: false, // 是否所有商品都没有库存
  14. smallGoods: {}, // 最小金额商品
  15. selected: {} as any,
  16. selectInstrumentId: '', // 选中的乐器
  17. selectedInstrument: {} as any,
  18. formatSelectGood: {
  19. typeName: '',
  20. showSalePrice: '', // 显示的现价
  21. originalPrice: 0, // 原价
  22. salePrice: 0, // 现价
  23. discountPrice: '' // 已省
  24. } as any, // 格式化所有选中的数据
  25. userBeneficiaryId: '', // 选中用户的编号
  26. userBeneficiaryInfo: {
  27. name: '',
  28. phoneNumber: '',
  29. schoolInfo: ''
  30. },
  31. },
  32. /**
  33. * 生命周期函数--监听页面加载
  34. */
  35. onLoad() {
  36. this.onInit()
  37. },
  38. /**
  39. * 获取基础信息
  40. */
  41. async onInit() {
  42. try {
  43. const result = await api_shopInstruments({ appId: app.globalData.appId })
  44. const instrumentList = result.data.data || []
  45. instrumentList?.forEach((item: any) => {
  46. item.showSalePrice = formatPrice(item.salePrice || 0, 'ALL')
  47. })
  48. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  49. const list = data.data || []
  50. let selected: any = {}
  51. let isOverSaled = true // 是否销售完
  52. // 最少金额商品
  53. let smallGoods: any = {}
  54. list.forEach((item: any) => {
  55. item.originalPrice = formatPrice(item.originalPrice, "ALL");
  56. item.showSalePrice = formatPrice(item.salePrice, "ALL");
  57. item.typeName = this.formatPeriod(item.num, item.period);
  58. item.discountPrice = formatPrice(
  59. item.originalPrice - item.salePrice,
  60. "ALL"
  61. );
  62. const prices: any = formatPrice(item.salePrice)
  63. item.integerPart = prices.integerPart
  64. item.decimalPart = prices.decimalPart
  65. if (item.stockNum > 0) {
  66. isOverSaled = false
  67. if (!selected.id) {
  68. selected = item
  69. }
  70. }
  71. // 获取最小金额
  72. if (smallGoods?.salePrice) {
  73. smallGoods = smallGoods.salePrice <= item.salePrice ? smallGoods : item
  74. } else {
  75. smallGoods = item
  76. }
  77. });
  78. if (isOverSaled) {
  79. // 没有可购买商品则默认选中第一个商品
  80. selected = list[0]
  81. }
  82. this.setData({
  83. list,
  84. instrumentList, // 乐器列表
  85. isOverSaled,
  86. selected,
  87. smallGoods,
  88. selectInstrumentId: '',
  89. selectedInstrument: {},
  90. userBeneficiaryId: '',
  91. userBeneficiaryInfo: {
  92. name: '',
  93. phoneNumber: '',
  94. schoolInfo: ''
  95. }
  96. }, () => {
  97. this.onFormatGoods()
  98. })
  99. } catch (e) {
  100. console.log(e, 'e')
  101. }
  102. },
  103. // 格式化类型
  104. formatPeriod(num: number, type: string) {
  105. const template: any = {
  106. DAY: "天卡",
  107. MONTH: "月卡",
  108. YEAR: "年卡"
  109. }
  110. if (type === "YEAR" && num >= 99) {
  111. return '永久卡'
  112. }
  113. return num + (template[type] || '')
  114. },
  115. // 选择
  116. onSelectGoods(e: any) {
  117. const { dataset } = e.currentTarget
  118. const item = this.data.list.find((item: any) => item.id === dataset.id)
  119. // 判断是否有库存
  120. if (item.stockNum <= 0) {
  121. return
  122. }
  123. this.setData({
  124. selected: item || {}
  125. }, () => {
  126. this.onFormatGoods()
  127. })
  128. },
  129. /** 选中乐器 */
  130. onSelectInstrument(e: any) {
  131. const { dataset } = e.currentTarget;
  132. if (dataset.id === this.data.selectInstrumentId) {
  133. this.setData({
  134. selectInstrumentId: '',
  135. selectedInstrument: {}
  136. }, () => {
  137. this.onFormatGoods()
  138. })
  139. } else {
  140. const item = this.data.instrumentList.find((item: any) => item.id === dataset.id);
  141. this.setData({
  142. selectInstrumentId: dataset.id,
  143. selectedInstrument: item || {}
  144. }, () => {
  145. this.onFormatGoods()
  146. })
  147. }
  148. },
  149. isLogin() {
  150. // 判断是否登录
  151. if (!app.globalData.isLogin) {
  152. wx.navigateTo({
  153. url: '../login/login',
  154. })
  155. return false
  156. }
  157. return true
  158. },
  159. /** 格式化选中的商品 */
  160. onFormatGoods() {
  161. const selected = this.data.selected;
  162. const selectedInstrument = this.data.selectedInstrument
  163. const params = {
  164. typeName: '',
  165. showSalePrice: '' as any, // 显示的现价
  166. originalPrice: 0, // 原价
  167. salePrice: 0, // 现价
  168. discountPrice: '' as any, // 已省
  169. integerPart: '',
  170. decimalPart: '',
  171. }
  172. // 选中期限
  173. if (selected.id) {
  174. params.typeName = selected.typeName
  175. params.showSalePrice = selected.showSalePrice
  176. params.originalPrice = selected.originalPrice
  177. params.salePrice = selected.salePrice
  178. params.discountPrice = selected.discountPrice
  179. const prices: any = formatPrice(params.salePrice);
  180. params.integerPart = prices.integerPart
  181. params.decimalPart = prices.decimalPart
  182. }
  183. // 选中乐器
  184. if (selectedInstrument.id) {
  185. params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
  186. params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
  187. params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
  188. params.showSalePrice = formatPrice(params.salePrice, "ALL");
  189. params.discountPrice = formatPrice(
  190. params.originalPrice - params.salePrice,
  191. "ALL"
  192. );
  193. const prices: any = formatPrice(params.salePrice);
  194. params.integerPart = prices.integerPart
  195. params.decimalPart = prices.decimalPart
  196. }
  197. this.setData({
  198. formatSelectGood: params
  199. })
  200. },
  201. onSubmit() {
  202. // 判断是否登录
  203. const that = this
  204. if (!this.data.userBeneficiaryId) {
  205. wx.showToast({
  206. title: '请填写享用者的个人信息',
  207. icon: 'none'
  208. })
  209. return
  210. }
  211. debounce(function () {
  212. if (!that.isLogin()) {
  213. return
  214. }
  215. const params = [] as any
  216. const selected = that.data.selected
  217. if (selected.id) {
  218. params.push({
  219. pic: selected.pic,
  220. name: selected.name,
  221. period: selected.period,
  222. num: selected.num,
  223. originalPrice: selected.originalPrice,
  224. salePrice: selected.salePrice,
  225. shopId: selected.shopId,
  226. id: selected.id,
  227. goodsType: 'ACTIVATION_CODE', // INSTRUMENTS
  228. })
  229. }
  230. const selectedInstrument = that.data.selectedInstrument
  231. if (selectedInstrument.id) {
  232. params.push({
  233. pic: selectedInstrument.pic,
  234. name: selectedInstrument.name,
  235. period: selectedInstrument?.period,
  236. num: selectedInstrument?.num || 0,
  237. originalPrice: selectedInstrument.originalPrice,
  238. salePrice: selectedInstrument.salePrice,
  239. shopId: selectedInstrument.shopId,
  240. id: selectedInstrument.id,
  241. goodsType: 'INSTRUMENTS', // INSTRUMENTS
  242. })
  243. }
  244. let info = JSON.stringify({
  245. ...params
  246. });
  247. info = encodeURIComponent(info);
  248. console.log(params, "params")
  249. wx.navigateTo({
  250. url: `../orders/order-detail?orderInfo=${info}&userBeneficiaryId=${that.data.userBeneficiaryId}`,
  251. success: () => {
  252. that.setData({
  253. popupShow: false,
  254. currentIndex: 1,
  255. })
  256. }
  257. })
  258. }, 500)()
  259. },
  260. })