jiaofei.vue 12 KB

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