pay.vue 11 KB

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