pay.vue 10.0 KB

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