Btn.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <div class="family-btn">
  3. <div class="pay-bottom1 clearfix">
  4. <van-button class="pay-btn" round type="primary" v-if="loading" loading :loading-text="title" />
  5. <van-button class="pay-btn" round type="primary" v-else @click="postApi">{{title}}</van-button>
  6. <div class="pay-sum"><span style="color:#4c4c4c">合计</span>: ¥<span class="money-text">{{total | getAcounting('', false)}}</span></div>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import { mapGetters } from 'vuex'
  12. export default {
  13. data () {
  14. return {
  15. oid: this.$route.params.order_id,
  16. payData: {},
  17. loading: false,
  18. countdownFlag: false,
  19. userData: {
  20. name: '',
  21. tel: ''
  22. }
  23. }
  24. },
  25. props: {
  26. total: Number,
  27. title: String,
  28. type: String,
  29. orderData: Object
  30. },
  31. computed: {
  32. ...mapGetters(['userInfo']),
  33. loadingText: function () {
  34. if (this.type === 'order' || this.type === 'orderJoin') {
  35. return '创建订单中...'
  36. } else {
  37. return '正在支付中...'
  38. }
  39. }
  40. },
  41. methods: {
  42. loadClick () {
  43. this.loading = true
  44. this.$toast.loading({
  45. message: this.loadingText,
  46. forbidClick: true,
  47. duration: 0
  48. })
  49. },
  50. cancelLoading () {
  51. this.loading = false
  52. this.$toast.clear()
  53. },
  54. // 创建订单
  55. createOrder () {
  56. this.$post({
  57. url: 'v1/spirit/order/create',
  58. data: {
  59. product_id: this.orderData.product_id,
  60. quantity: this.orderData.count,
  61. address_id: this.orderData.address_id,
  62. use_balance: this.orderData.use_balance,
  63. remark: '',
  64. by_vip_user: this.orderData.by_vip_user
  65. }
  66. }).then((resp) => {
  67. this.cancelLoading()
  68. if (resp.resp_common && resp.resp_common.code) {
  69. this.$toast(resp.resp_common.msg)
  70. return
  71. }
  72. this.oid = resp.order_id
  73. // 提交订单为0元,跳支付成功
  74. if (!resp.IsPaid) {
  75. this.$router.push(`/pay/${resp.order_id}`)
  76. } else {
  77. this.$toast('支付成功~')
  78. this.$router.replace(`/order/${resp.order_id}`)
  79. }
  80. })
  81. },
  82. goSuccess () {
  83. this.$router.push(`/order/${this.oid}`)
  84. },
  85. // 微信支付
  86. onBridgeReady () {
  87. this.$toast.clear()
  88. let that = this
  89. // eslint-disable-next-line
  90. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  91. appId: this.payData.appId,
  92. timeStamp: this.payData.timeStamp,
  93. nonceStr: this.payData.nonceStr,
  94. package: this.payData.package,
  95. signType: this.payData.signType,
  96. paySign: this.payData.paySign
  97. },
  98. function (res) {
  99. if (res.err_msg === 'get_brand_wcpay_request:ok') {
  100. that.$toast.loading('支付成功,跳转中···')
  101. setTimeout(() => { that.goSuccess() }, 1000)
  102. } else {
  103. that.$toast('微信支付失败!')
  104. }
  105. })
  106. },
  107. pay () {
  108. this.$post({
  109. url: 'v1/pay',
  110. data: {
  111. order_id: this.oid,
  112. pay_way: 'weixinpay'
  113. }
  114. }).then((resp) => {
  115. this.cancelLoading()
  116. this.payData = resp.pay_data
  117. // eslint-disable-next-line
  118. if (typeof WeixinJSBridge === undefined) {
  119. if (document.addEventListener) {
  120. document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady, false)
  121. } else if (document.attachEvent) {
  122. document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady)
  123. document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady)
  124. }
  125. } else {
  126. this.onBridgeReady()
  127. }
  128. })
  129. },
  130. validatorOrder () {
  131. let msg
  132. if (this.type === 'payOrder') {
  133. return { isOk: !msg, msg }
  134. }
  135. if (!this.orderData.address_id) {
  136. msg = '请先新增收件地址'
  137. }
  138. return { isOk: !msg, msg }
  139. },
  140. postApi () {
  141. let { isOk, msg } = this.validatorOrder()
  142. if (isOk) {
  143. this.loadClick()
  144. if (this.type === 'order') {
  145. this.createOrder()
  146. } else {
  147. this.$toast.loading('支付中···')
  148. this.pay()
  149. }
  150. } else {
  151. this.$toast(msg)
  152. }
  153. }
  154. },
  155. created () {
  156. },
  157. mounted () {
  158. },
  159. watch: {
  160. },
  161. components: {}
  162. }
  163. </script>
  164. <style lang="less" scoped>
  165. .family-btn {
  166. .pay-bottom1 {
  167. background: #fff;
  168. box-sizing: border-box;
  169. padding: 0.2rem;
  170. position: fixed;
  171. bottom: 0;
  172. width: 100%;
  173. z-index: 999;
  174. .pay-btn {
  175. height: 0.74rem;
  176. min-width: 2.62rem;
  177. background: #07c160;
  178. float: right;
  179. color: #fff;
  180. font-size: 0.28rem;
  181. margin-left: 0.4rem;
  182. border: 1px solid #07c160;
  183. }
  184. .pay-btn1 {
  185. background: linear-gradient(#ffd01e, #ff8917);
  186. }
  187. .pay-btn2 {
  188. background: #999;
  189. }
  190. .pay-sum {
  191. color: #f47b7b;
  192. font-size: 0.28rem;
  193. float: right;
  194. margin-top: 0.2rem;
  195. .money-text {
  196. font-size: 0.34rem;
  197. }
  198. }
  199. }
  200. }
  201. </style>