jiaofei.vue 16 KB

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