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. <!-- #ifdef H5 -->
  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)">
  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 { mapState } from 'vuex';
  41. export default {
  42. data() {
  43. return {
  44. // #ifdef H5
  45. payType: 1, //支付类型
  46. payName: 'weixin',
  47. // #endif
  48. // #ifdef APP-PLUS
  49. payType: 2, //支付类型
  50. payName: 'ali',
  51. // #endif
  52. // #ifdef MP-WEIXIN
  53. payName: 'weixin',
  54. // #endif
  55. orderInfo: {},
  56. money: 0.0, //订单金额
  57. now_money: 0.0, //余额
  58. orderKey: '',
  59. orderId: '', //保存订单id
  60. payLoding: false, //判断是否支付中
  61. type: '', //判断是否从订单中进入
  62. // #ifdef H5
  63. froms: '', //保存h5中数据来源对象
  64. // #endif
  65. pinkid: '' //保存拼团商品id
  66. };
  67. },
  68. computed: {
  69. // #ifdef H5
  70. ...mapState(['weichatObj'])
  71. // #endif
  72. },
  73. onLoad(options) {
  74. if (options.type == 1) {
  75. this.type = 1;
  76. this.orderId = options.ordid;
  77. this.money = options.money;
  78. } else {
  79. this.orderKey = options.key;
  80. let prepage = this.$api.prePage();
  81. computedOrderkey({
  82. orderkey: this.orderKey,
  83. couponId: prepage.couponChecked.id, //优惠券编号
  84. addressId: prepage.addressData.id, //地址编号
  85. useIntegral: prepage.checkedPoints ? 1 : 0
  86. }).then(({ data }) => {
  87. // 获取支付金额
  88. this.money = data.result.pay_price;
  89. });
  90. }
  91. // 保存pinkid
  92. if (options.pinkid) {
  93. this.pinkid = options.pinkid;
  94. }
  95. // 载入余额
  96. balance({}).then(({ data }) => {
  97. // 获取余额
  98. this.now_money = data.now_money;
  99. });
  100. },
  101. methods: {
  102. //选择支付方式
  103. changePayType(type) {
  104. this.payType = type;
  105. if (this.payType == 1) {
  106. this.payName = 'weixin';
  107. }
  108. if (this.payType == 2) {
  109. this.payName = 'ali';
  110. }
  111. if (this.payType == 3) {
  112. this.payName = 'yue';
  113. }
  114. },
  115. // 支付金额
  116. orderMoneyPay() {
  117. let obj = this;
  118. console.log(obj.payName,'123456')
  119. orderPay({
  120. uni: obj.orderId,
  121. // #ifdef H5
  122. from: obj.froms ? 'weixin' : 'H5', //来源
  123. // #endif
  124. // #ifdef MP-WEIXIN
  125. from: 'routine', //来源
  126. // #endif
  127. // #ifdef APP-PLUS
  128. from: 'app', //来源
  129. // #endif
  130. paytype: obj.payName //支付类型 weixin-微信 yue-余额
  131. })
  132. .then(e => {
  133. // 判断是否为余额支付
  134. if (obj.payName == 'yue' && e.data.status == 'SUCCESS') {
  135. if (e.status == 200) {
  136. obj.paySuccessTo();
  137. } else {
  138. obj.$api.msg(msg);
  139. }
  140. }
  141. //#ifndef APP-PLUS
  142. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  143. let da = e.data.result.jsConfig;
  144. console.log(da, '数据');
  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: e => {
  160. console.log(e, '失败');
  161. uni.navigateTo({
  162. url: '/pages/order/order?state=0'
  163. });
  164. }
  165. };
  166. // #ifdef H5
  167. if (obj.payName == 'weixin') {
  168. obj.weichatObj.chooseWXPay(data);
  169. }
  170. // #endif
  171. // #ifdef MP-WEIXIN
  172. if (obj.payName == 'weixin') {
  173. wx.requestPayment(data);
  174. }
  175. // #endif
  176. }
  177. //#endif
  178. //#ifdef APP-PLUS
  179. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  180. let da = e.data.result.jsConfig;
  181. console.log('--da--', da);
  182. let data = {
  183. appid: da.appid,
  184. noncestr: da.noncestr,
  185. package: da.package,
  186. partnerid: da.partnerid,
  187. prepayid: da.prepayid,
  188. timestamp: da.timestamp,
  189. sign: da.sign
  190. };
  191. console.log('--data--', data);
  192. uni.requestPayment({
  193. provider: 'wxpay',
  194. orderInfo: data,
  195. success(res) {
  196. console.log(res);
  197. uni.showToast({
  198. title: '支付成功',
  199. duration: 2000
  200. });
  201. obj.paySuccessTo();
  202. },
  203. fail(res) {
  204. console.log(res, '失败');
  205. }
  206. });
  207. }
  208. if (obj.payName == 'ali') {
  209. const url = e.data.result.jsConfig;
  210. console.log(url, 'url');
  211. uni.requestPayment({
  212. provider: 'alipay',
  213. orderInfo: url,
  214. success: res => {
  215. console.log(res);
  216. uni.showToast({
  217. title: '支付成功',
  218. duration: 2000
  219. });
  220. obj.paySuccessTo();
  221. },
  222. fail: e => {
  223. console.log(e);
  224. },
  225. complete: () => {}
  226. });
  227. obj.payLoding = false;
  228. }
  229. //#endif
  230. uni.hideLoading();
  231. obj.payLoding = false;
  232. })
  233. .catch(e => {
  234. // 支付完成
  235. uni.hideLoading();
  236. obj.payLoding = false;
  237. console.log(e);
  238. });
  239. },
  240. // 支付成功跳转
  241. paySuccessTo() {
  242. uni.hideLoading();
  243. uni.redirectTo({
  244. url: '/pages/money/paySuccess?orderid=' + this.orderId
  245. });
  246. },
  247. //确认支付
  248. confirm: async function() {
  249. let obj = this;
  250. // 判断是否余额不足
  251. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  252. uni.showModal({
  253. title: '提示',
  254. content: '账户余额不足!',
  255. showCancel: false,
  256. success: res => {},
  257. fail: () => {},
  258. complete: () => {}
  259. });
  260. return;
  261. }
  262. uni.showLoading({
  263. title: '支付中',
  264. mask: true
  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,'123456')
  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);
  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>