pay.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 class="type-item" @click="changePayType(4)">
  32. <!-- <text class="icon iconfont iconyue"></text> -->
  33. <image src="../../static/icon/bdb.png" mode="widthFix" class="icon1"></image>
  34. <view class="con">
  35. <text class="tit">报单币支付</text>
  36. <text>可用余额 ¥{{ now_money }}</text>
  37. </view>
  38. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 4"></radio></label>
  39. </view>
  40. <view class="type-item" @click="changePayType(5)">
  41. <!-- <text class="icon iconfont iconyue"></text> -->
  42. <image src="../../static/icon/jlb.png" mode="widthFix" class="icon1"></image>
  43. <view class="con">
  44. <text class="tit">奖励支付</text>
  45. <text>可用余额 ¥{{ now_money }}</text>
  46. </view>
  47. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 5"></radio></label>
  48. </view>
  49. </view>
  50. <text class="mix-btn" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</text>
  51. </view>
  52. </template>
  53. <script>
  54. import { balance } from '@/api/wallet.js';
  55. import { createOrderkey,computedOrderkey,orderPay } from '@/api/order.js';
  56. import { mapState } from 'vuex';
  57. // #ifdef H5
  58. import weichatObj from '@/plugin/jweixin-module/index.js';
  59. // #endif
  60. export default {
  61. data() {
  62. return {
  63. payType: 1, //支付类型
  64. // #ifdef H5
  65. payName: 'weixin',
  66. // #endif
  67. // #ifdef MP-WEIXIN
  68. payName: 'weixin',
  69. // #endif
  70. orderInfo: {},
  71. money: 0.0, //订单金额
  72. now_money: 0.0, //余额
  73. orderKey: '',
  74. orderId: '', //保存订单id
  75. payLoding: false, //判断是否支付中
  76. type: '', //判断是否从订单中进入
  77. // #ifdef H5
  78. froms: '', //保存h5中数据来源对象
  79. // #endif
  80. pinkid: '' //保存拼团商品id
  81. };
  82. },
  83. computed: {
  84. // #ifdef H5
  85. ...mapState(['weichatObj']),
  86. // #endif
  87. ...mapState('user',['userInfo'])
  88. },
  89. onLoad(options) {
  90. if (options.type == 1) {
  91. this.type = 1;
  92. this.orderId = options.ordid;
  93. this.money = options.money;
  94. } else {
  95. this.orderKey = options.key;
  96. let prepage = this.$api.prePage();
  97. computedOrderkey({
  98. orderkey: this.orderKey,
  99. couponId: prepage.couponChecked.id, //优惠券编号
  100. addressId: prepage.addressData.id, //地址编号
  101. useIntegral: prepage.checkedPoints ? 1 : 0
  102. }).then(({ data }) => {
  103. // 获取支付金额
  104. this.money = data.result.pay_price;
  105. });
  106. }
  107. // 保存pinkid
  108. if (options.pinkid) {
  109. this.pinkid = options.pinkid;
  110. }
  111. // 载入余额
  112. balance({}).then(({ data }) => {
  113. // 获取余额
  114. this.now_money = data.now_money;
  115. });
  116. },
  117. methods: {
  118. //选择支付方式
  119. changePayType(type) {
  120. this.payType = type;
  121. if (this.payType == 1) {
  122. this.payName = 'weixin';
  123. }
  124. if (this.payType == 2) {
  125. this.payName = 'ali';
  126. }
  127. if (this.payType == 3) {
  128. this.payName = 'yue';
  129. }
  130. if(this.payType == 4) {
  131. this.payName = 'tickets';
  132. }
  133. if(this.payType == 5) {
  134. this.payName = 'brokerage';
  135. }
  136. },
  137. // 支付金额
  138. orderMoneyPay() {
  139. let obj = this;
  140. console.log('支付金额')
  141. orderPay({
  142. uni: obj.orderId,
  143. // #ifdef H5
  144. from: obj.froms ? 'weixin' : 'H5', //来源
  145. // #endif
  146. // #ifdef MP-WEIXIN
  147. from: 'routine', //来源
  148. // #endif
  149. // #ifdef APP-PLUS
  150. from: 'app', //来源
  151. // #endif
  152. paytype: obj.payName //支付类型 weixin-微信 yue-余额
  153. })
  154. .then(e => {
  155. console.log(obj.payName,'支付类型')
  156. console.log(e,'支付金额')
  157. // 判断是否为余额支付
  158. if ((obj.payName == 'yue' || obj.payName == 'tickets' || obj.payName == 'brokerage') && e.data.status == 'SUCCESS') {
  159. if (e.status == 200) {
  160. obj.paySuccessTo();
  161. } else {
  162. obj.$api.msg(msg);
  163. }
  164. }
  165. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  166. let da = e.data.result.jsConfig;
  167. let data = {
  168. // #ifdef H5
  169. timestamp: da.timestamp,
  170. // #endif
  171. // #ifdef MP
  172. timeStamp: da.timestamp,
  173. // #endif
  174. nonceStr: da.nonceStr,
  175. package: da.package,
  176. signType: da.signType,
  177. paySign: da.paySign,
  178. success: function(res) {
  179. obj.paySuccessTo();
  180. },
  181. fail: () => {
  182. uni.navigateTo({
  183. url: '/pages/order/order?state=0'
  184. });
  185. }
  186. };
  187. // #ifdef H5
  188. if(obj.payName == 'weixin'){
  189. weichatObj.chooseWXPay(data);
  190. }
  191. // #endif
  192. // #ifdef MP-WEIXIN
  193. if(obj.payName == 'weixin' || obj.payName == 'routine' ){
  194. console.log('开始支付')
  195. wx.requestPayment(data);
  196. }
  197. // #endif
  198. }
  199. uni.hideLoading();
  200. obj.payLoding = false;
  201. })
  202. .catch(e => {
  203. // 支付完成
  204. uni.hideLoading();
  205. obj.payLoding = false;
  206. console.log(e);
  207. });
  208. },
  209. // 支付成功跳转
  210. paySuccessTo() {
  211. let obj = this
  212. uni.hideLoading();
  213. uni.redirectTo({
  214. url: '/pages/money/paySuccess?orderid=' + this.orderId
  215. });
  216. },
  217. //确认支付
  218. confirm: async function() {
  219. let obj = this;
  220. // 判断是否余额不足
  221. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  222. uni.showModal({
  223. title: '提示',
  224. content: '账户余额不足!',
  225. showCancel: false,
  226. success: res => {},
  227. fail: () => {},
  228. complete: () => {}
  229. });
  230. return;
  231. }
  232. uni.showLoading({
  233. title: '支付中',
  234. mask: true
  235. })
  236. // 支付中
  237. obj.payLoding = true;
  238. // #ifdef H5
  239. // 获取当前是否为微信浏览器
  240. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  241. // #endif
  242. // 判断是否为未支付订单中跳转进入
  243. if (obj.type != 1) {
  244. // 初次生成订单
  245. obj.firstCreateOrder();
  246. } else {
  247. // 已经生成订单未支付
  248. obj.orderMoneyPay();
  249. }
  250. },
  251. // 初次订单创建
  252. firstCreateOrder() {
  253. let obj = this;
  254. // 获取下单页面数据
  255. let prepage = obj.$api.prePage();
  256. let data = {
  257. real_name: prepage.addressData.real_name, //联系人名称
  258. phone: prepage.addressData.phone, //联系人号码
  259. couponId: prepage.couponChecked.id, //优惠券编号
  260. addressId: prepage.addressData.id, //支付地址id
  261. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  262. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  263. mark: prepage.desc, //备注
  264. // #ifdef H5
  265. from: obj.froms ? 'weixin' : 'H5', //来源
  266. // #endif
  267. // #ifdef MP-WEIXIN
  268. from: 'routine', //来源
  269. // #endif
  270. // #ifdef APP-PLUS
  271. from: 'app', //来源
  272. // #endif
  273. shipping_type: 1 //提货方式 1 快递 2自提
  274. };
  275. // 判断是否拼团商品
  276. if (obj.pinkid) {
  277. data.pinkId = obj.pinkid;
  278. }
  279. // 生成订单
  280. createOrderkey(data, obj.orderKey)
  281. .then(({ data, status, msg }) => {
  282. console.log('生成订单')
  283. // 判断是否支付失败
  284. if (data.status == 'ORDER_EXIST') {
  285. uni.showModal({
  286. title: '提示',
  287. content: msg,
  288. showCancel: false
  289. });
  290. uni.hideLoading();
  291. obj.payLoding = false;
  292. return;
  293. }
  294. // 保存订单号
  295. obj.orderId = data.result.orderId;
  296. // 判断是否为余额支付
  297. if (obj.payName == 'yue' || obj.payName == 'tickets' || obj.payName == 'brokerage') {
  298. if (status == 200 && data.status == 'SUCCESS') {
  299. obj.paySuccessTo();
  300. } else {
  301. obj.$api.msg(msg);
  302. }
  303. } else {
  304. // 立即支付
  305. obj.orderMoneyPay();
  306. }
  307. })
  308. .catch(e => {
  309. uni.hideLoading();
  310. obj.payLoding = false;
  311. console.log(e);
  312. });
  313. }
  314. }
  315. };
  316. </script>
  317. <style lang="scss">
  318. .app {
  319. width: 100%;
  320. }
  321. .price-box {
  322. background-color: #fff;
  323. height: 265upx;
  324. display: flex;
  325. flex-direction: column;
  326. justify-content: center;
  327. align-items: center;
  328. font-size: 28upx;
  329. color: #909399;
  330. .price {
  331. font-size: 50upx;
  332. color: #303133;
  333. margin-top: 12upx;
  334. &:before {
  335. content: '¥';
  336. font-size: 40upx;
  337. }
  338. }
  339. }
  340. .pay-type-list {
  341. margin-top: 20upx;
  342. background-color: #fff;
  343. padding-left: 60upx;
  344. .type-item {
  345. height: 120upx;
  346. padding: 20upx 0;
  347. display: flex;
  348. justify-content: space-between;
  349. align-items: center;
  350. padding-right: 60upx;
  351. font-size: 30upx;
  352. position: relative;
  353. }
  354. .icon1 {
  355. width: 60rpx;
  356. margin-right: 40rpx;
  357. }
  358. .icon {
  359. width: 100upx;
  360. font-size: 52upx;
  361. }
  362. .iconyue {
  363. color: #fe8e2e;
  364. }
  365. .iconweixin {
  366. color: #36cb59;
  367. }
  368. .iconzhifubao {
  369. color: #01aaef;
  370. }
  371. .tit {
  372. font-size: $font-lg;
  373. color: $font-color-dark;
  374. margin-bottom: 4upx;
  375. }
  376. .con {
  377. flex: 1;
  378. display: flex;
  379. flex-direction: column;
  380. font-size: $font-sm;
  381. color: $font-color-light;
  382. }
  383. }
  384. .mix-btn {
  385. display: flex;
  386. align-items: center;
  387. justify-content: center;
  388. width: 630upx;
  389. height: 80upx;
  390. margin: 80upx auto 30upx;
  391. font-size: $font-lg;
  392. color: #fff;
  393. background-color: $base-color;
  394. border-radius: 10upx;
  395. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  396. }
  397. .clickbg {
  398. background-color: $color-gray !important;
  399. }
  400. </style>