jiaofei.vue 10 KB

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