jiaofei.vue 14 KB

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