pay.vue 11 KB

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