pay.vue 9.9 KB

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