detail.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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="address-icon">
  34. <u-icon name="map-fill" color="#ffffff" size="46"></u-icon>
  35. </view>
  36. <view class="padding-left text-sm" v-if="!$u.test.isEmpty(address)">
  37. <view style="font-size: 28upx;font-family: PingFang SC;font-weight: 800;color: #000000;">
  38. {{address.consignee}} {{address.phone}}
  39. </view>
  40. <view class="text-gray">{{address.address}}</view>
  41. </view>
  42. <view class="padding-left"
  43. style="font-size: 28upx;font-family: PingFang SC;font-weight: 800;color: #000000;" v-else>
  44. 请选择收货地址</view>
  45. </view>
  46. <view class="padding">
  47. <u-icon name="arrow-right" color="#d4d4d4"></u-icon>
  48. </view>
  49. </navigator>
  50. <view class="padding-sm">
  51. <view class="flex padding-xs">
  52. <text class="flex justify-center align-center">兑换数量:</text>
  53. <u-number-box :step="1" :disabled="$u.test.isEmpty(detail.remain)" @change="numChange"
  54. :input-width="150" :min="1" :max="maxNum" v-model="exChangeData.num">
  55. </u-number-box>
  56. <view class="center" style="color: #888888;margin-left: 20rpx;font-family: PingFang-SC-Medium;">
  57. <text>剩余库存: {{detail.remain || 0}}</text>
  58. </view>
  59. </view>
  60. <view class="flex padding-xs" v-if="exchangeType != 0">
  61. <text class="flex justify-center align-center">积分数量:</text>
  62. <u-number-box :step="10" :disabled="$u.test.isEmpty(userData.userPufaPoint)"
  63. @change="pufaPointChange" :input-width="150" :min="0" :max="maxValue"
  64. v-model="exChangeData.point">
  65. </u-number-box>
  66. <view class="center" style="color: #888888;margin-left: 20rpx;font-family: PingFang-SC-Medium;">
  67. <text>可用积分: {{userData.userPufaPoint || 0}}</text>
  68. </view>
  69. </view>
  70. </view>
  71. </view>
  72. <view class="" style="height: 300rpx;">
  73. </view>
  74. </view>
  75. <view class="bottom-change">
  76. <view v-if="!exchangeShow" class="footer-fixed flex align-center justify-end bg-white"
  77. style="padding:30rpx;;border-top: 1rpx solid #e5e5e5;z-index: 9;">
  78. <button class="cu-btn round text-white bg-base" style="width: 180upx;height: 80upx;"
  79. @click="login">兑换</button>
  80. </view>
  81. <view v-else class="footer-fixed flex align-center justify-between bg-white"
  82. style="border-top: 1rpx solid #e5e5e5;padding: 30rpx;z-index: 9;">
  83. <view class="text-red flex-direction flex" >
  84. <text v-if="exchangeType == 0">需支付{{cashValue}}元</text>
  85. <text v-if="pointValue > 0 && exchangeType == 1">仍需{{pointValue}}积分</text>
  86. <text v-if="cashValue > 0 && exchangeType == 2">需支付{{cashValue}}元</text>
  87. </view>
  88. <view>
  89. <button class="cu-btn round text-white bg-base"
  90. style="width: 180upx;height: 80upx;margin-right: 10upx;" @click="confirmTips">兑换</button>
  91. <button @click="cancel" class="cu-btn round line-gray"
  92. style="width: 180upx;height: 80upx;z-index: 99;">取消</button>
  93. </view>
  94. </view>
  95. </view>
  96. <u-action-sheet
  97. :list="exchangeTypeList"
  98. v-model="exchangeTypeShow"
  99. safe-area-inset-bottom
  100. @click="selectExchangeType"
  101. >
  102. </u-action-sheet>
  103. <u-modal
  104. v-model="confirmExchangeShow"
  105. :content="tipsContent"
  106. show-cancel-button
  107. :confirm-style="confirmStyle"
  108. @confirm="confirm"
  109. >
  110. </u-modal>
  111. <webAuthAlert ref="webAuthAlert" @confirm="webAuthConfirm"></webAuthAlert>
  112. <login ref="login" @signIn="signIn" @phoneSuccess="phoneSuccess"></login>
  113. <toast ref="toast"></toast>
  114. <home-btn></home-btn>
  115. <exchange-success ref="exchangeSuccess"></exchange-success>
  116. </view>
  117. </template>
  118. <script>
  119. import exchangeSuccess from "@/components/alert/exchangeSuccess.vue"
  120. import webAuthAlert from "@/components/alert/webAuthAlert.vue"
  121. export default {
  122. components: {
  123. exchangeSuccess,
  124. webAuthAlert
  125. },
  126. data() {
  127. return {
  128. id: '',
  129. //商品详情
  130. detail: {},
  131. //商品地址
  132. address: {},
  133. //点击兑换
  134. exchangeShow: false,
  135. //我的积分数
  136. userData: {
  137. userPufaPoint: ''
  138. },
  139. maxNum:1,
  140. maxValue: 0,
  141. //兑换的数据
  142. exChangeData: {
  143. num: 1,
  144. point: 0
  145. },
  146. //兑换列表
  147. exchangeTypeList: [{
  148. text: '现金兑换'
  149. }, {
  150. text: '积分兑换'
  151. }, {
  152. text: '积分+现金兑换'
  153. }],
  154. //兑换方式
  155. exchangeTypeShow: false,
  156. //兑换类型
  157. exchangeType: null, //可选值 0:现金 1:积分 2:现金+积分
  158. //确认兑换
  159. confirmExchangeShow: false,
  160. //提示内容
  161. tipsContent: '',
  162. confirmStyle: {
  163. color: 'white',
  164. },
  165. }
  166. },
  167. onLoad(options) {
  168. this.id = options.id;
  169. if (this.$u.test.isEmpty(this.id)) {
  170. this.$u.toast('商品id不能为空')
  171. return
  172. }
  173. this.fetchGoodsDetail()
  174. this.confirmStyle.color = this.vuex_theme.bgColor;
  175. },
  176. onShow() {
  177. uni.$on('ADDRESS',(res)=>{
  178. this.address=res
  179. })
  180. if (this.$isEmpty(this.address)) {
  181. this.getAddress()
  182. return
  183. }
  184. },
  185. computed: {
  186. pointValue: {
  187. get() {
  188. let m = this.$digital.floatMul(this.detail.point, this.exChangeData.num)
  189. let n = this.exChangeData.point
  190. let val = this.$digital.floatSub(m, n)
  191. if (val < 0) {
  192. val = 0
  193. this.$u.toast('不可以超过商品所需积分')
  194. return val
  195. }
  196. return val
  197. }
  198. },
  199. cashValue: {
  200. get() {
  201. let value = null;
  202. if(this.exchangeType == 0){
  203. value = this.pointValue / 100;
  204. return value
  205. }
  206. value = ((this.detail.point * this.exChangeData.num) - this.exChangeData.point) / 100;
  207. return value
  208. }
  209. }
  210. },
  211. methods: {
  212. resetData() {
  213. this.userData = {
  214. userPufaPoint: ''
  215. }
  216. this.exChangeData = {
  217. num: 1,
  218. point: 0
  219. }
  220. },
  221. /**
  222. * 兑换提示
  223. */
  224. confirmTips(){
  225. if (this.detail.remain < this.exChangeData.num) {
  226. this.$refs.toast.error('库存不足')
  227. return
  228. }
  229. if (this.$isEmpty(this.address)) {
  230. this.$refs.toast.error('请选择收货地址')
  231. return
  232. }
  233. if (this.$isEmpty(this.vuex_userId)) {
  234. this.$refs.toast.error('用户未登录')
  235. return
  236. }
  237. if (this.$isEmpty(this.userData.userPufaPoint) && this.exchangeType != 0) {
  238. this.$refs.toast.error('无可用积分')
  239. return
  240. }
  241. if (this.pointValue > 0 && this.exchangeType == 1) {
  242. this.$refs.toast.error('积分不足')
  243. return
  244. }
  245. if (this.pointValue < 0) {
  246. this.$refs.toast.error('不可以超过商品所需积分')
  247. return
  248. }
  249. if (this.exChangeData.point > this.userData.userPufaPoint) {
  250. this.$refs.toast.error('积分有误')
  251. return
  252. }
  253. this.confirmExchangeShow = true;
  254. if(this.exchangeType == 0){
  255. this.tipsContent = `确实使用${this.cashValue}元兑换该商品吗?`
  256. }
  257. if(this.exchangeType == 1){
  258. this.tipsContent = `确实使用${this.exChangeData.point}积分兑换该商品吗?`
  259. }
  260. if(this.exchangeType == 2){
  261. this.tipsContent = `确实使用${this.exChangeData.point}积分+${this.cashValue}元兑换该商品吗?`
  262. }
  263. },
  264. /**
  265. * 确认兑换
  266. */
  267. async confirm() {
  268. // #ifdef H5
  269. if(this.$isEmpty(this.vuex_gzh_userOpenid)){
  270. let code = this.$util.getUrlParam("code");
  271. if(this.$isEmpty(code)){
  272. this.$refs.webAuthAlert.show = true;
  273. }else{
  274. this.getWxUserInfo(code)
  275. }
  276. }else{
  277. this.exchangeGoods();
  278. }
  279. // #endif
  280. // #ifdef MP-WEIXIN
  281. this.exchangeGoods();
  282. // this.$refs.toast.warn('小程序暂未开放现金兑换,请前往H5兑换')
  283. // #endif
  284. },
  285. //获取微信网页授权用户信息
  286. async getWxUserInfo(code) {
  287. let params = {
  288. code: code
  289. }
  290. let res = await this.$api.wxApp.getH5WxUserInfo(params)
  291. if (res.data.success) {
  292. this.$u.vuex('vuex_gzh_userOpenid', res.data.data.openid);
  293. this.exchangeGoods();
  294. } else {
  295. this.$refs.toast.error(res.data.msg)
  296. }
  297. },
  298. //商品兑换
  299. async exchangeGoods() {
  300. let params = {
  301. type: this.exchangeType,
  302. phone: this.vuex_phone,
  303. goodsId: this.id,
  304. addressId: this.address.id,
  305. num: this.exChangeData.num,
  306. activeId: this.vuex_active_setting.defaultActiveId,
  307. openId: this.vuex_gzh_userOpenid
  308. }
  309. if(this.exchangeType === 2){params.point = this.exChangeData.point;}
  310. this.$api.pointgoods.exchange(params).then(res=>{
  311. if (res.data.success) {
  312. this.resetData()
  313. this.fetchUserData()
  314. this.getMaxValue()
  315. this.getAddress()
  316. this.fetchGoodsDetail()
  317. if(this.exchangeType == 1){
  318. this.$refs.exchangeSuccess.showSuccess(this.detail.imgUrl)
  319. }else{
  320. //混合兑换,用户选择纯积分兑换
  321. if(!this.$isNotEmpty(res.data.data)){
  322. this.$refs.exchangeSuccess.showSuccess(this.detail.imgUrl)
  323. return;
  324. }
  325. // #ifdef MP-WEIXIN
  326. this.$refs.toast.warn('小程序暂未开放现金兑换,请前往H5兑换')
  327. // #endif
  328. // #ifdef H5
  329. console.log("支付订单",res.data.data)
  330. this.$shareConfig.H5Pay(res.data.data).then((res) => {
  331. console.log("商品兑换页面res", res)
  332. this.$refs.exchangeSuccess.showSuccess(this.detail.imgUrl)
  333. },(err) => {
  334. console.log("商品兑换页面err", err)
  335. this.$refs.toast.error(err.errMsg)
  336. })
  337. // #endif
  338. }
  339. }
  340. }).catch(err=>{
  341. console.log(error);
  342. })
  343. },
  344. //网页授权
  345. webAuthConfirm(){
  346. this.$webAuth.getUserOpenid();
  347. },
  348. cancel(){
  349. this.exchangeShow = false;
  350. this.resetData();
  351. },
  352. pufaPointChange(e) {
  353. this.exChangeData.point = e.value
  354. },
  355. numChange(e) {
  356. this.exChangeData.num = e.value
  357. this.getMaxValue()
  358. },
  359. selectExchangeType(index){
  360. this.exchangeType = index;
  361. this.exchangeShow = true;
  362. },
  363. async init() {
  364. await this.fetchUserData()
  365. this.getMaxValue()
  366. this.getAddress()
  367. this.fetchGoodsDetail()
  368. // this.exchangeShow = true
  369. this.exchangeTypeShow = true;
  370. },
  371. /**
  372. * 获取最大购买数量
  373. */
  374. getMaxNum(){
  375. // 是否限购
  376. if (!this.detail.isLimit) {
  377. //不限购,返回库存量
  378. this.maxNum=this.detail.remain
  379. return
  380. }
  381. //限购
  382. if (this.detail.remain > this.detail.limitBuy) {
  383. this.maxNum=this.detail.limitBuy
  384. return
  385. }
  386. this.maxNum=this.detail.remain
  387. },
  388. /**
  389. * 获取步进器的最大值
  390. */
  391. getMaxValue() {
  392. let userPoint = parseInt(this.userData.userPufaPoint)
  393. let detailPoint = this.$digital.floatMul(parseInt(this.detail.point), this.exChangeData.num)
  394. if (userPoint > detailPoint) {
  395. this.maxValue = detailPoint
  396. } else {
  397. this.maxValue = userPoint
  398. }
  399. },
  400. /**
  401. * 获取用户可用积分
  402. */
  403. async fetchUserData() {
  404. if (!this.vuex_phone) {
  405. return
  406. }
  407. let params = {
  408. phone: this.vuex_phone
  409. }
  410. let res = await this.$api.loginUser.userHeatValueAndPufaPoint(params)
  411. this.userData = res.data.data
  412. },
  413. /**
  414. * 获取用户地址
  415. */
  416. async getAddress() {
  417. if (this.$isEmpty(this.vuex_userId)) {
  418. return
  419. }
  420. let params = {
  421. userId: this.vuex_userId
  422. }
  423. let res = await this.$api.address.list(params)
  424. this.address = res.data.data.records[0];
  425. },
  426. /**
  427. * 获取商品详情
  428. */
  429. async fetchGoodsDetail() {
  430. let params = {
  431. id: this.id
  432. }
  433. let res=await this.$api.pointgoods.detail(params)
  434. this.detail = res.data.data
  435. this.getMaxNum()
  436. }
  437. }
  438. }
  439. </script>
  440. <style lang="scss">
  441. page {
  442. background-color: #FFFFFF;
  443. }
  444. .red {
  445. color: #e72226;
  446. }
  447. .goods {
  448. padding-bottom: 40rpx;
  449. border-bottom: 1rpx solid #efefef;
  450. .title {
  451. padding-top: 20rpx;
  452. margin: 0 20rpx;
  453. font-size: 32rpx;
  454. color: #363636;
  455. justify-content: center;
  456. }
  457. }
  458. .address-icon{
  459. background: var(--bgColor);
  460. width: 80rpx;
  461. height: 80rpx;
  462. border-radius: 16rpx;
  463. display: flex;
  464. justify-content: center;
  465. align-items: center;
  466. }
  467. </style>