jiaofei.vue 9.9 KB

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