pay.vue 9.3 KB

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