pay.vue 9.3 KB

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