index.ts 9.3 KB

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