jiaofei.vue 11 KB

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