jiaofei.vue 12 KB

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