pay.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <view class="app">
  3. <view class="price-box">
  4. <text>支付金额</text>
  5. <text class="price">{{ money }}</text>
  6. </view>
  7. <view class="pay-type-list">
  8. <view class="type-item b-b" @click="changePayType(1)">
  9. <text class="icon iconfont iconweixin"></text>
  10. <view class="con">
  11. <text class="tit">微信支付</text>
  12. <text>推荐使用微信支付</text>
  13. </view>
  14. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 1"></radio></label>
  15. </view>
  16. <!-- #ifdef APP-PLUS -->
  17. <view class="type-item b-b" @click="changePayType(2)">
  18. <text class="icon iconfont iconzhifubao"></text>
  19. <view class="con"><text class="tit">支付宝支付</text></view>
  20. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 2"></radio></label>
  21. </view>
  22. <!-- #endif -->
  23. <view class="type-item" @click="changePayType(3)">
  24. <text class="icon iconfont iconyue"></text>
  25. <view class="con">
  26. <text class="tit">余额支付</text>
  27. <text>可用余额 ¥{{ now_money }}</text>
  28. </view>
  29. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 3"></radio></label>
  30. </view>
  31. </view>
  32. <text class="mix-btn" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</text>
  33. </view>
  34. </template>
  35. <script>
  36. import { balance } from '@/api/wallet.js';
  37. import { createOrderkey, orderPay } from '@/api/order.js';
  38. import { mapState } from 'vuex';
  39. export default {
  40. data() {
  41. return {
  42. payType: 1, //支付类型
  43. // #ifdef H5
  44. payName: 'weixin',
  45. // #endif
  46. // #ifdef MP-WEIXIN
  47. payName: 'weixin',
  48. // #endif
  49. orderInfo: {},
  50. money: 0.0, //订单金额
  51. now_money: 0.0, //余额
  52. orderId: '', //保存订单id
  53. payLoding: false, //判断是否支付中
  54. type: '', //判断是否从订单中进入
  55. // #ifdef H5
  56. froms: '' //保存h5中数据来源对象
  57. // #endif
  58. };
  59. },
  60. computed: {
  61. // #ifdef H5
  62. ...mapState(['weichatObj'])
  63. // #endif
  64. },
  65. onLoad(options) {
  66. this.orderId = options.orderId;
  67. this.money = options.money;
  68. // 载入余额
  69. balance({}).then(({ data }) => {
  70. // 获取余额
  71. this.now_money = data.now_money;
  72. });
  73. },
  74. methods: {
  75. //选择支付方式
  76. changePayType(type) {
  77. this.payType = type;
  78. if (this.payType == 1) {
  79. this.payName = 'weixin';
  80. }
  81. if (this.payType == 2) {
  82. this.payName = 'ali';
  83. }
  84. if (this.payType == 3) {
  85. this.payName = 'yue';
  86. }
  87. },
  88. // 支付成功跳转
  89. paySuccessTo() {
  90. uni.hideLoading();
  91. uni.redirectTo({
  92. url: '/pages/money/paySuccess?orderid=' + this.orderId
  93. });
  94. },
  95. //确认支付
  96. confirm: async function() {
  97. let obj = this;
  98. uni.showLoading({
  99. title: '支付中',
  100. mask: true
  101. });
  102. // 判断是否余额不足
  103. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  104. uni.showModal({
  105. title: '提示',
  106. content: '账户余额不足!',
  107. showCancel: false,
  108. success: res => {},
  109. fail: () => {},
  110. complete: () => {}
  111. });
  112. return;
  113. }
  114. // 支付中
  115. obj.payLoding = true;
  116. // #ifdef H5
  117. // 获取当前是否为微信浏览器
  118. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  119. // #endif
  120. // 判断是否为未支付订单中跳转进入
  121. obj.firstCreateOrder();
  122. },
  123. // 订单创建和支付
  124. firstCreateOrder() {
  125. let obj = this;
  126. let data = {
  127. cache: obj.orderId,
  128. // #ifdef H5
  129. pay_type: obj.payType == 3 ? 1 : 3, //来源
  130. // #endif
  131. // #ifdef MP-WEIXIN
  132. pay_type: obj.payType == 3 ? 1 : 2 //来源
  133. // #endif
  134. };
  135. // 生成订单
  136. createOrderkey(data)
  137. .then(e => {
  138. if (obj.payName == 'yue' && e.data.status == 'SUCCESS') {
  139. if (e.status == 200) {
  140. obj.paySuccessTo();
  141. } else {
  142. obj.$api.msg(msg);
  143. }
  144. }
  145. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  146. if (!e.data.result.jsConfig && e.data.status == 'SUCCESS') {
  147. if (e.status == 200) {
  148. obj.paySuccessTo();
  149. } else {
  150. obj.$api.msg(msg);
  151. }
  152. }
  153. let da = e.data.result.jsConfig;
  154. let data = {
  155. // #ifdef H5
  156. timestamp: da.timestamp,
  157. // #endif
  158. // #ifdef MP
  159. timeStamp: da.timestamp,
  160. // #endif
  161. nonceStr: da.nonceStr,
  162. package: da.package,
  163. signType: da.signType,
  164. paySign: da.paySign,
  165. success: function(res) {
  166. obj.paySuccessTo();
  167. },
  168. fail: () => {
  169. uni.navigateTo({
  170. url: '/pages/order/order?state=0'
  171. });
  172. }
  173. };
  174. // #ifdef H5
  175. if (obj.payName == 'weixin') {
  176. obj.weichatObj.chooseWXPay(data);
  177. }
  178. // #endif
  179. // #ifdef MP-WEIXIN
  180. if (obj.payName == 'weixin') {
  181. wx.requestPayment(data);
  182. }
  183. // #endif
  184. }
  185. uni.hideLoading();
  186. obj.payLoding = false;
  187. })
  188. .catch(e => {
  189. uni.hideLoading();
  190. obj.payLoding = false;
  191. console.log(e);
  192. });
  193. }
  194. }
  195. };
  196. </script>
  197. <style lang="scss">
  198. .app {
  199. width: 100%;
  200. }
  201. .price-box {
  202. background-color: #fff;
  203. height: 265upx;
  204. display: flex;
  205. flex-direction: column;
  206. justify-content: center;
  207. align-items: center;
  208. font-size: 28upx;
  209. color: #909399;
  210. .price {
  211. font-size: 50upx;
  212. color: #303133;
  213. margin-top: 12upx;
  214. &:before {
  215. content: '¥';
  216. font-size: 40upx;
  217. }
  218. }
  219. }
  220. .pay-type-list {
  221. margin-top: 20upx;
  222. background-color: #fff;
  223. padding-left: 60upx;
  224. .type-item {
  225. height: 120upx;
  226. padding: 20upx 0;
  227. display: flex;
  228. justify-content: space-between;
  229. align-items: center;
  230. padding-right: 60upx;
  231. font-size: 30upx;
  232. position: relative;
  233. }
  234. .icon {
  235. width: 100upx;
  236. font-size: 52upx;
  237. }
  238. .iconyue {
  239. color: #fe8e2e;
  240. }
  241. .iconweixin {
  242. color: #36cb59;
  243. }
  244. .iconzhifubao {
  245. color: #01aaef;
  246. }
  247. .tit {
  248. font-size: $font-lg;
  249. color: $font-color-dark;
  250. margin-bottom: 4upx;
  251. }
  252. .con {
  253. flex: 1;
  254. display: flex;
  255. flex-direction: column;
  256. font-size: $font-sm;
  257. color: $font-color-light;
  258. }
  259. }
  260. .mix-btn {
  261. display: flex;
  262. align-items: center;
  263. justify-content: center;
  264. width: 630upx;
  265. height: 80upx;
  266. margin: 80upx auto 30upx;
  267. font-size: $font-lg;
  268. color: #fff;
  269. background-color: $base-color;
  270. border-radius: 10upx;
  271. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  272. }
  273. .clickbg {
  274. background-color: $color-gray !important;
  275. }
  276. </style>