index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <template>
  2. <view class="pages">
  3. <!-- 余额 -->
  4. <view class="wallet">
  5. <view class="wallet_header">
  6. <view class="wallet_header_text">当前余额</view>
  7. <view class="wallet_header_xin">
  8. <view class="wallet_header_le">{{myMoney}} <text> 元</text></view>
  9. </view>
  10. </view>
  11. </view>
  12. <!-- 充值 -->
  13. <view class="wallet_topup">
  14. <view class="wallet_header_text">充值金额</view>
  15. <view class="wallet_header_xin2">
  16. <view class="wallet_header_xin2_sty" v-for="(item,index) in wallet" :key='index' :class="{topup:item.isSelect}" @tap="active(item)">
  17. <view class="topup_img" v-if="item.isSelect">
  18. <image src="../static/wallet/face.png" mode=""></image>
  19. </view>
  20. 充值<text>{{item.money}}</text>元
  21. </view>
  22. </view>
  23. </view>
  24. <!-- 消费 -->
  25. <view class="wallet_header_consum" @click="record()">
  26. <view class="wallet_header_consum_le">消费记录</view>
  27. <view class="wallet_header_consum_ri">
  28. <image src="../static/wallet/right1.png" mode="scaleToFill"></image>
  29. </view>
  30. </view>
  31. <!-- 立即充值 -->
  32. <view class="wallet_bottom" @click="showpay = true" >立即充值</view>
  33. <!-- 支付方式 -->
  34. <u-popup v-model="showpay" mode="bottom" :closeable="closeable">
  35. <view class="popup_pay">
  36. <view style="background-color: #fff;">
  37. <view style="padding: 0 20upx;margin-top: 60rpx;margin-bottom: 20rpx;">
  38. <view
  39. style="display: flex;height: 100upx;align-items: center;padding: 20upx 0;justify-content: center;"
  40. v-for="(item,index) in openLists" :key='index'>
  41. <image :src="item.image" style="width: 55upx;height: 55upx;border-radius: 50upx;">
  42. </image>
  43. <view style="font-size: 30upx;margin-left: 20upx;width: 70%;">
  44. {{item.text}}
  45. </view>
  46. <radio-group name="openWay" style="margin-left: 45upx;" @tap='selectWay(item)'>
  47. <label class="tui-radio">
  48. <radio color="#1777FF" :checked="openWay === item.id ? true : false" />
  49. </label>
  50. </radio-group>
  51. </view>
  52. </view>
  53. </view>
  54. <view class="pay_btn" @click="payment()">确认支付</view>
  55. </view>
  56. </u-popup>
  57. </view>
  58. </template>
  59. <script>
  60. export default {
  61. data() {
  62. return {
  63. showpay: false,
  64. closeable: true,
  65. openLists: [],
  66. openWay: 2,
  67. myMoney: 0,
  68. wallet: [],
  69. thisSelect: {}
  70. }
  71. },
  72. onLoad() {
  73. // #ifdef APP
  74. this.openLists = [ {
  75. image: '../../static/images/my/weixin.png',
  76. text: '微信',
  77. id: 2
  78. }, {
  79. image: '../../static/images/my/zhifubao.png',
  80. text: '支付宝',
  81. id: 3
  82. }]
  83. // #endif
  84. // #ifdef MP-WEIXIN
  85. this.openLists = [{
  86. image: '../../static/images/my/weixin.png',
  87. text: '微信',
  88. id: 2
  89. }]
  90. // #endif
  91. // #ifdef H5
  92. this.openLists = [ {
  93. image: '../../static/images/my/weixin.png',
  94. text: '微信',
  95. id: 2
  96. }, {
  97. image: '../../static/images/my/zhifubao.png',
  98. text: '支付宝',
  99. id: 3
  100. }]
  101. // #endif
  102. },
  103. onShow() {
  104. this.deploy()
  105. this.getMoneyNum()
  106. },
  107. methods: {
  108. selectWay: function(item) {
  109. this.openWay = item.id;
  110. },
  111. deploy() {
  112. this.$Request.get("/app/topupmoney/selectTopUpMoney").then(res => {
  113. if (res.code == 0) {
  114. res.data.forEach(res=> {
  115. res.isSelect = false
  116. })
  117. this.wallet = res.data
  118. }
  119. });
  120. },
  121. active(e) {
  122. this.wallet.forEach(res => {
  123. if (res.id == e.id) {
  124. res.isSelect = true
  125. this.thisSelect = e
  126. } else {
  127. res.isSelect = false
  128. }
  129. })
  130. console.log(this.wallet)
  131. },
  132. record() {
  133. uni.navigateTo({
  134. url: '/my/wallet/walletDet'
  135. })
  136. },
  137. getMoneyNum() {
  138. this.$Request.get("/app/userMoney/selectMyMoney").then(res => {
  139. if (res.code == 0) {
  140. this.myMoney = res.data.money
  141. }
  142. });
  143. },
  144. payment() {
  145. let that = this
  146. that.showpay = false
  147. if(!that.thisSelect.money) {
  148. uni.showToast({
  149. title: '请选择充值金额!',
  150. icon: 'none'
  151. })
  152. return
  153. }
  154. if (that.openWay == 2) { //微信支付
  155. // #ifdef MP-WEIXIN
  156. that.$Request.post('/app/wxPay/wxPayJsApiMoney', {
  157. money: that.thisSelect.money,
  158. type: 3,
  159. }).then(ret => {
  160. uni.hideLoading()
  161. uni.requestPayment({
  162. provider: 'wxpay',
  163. timeStamp: ret.data.timestamp,
  164. nonceStr: ret.data.noncestr,
  165. package: ret.data.package,
  166. signType: ret.data.signType,
  167. paySign: ret.data.sign,
  168. success: function(suc) {
  169. uni.showToast({
  170. title: '支付成功',
  171. icon: 'success'
  172. })
  173. that.getMoneyNum()
  174. },
  175. fail: function(err) {
  176. console.log('fail:' + JSON.stringify(err));
  177. uni.showToast({
  178. title: '支付失败',
  179. icon: 'none'
  180. })
  181. }
  182. });
  183. });
  184. // #endif
  185. // #ifdef APP
  186. let data = {
  187. type: 1,
  188. money: that.thisSelect.money
  189. }
  190. that.$Request.post('/app/wxPay/wxPayJsApiMoney', data).then(res => {
  191. console.log(res)
  192. that.showpay = false
  193. if (res.code == 0) {
  194. that.isCheckPay(res.code, 'wxpay', JSON.stringify(res.data));
  195. }
  196. });
  197. // #endif
  198. // #ifdef H5
  199. let data = {
  200. money: that.thisSelect.money,
  201. type: 2
  202. }
  203. this.$Request.post('/app/wxPay/wxPayJsApiMoney', data).then(res => {
  204. this.showpay = false
  205. that.callPay(res.data);
  206. });
  207. // #endif
  208. } else if(that.openWay == 3) {
  209. // #ifdef APP-PLUS
  210. let data = {
  211. type: 4,
  212. money: that.thisSelect.money
  213. }
  214. that.$Request.post('/app/wxPay/wxPayJsApiMoney', data).then(res => {
  215. that.showpay = false
  216. that.setPayment('alipay', res.data);
  217. });
  218. // #endif
  219. // #ifdef H5
  220. let data = {
  221. type: 5,
  222. money: that.thisSelect.money
  223. }
  224. this.$Request.post('/app/wxPay/wxPayJsApiMoney', data).then(res => {
  225. this.showpay = false
  226. const div = document.createElement('div')
  227. div.innerHTML = res.data //此处form就是后台返回接收到的数据
  228. document.body.appendChild(div)
  229. document.forms[0].submit()
  230. });
  231. // #endif
  232. }
  233. },
  234. callPay: function(response) {
  235. if (typeof WeixinJSBridge === "undefined") {
  236. console.log(document.addEventListener,'999')
  237. if (document.addEventListener) {
  238. document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(response), false);
  239. } else if (document.attachEvent) {
  240. document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(response));
  241. document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(response));
  242. }
  243. } else {
  244. console.log(888)
  245. this.onBridgeReady(response);
  246. }
  247. },
  248. onBridgeReady: function(response) {
  249. console.log(response)
  250. let that = this;
  251. if (!response.package) {
  252. return;
  253. }
  254. WeixinJSBridge.invoke(
  255. 'getBrandWCPayRequest', {
  256. "appId": response.appid, //公众号名称,由商户传入
  257. "timeStamp": response.timestamp, //时间戳,自1970年以来的秒数
  258. "nonceStr": response.noncestr, //随机串
  259. "package": response.package,
  260. "signType": response.signType, //微信签名方式:
  261. "paySign": response.sign //微信签名
  262. },
  263. function(res) {
  264. if (res.err_msg === "get_brand_wcpay_request:ok") {
  265. // 使用以上方式判断前端返回,微信团队郑重提示:
  266. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  267. uni.showLoading({
  268. title: '支付成功'
  269. });
  270. uni.hideLoading();
  271. that.getMoneyNum()
  272. uni.navigateTo({
  273. url: '/pages/my/index'
  274. })
  275. } else {
  276. uni.hideLoading();
  277. }
  278. WeixinJSBridge.log(response.err_msg);
  279. }
  280. );
  281. },
  282. isCheckPay(code, name, order) {
  283. if (code == 0) {
  284. console.log('999999999999')
  285. this.setPayment(name, order);
  286. } else {
  287. uni.hideLoading();
  288. uni.showToast({
  289. title: '支付信息有误'
  290. });
  291. }
  292. },
  293. setPayment(name, order) {
  294. console.log(777777777, name, order)
  295. uni.requestPayment({
  296. provider: name,
  297. orderInfo: order, //微信、支付宝订单数据
  298. success: function(res) {
  299. uni.hideLoading();
  300. uni.showLoading({
  301. title: '支付成功',
  302. icon: 'none'
  303. });
  304. that.getMoneyNum()
  305. // setTimeout(function() {
  306. // uni.navigateBack()
  307. // }, 1000)
  308. },
  309. fail: function(err) {
  310. uni.hideLoading();
  311. },
  312. complete() {
  313. uni.hideLoading();
  314. }
  315. });
  316. }
  317. }
  318. }
  319. </script>
  320. <style scoped>
  321. /* 余额 */
  322. .wallet {
  323. width: 100%;
  324. height: 260rpx;
  325. background: -webkit-linear-gradient(top, #FCD202, #F5F5F5);
  326. }
  327. .wallet_header {
  328. width: 94%;
  329. margin: 0 auto;
  330. background-color: #FFFFFF;
  331. border-radius: 18rpx;
  332. position: relative;
  333. top: 100rpx;
  334. }
  335. .wallet_header_text {
  336. padding: 3% 3% 1%;
  337. line-height: 32rpx;
  338. font-size: 30rpx;
  339. font-weight: 500;
  340. color: #333333;
  341. line-height: 32px;
  342. }
  343. .wallet_header_xin {
  344. padding: 2% 3% 5%;
  345. display: flex;
  346. }
  347. .wallet_header_le {
  348. flex: 2;
  349. font-size: 68rpx;
  350. font-weight: 500;
  351. color: #333333;
  352. }
  353. .wallet_header_le text {
  354. font-size: 34rpx;
  355. }
  356. .wallet_header_ri {
  357. margin-top: 20rpx;
  358. }
  359. .wallet_header_ri image {
  360. width: 40rpx;
  361. height: 30rpx;
  362. }
  363. .wallet_header_ri text {
  364. font-size: 34rpx;
  365. font-weight: 500;
  366. color: #333333;
  367. }
  368. /* 充值 */
  369. .wallet_topup {
  370. width: 94%;
  371. margin: 13% auto 0;
  372. background-color: #FFFFFF;
  373. border-radius: 18rpx;
  374. padding: 0 0 3%;
  375. }
  376. .wallet_header_xin2 {
  377. /* padding: 0 3% 2%; */
  378. display: flex;
  379. flex-wrap: wrap;
  380. justify-content: space-between;
  381. padding: 0 2%;
  382. }
  383. .wallet_header_xin2_sty {
  384. width: 48.5%;
  385. /* margin: 1% 0 0 3%; */
  386. border: 2rpx solid #E6E6E6;
  387. border-radius: 18rpx;
  388. text-align: center;
  389. line-height: 3;
  390. margin-bottom: 2%;
  391. }
  392. .wallet_header_xin2_sty text {
  393. font-size: 44rpx;
  394. }
  395. .wallet_header_xin2_sty:first-child {
  396. margin-left: 0;
  397. }
  398. .topup {
  399. color: #FF130A;
  400. border: 2rpx solid #FF130A;
  401. position: relative;
  402. }
  403. .topup_img {
  404. position: absolute;
  405. bottom: 61rpx;
  406. }
  407. .topup_img image {
  408. width: 48rpx;
  409. height: 40rpx;
  410. }
  411. /* 消费 */
  412. .wallet_header_consum {
  413. width: 94%;
  414. margin: 3% auto;
  415. background-color: #FFFFFF;
  416. border-radius: 18rpx;
  417. padding: 3%;
  418. display: flex;
  419. }
  420. .wallet_header_consum_le {
  421. width: 97%;
  422. font-family: PingFang SC;
  423. font-weight: 500;
  424. color: #333333;
  425. }
  426. .wallet_header_consum_ri {
  427. line-height: 1.5;
  428. }
  429. .wallet_header_consum_ri image {
  430. width: 16rpx;
  431. height: 28rpx;
  432. }
  433. /* 立即充值 */
  434. .wallet_bottom {
  435. width: 94%;
  436. position: fixed;
  437. bottom: 3%;
  438. left: 3%;
  439. line-height: 2.5;
  440. background: #FCD202;
  441. text-align: center;
  442. border-radius: 49rpx;
  443. font-size: 38rpx;
  444. font-weight: bold;
  445. color: #333333;
  446. }
  447. .popup_pay {
  448. width: 100%;
  449. position: relative;
  450. padding-bottom: 45rpx;
  451. }
  452. .pay_btn {
  453. width: 90%;
  454. margin: 0 auto;
  455. text-align: center;
  456. background: #FCD202;
  457. height: 80rpx;
  458. border-radius: 16rpx;
  459. color: #ffffff;
  460. line-height: 80rpx;
  461. }
  462. </style>