pay.vue 10 KB

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