pay.vue 11 KB

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