jiaofei.vue 10 KB

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