pay.vue 11 KB

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