index.ts 8.8 KB

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