pay.vue 10 KB

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