jiaofei.vue 16 KB

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