jiaofei.vue 15 KB

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