pay.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. if (obj.money * 1 == 0) {
  181. obj.paySuccessTo();
  182. } else {
  183. wx.requestPayment(data);
  184. }
  185. }
  186. // #endif
  187. }
  188. // #endif
  189. //#ifdef APP-PLUS
  190. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  191. let da = e.data.result.jsConfig;
  192. console.log('--da--', da);
  193. let data = {
  194. appid: da.appid,
  195. noncestr: da.noncestr,
  196. package: da.package,
  197. partnerid: da.partnerid,
  198. prepayid: da.prepayid,
  199. timestamp: da.timestamp,
  200. sign: da.sign
  201. };
  202. console.log('--data--', data);
  203. uni.requestPayment({
  204. provider: 'wxpay',
  205. orderInfo: data,
  206. success(res) {
  207. console.log(res);
  208. uni.showToast({
  209. title: '支付成功',
  210. duration: 2000
  211. });
  212. obj.paySuccessTo();
  213. },
  214. fail(res) {
  215. console.log(res, '失败');
  216. }
  217. });
  218. }
  219. if (obj.payName == 'ali') {
  220. const url = e.data.result.jsConfig;
  221. console.log(url, 'url');
  222. uni.requestPayment({
  223. provider: 'alipay',
  224. orderInfo: url,
  225. success: res => {
  226. console.log(res);
  227. uni.showToast({
  228. title: '支付成功',
  229. duration: 2000
  230. });
  231. obj.paySuccessTo();
  232. },
  233. fail: e => {
  234. console.log(e);
  235. },
  236. complete: () => {}
  237. });
  238. obj.payLoding = false;
  239. }
  240. //#endif
  241. uni.hideLoading();
  242. obj.payLoding = false;
  243. })
  244. .catch(e => {
  245. // 支付完成
  246. uni.hideLoading();
  247. obj.payLoding = false;
  248. console.log(e);
  249. });
  250. },
  251. // 支付成功跳转
  252. paySuccessTo() {
  253. uni.hideLoading();
  254. uni.redirectTo({
  255. url: '/pages/money/paySuccess?orderid=' + this.orderId
  256. });
  257. },
  258. //确认支付
  259. confirm: async function() {
  260. let obj = this;
  261. // 判断是否余额不足
  262. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  263. uni.showModal({
  264. title: '提示',
  265. content: '账户余额不足!',
  266. showCancel: false,
  267. success: res => {},
  268. fail: () => {},
  269. complete: () => {}
  270. });
  271. return;
  272. }
  273. // 判断是否余额不足
  274. uni.showLoading({
  275. title: '支付中',
  276. mask: true
  277. });
  278. // 支付中
  279. obj.payLoding = true;
  280. // #ifdef H5
  281. // 获取当前是否为微信浏览器
  282. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  283. // #endif
  284. // 判断是否为未支付订单中跳转进入
  285. if (obj.type != 1) {
  286. // 初次生成订单
  287. obj.firstCreateOrder();
  288. } else {
  289. // 已经生成订单未支付
  290. obj.orderMoneyPay();
  291. }
  292. },
  293. // 初次订单创建
  294. firstCreateOrder() {
  295. let obj = this;
  296. // 获取下单页面数据
  297. let prepage = obj.$api.prePage();
  298. let data = {
  299. real_name: prepage.addressData.real_name, //联系人名称
  300. phone: prepage.addressData.phone, //联系人号码
  301. couponId: prepage.couponChecked.id, //优惠券编号
  302. addressId: prepage.addressData.id, //支付地址id
  303. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  304. useNowMoney: prepage.checkedYue ? 1 : 0, //是否余额抵扣
  305. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  306. mark: prepage.desc, //备注
  307. // #ifdef H5
  308. from: obj.froms ? 'weixin' : 'H5', //来源
  309. // #endif
  310. // #ifdef MP-WEIXIN
  311. from: 'routine', //来源
  312. // #endif
  313. // #ifdef APP-PLUS
  314. from: 'app', //来源
  315. // #endif
  316. shipping_type: 1 //提货方式 1 快递 2自提
  317. };
  318. // 判断是否拼团商品
  319. if (obj.pinkid) {
  320. data.pinkId = obj.pinkid;
  321. }
  322. console.log(data, '订单参数', obj.orderKey, '订单key');
  323. // 生成订单
  324. createOrderkey(data, obj.orderKey)
  325. .then(({ data, status, msg }) => {
  326. console.log(data, '生成订单');
  327. // 判断是否支付失败
  328. if (data.status == 'ORDER_EXIST') {
  329. uni.showModal({
  330. title: '提示',
  331. content: msg,
  332. showCancel: false
  333. });
  334. uni.hideLoading();
  335. obj.payLoding = false;
  336. return;
  337. }
  338. // 保存订单号
  339. obj.orderId = data.result.orderId;
  340. // 判断是否为余额支付
  341. if (obj.payName == 'yue') {
  342. if (status == 200) {
  343. obj.paySuccessTo();
  344. } else {
  345. obj.$api.msg(msg);
  346. }
  347. } else {
  348. // 立即支付
  349. obj.orderMoneyPay();
  350. }
  351. })
  352. .catch(e => {
  353. uni.hideLoading();
  354. obj.payLoding = false;
  355. console.log(e, '123456789');
  356. });
  357. }
  358. }
  359. };
  360. </script>
  361. <style lang="scss">
  362. .app {
  363. width: 100%;
  364. }
  365. .price-box {
  366. background-color: #fff;
  367. height: 265upx;
  368. display: flex;
  369. flex-direction: column;
  370. justify-content: center;
  371. align-items: center;
  372. font-size: 28upx;
  373. color: #909399;
  374. .price {
  375. font-size: 50upx;
  376. color: #303133;
  377. margin-top: 12upx;
  378. &:before {
  379. content: '¥';
  380. font-size: 40upx;
  381. }
  382. }
  383. }
  384. .pay-type-list {
  385. margin-top: 20upx;
  386. background-color: #fff;
  387. padding-left: 60upx;
  388. .type-item {
  389. height: 120upx;
  390. padding: 20upx 0;
  391. display: flex;
  392. justify-content: space-between;
  393. align-items: center;
  394. padding-right: 60upx;
  395. font-size: 30upx;
  396. position: relative;
  397. }
  398. .icon {
  399. width: 100upx;
  400. font-size: 52upx;
  401. }
  402. .iconweixin1 {
  403. color: #ff0000;
  404. }
  405. .iconyue {
  406. color: #fe8e2e;
  407. }
  408. .iconweixin {
  409. color: #36cb59;
  410. }
  411. .iconzhifubao {
  412. color: #01aaef;
  413. }
  414. .tit {
  415. font-size: $font-lg;
  416. color: $font-color-dark;
  417. margin-bottom: 4upx;
  418. }
  419. .con {
  420. flex: 1;
  421. display: flex;
  422. flex-direction: column;
  423. font-size: $font-sm;
  424. color: $font-color-light;
  425. }
  426. }
  427. .mix-btn {
  428. display: flex;
  429. align-items: center;
  430. justify-content: center;
  431. width: 630upx;
  432. height: 80upx;
  433. margin: 80upx auto 30upx;
  434. font-size: $font-lg;
  435. color: #fff;
  436. background-color: $base-color;
  437. border-radius: 10upx;
  438. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  439. }
  440. .clickbg {
  441. background-color: $color-gray !important;
  442. }
  443. </style>