pay.vue 10.0 KB

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