detail.vue 8.8 KB

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