pay.vue 12 KB

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