pay.vue 6.5 KB

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