detail.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <view :style="vuex_skin">
  3. <view>
  4. <view class="goods">
  5. <image @click="$util.preview(detail.imgUrl)" :src="detail.imgUrl" style="width: 100vw;" mode="widthFix">
  6. </image>
  7. <view class="title">
  8. <view class="text-bold" style="color: #353535;font-family: PingFang-SC-Bold;font-size: 34rpx;">
  9. <text>{{detail.name}}</text>
  10. <text class="red" v-if="detail.isLimit">(每人限购{{detail.limitBuy}}个)</text>
  11. </view>
  12. <view class=" text-base" style="padding: 20rpx 0;">
  13. <view class="text-bold text-lg">
  14. {{detail.point || 0}}
  15. <text style="padding-left: 6rpx;">积分</text>
  16. </view>
  17. </view>
  18. <block v-if="!$u.test.isEmpty(detail.content)">
  19. <view class="text-sm" style="color: #888888;line-height: 42rpx;">产品说明:
  20. <u-parse :html="detail.content"></u-parse>
  21. </view>
  22. </block>
  23. </view>
  24. </view>
  25. <view v-if="exchangeShow" style="padding: 60rpx da;">
  26. <navigator url="/pages/address/address" hover-class="none"
  27. class="container flex align-center justify-between" style="border-bottom: 1rpx solid #efefef;">
  28. <view class="flex padding align-center">
  29. <image src="@/static/mine/address.png" style="width: 65upx;height: 65upx;"></image>
  30. <view class="padding-left text-sm" v-if="!$u.test.isEmpty(address)">
  31. <view style="font-size: 28upx;font-family: PingFang SC;font-weight: 800;color: #000000;">
  32. {{address.consignee}} {{address.phone}}
  33. </view>
  34. <view class="text-gray">{{address.address}}</view>
  35. </view>
  36. <view class="padding-left"
  37. style="font-size: 28upx;font-family: PingFang SC;font-weight: 800;color: #000000;" v-else>
  38. 请选择收货地址</view>
  39. </view>
  40. <view class="padding">
  41. <u-icon name="arrow-right" color="#d4d4d4"></u-icon>
  42. </view>
  43. </navigator>
  44. <view class="padding-sm">
  45. <view class="flex padding-xs">
  46. <text class="flex justify-center align-center">兑换数量:</text>
  47. <u-number-box :step="1" :disabled="$u.test.isEmpty(detail.remain)" @change="numChange"
  48. :input-width="150" :min="1" :max="maxNum" v-model="exChangeData.num">
  49. </u-number-box>
  50. <view class="center" style="color: #888888;margin-left: 20rpx;font-family: PingFang-SC-Medium;">
  51. <text>剩余库存: {{detail.remain || 0}}</text>
  52. </view>
  53. </view>
  54. <view class="flex padding-xs">
  55. <text class="flex justify-center align-center">积分数量:</text>
  56. <u-number-box :step="10" :disabled="$u.test.isEmpty(userData.userPufaPoint)"
  57. @change="pufaPointChange" :input-width="150" :min="0" :max="maxValue"
  58. v-model="exChangeData.point">
  59. </u-number-box>
  60. <view class="center" style="color: #888888;margin-left: 20rpx;font-family: PingFang-SC-Medium;">
  61. <text>可用积分: {{userData.userPufaPoint || 0}}</text>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. <view class="" style="height: 300rpx;">
  67. </view>
  68. </view>
  69. <view class="bottom-change">
  70. <view v-if="!exchangeShow" class="footer-fixed flex align-center justify-end bg-white"
  71. style="padding:30rpx;;border-top: 1rpx solid #e5e5e5;z-index: 9;">
  72. <button class="cu-btn round text-white bg-base" style="width: 180upx;height: 80upx;"
  73. @click="login">兑换</button>
  74. </view>
  75. <view v-else class="footer-fixed flex align-center justify-between bg-white"
  76. style="border-top: 1rpx solid #e5e5e5;padding: 30rpx;z-index: 9;">
  77. <view class="text-red flex-direction flex" v-if="pointValue>0">
  78. <text>仍需{{pointValue}}积分</text>
  79. </view>
  80. <view class="" v-else>
  81. </view>
  82. <view>
  83. <button class="cu-btn round text-white bg-base"
  84. style="width: 180upx;height: 80upx;margin-right: 10upx;" @click="confirm">兑换</button>
  85. <button @click="exchangeShow=false" class="cu-btn round line-gray"
  86. style="width: 180upx;height: 80upx;z-index: 99;">取消</button>
  87. </view>
  88. </view>
  89. </view>
  90. <login ref="login" @signIn="signIn" @phoneSuccess="phoneSuccess"></login>
  91. <toast ref="toast"></toast>
  92. <exchange-success ref="exchangeSuccess"></exchange-success>
  93. </view>
  94. </template>
  95. <script>
  96. import exchangeSuccess from "@/components/alert/exchangeSuccess.vue"
  97. export default {
  98. components: {
  99. exchangeSuccess
  100. },
  101. data() {
  102. return {
  103. id: '',
  104. //商品详情
  105. detail: {},
  106. //商品地址
  107. address: {},
  108. //点击兑换
  109. exchangeShow: false,
  110. //我的积分数
  111. userData: {
  112. userPufaPoint: ''
  113. },
  114. maxNum:1,
  115. maxValue: 0,
  116. //兑换的数据
  117. exChangeData: {
  118. num: 1,
  119. point: 0
  120. }
  121. }
  122. },
  123. onLoad(options) {
  124. this.id = options.id;
  125. if (this.$u.test.isEmpty(this.id)) {
  126. this.$u.toast('商品id不能为空')
  127. return
  128. }
  129. this.fetchGoodsDetail()
  130. },
  131. onShow() {
  132. this.address = this.$util.getBackParams('addressData')
  133. if (this.$isEmpty(this.address)) {
  134. this.getAddress()
  135. }
  136. },
  137. computed: {
  138. pointValue: {
  139. get() {
  140. let m = this.$digital.floatMul(this.detail.point, this.exChangeData.num)
  141. let n = this.exChangeData.point
  142. let val = this.$digital.floatSub(m, n)
  143. if (val < 0) {
  144. val = 0
  145. this.$u.toast('不可以超过商品所需积分')
  146. return val
  147. }
  148. return val
  149. }
  150. },
  151. },
  152. methods: {
  153. resetData() {
  154. this.userData = {
  155. userPufaPoint: ''
  156. }
  157. this.exChangeData = {
  158. num: 1,
  159. point: 0
  160. }
  161. },
  162. /**
  163. * 确认兑换
  164. */
  165. async confirm() {
  166. if (this.detail.remain < this.exChangeData.num) {
  167. this.$refs.toast.error('库存不足')
  168. return
  169. }
  170. if (this.$isEmpty(this.address)) {
  171. this.$refs.toast.error('请选择收货地址')
  172. return
  173. }
  174. if (this.$isEmpty(this.vuex_userId)) {
  175. this.$refs.toast.error('用户未登录')
  176. return
  177. }
  178. if (this.$isEmpty(this.userData.userPufaPoint)) {
  179. this.$refs.toast.error('无可用积分')
  180. return
  181. }
  182. if (this.pointValue > 0) {
  183. this.$refs.toast.error('积分不足')
  184. return
  185. }
  186. if (this.pointValue < 0) {
  187. this.$refs.toast.error('不可以超过商品所需积分')
  188. return
  189. }
  190. if (this.exChangeData.point > this.userData.userPufaPoint) {
  191. this.$refs.toast.error('积分有误')
  192. return
  193. }
  194. let params = {
  195. type: 1,
  196. phone: this.vuex_phone,
  197. goodsId: this.id,
  198. addressId: this.address.id,
  199. num: this.exChangeData.num
  200. }
  201. let res = await this.$api.pointgoods.exchange(params)
  202. if (res.data.success) {
  203. this.resetData()
  204. this.init()
  205. this.$refs.exchangeSuccess.showSuccess(this.detail.imgUrl)
  206. }
  207. console.log(res.data);
  208. },
  209. pufaPointChange(e) {
  210. this.exChangeData.point = e.value
  211. },
  212. numChange(e) {
  213. this.exChangeData.num = e.value
  214. this.getMaxValue()
  215. },
  216. async init() {
  217. await this.fetchUserData()
  218. this.getMaxValue()
  219. this.getAddress()
  220. this.fetchGoodsDetail()
  221. this.exchangeShow = true
  222. },
  223. /**
  224. * 获取最大购买数量
  225. */
  226. getMaxNum(){
  227. // 是否限购
  228. if (!this.detail.isLimit) {
  229. //不限购,返回库存量
  230. this.maxNum=this.detail.remain
  231. return
  232. }
  233. //限购
  234. if (this.detail.remain > this.detail.limitBuy) {
  235. this.maxNum=this.detail.limitBuy
  236. return
  237. }
  238. this.maxNum=this.detail.remain
  239. },
  240. /**
  241. * 获取步进器的最大值
  242. */
  243. getMaxValue() {
  244. let userPoint = parseInt(this.userData.userPufaPoint)
  245. let detailPoint = this.$digital.floatMul(parseInt(this.detail.point), this.exChangeData.num)
  246. if (userPoint > detailPoint) {
  247. this.maxValue = detailPoint
  248. } else {
  249. this.maxValue = userPoint
  250. }
  251. },
  252. /**
  253. * 获取用户可用积分
  254. */
  255. async fetchUserData() {
  256. if (!this.vuex_phone) {
  257. return
  258. }
  259. let params = {
  260. phone: this.vuex_phone
  261. }
  262. let res = await this.$api.loginUser.userHeatValueAndPufaPoint(params)
  263. this.userData = res.data.data
  264. },
  265. /**
  266. * 获取用户地址
  267. */
  268. async getAddress() {
  269. if (this.$isEmpty(this.vuex_userId)) {
  270. return
  271. }
  272. let params = {
  273. userId: this.vuex_userId
  274. }
  275. let res = await this.$api.address.list(params)
  276. this.address = res.data.data.records[0];
  277. },
  278. /**
  279. * 获取商品详情
  280. */
  281. async fetchGoodsDetail() {
  282. let params = {
  283. id: this.id
  284. }
  285. let res=await this.$api.pointgoods.list(params)
  286. this.detail = res.data.data.records[0]
  287. this.getMaxNum()
  288. }
  289. }
  290. }
  291. </script>
  292. <style lang="scss">
  293. page {
  294. background-color: #FFFFFF;
  295. }
  296. .red {
  297. color: #e72226;
  298. }
  299. .goods {
  300. padding-bottom: 40rpx;
  301. border-bottom: 1rpx solid #efefef;
  302. .title {
  303. padding-top: 20rpx;
  304. margin: 0 20rpx;
  305. font-size: 32rpx;
  306. color: #363636;
  307. }
  308. }
  309. </style>