pay.vue 9.5 KB

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