pay.vue 10 KB

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