jiaofei.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. // 栋宿舍号
  207. this.dorm_number = res.data.info[0].build + res.data.info[0].dom
  208. // 校区
  209. this.compus = res.data.info[0].campus
  210. // 填充校区宿舍号
  211. if (typeof(this.dorm_number.split('栋')[1]) == 'undefined') {
  212. this.roomSelect = this.compus + ''
  213. } else {
  214. this.roomSelect = this.compus + this.dorm_number.split('栋')[1]
  215. }
  216. // 存储选择
  217. this.$store.state.building.roomSelect = this.roomSelect
  218. // 样式
  219. this.add_class = 1
  220. // 请求选定的月份消费记录
  221. this.getDianLiang()
  222. } else if (res.data.mess == '未查询到用户信息') {
  223. // 跳转到首页
  224. if (options && typeof(options.from) != 'undefined' && typeof(options.from) != '') {
  225. uni.navigateTo({
  226. url: '../index/index?from=' + options.from
  227. })
  228. } else {
  229. uni.redirectTo({
  230. url: '../index/index?from=0'
  231. });
  232. }
  233. }
  234. if (!this.roomSelect || this.roomSelect == '' || typeof(this.roomSelect) == 'undefined') {
  235. const userinfo = uni.getStorageSync('userinfo_storage_key')
  236. if (userinfo && typeof(userinfo) != 'undefined' && typeof(userinfo.campus) != 'undefined' &&
  237. typeof(userinfo.dorm_number) != 'undefined' && userinfo.campus != '' && userinfo
  238. .dorm_number != '') {
  239. this.roomSelect = userinfo.campus + userinfo.dorm_number
  240. } else {
  241. this.roomSelect = '请选择宿舍号'
  242. }
  243. }
  244. } else {
  245. uni.showToast({
  246. title: 'code为空!',
  247. icon: 'success'
  248. });
  249. }
  250. },
  251. /**
  252. * 取得电费额度
  253. */
  254. async getDianLiang() {
  255. if (this.roomSelect != '' && typeof(this.roomSelect) != 'undefined') {
  256. const res = await this.$myRequest({
  257. host: this.ceshi,
  258. url: '/HotWaters/buildoverElec.action',
  259. method: 'POST',
  260. header: {
  261. 'content-type': 'application/x-www-form-urlencoded'
  262. },
  263. data: {
  264. 'roomSelect': this.roomSelect
  265. }
  266. })
  267. // console.log('取得电费额度', res);
  268. if (res.data.mess == '0') {
  269. setTimeout(() => {
  270. this.remainElec = res.data.amount.toFixed(2)
  271. }, 30);
  272. } else {
  273. this.remainElec = 0.00.toFixed(2)
  274. }
  275. } else {
  276. uni.showToast({
  277. title: '请先选择宿舍号,\n再查询电量 或 充值',
  278. icon: 'none',
  279. duration: 3000
  280. });
  281. }
  282. },
  283. //跳转到选择页面
  284. navigateToSelect() {
  285. uni.navigateTo({
  286. url: '../select/select',
  287. });
  288. },
  289. //跳转到台账页面
  290. navigateToShow(e) {
  291. uni.navigateTo({
  292. url: '../show/show',
  293. });
  294. },
  295. /**
  296. * 增加固定金额
  297. */
  298. add_money(e) {
  299. this.amount = e.currentTarget.dataset.item
  300. },
  301. /**
  302. * 确认宿舍号
  303. */
  304. confirm_room(op) {
  305. // 检查房间号
  306. if (this.roomSelect == '' || this.roomSelect == '请选择宿舍号') {
  307. uni.showToast({
  308. title: '请选择宿舍号',
  309. icon: 'success'
  310. })
  311. return
  312. }
  313. // 输入金额进行充值
  314. if (op == 'input_money') {
  315. //判断输入是否为空或不是数字
  316. if (this.amount == '' || this.amount == null || this.amount == 0) {
  317. uni.showToast({
  318. title: '请输入充值金额',
  319. duration: 2000
  320. })
  321. return
  322. }
  323. if (isNaN(this.amount)) {
  324. uni.showToast({
  325. title: '请输入正确金额',
  326. duration: 2000
  327. })
  328. return
  329. }
  330. if (!this.test) { // 非测试环境
  331. if (this.amount > 200 || this.amount < 10) {
  332. uni.showToast({
  333. title: '限额10~200元',
  334. duration: 2000
  335. })
  336. return
  337. }
  338. }
  339. }
  340. // 确认充值对话框
  341. this.confirm_dialog()
  342. },
  343. /**
  344. * 确认充值对话框
  345. */
  346. confirm_dialog() {
  347. uni.showModal({
  348. // title: '确定充值'+this.amount+'元',
  349. title: '确定充值' + this.amount + '元',
  350. success: (res) => {
  351. if (res.confirm) {
  352. // 获取code
  353. this.getCode('options', 'comfirm')
  354. } else if (res.cancel) {
  355. // console.log('用户点击取消');
  356. }
  357. }
  358. })
  359. },
  360. //增加可变金额
  361. input_money(e) {
  362. // console.log(e)
  363. this.confirm_room('input_money')
  364. },
  365. /**
  366. * 请求服务器,获取支付参数,并支付
  367. */
  368. async jsapi(param_code, param_ip) {
  369. if (param_code == '' || this.amount == '' || param_ip == '' || this.roomSelect == '') {
  370. uni.showToast({
  371. title: 'code、充值金额、宿舍号或IP为空'
  372. });
  373. return
  374. }
  375. const res = await this.$myRequest({
  376. host: this.ceshi,
  377. url: '/HotWaters/elPay.action',
  378. method: 'POST',
  379. header: {
  380. 'content-type': 'application/x-www-form-urlencoded'
  381. },
  382. data: {
  383. code: param_code,
  384. num: this.amount,
  385. ip: param_ip,
  386. roomSelect: this.roomSelect
  387. }
  388. })
  389. // console.log(res);
  390. if (res.data.pay == 'error') {
  391. uni.showToast({
  392. title: '未获得支付参数',
  393. icon: 'success',
  394. duration: 3000
  395. });
  396. } else {
  397. wx.requestPayment({
  398. timeStamp: res.data.pay.timeStamp,
  399. nonceStr: res.data.pay.nonceStr,
  400. package: 'prepay_id=' + res.data.pay.prepay_id,
  401. signType: res.data.pay.signType,
  402. paySign: res.data.pay.paySign,
  403. success: res => {
  404. if (res.errMsg == 'requestPayment:ok') {
  405. uni.showToast({
  406. title: '支付成功',
  407. icon: 'success'
  408. });
  409. }
  410. },
  411. fail: (res) => {
  412. if (res.errMsg == 'requestPayment:fail cancel') {
  413. uni.showToast({
  414. title: '取消支付',
  415. icon: 'success',
  416. duration: 2000
  417. });
  418. }
  419. },
  420. complete: (res) => {
  421. // console.log(res);
  422. }
  423. });
  424. }
  425. },
  426. /**
  427. * 输入充值金额
  428. */
  429. onInput(e) {
  430. if (this.test) { // 测试环境
  431. return
  432. }
  433. const v = e.detail.value
  434. const zero = /^(0{1,})|[^0-9]/g
  435. let final = 0
  436. if (!v) {
  437. final = 0
  438. } else {
  439. final = v.toString().replace(zero, (v) => {
  440. return 0
  441. })
  442. if (final.split('')[0] * 1 === 0) {
  443. final = final.slice(1) - 0 || 0
  444. }
  445. if (final > 200) {
  446. final = 200
  447. }
  448. }
  449. this.$nextTick(() => {
  450. if (final.toString() == '0') {
  451. this.amount = ''
  452. } else {
  453. this.amount = final.toString() || '0'
  454. }
  455. })
  456. },
  457. /**
  458. * 失去焦点时
  459. */
  460. onBlur() {
  461. if (this.test) { // 测试环境
  462. return
  463. }
  464. if (this.amount < 10) {
  465. this.amount = 10
  466. }
  467. }
  468. },
  469. }
  470. </script>
  471. <style>
  472. @import url("jiaofei.css");
  473. </style>