pay.vue 9.9 KB

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