checkout.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <view class="cu-modal bottom-modal" :class="{ show: visible }" v-if="visible">
  3. <view class="mask" @click="close"></view>
  4. <view class="wrapper cu-dialog" v-if="data">
  5. <view class="relative">
  6. <view class="title">
  7. {{ data.name }}
  8. <view class="close" @click="close">
  9. <text class="cuIcon-close"></text>
  10. </view>
  11. </view>
  12. </view>
  13. <view>
  14. <view class="flex-align paddingY10">
  15. <view class="flex1 font4">抽取次数</view>
  16. <view class="color-theme">
  17. <text class="font6">¥</text>
  18. <text class="font6 marginR5">{{ data.price }} x {{ num }}</text>
  19. </view>
  20. </view>
  21. <view class="flex-align paddingY10">
  22. <view class="flex1 font4">优惠券</view>
  23. <view class="font2 marginR5" :class="couponAmount ? 'color-theme' : ''">
  24. {{ couponAmount ? '已使用优惠¥' : '' }}
  25. {{ couponAmount || '暂无优惠券可选' }}
  26. </view>
  27. </view>
  28. <view class="flex-align paddingY10">
  29. <view class="flex1 font4" v-if="magic">
  30. 源石
  31. <view class="blance">(剩余源石:{{ magic.balance || 0 }})</view>
  32. </view>
  33. <view class="font2 blance marginR5">
  34. 已抵扣
  35. <span class="font6 color-theme">¥{{ coin || 0 }}</span>
  36. </view>
  37. </view>
  38. <view v-if="cash && hide" class="flex-align paddingY10">
  39. <view class="flex1 font4">
  40. 使用钱包支付
  41. <text class="color-theme">(余额:¥{{ cash.balance ||0 }})</text>
  42. </view>
  43. <radio
  44. color="#FF9600"
  45. value="r1"
  46. :checked="cashChecked"
  47. style="transform: scale(0.7)"
  48. @click.stop="changeCashChecked"
  49. />
  50. </view>
  51. <view class="paddingY10 flex-align-between">
  52. <view>
  53. <view class="font3 flex-align" @click="showWeb">
  54. 我已满18周岁,已阅读并同意
  55. <text style="color: #ff9600">《宝箱服务协议》</text>
  56. </view>
  57. <view class="font2 color-2 paddingY6">
  58. 宝箱商品存在概率性,请谨慎消费,未满18周岁禁止参与
  59. </view>
  60. <view class="color-2 font2 line-ellipsis">不支持7天无理由退换货</view>
  61. </view>
  62. <radio
  63. color="#FF9600"
  64. value="r1"
  65. :checked="checked"
  66. style="transform: scale(0.7)"
  67. @click.stop="changeChecked"
  68. />
  69. </view>
  70. </view>
  71. <view class="payBox">
  72. <view class="payPrice" v-if="lastPrice != null">
  73. 实付:
  74. <span class="payNum">¥{{ lastPrice.toFixed(2) }}</span>
  75. </view>
  76. <button class="payBtn" style="" v-if="lastPrice != null" @click="pay()">
  77. 立即支付
  78. </button>
  79. </view>
  80. <view class="fill"></view>
  81. </view>
  82. </view>
  83. </template>
  84. <script>
  85. import cmButton from '../../components/cm-button.vue'
  86. import { throttle } from '@/utils'
  87. import { H5 } from '@/utils/config'
  88. import resource from '@/utils/resource'
  89. import { baseEncode } from '../../utils/utils'
  90. export default {
  91. components: { cmButton },
  92. props: {
  93. data: Object,
  94. poolId: [Number,String]
  95. },
  96. data() {
  97. return {
  98. resource,
  99. num: 1,
  100. visible: false,
  101. checked: true,
  102. coin: '',
  103. couponAmount: null,
  104. cash: null,
  105. cashChecked: false,
  106. lastPrice: null, //最终金额
  107. clickFlag: false,
  108. magic: null,
  109. bizTradeNo: ''
  110. }
  111. },
  112. computed: {
  113. hide() {
  114. return !this.$store.state.hide
  115. },
  116. payInfo() {
  117. return this.$store.state.payInfo
  118. }
  119. },
  120. watch: {
  121. payInfo(val) {
  122. if (val && val.payResult == 'SUCCESS') {
  123. this.paySuccess(val)
  124. } else if (val && val.payResult !== 'SUCCESS') {
  125. }
  126. }
  127. },
  128. methods: {
  129. show(num, data = {}, boxNum, seatNumbers, packFlag) {
  130. this.num = num
  131. this.boxNum = boxNum
  132. this.seatNumbers = seatNumbers
  133. this.coin = data.magicAmount || null
  134. this.couponAmount = data.couponAmount || null
  135. this.lastPrice = data.paymentAmount
  136. if (data.cash && data.cash.balance > data.paymentAmount) {
  137. this.cash = data.cash
  138. this.magic = data.magic
  139. this.cashChecked = true
  140. } else {
  141. this.cash = null
  142. this.cashChecked = false
  143. }
  144. this.packFlag = packFlag || null
  145. this.visible = true
  146. },
  147. close() {
  148. this.visible = false
  149. this.$emit('close', false)
  150. },
  151. changeChecked(e) {
  152. this.checked = !this.checked
  153. },
  154. changeCashChecked(e) {
  155. this.cashChecked = !this.cashChecked
  156. },
  157. async pay() {
  158. if (this.clickFlag) return
  159. this.realPay()
  160. },
  161. async realPay() {
  162. if (!this.checked) {
  163. this.$message.error('请同意《宝箱服务协议》')
  164. return
  165. }
  166. let paymentType = this.cashChecked ? 'WALLET' : 'WXPAY_JSAPI'
  167. const res = await this.$service.award.apply(
  168. this.poolId,
  169. this.packFlag ? 1 : this.num,
  170. 'ALIPAY_APP',
  171. this.boxNum,
  172. this.seatNumbers,
  173. this.packFlag
  174. )
  175. //判断是否是微信支付
  176. if (res) {
  177. if (res.paySuccess) {
  178. this.clickFlag = true
  179. setTimeout(() => {
  180. this.$emit('success', { tradeNo: res.bizTradeNo, num: this.num })
  181. }, 50)
  182. setTimeout(() => {
  183. this.close()
  184. this.clickFlag = false
  185. }, 350)
  186. return
  187. }
  188. let self = this
  189. // 支付宝支付
  190. uni.requestPayment({
  191. provider: 'alipay',
  192. orderInfo: res.payInfo,
  193. success: function (res) {
  194. self.clickFlag = true
  195. setTimeout(() => {
  196. self.$emit('success', { tradeNo: data.bizTradeNo, num: this.num })
  197. }, 50)
  198. setTimeout(() => {
  199. self.close()
  200. self.clickFlag = false
  201. }, 350)
  202. },
  203. fail: function (err) {
  204. console.log('fail:' + JSON.stringify(err));
  205. }
  206. });
  207. // if(res.redirectUrl){
  208. // this.thirdPay(res)
  209. // }else{
  210. // this.wechatPay(res)
  211. // }
  212. } else {
  213. this.$event.emit(this.$event.key.NUM_REFRESH)
  214. }
  215. },
  216. encode(arraybuffer) {
  217. var bytes = new Uint8Array(arraybuffer),
  218. i,
  219. len = bytes.length,
  220. base64 = ''
  221. for (i = 0; i < len; i += 3) {
  222. base64 += chars[bytes[i] >> 2]
  223. base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]
  224. base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]
  225. base64 += chars[bytes[i + 2] & 63]
  226. }
  227. if (len % 3 === 2) {
  228. base64 = base64.substring(0, base64.length - 1) + '='
  229. } else if (len % 3 === 1) {
  230. base64 = base64.substring(0, base64.length - 2) + '=='
  231. }
  232. return base64
  233. },
  234. wechatPay(data) {
  235. let payInfo = data.payInfo
  236. wx.requestPayment({
  237. timeStamp: payInfo.timeStamp,
  238. nonceStr: payInfo.nonceStr,
  239. package: payInfo.packageValue,
  240. signType: payInfo.signType,
  241. paySign: payInfo.paySign,
  242. success: (res) => {
  243. this.clickFlag = true
  244. setTimeout(() => {
  245. this.$emit('success', { tradeNo: data.bizTradeNo, num: this.num })
  246. }, 50)
  247. setTimeout(() => {
  248. this.close()
  249. this.clickFlag = false
  250. }, 350)
  251. },
  252. fail: (res) => {}
  253. })
  254. },
  255. thirdPay(data) {
  256. let self = this
  257. let redirectUrl = data.redirectUrl
  258. let requestId = data.requestId
  259. this.bizTradeNo = data.bizTradeNo
  260. const base64Url = encodeURIComponent(baseEncode(redirectUrl))
  261. wx.openEmbeddedMiniProgram({
  262. path: 'pages/pay/order', // 启动目标页面
  263. appId: 'wx0448557563ffc600', // 启动的小程序
  264. envVersion: 'release', // develop | trial | release
  265. verify: 'binding',
  266. extraData: {
  267. title: '购买' + `${self.data.name}X${self.num}`, // 导航Title
  268. tip: `¥${self.data.price}(${self.data.name}X${self.num})`, // 支付提示内容
  269. requestId: requestId, // 本次请求ID
  270. action: 'epPay', // 支付模式
  271. payUrl: base64Url // 支付地址, 需要进行Base64转换
  272. },
  273. success: (res) => {
  274. console.info('启动成功', res)
  275. },
  276. fail: (err) => {
  277. console.info('启动失败', err)
  278. }
  279. })
  280. },
  281. paySuccess(payInfo) {
  282. this.clickFlag = true
  283. console.log(this.bizTradeNo)
  284. setTimeout(() => {
  285. this.$emit('success', { tradeNo: this.bizTradeNo, num: this.num })
  286. }, 50)
  287. setTimeout(() => {
  288. this.bizTradeNo = ''
  289. this.$store.state.payInfo = null
  290. this.close()
  291. this.clickFlag = false
  292. }, 350)
  293. },
  294. showWeb() {
  295. this.$router.web(H5.moli.url, H5.moli.title)
  296. }
  297. }
  298. }
  299. </script>
  300. <style lang="scss" scoped>
  301. .mask {
  302. position: absolute;
  303. left: 0;
  304. right: 0;
  305. top: 0;
  306. bottom: 0;
  307. }
  308. .wrapper {
  309. padding: 0 28rpx;
  310. background: #fff;
  311. border-radius: 15px 15px 2px 2px !important;
  312. overflow: hidden;
  313. .title {
  314. text-align: center;
  315. font-size: 32rpx;
  316. font-family: Source Han Sans, Source Han Sans;
  317. font-weight: 700;
  318. color: #000000;
  319. padding: 44rpx 0 48rpx 0;
  320. position: relative;
  321. }
  322. .x {
  323. width: 32rpx;
  324. height: 32rpx;
  325. }
  326. .close {
  327. position: absolute;
  328. right: 0rpx;
  329. width: 48rpx;
  330. height: 48rpx;
  331. background: #ebebeb;
  332. border-radius: 48rpx;
  333. color: #a2a2a2;
  334. top: 30rpx;
  335. line-height: 48rpx;
  336. }
  337. .logo {
  338. width: 180rpx;
  339. height: 180rpx;
  340. }
  341. .fill {
  342. height: 50rpx;
  343. }
  344. }
  345. .blance {
  346. display: inline-block;
  347. font-size: 24rpx;
  348. font-family: Source Han Sans, Source Han Sans;
  349. font-weight: 350;
  350. color: #999999;
  351. margin-left: 10rpx;
  352. }
  353. .payBox {
  354. display: flex;
  355. width: 100%;
  356. justify-content: space-between;
  357. align-items: center;
  358. margin-top: 40rpx;
  359. .payPrice {
  360. font-size: 28rpx;
  361. font-family: Source Han Sans, Source Han Sans;
  362. font-weight: 350;
  363. color: #000000;
  364. flex: 1;
  365. .payNum {
  366. font-size: 44rpx;
  367. font-family: Source Han Sans, Source Han Sans;
  368. font-weight: 500;
  369. color: #ff9600;
  370. }
  371. }
  372. .payBtn {
  373. background-color: #fec433;
  374. color: #000;
  375. height: 80rpx;
  376. line-height: 80rpx;
  377. font-size: 28rpx;
  378. font-family: Source Han Sans, Source Han Sans;
  379. font-weight: 350;
  380. color: #000000;
  381. width: 350rpx;
  382. border-radius: 40rpx;
  383. border: none;
  384. box-shadow: none;
  385. }
  386. }
  387. </style>