jiaofei.vue 13 KB

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