pay.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 { computedOrderkey, balance, createOrderkey, orderPay } from '@/api/money.js';
  37. import { bvip } from '@/api/user.js';
  38. import { mapState } from 'vuex';
  39. export default {
  40. data() {
  41. return {
  42. payType: 1, //支付类型
  43. // #ifdef H5
  44. payName: 'weixin',
  45. // #endif
  46. // #ifdef MP-WEIXIN
  47. payName: 'weixin',
  48. // #endif
  49. orderInfo: {},
  50. money: 0.0, //订单金额
  51. now_money: 0.0, //余额
  52. orderKey: '',
  53. orderId: '', //保存订单id
  54. payLoding: false, //判断是否支付中
  55. type: '', //判断是否从订单中进入
  56. // #ifdef H5
  57. froms: '', //保存h5中数据来源对象
  58. // #endif
  59. pinkid: '', //保存拼团商品id
  60. typegoods:'',//区分普通和拼团商品
  61. level:'',//用户等级
  62. use_integral_pink: '',
  63. id: '', // 地址编号
  64. goodstype: '', // 商品类型
  65. };
  66. },
  67. computed: {
  68. // #ifdef H5
  69. ...mapState(['weichatObj','userInfo'])
  70. // #endif
  71. },
  72. onLoad(options) {
  73. console.log(options,99999)
  74. this.goodstype = options.goodsType;
  75. if (options.type == 1) {
  76. this.type = 1;
  77. this.orderId = options.ordid;
  78. this.money = options.money;
  79. } else {
  80. this.orderKey = options.key;
  81. this.id = options.id;
  82. console.log(this.orderKey,989)
  83. this.use_integral_pink = options.use_integral_pink;
  84. let prepage = this.$api.prePage();
  85. // console.log(prepage, 88);
  86. computedOrderkey({
  87. orderkey: this.orderKey,
  88. use_integral_pink: this.use_integral_pink,
  89. // couponId: prepage.couponChecked.id, //优惠券编号
  90. useShopping_card: prepage.shoppingPoints ? 1 : 0, //是否使用购物卡
  91. addressId: this.id, //地址编号
  92. useIntegral: prepage.checkedPoints ? 1 : 0
  93. },this.orderKey).then(({ data }) => {
  94. console.log(data)
  95. // 获取支付金额
  96. this.money = data.result.pay_price;
  97. });
  98. }
  99. // 保存pinkid
  100. if (options.pinkid) {
  101. this.pinkid = options.pinkid;
  102. }
  103. //区分普通商品和拼团商品
  104. if(options.typegoods){
  105. this.typegoods = options.typegoods;
  106. }
  107. //保存用户等级
  108. if(options.level){
  109. this.level = options.level;
  110. }
  111. // 载入余额
  112. balance({}).then(({ data }) => {
  113. // 获取余额
  114. this.now_money = data.now_money;
  115. });
  116. },
  117. methods: {
  118. //选择支付方式
  119. changePayType(type) {
  120. // console.log(type, 88);
  121. this.payType = type;
  122. if (this.payType == 1) {
  123. // #ifdef H5
  124. this.payName = 'weixin';
  125. // #endif
  126. // #ifdef MP-WEIXIN
  127. this.payName = 'weixin';
  128. // #endif
  129. // #ifdef APP-PLUS
  130. this.payName = 'weixin';
  131. // #endif
  132. }
  133. if (this.payType == 2) {
  134. this.payName = 'ali';
  135. }
  136. if (this.payType == 3) {
  137. this.payName = 'yue';
  138. }
  139. },
  140. // 支付金额
  141. orderMoneyPay() {
  142. console.log(55555555555555555)
  143. let obj = this;
  144. orderPay({
  145. uni: obj.orderId,
  146. // #ifdef H5
  147. from: obj.froms ? 'weixin' : 'H5', //来源
  148. // #endif
  149. // #ifdef MP-WEIXIN
  150. from: 'routine', //来源
  151. // #endif
  152. // #ifdef APP-PLUS
  153. from: 'app', //来源
  154. // #endif
  155. paytype: obj.payName //支付类型 weixin-微信 yue-余额
  156. }).then(e => {
  157. // 判断是否为余额支付
  158. if (obj.payName == 'yue') {
  159. if (e.status == 200) {
  160. obj.paySuccessTo();
  161. } else {
  162. alert(e.msg);
  163. obj.$api.msg(e.msg);
  164. }
  165. }
  166. // #ifdef APP-PLUS
  167. if(obj.type == 'ali')
  168. {
  169. uni.requestPayment({
  170. provider: 'alipay',
  171. orderInfo: 'orderInfo', //微信、支付宝订单数据
  172. success: function (res) {
  173. console.log('success:' + JSON.stringify(res));
  174. },
  175. fail: function (err) {
  176. console.log('fail:' + JSON.stringify(err));
  177. }
  178. });
  179. }else{
  180. uni.requestPayment({
  181. provider: 'weixin',
  182. orderInfo: 'orderInfo', //微信、支付宝订单数据
  183. success: function (res) {
  184. console.log('success:' + JSON.stringify(res));
  185. },
  186. fail: function (err) {
  187. console.log('fail:' + JSON.stringify(err));
  188. }
  189. });
  190. }
  191. // #endif
  192. // #ifdef H5
  193. if (obj.payName == 'weixin') {
  194. let da = e.data.result.jsConfig;
  195. obj.weichatObj.chooseWXPay({
  196. timestamp: da.timestamp,
  197. nonceStr: da.nonceStr,
  198. package: da.package,
  199. signType: da.signType,
  200. paySign: da.paySign,
  201. success: function(res) {
  202. obj.paySuccessTo();
  203. },
  204. fail: function (res) {
  205. console.log(res,"失败")
  206. console.log(res.errMsg)
  207. }
  208. });
  209. }
  210. // #endif
  211. // #ifdef MP-WEIXIN
  212. if (obj.payName == 'weixin') {
  213. let da = e.data.result.jsConfig;
  214. wx.requestPayment({
  215. timeStamp: da.timestamp,
  216. nonceStr: da.nonceStr,
  217. package: da.package,
  218. signType: da.signType,
  219. paySign: da.paySign,
  220. success: function(res) {
  221. obj.paySuccessTo();
  222. }
  223. });
  224. }
  225. // #endif
  226. obj.payLoding = false;
  227. })
  228. .catch(e => {
  229. // 支付完成
  230. alert(e.message+";file="+e.file+";line="+e.line);
  231. obj.payLoding = false;
  232. console.log(e.message)
  233. if(e.message == "该订单已支付!"){
  234. obj.paySuccessTo();
  235. }
  236. });
  237. },
  238. // 支付成功跳转
  239. paySuccessTo() {
  240. let url = '/pages/money/paySuccess?orderid=' + this.orderId + '&goodstype=' + this.goodstype + '&money=' + this.money
  241. console.log();
  242. if(!this.typegoods){
  243. url += "&newGoods=true"
  244. }
  245. uni.redirectTo({
  246. url
  247. });
  248. },
  249. //确认支付
  250. confirm: async function() {
  251. let obj = this;
  252. // 判断是否余额不足
  253. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  254. uni.showModal({
  255. title: '提示',
  256. content: '账户余额不足!',
  257. showCancel: true,
  258. success: res => {},
  259. fail: () => {},
  260. complete: () => {}
  261. });
  262. return;
  263. }
  264. // 支付中
  265. obj.payLoding = true;
  266. // #ifdef H5
  267. // 获取当前是否为微信浏览器
  268. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  269. // #endif
  270. // 判断是否为未支付订单中跳转进入
  271. if (obj.type != 1) {
  272. // 初次生成订单
  273. obj.firstCreateOrder();
  274. } else {
  275. // 已经生成订单未支付
  276. obj.orderMoneyPay();
  277. }
  278. },
  279. // 初次订单创建
  280. firstCreateOrder() {
  281. let obj = this;
  282. // 获取下单页面数据
  283. let prepage = obj.$api.prePage();
  284. let data = {
  285. use_integral_pink: obj.use_integral_pink,
  286. real_name: prepage.addressData.real_name, //联系人名称
  287. phone: prepage.addressData.phone, //联系人号码
  288. couponId: prepage.couponChecked.id, //优惠券编号
  289. addressId: prepage.addressData.id, //支付地址id
  290. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  291. useShopping_card: prepage.shoppingPoints ? 1 : 0, //是否使用购物卡
  292. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  293. mark: prepage.idcardName+prepage.idcardNum, //备注
  294. // #ifdef H5
  295. from: obj.froms ? 'weixin' : 'H5', //来源
  296. // #endif
  297. // #ifdef MP-WEIXIN
  298. from: 'routine', //来源
  299. // #endif
  300. // #ifdef APP-PLUS
  301. from: 'app', //来源
  302. // #endif
  303. shipping_type: 1, //提货方式 1 快递 2自提
  304. };
  305. // 判断是否拼团商品
  306. if (obj.typegoods == 6) {
  307. data.level = obj.level;
  308. }else{
  309. data.pinkId = obj.pinkid;
  310. }
  311. // 生成订单
  312. createOrderkey(data, obj.orderKey)
  313. .then(({ data, status, msg }) => {
  314. // 保存订单号
  315. obj.orderId = data.result.orderId;
  316. // 允许支付
  317. obj.payLoding = false;
  318. if (data.status == 'ORDER_EXIST') {
  319. obj.$api.msg(msg);
  320. return;
  321. }
  322. // 判断是否为余额支付
  323. if (obj.payName == 'yue') {
  324. if (status == 200&&data.status == 'SUCCESS') {
  325. obj.paySuccessTo();
  326. } else {
  327. alert(msg);
  328. obj.$api.msg(msg)
  329. }
  330. } else {
  331. // 立即支付
  332. obj.orderMoneyPay();
  333. }
  334. })
  335. .catch(e => {
  336. alert(e.message);
  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. background:linear-gradient(0deg,rgba(250,39,64,1),rgba(254,85,68,1));
  417. border-radius: 10upx;
  418. }
  419. .clickbg {
  420. background-color: $color-gray !important;
  421. }
  422. </style>