pay.vue 11 KB

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