jiaofei.vue 11 KB

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