pay.vue 9.2 KB

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