pay.vue 9.9 KB

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