jiaofei.vue 10 KB

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