pay.vue 11 KB

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