index.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. const formatSalePrice: any = formatPrice(item.salePrice || 0)
  48. item.integerPart = formatSalePrice.integerPart
  49. item.decimalPart = formatSalePrice.decimalPart
  50. item.originalPrice = formatPrice(item.originalPrice, "ALL");
  51. })
  52. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  53. const list = data.data || []
  54. let selected: any = {}
  55. let isOverSaled = true // 是否销售完
  56. // 最少金额商品
  57. let smallGoods: any = {}
  58. list.forEach((item: any) => {
  59. item.originalPrice = formatPrice(item.originalPrice, "ALL");
  60. item.showSalePrice = formatPrice(item.salePrice, "ALL");
  61. item.typeName = this.formatPeriod(item.num, item.period);
  62. item.discountPrice = formatPrice(
  63. item.originalPrice - item.salePrice,
  64. "ALL"
  65. );
  66. const prices: any = formatPrice(item.salePrice)
  67. item.integerPart = prices.integerPart
  68. item.decimalPart = prices.decimalPart
  69. if (item.stockNum > 0) {
  70. isOverSaled = false
  71. if (!selected.id) {
  72. selected = item
  73. }
  74. }
  75. // 获取最小金额
  76. if (smallGoods?.salePrice) {
  77. smallGoods = smallGoods.salePrice <= item.salePrice ? smallGoods : item
  78. } else {
  79. smallGoods = item
  80. }
  81. });
  82. if (isOverSaled) {
  83. // 没有可购买商品则默认选中第一个商品
  84. selected = list[0]
  85. }
  86. this.setData({
  87. list,
  88. instrumentList, // 乐器列表
  89. isOverSaled,
  90. selected,
  91. smallGoods,
  92. selectInstrumentId: '',
  93. selectedInstrument: {},
  94. userBeneficiaryId: '',
  95. userBeneficiaryInfo: {
  96. name: '',
  97. phoneNumber: '',
  98. schoolInfo: ''
  99. }
  100. }, () => {
  101. this.onFormatGoods()
  102. })
  103. } catch (e) {
  104. console.log(e, 'e')
  105. }
  106. },
  107. // 格式化类型
  108. formatPeriod(num: number, type: string) {
  109. const template: any = {
  110. DAY: "天卡",
  111. MONTH: "月卡",
  112. YEAR: "年卡"
  113. }
  114. if (type === "YEAR" && num >= 99) {
  115. return '永久卡'
  116. }
  117. return num + (template[type] || '')
  118. },
  119. onBack() {
  120. wx.navigateBack()
  121. },
  122. // 选择
  123. onSelectGoods(e: any) {
  124. const { dataset } = e.currentTarget
  125. const item = this.data.list.find((item: any) => item.id === dataset.id)
  126. // 判断是否有库存
  127. if (item.stockNum <= 0) {
  128. return
  129. }
  130. this.setData({
  131. selected: item || {}
  132. }, () => {
  133. this.onFormatGoods()
  134. })
  135. },
  136. /** 选中乐器 */
  137. onSelectInstrument(e: any) {
  138. const { dataset } = e.currentTarget;
  139. if (dataset.id === this.data.selectInstrumentId) {
  140. this.setData({
  141. selectInstrumentId: '',
  142. selectedInstrument: {}
  143. }, () => {
  144. this.onFormatGoods()
  145. })
  146. } else {
  147. const item = this.data.instrumentList.find((item: any) => item.id === dataset.id);
  148. this.setData({
  149. selectInstrumentId: dataset.id,
  150. selectedInstrument: item || {}
  151. }, () => {
  152. this.onFormatGoods()
  153. })
  154. }
  155. },
  156. isLogin() {
  157. // 判断是否登录
  158. if (!app.globalData.isLogin) {
  159. wx.navigateTo({
  160. url: '../login/login',
  161. })
  162. return false
  163. }
  164. return true
  165. },
  166. /** 格式化选中的商品 */
  167. onFormatGoods() {
  168. const selected = this.data.selected;
  169. const selectedInstrument = this.data.selectedInstrument
  170. const params = {
  171. typeName: '',
  172. showSalePrice: '' as any, // 显示的现价
  173. originalPrice: 0, // 原价
  174. salePrice: 0, // 现价
  175. discountPrice: '' as any, // 已省
  176. integerPart: '',
  177. decimalPart: '',
  178. }
  179. // 选中期限
  180. if (selected.id) {
  181. params.typeName = selected.typeName
  182. params.showSalePrice = selected.showSalePrice
  183. params.originalPrice = selected.originalPrice
  184. params.salePrice = selected.salePrice
  185. params.discountPrice = selected.discountPrice
  186. const prices: any = formatPrice(params.salePrice);
  187. params.integerPart = prices.integerPart
  188. params.decimalPart = prices.decimalPart
  189. }
  190. // 选中乐器
  191. if (selectedInstrument.id) {
  192. params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
  193. params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
  194. params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
  195. params.showSalePrice = formatPrice(params.salePrice, "ALL");
  196. params.discountPrice = formatPrice(
  197. params.originalPrice - params.salePrice,
  198. "ALL"
  199. );
  200. const prices: any = formatPrice(params.salePrice);
  201. params.integerPart = prices.integerPart
  202. params.decimalPart = prices.decimalPart
  203. }
  204. this.setData({
  205. formatSelectGood: params
  206. })
  207. },
  208. onSubmit() {
  209. // 判断是否登录
  210. const that = this
  211. if (!this.data.userBeneficiaryId) {
  212. wx.showToast({
  213. title: '请填写享用者的个人信息',
  214. icon: 'none'
  215. })
  216. return
  217. }
  218. debounce(function () {
  219. if (!that.isLogin()) {
  220. return
  221. }
  222. const params = [] as any
  223. const selected = that.data.selected
  224. if (selected.id) {
  225. params.push({
  226. pic: selected.pic,
  227. name: selected.name,
  228. period: selected.period,
  229. num: selected.num,
  230. originalPrice: selected.originalPrice,
  231. salePrice: selected.salePrice,
  232. shopId: selected.shopId,
  233. id: selected.id,
  234. goodsType: 'ACTIVATION_CODE', // INSTRUMENTS
  235. })
  236. }
  237. const selectedInstrument = that.data.selectedInstrument
  238. if (selectedInstrument.id) {
  239. params.push({
  240. pic: selectedInstrument.pic,
  241. name: selectedInstrument.name,
  242. period: selectedInstrument?.period,
  243. num: selectedInstrument?.num || 0,
  244. originalPrice: selectedInstrument.originalPrice,
  245. salePrice: selectedInstrument.salePrice,
  246. shopId: selectedInstrument.shopId,
  247. id: selectedInstrument.id,
  248. goodsType: 'INSTRUMENTS', // INSTRUMENTS
  249. })
  250. }
  251. let info = JSON.stringify({
  252. ...params
  253. });
  254. info = encodeURIComponent(info);
  255. console.log(params, "params")
  256. wx.navigateTo({
  257. url: `../orders/order-detail?orderInfo=${info}&userBeneficiaryId=${that.data.userBeneficiaryId}`,
  258. success: () => {
  259. that.setData({
  260. popupShow: false,
  261. currentIndex: 1,
  262. })
  263. }
  264. })
  265. }, 500)()
  266. },
  267. /** 添加购买人 */
  268. onAddBuyer() {
  269. wx.navigateTo({
  270. url: "../buyerInformation/index?userBeneficiaryId=" + this.data.userBeneficiaryId,
  271. });
  272. },
  273. })