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