jiaofei.vue 13 KB

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