index.ts 8.8 KB

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