jiaofei.vue 9.8 KB

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