jiaofei.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <template>
  2. <view class="container">
  3. <view class="select-item" @tap="navigateToSelect">
  4. <view class="picker-item-logol">
  5. <image class="picker-item-logo-left" src="../static/images/room.png"></image>
  6. </view>
  7. <view class="picker-item-label">已选房间</view>
  8. <view class="picker-item-content" :class="{'font-txt':add_class==1}">{{roomSelect}}</view>
  9. <view class="picker-item-logor">
  10. <image class="picker-item-logo-right" src="../static/images/right.png"></image>
  11. </view>
  12. </view>
  13. <view class="show-item">
  14. <view class="picker-item-logol">
  15. <image class="picker-item-logo-left" src="../static/images/elec.png"></image>
  16. </view>
  17. <view class="picker-item-label">账户金额</view>
  18. <view class="picker-item-content font-txt">¥ {{remainElec}}</view>
  19. </view>
  20. <view class="show-item details">
  21. <view class="xiangqing" @tap="dfxq_click">
  22. <image class="xiangqing-icon" src="../static/images/dfxq.png"></image>
  23. <view>电费详情</view>
  24. </view>
  25. <view class="xiangqing" @tap="sfxq_click">
  26. <image class="xiangqing-icon" src="../static/images/sfxq.png"></image>
  27. <view>水费详情</view>
  28. </view>
  29. </view>
  30. <view class="show-item add-money">
  31. <view class="add-money-show">
  32. <view class="picker-item-logol money-logo">
  33. <image class="picker-item-logo-left" src="../static/images/money.png"></image>
  34. </view>
  35. <view class="picker-item-label money-show">请选择充值金额</view>
  36. </view>
  37. <view class="add-money-list">
  38. <view class="add-money-button">
  39. <button class="select-submit" :data-item="10" @tap="add_money"
  40. :class="{selStyle:amount == 10}">10元</button>
  41. </view>
  42. <view class="add-money-button">
  43. <button class="select-submit" :data-item="20" @tap="add_money"
  44. :class="{selStyle:amount == 20}">20元</button>
  45. </view>
  46. <view class="add-money-button">
  47. <button class="select-submit" :data-item="30" @tap="add_money"
  48. :class="{selStyle:amount == 30}">30元</button>
  49. </view>
  50. <view class="add-money-button">
  51. <button class="select-submit" :data-item="50" @tap="add_money"
  52. :class="{selStyle:amount == 50}">50元</button>
  53. </view>
  54. <view class="add-money-button">
  55. <button class="select-submit" :data-item="100" @tap="add_money"
  56. :class="{selStyle:amount == 100}">100元</button>
  57. </view>
  58. <view class="add-money-button">
  59. <button class="select-submit" :data-item="200" @tap="add_money"
  60. :class="{selStyle:amount == 200}">200元</button>
  61. </view>
  62. </view>
  63. </view>
  64. <view class="show-item jine">
  65. <input type="number" class="input-money" maxlength="4" placeholder="请输入金额(10-200)"
  66. placeholder-class="input-ph-color" v-model:value="amount" @input="onInput" @blur="onBlur" />
  67. </view>
  68. <view class="submit-item" v-if="true">
  69. <button class="submit" :data-rooms='room' @tap="input_money">充值</button>
  70. </view>
  71. <view class="change-show" @tap="navigateToShow">
  72. 台账管理
  73. </view>
  74. <view v-if="img_url !== ''" class="ad">
  75. <image :src="img_url" mode="aspectFill" class="ad_img" @tap="ad_redirect" @error="loadImgError"></image>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. export default {
  81. data() {
  82. return {
  83. roomSelect: '', //校区宿舍号
  84. remainElec: 0.00.toFixed(2), //剩余电量
  85. add_class: '', //增加class属性
  86. amount: '',
  87. sub_appid: '', //商户号
  88. xiaofei_items: '', // 消费记录
  89. dorm_number: '', //栋宿舍号
  90. stu_number: '',
  91. compus: '', //校区
  92. ceshi: 'code',
  93. test: this.$store.state.test,
  94. img_url: ''
  95. }
  96. },
  97. onLoad(options) {
  98. // console.log(options);
  99. // if (typeof(options.from) != 'undefined' && options.from == 2) {
  100. // uni.navigateTo({
  101. // url: '../../pages/index/index?from=' + options.from
  102. // })
  103. // return
  104. // }
  105. // 获取基本信息
  106. this.get_base_info(options, 'onLoad');
  107. this.get_img();
  108. },
  109. onShow() {
  110. // 获取基本信息
  111. // this.get_base_info('options', 'onShow')
  112. // 获取电量
  113. if (this.$store.state.building.roomSelect != '') {
  114. // 如果是选择宿舍号返回
  115. this.roomSelect = this.$store.state.building.roomSelect;
  116. this.add_class = this.$store.state.building.add_class;
  117. this.dorm_number = this.$store.state.building.dorm_number;
  118. this.add_class = 1
  119. if (this.roomSelect && typeof(this.roomSelect) != 'undefined') {
  120. this.getDianLiang()
  121. }
  122. // 初始化参数
  123. this.$store.state.building.add_class = 0;
  124. // 获取code
  125. this.getCode('options', 'onShow')
  126. }
  127. },
  128. methods: {
  129. /**
  130. * 获取广告缩略图出错
  131. */
  132. loadImgError(e) {
  133. // console.log(e);
  134. this.img_url = ''
  135. },
  136. /**
  137. * 获取广告缩略图
  138. */
  139. async get_img() {
  140. let res = await this.$myRequest({
  141. host: this.ceshi,
  142. url: '/HotWaters/advertquery_h.action',
  143. method: 'POST',
  144. header: {
  145. 'content-type': 'application/x-www-form-urlencoded'
  146. },
  147. data: {
  148. 'url_name': '水电广告'
  149. }
  150. })
  151. // console.log(res.data)
  152. if (res.data.data.length == 0) {
  153. return
  154. }
  155. if (res.data.code == 200) {
  156. let tf = false;
  157. let tmpData = res.data.data;
  158. for (var i = 0; i < tmpData.length; i++) {
  159. if (tmpData[i].state == 1 && tmpData[i].remark.indexOf('缩略图') != -1) {
  160. this.img_url = tmpData[i].url;
  161. tf = true;
  162. }
  163. }
  164. if (tf == false) {
  165. uni.showToast({
  166. icon: 'none',
  167. title: '没有启用的缩略图',
  168. duration: 2000
  169. });
  170. }
  171. } else {
  172. uni.showToast({
  173. title: '加载数据异常',
  174. duration: 1500
  175. })
  176. return
  177. }
  178. },
  179. /**
  180. * 电费详情页面
  181. */
  182. dfxq_click() {
  183. if (this.haveSelectRoom()) {
  184. return
  185. }
  186. let _this = this;
  187. uni.navigateTo({
  188. url: '../dfxq/dfxq',
  189. success: function(res) {
  190. res.eventChannel.emit('params', {
  191. data: {
  192. roomSelect: _this.roomSelect,
  193. card_number: _this.stu_number
  194. }
  195. })
  196. }
  197. })
  198. },
  199. /**
  200. * 水费详情页面
  201. */
  202. sfxq_click() {
  203. if (this.haveSelectRoom()) {
  204. return
  205. }
  206. let _this = this;
  207. uni.navigateTo({
  208. url: '../sfxq/sfxq',
  209. success: function(res) {
  210. res.eventChannel.emit('params', {
  211. data: {
  212. roomSelect: _this.roomSelect,
  213. card_number: _this.stu_number
  214. }
  215. })
  216. }
  217. })
  218. },
  219. /**
  220. * 跳转到ad页面
  221. */
  222. ad_redirect() {
  223. uni.navigateTo({
  224. url: 'ad_dianfei'
  225. })
  226. },
  227. /**
  228. * 获取基本信息
  229. */
  230. get_base_info(options, param) {
  231. try {
  232. if (this.stu_number == '' || typeof(this.stu_number) == 'undefined') {
  233. // 获取学号
  234. this.stu_number = this.$store.state.userInfo.card_number
  235. }
  236. if (this.stu_number == '' || typeof(this.stu_number) == 'undefined') {
  237. const userinfo = uni.getStorageSync('userinfo_storage_key')
  238. if (userinfo) {
  239. this.stu_number = userinfo.card_number
  240. } else {
  241. uni.navigateTo({
  242. url: '../../pages/index/index?from=' + options.from
  243. })
  244. uni.showToast({
  245. icon: 'none',
  246. title: '学号为空,请进行授权',
  247. duration: 3000
  248. });
  249. return
  250. }
  251. }
  252. } catch (e) {
  253. console.log(e);
  254. }
  255. // 初始化参数
  256. this.$store.state.building.add_class = 0;
  257. // 获取code
  258. this.getCode(options, param)
  259. },
  260. /**
  261. * 获取code
  262. */
  263. getCode(options, param) {
  264. // console.log(options, param)
  265. uni.login({
  266. success: (res) => {
  267. if (res.code) {
  268. if (param == 'comfirm') {
  269. // 组合地址,发起支付
  270. this.jsapi(res.code)
  271. } else {
  272. // 请求服务器,获得openid
  273. this.getOpenId(options, res.code)
  274. }
  275. } else {
  276. uni.showToast({
  277. title: res.errMsg,
  278. icon: 'none'
  279. });
  280. // 跳转到首页
  281. uni.redirectTo({
  282. url: '../../pages/index/index?from=0'
  283. });
  284. }
  285. }
  286. })
  287. },
  288. /**
  289. * 请求服务器,获得openid
  290. */
  291. async getOpenId(options, param_code) {
  292. // console.log(options, param_code)
  293. if (param_code != '') {
  294. const res = await this.$myRequest({
  295. host: this.ceshi,
  296. url: '/HotWaters/wpget_stu.action',
  297. method: 'POST',
  298. header: {
  299. 'content-type': 'application/x-www-form-urlencoded'
  300. },
  301. data: {
  302. code: param_code
  303. }
  304. })
  305. // console.log('获得openid', res);
  306. if (res.data.mess == '返回成功') {
  307. if (!(this.$store.state.building.roomSelect && typeof(this.$store.state.building.roomSelect) !=
  308. 'undefined')) {
  309. // 栋宿舍号
  310. this.dorm_number = res.data.info[0].build + res.data.info[0].dom
  311. // 校区
  312. this.compus = res.data.info[0].campus
  313. // 填充校区宿舍号
  314. if (typeof(this.dorm_number.split('栋')[1]) == 'undefined') {
  315. this.roomSelect = this.compus + ''
  316. } else {
  317. this.roomSelect = this.compus + this.dorm_number.split('栋')[1]
  318. }
  319. // 存储选择
  320. this.$store.state.building.roomSelect = this.roomSelect
  321. // 样式
  322. this.add_class = 1
  323. }
  324. // 请求选定的月份消费记录
  325. this.getDianLiang()
  326. } else if (res.data.mess == '未查询到用户信息') {
  327. // 数据库中 未查询到用户信息,就清除本地存储
  328. uni.removeStorageSync('userinfo_storage_key');
  329. uni.showToast({
  330. title: '获取用户信息失败!'
  331. })
  332. // 跳转到首页
  333. if (options && typeof(options.from) != 'undefined' && typeof(options.from) != '') {
  334. uni.navigateTo({
  335. url: '../../pages/index/index?from=' + options.from
  336. })
  337. } else {
  338. uni.redirectTo({
  339. url: '../../pages/index/index/index?from=0'
  340. });
  341. }
  342. }
  343. if (!this.roomSelect || this.roomSelect == '' || typeof(this.roomSelect) == 'undefined') {
  344. const userinfo = uni.getStorageSync('userinfo_storage_key')
  345. if (userinfo && typeof(userinfo) != 'undefined' && typeof(userinfo.campus) != 'undefined' &&
  346. typeof(userinfo.dorm_number) != 'undefined' && userinfo.campus != '' && userinfo
  347. .dorm_number != '') {
  348. this.roomSelect = userinfo.campus + userinfo.dorm_number
  349. } else {
  350. this.roomSelect = '请选择宿舍号'
  351. }
  352. }
  353. } else {
  354. uni.showToast({
  355. title: 'code为空!',
  356. icon: 'success'
  357. });
  358. }
  359. },
  360. /**
  361. * 取得电费额度
  362. */
  363. async getDianLiang() {
  364. if (this.roomSelect != '' && typeof(this.roomSelect) != 'undefined') {
  365. const res = await this.$myRequest({
  366. host: this.ceshi,
  367. url: '/HotWaters/buildoverElec.action',
  368. method: 'POST',
  369. header: {
  370. 'content-type': 'application/x-www-form-urlencoded'
  371. },
  372. data: {
  373. 'roomSelect': this.roomSelect
  374. }
  375. })
  376. // console.log('取得电费额度', res);
  377. if (res.data.mess == '0') {
  378. setTimeout(() => {
  379. this.remainElec = res.data.amount.toFixed(2)
  380. }, 30);
  381. } else {
  382. this.remainElec = 0.00.toFixed(2)
  383. }
  384. } else {
  385. uni.showToast({
  386. title: '请先选择宿舍号,\n再查询详情 或 充值',
  387. icon: 'none',
  388. duration: 3000
  389. });
  390. }
  391. },
  392. //跳转到选择页面
  393. navigateToSelect() {
  394. uni.navigateTo({
  395. url: '../select/select',
  396. });
  397. },
  398. // 是否选择了宿舍号
  399. haveSelectRoom() {
  400. if (this.roomSelect == '请选择宿舍号') {
  401. uni.showToast({
  402. title: '请选择宿舍号',
  403. icon: 'success'
  404. })
  405. return true
  406. }
  407. return false
  408. },
  409. //跳转到台账页面
  410. navigateToShow(e) {
  411. if (this.haveSelectRoom()) {
  412. return
  413. }
  414. uni.navigateTo({
  415. url: '../show/show',
  416. });
  417. },
  418. /**
  419. * 增加固定金额
  420. */
  421. add_money(e) {
  422. this.amount = e.currentTarget.dataset.item
  423. },
  424. /**
  425. * 确认宿舍号
  426. */
  427. confirm_room(op) {
  428. // 检查房间号
  429. if (this.roomSelect == '' || this.roomSelect == '请选择宿舍号') {
  430. uni.showToast({
  431. title: '请选择宿舍号',
  432. icon: 'success'
  433. })
  434. return
  435. }
  436. // 输入金额进行充值
  437. if (op == 'input_money') {
  438. //判断输入是否为空或不是数字
  439. if (this.amount == '' || this.amount == null || this.amount == 0) {
  440. uni.showToast({
  441. title: '请输入充值金额',
  442. duration: 2000
  443. })
  444. return
  445. }
  446. if (isNaN(this.amount)) {
  447. uni.showToast({
  448. title: '请输入正确金额',
  449. duration: 2000
  450. })
  451. return
  452. }
  453. if (!this.test) { // 非测试环境
  454. if (this.amount > 200 || this.amount < 10) {
  455. uni.showToast({
  456. title: '限额10~200元',
  457. duration: 2000
  458. })
  459. return
  460. }
  461. }
  462. }
  463. // 确认充值对话框
  464. this.confirm_dialog()
  465. },
  466. /**
  467. * 确认充值对话框
  468. */
  469. confirm_dialog() {
  470. uni.showModal({
  471. // title: '确定充值'+this.amount+'元',
  472. title: '确定充值' + this.amount + '元',
  473. success: (res) => {
  474. if (res.confirm) {
  475. // 获取code
  476. this.getCode('options', 'comfirm')
  477. } else if (res.cancel) {
  478. // console.log('用户点击取消');
  479. }
  480. }
  481. })
  482. },
  483. //增加可变金额
  484. input_money(e) {
  485. // console.log(this.test);
  486. // console.log(e)
  487. this.confirm_room('input_money')
  488. },
  489. /**
  490. * 请求服务器,获取支付参数,并支付
  491. */
  492. async jsapi(param_code) {
  493. if (param_code == '' || this.amount == '' || this.roomSelect == '') {
  494. uni.showToast({
  495. title: 'code、充值金额、宿舍号或IP为空'
  496. });
  497. return
  498. }
  499. const res = await this.$myRequest({
  500. host: this.ceshi,
  501. url: '/HotWaters/elpay_ns.action',
  502. method: 'POST',
  503. header: {
  504. 'content-type': 'application/x-www-form-urlencoded'
  505. },
  506. data: {
  507. code: param_code,
  508. num: this.amount,
  509. stu_number: this.stu_number,
  510. roomSelect: this.roomSelect
  511. }
  512. })
  513. // console.log(res);
  514. if (res.statusCode == 200 && res.data.sub_openid && res.data.sub_openid != '') {
  515. wx.requestPayment({
  516. appId: res.data.appId,
  517. timeStamp: res.data.timeStamp,
  518. nonceStr: res.data.nonceStr,
  519. package: res.data.package,
  520. signType: res.data.signType,
  521. paySign: res.data.paySign,
  522. success: res => {
  523. if (res.errMsg == 'requestPayment:ok') {
  524. uni.showToast({
  525. title: '支付成功',
  526. icon: 'success'
  527. });
  528. }
  529. },
  530. fail: (res) => {
  531. if (res.errMsg == 'requestPayment:fail cancel') {
  532. uni.showToast({
  533. title: '取消支付',
  534. icon: 'success',
  535. duration: 2000
  536. });
  537. }
  538. },
  539. complete: (res) => {
  540. // console.log(res);
  541. }
  542. });
  543. } else {
  544. uni.showToast({
  545. title: '未获得支付参数',
  546. icon: 'success',
  547. duration: 3000
  548. });
  549. }
  550. },
  551. /**
  552. * 输入充值金额
  553. */
  554. onInput(e) {
  555. if (this.test) { // 测试环境
  556. return
  557. }
  558. const v = e.detail.value
  559. const zero = /^(0{1,})|[^0-9]/g
  560. let final = 0
  561. if (!v) {
  562. final = 0
  563. } else {
  564. final = v.toString().replace(zero, (v) => {
  565. return 0
  566. })
  567. if (final.split('')[0] * 1 === 0) {
  568. final = final.slice(1) - 0 || 0
  569. }
  570. if (final > 200) {
  571. final = 200
  572. }
  573. }
  574. this.$nextTick(() => {
  575. if (final.toString() == '0') {
  576. this.amount = ''
  577. } else {
  578. this.amount = final.toString() || '0'
  579. }
  580. })
  581. },
  582. /**
  583. * 失去焦点时
  584. */
  585. onBlur() {
  586. if (this.test) { // 测试环境
  587. return
  588. }
  589. if (this.amount < 10) {
  590. this.amount = 10
  591. }
  592. }
  593. },
  594. }
  595. </script>
  596. <style>
  597. @import url("jiaofei.css");
  598. </style>