pay.vue 10 KB

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