index.ts 9.1 KB

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