index.ts 8.9 KB

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