index.ts 8.5 KB

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