pay.vue 9.5 KB

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