jiaofei.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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">
  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">
  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>
  65. </template>
  66. <script>
  67. export default {
  68. data() {
  69. return {
  70. roomSelect: '', //校区宿舍号
  71. remainElec: 0.00.toFixed(2), //剩余电量
  72. add_class: '', //增加class属性
  73. amount: '',
  74. sub_appid: '', //商户号
  75. xiaofei_items: '', // 消费记录
  76. dorm_number: '', //栋宿舍号
  77. compus: '', //校区
  78. ceshi: 'code',
  79. test: this.$store.state.test
  80. }
  81. },
  82. onLoad(options) {
  83. // console.log(options);
  84. // if (typeof(options.from) != 'undefined' && options.from == 2) {
  85. // uni.navigateTo({
  86. // url: '../index/index?from=' + options.from
  87. // })
  88. // return
  89. // }
  90. // 获取基本信息
  91. this.get_base_info(options, 'onLoad')
  92. },
  93. onShow() {
  94. // 获取基本信息
  95. this.get_base_info('options', 'onShow')
  96. },
  97. methods: {
  98. /**
  99. * 获取基本信息
  100. */
  101. get_base_info(options, param) {
  102. try {
  103. // 获取学号
  104. this.stu_number = this.$store.state.userInfo.card_number
  105. if (this.stu_number == '' || typeof(this.stu_number) == 'undefined') {
  106. const userinfo = uni.getStorageSync('userinfo_storage_key')
  107. if (userinfo) {
  108. this.stu_number = userinfo.card_number
  109. } else {
  110. uni.navigateTo({
  111. url: '../index/index?from=' + options.from
  112. })
  113. uni.showToast({
  114. icon: 'none',
  115. title: '学号为空,请进行授权',
  116. duration: 3000
  117. });
  118. return
  119. }
  120. }
  121. } catch (e) {
  122. console.log(e);
  123. }
  124. if (param == 'onShow') {
  125. // 获取电量
  126. if (this.$store.state.building.roomSelect != '') {
  127. // 如果是选择宿舍号返回
  128. this.roomSelect = this.$store.state.building.roomSelect;
  129. this.add_class = this.$store.state.building.add_class;
  130. this.dorm_number = this.$store.state.building.dorm_number;
  131. this.add_class = 1
  132. if (this.roomSelect && typeof(this.roomSelect) != 'undefined') {
  133. this.getDianLiang()
  134. }
  135. }
  136. }
  137. // 初始化参数
  138. this.$store.state.building.add_class = 0;
  139. // 获取code
  140. this.getCode(options, param)
  141. },
  142. /**
  143. * 获取code
  144. */
  145. getCode(options, param) {
  146. // console.log(options, param)
  147. uni.login({
  148. success: (res) => {
  149. if (res.code) {
  150. // 请求服务器,获得openid
  151. if (param == 'comfirm') {
  152. // 获取IP
  153. this.getIP(res.code)
  154. } else
  155. this.getOpenId(options, res.code)
  156. } else {
  157. uni.showToast({
  158. title: res.errMsg,
  159. icon: 'none'
  160. });
  161. // 跳转到首页
  162. uni.redirectTo({
  163. url: '../index/index?from=0'
  164. });
  165. }
  166. }
  167. })
  168. },
  169. /**
  170. * 获取IP
  171. */
  172. async getIP(param_code) {
  173. // console.log(param_code)
  174. const res = await this.$myRequest({
  175. host: 'ip',
  176. url: '?ie=utf-8',
  177. method: 'POST'
  178. })
  179. if (res) {
  180. // console.log(res);
  181. const reg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
  182. let ip = reg.exec(res.data);
  183. // 组合地址,发起支付
  184. this.jsapi(param_code, ip[0])
  185. }
  186. },
  187. /**
  188. * 请求服务器,获得openid
  189. */
  190. async getOpenId(options, param_code) {
  191. // console.log(options, param_code)
  192. if (param_code != '') {
  193. const res = await this.$myRequest({
  194. host: this.ceshi,
  195. url: '/HotWaters/wpget_stu.action',
  196. method: 'POST',
  197. header: {
  198. 'content-type': 'application/x-www-form-urlencoded'
  199. },
  200. data: {
  201. code: param_code
  202. }
  203. })
  204. // console.log('获得openid', res);
  205. if (res.data.mess == '返回成功') {
  206. if (!(this.$store.state.building.roomSelect && typeof(this.$store.state.building.roomSelect) !=
  207. 'undefined')) {
  208. // 栋宿舍号
  209. this.dorm_number = res.data.info[0].build + res.data.info[0].dom
  210. // 校区
  211. this.compus = res.data.info[0].campus
  212. // 填充校区宿舍号
  213. if (typeof(this.dorm_number.split('栋')[1]) == 'undefined') {
  214. this.roomSelect = this.compus + ''
  215. } else {
  216. this.roomSelect = this.compus + this.dorm_number.split('栋')[1]
  217. }
  218. // 存储选择
  219. this.$store.state.building.roomSelect = this.roomSelect
  220. // 样式
  221. this.add_class = 1
  222. }
  223. // 请求选定的月份消费记录
  224. this.getDianLiang()
  225. } else if (res.data.mess == '未查询到用户信息') {
  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. uni.navigateTo({
  295. url: '../show/show',
  296. });
  297. },
  298. /**
  299. * 增加固定金额
  300. */
  301. add_money(e) {
  302. this.amount = e.currentTarget.dataset.item
  303. },
  304. /**
  305. * 确认宿舍号
  306. */
  307. confirm_room(op) {
  308. // 检查房间号
  309. if (this.roomSelect == '' || this.roomSelect == '请选择宿舍号') {
  310. uni.showToast({
  311. title: '请选择宿舍号',
  312. icon: 'success'
  313. })
  314. return
  315. }
  316. // 输入金额进行充值
  317. if (op == 'input_money') {
  318. //判断输入是否为空或不是数字
  319. if (this.amount == '' || this.amount == null || this.amount == 0) {
  320. uni.showToast({
  321. title: '请输入充值金额',
  322. duration: 2000
  323. })
  324. return
  325. }
  326. if (isNaN(this.amount)) {
  327. uni.showToast({
  328. title: '请输入正确金额',
  329. duration: 2000
  330. })
  331. return
  332. }
  333. if (!this.test) { // 非测试环境
  334. if (this.amount > 200 || this.amount < 10) {
  335. uni.showToast({
  336. title: '限额10~200元',
  337. duration: 2000
  338. })
  339. return
  340. }
  341. }
  342. }
  343. // 确认充值对话框
  344. this.confirm_dialog()
  345. },
  346. /**
  347. * 确认充值对话框
  348. */
  349. confirm_dialog() {
  350. uni.showModal({
  351. // title: '确定充值'+this.amount+'元',
  352. title: '确定充值' + this.amount + '元',
  353. success: (res) => {
  354. if (res.confirm) {
  355. // 获取code
  356. this.getCode('options', 'comfirm')
  357. } else if (res.cancel) {
  358. // console.log('用户点击取消');
  359. }
  360. }
  361. })
  362. },
  363. //增加可变金额
  364. input_money(e) {
  365. // console.log(e)
  366. this.confirm_room('input_money')
  367. },
  368. /**
  369. * 请求服务器,获取支付参数,并支付
  370. */
  371. async jsapi(param_code, param_ip) {
  372. if (param_code == '' || this.amount == '' || param_ip == '' || this.roomSelect == '') {
  373. uni.showToast({
  374. title: 'code、充值金额、宿舍号或IP为空'
  375. });
  376. return
  377. }
  378. const res = await this.$myRequest({
  379. host: this.ceshi,
  380. url: '/HotWaters/elPay.action',
  381. method: 'POST',
  382. header: {
  383. 'content-type': 'application/x-www-form-urlencoded'
  384. },
  385. data: {
  386. code: param_code,
  387. num: this.amount,
  388. ip: param_ip,
  389. roomSelect: this.roomSelect
  390. }
  391. })
  392. // console.log(res);
  393. if (res.data.pay == 'error') {
  394. uni.showToast({
  395. title: '未获得支付参数',
  396. icon: 'success',
  397. duration: 3000
  398. });
  399. } else {
  400. wx.requestPayment({
  401. timeStamp: res.data.pay.timeStamp,
  402. nonceStr: res.data.pay.nonceStr,
  403. package: 'prepay_id=' + res.data.pay.prepay_id,
  404. signType: res.data.pay.signType,
  405. paySign: res.data.pay.paySign,
  406. success: res => {
  407. if (res.errMsg == 'requestPayment:ok') {
  408. uni.showToast({
  409. title: '支付成功',
  410. icon: 'success'
  411. });
  412. }
  413. },
  414. fail: (res) => {
  415. if (res.errMsg == 'requestPayment:fail cancel') {
  416. uni.showToast({
  417. title: '取消支付',
  418. icon: 'success',
  419. duration: 2000
  420. });
  421. }
  422. },
  423. complete: (res) => {
  424. // console.log(res);
  425. }
  426. });
  427. }
  428. },
  429. /**
  430. * 输入充值金额
  431. */
  432. onInput(e) {
  433. if (this.test) { // 测试环境
  434. return
  435. }
  436. const v = e.detail.value
  437. const zero = /^(0{1,})|[^0-9]/g
  438. let final = 0
  439. if (!v) {
  440. final = 0
  441. } else {
  442. final = v.toString().replace(zero, (v) => {
  443. return 0
  444. })
  445. if (final.split('')[0] * 1 === 0) {
  446. final = final.slice(1) - 0 || 0
  447. }
  448. if (final > 200) {
  449. final = 200
  450. }
  451. }
  452. this.$nextTick(() => {
  453. if (final.toString() == '0') {
  454. this.amount = ''
  455. } else {
  456. this.amount = final.toString() || '0'
  457. }
  458. })
  459. },
  460. /**
  461. * 失去焦点时
  462. */
  463. onBlur() {
  464. if (this.test) { // 测试环境
  465. return
  466. }
  467. if (this.amount < 10) {
  468. this.amount = 10
  469. }
  470. }
  471. },
  472. }
  473. </script>
  474. <style>
  475. @import url("jiaofei.css");
  476. </style>