pay.vue 12 KB

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