pay.vue 10 KB

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