jiaofei.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 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. userinfo: {}, //用户信息
  73. order_id: '', // 订单号
  74. card_number: '', //用户卡号
  75. dom: '', //房间号
  76. campus: '', //校区
  77. access_token: '',
  78. sub_appid: '', //商户号
  79. pay_info: '',
  80. xiaofei_items: '', // 消费记录
  81. test: getApp().globalData.test
  82. }
  83. },
  84. onLoad: function(options) {
  85. let item_tmp = decodeURIComponent(options.item)
  86. //将字符串解密转换成对象
  87. let item = JSON.parse(item_tmp);
  88. // 请求选定的月份消费记录
  89. // this.request_consumption_records()
  90. try {
  91. // 获取存储的用户数据
  92. const value = uni.getStorageSync('userinfo_storage_key')
  93. // console.log(item)
  94. if (item_tmp == '{}') { // 如果没有用户信息,返回首页
  95. uni.redirectTo({
  96. url: '../index/index'
  97. })
  98. return
  99. }
  100. // 如果是选择宿舍号返回
  101. if (options.o == 'index') {
  102. this.$store.state.building.roomSelect = '';
  103. this.$store.state.building.add_class = 0;
  104. this.$store.state.building.dom = '';
  105. this.userinfo = item;
  106. // 更新存储的用户信息
  107. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  108. this.roomSelect = this.campus + this.dom
  109. } else if (JSON.stringify(value) !== '{}') {
  110. this.userinfo = value;
  111. }
  112. this.campus = this.userinfo.campus
  113. this.dom = this.userinfo.dorm_number
  114. this.sub_appid = this.userinfo.sub_appid
  115. this.add_class = 1
  116. } catch (e) {
  117. console.log(e)
  118. }
  119. //将缓存中的卡号进行获取
  120. this.card_number = this.userinfo.card_number
  121. if (this.$store.state.hasUserInfo == false) {
  122. // 获取应用有效凭证
  123. this.get_valid_credentials()
  124. }
  125. },
  126. onShow() {
  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.dom = this.$store.state.building.dom;
  132. if (this.roomSelect != '') {
  133. this.getDianLiang()
  134. }
  135. }
  136. },
  137. methods: {
  138. /**
  139. * 取得电费额度
  140. */
  141. async getDianLiang() {
  142. let res;
  143. if (this.test) { // 测试环境
  144. res = await this.$myRequest({
  145. host: 'ceshi',
  146. url: 'http://tdqdjt.natappfree.cc/HotWaters/buildoverElec.action',
  147. method: 'POST',
  148. header: {
  149. 'content-type': 'application/x-www-form-urlencoded'
  150. },
  151. data: {
  152. 'roomSelect': this.roomSelect
  153. }
  154. })
  155. } else {
  156. res = await this.$myRequest({
  157. host: 'code',
  158. url: '/HotWater/buildoverElec.action',
  159. method: 'POST',
  160. header: {
  161. 'content-type': 'application/x-www-form-urlencoded'
  162. },
  163. data: {
  164. 'roomSelect': this.roomSelect
  165. }
  166. })
  167. }
  168. console.log(res.data.amount);
  169. if (res.mess != '请选择房间号!') {
  170. setTimeout(() => {
  171. this.remainElec = res.data.amount.toFixed(2)
  172. }, 300);
  173. } else {
  174. uni.showToast({
  175. title: '获取电量失败!',
  176. duration: 2000
  177. })
  178. }
  179. },
  180. /**
  181. * 获取应用有效凭证
  182. */
  183. async get_valid_credentials() {
  184. const value = uni.getStorageSync('userinfo_storage_key')
  185. // console.log(value)
  186. const res = await this.$myRequest({
  187. host: 'wecard',
  188. url: '/cgi-bin/oauth2/token',
  189. method: 'POST',
  190. header: {
  191. 'content-type': 'application/json'
  192. },
  193. data: {
  194. 'app_key': value.appkey,
  195. 'app_secret': value.app_secret,
  196. 'grant_type': 'client_credentials',
  197. 'scope': 'base',
  198. 'ocode': value.ocode
  199. }
  200. });
  201. if (typeof(res.data.access_token) != 'undefined') {
  202. // 获取到应用有效凭证,保存到页面变量中
  203. this.access_token = res.data.access_token
  204. } else {
  205. uni.showToast({
  206. title: '获取凭证失败',
  207. duration: 2000
  208. })
  209. }
  210. // 查询用户信息
  211. this.select_user_info()
  212. },
  213. /**
  214. * 查询用户信息
  215. */
  216. async select_user_info() {
  217. const res = await this.$myRequest({
  218. host: 'code',
  219. url: '/HotWater/userget_stu.action',
  220. method: 'POST',
  221. header: {
  222. 'content-type': 'application/x-www-form-urlencoded'
  223. },
  224. data: {
  225. card_number: this.userinfo.card_number
  226. }
  227. });
  228. // console.log(res, 'select_user_info');
  229. if (res.data.info) {
  230. let cnumber = this.userinfo.card_number
  231. if (cnumber == null || cnumber == undefined || cnumber == '') {
  232. // 之前未绑定用户信息,现在去绑定用户信息
  233. this.send_save_user_info()
  234. }
  235. } else {
  236. uni.showToast({
  237. icon: 'success',
  238. title: '获取用户信息失败'
  239. })
  240. }
  241. },
  242. /**
  243. * 请求选定的月份消费记录
  244. */
  245. async request_consumption_records() {
  246. const userinfo = uni.getStorageInfoSync('userinfo_storage_key')
  247. this.card_number = userinfo.card_number
  248. if (this.card_number != '') {
  249. this.xiaofei_items = []
  250. const res = await this.$myRequest({
  251. host: 'code',
  252. url: '/HotWater/wxpayqueryConsume.action',
  253. method: 'POST',
  254. header: {
  255. 'content-type': 'application/x-www-form-urlencoded'
  256. },
  257. data: {
  258. card_number: this.card_number,
  259. begin_time: this.date
  260. }
  261. });
  262. // console.log(res.data.mess, 'request_consumption_records');
  263. if (typeof(res.data.mess) != 'undefined') {
  264. let items = res.data.mess
  265. // 按时间倒序排列
  266. // items.sort((a, b) => {
  267. // return a.begin_time < b.begin_time ? 1 : -1;
  268. // })
  269. for (var i = 0; i < items.length; i++) {
  270. // 过滤掉为0的记录,只显示消费大于0的记录
  271. // if (items[i].use_amount > 0.00) {
  272. this.xiaofei_items.push(items[i])
  273. // }
  274. }
  275. } else {
  276. uni.showToast({
  277. icon: 'success',
  278. title: '该月无消费记录'
  279. })
  280. }
  281. } else {
  282. uni.showToast({
  283. icon: 'success',
  284. title: '用户卡号为空'
  285. })
  286. }
  287. },
  288. /**
  289. * 绑定用户信息
  290. */
  291. async send_save_user_info() {
  292. const res = await this.$myRequest({
  293. host: 'code',
  294. url: '/HotWater/userstu_info.action',
  295. method: 'POST',
  296. header: {
  297. 'content-type': 'application/x-www-form-urlencoded'
  298. },
  299. data: {
  300. user: JSON.stringify(this.userinfo)
  301. }
  302. });
  303. // console.log('send_save_user_info:', res)
  304. let error = res.data.erro;
  305. if (error == '未获取到数据!') {
  306. uni.showToast({
  307. title: '绑定用户失败',
  308. duration: 2000
  309. })
  310. } else {
  311. uni.showToast({
  312. icon: 'success',
  313. title: '绑定用户成功',
  314. duration: 2000
  315. })
  316. }
  317. },
  318. //跳转到选择页面
  319. navigateToSelect() {
  320. uni.navigateTo({
  321. url: '../select/select?roomSelect=' + this.roomSelect,
  322. });
  323. },
  324. //跳转到台账页面
  325. navigateToShow(e) {
  326. uni.navigateTo({
  327. url: '../show/show?card_number=' + this.card_number,
  328. });
  329. },
  330. //增加固定金额
  331. add_money(e) {
  332. this.add_class1 = 0
  333. this.addMoney = e.currentTarget.dataset.item
  334. this.confirm_room('add_money')
  335. },
  336. confirm_room(op) {
  337. // 检查房间号
  338. if (this.roomSelect == '') {
  339. uni.showToast({
  340. title: '请选择房间号',
  341. icon: 'success'
  342. })
  343. return
  344. }
  345. // 输入金额进行充值
  346. if (op == 'input_money') {
  347. //判断输入是否为空或不是数字
  348. if (this.inputMoney == '' || this.inputMoney == null) {
  349. uni.showToast({
  350. title: '请输入充值金额',
  351. duration: 2000
  352. })
  353. return
  354. }
  355. if (isNaN(this.inputMoney)) {
  356. uni.showToast({
  357. title: '请输入数值',
  358. duration: 2000
  359. })
  360. return
  361. }
  362. if (!this.test) { // 非测试环境
  363. if (this.inputMoney > 200 || this.inputMoney < 10) {
  364. uni.showToast({
  365. title: '限额10~200元',
  366. duration: 2000
  367. })
  368. return
  369. }
  370. }
  371. this.addMoney = this.inputMoney
  372. }
  373. // 确认充值对话框
  374. this.confirm_dialog()
  375. },
  376. // 确认充值对话框
  377. confirm_dialog() {
  378. uni.showModal({
  379. // title: '确定充值'+this.inputMoney+'元',
  380. title: '确定充值' + this.addMoney + '元',
  381. success: (res) => {
  382. if (res.confirm) {
  383. this.jsapi()
  384. } else if (res.cancel) {
  385. console.log('用户点击取消');
  386. }
  387. }
  388. })
  389. },
  390. //增加可变金额
  391. input_money(e) {
  392. // console.log(e)
  393. this.confirm_room('input_money')
  394. },
  395. //准备支付
  396. async jsapi() {
  397. const res = await this.$myRequest({
  398. host: 'wecard',
  399. url: "/cgi-bin/pay/app/mppay",
  400. method: 'POST',
  401. header: {
  402. 'content-type': 'application/json'
  403. },
  404. data: {
  405. 'access_token': this.access_token,
  406. 'sub_appid': this.sub_appid,
  407. 'user_id': this.card_number,
  408. "order_id": this.order_id = this.$getOrderId('dianfei'),
  409. "amount": this.addMoney * 100,
  410. 'order_type': 1,
  411. 'callback_url': ''
  412. }
  413. })
  414. if (res.data.code == 0) {
  415. this.pay_info = res.data.data.pay_info
  416. // 发起支付
  417. this.pay_amount()
  418. } else {
  419. uni.showToast({
  420. icon: 'none',
  421. title: res.data.message,
  422. duration: 3000
  423. })
  424. }
  425. },
  426. // 调起支付
  427. pay_amount() {
  428. var OpenMidas = require("@/static/openMidas.js"); // 引入小程序目录下的SDK文件
  429. // 设置支付配置
  430. wx['OpenMidasConfig'] = {
  431. apiCommonConf: {
  432. version: "weixiao"
  433. },
  434. cgiDomain: {
  435. test: "midas.weixiao.qq.com/api", // 私有化参数联系微卡客服进行获取
  436. },
  437. webDomain: "https://midas.weixiao.qq.com/h5", // 私有化参数联系微卡客服进行获取
  438. sandboxWebDomain: "https://midas.weixiao.qq.com/h5" // 私有化参数联系微卡客服进行获取
  439. }
  440. var payInfo = this.pay_info; // 请求mppay接口返回的数据
  441. // console.log(payInfo)
  442. var appMetaData = "app=test&version=1.1"; // 自定义回调数据
  443. OpenMidas.init("test");
  444. OpenMidas.pay(
  445. payInfo,
  446. function(resultCode, innerCode, resultMsg, appMetaData) {
  447. console.log(resultCode); // 支付响应状态码
  448. console.log(innerCode); // 支付响应内部错误码
  449. console.log(resultMsg); // 支付响应说明
  450. console.log(appMetaData); // 自定义回调数据
  451. // todo:处理业务逻辑
  452. },
  453. appMetaData
  454. );
  455. },
  456. /**
  457. * 输入充值金额
  458. */
  459. onInput(e) {
  460. if (this.test) { // 测试环境
  461. return
  462. }
  463. const v = e.detail.value
  464. this.inputMoney = 10
  465. const zero = /^(0{1,})|[^0-9]/g
  466. let final = 0
  467. if (!v) {
  468. final = 0
  469. } else {
  470. final = v.toString().replace(zero, (v) => {
  471. return 0
  472. })
  473. if (final.split('')[0] * 1 === 0) {
  474. final = final.slice(1) - 0 || 0
  475. }
  476. if (final > 200) {
  477. final = 200
  478. }
  479. }
  480. this.$nextTick(() => {
  481. this.inputMoney = final.toString() || '0'
  482. })
  483. },
  484. /**
  485. * 失去焦点时
  486. */
  487. onBlur() {
  488. if (this.test) { // 测试环境
  489. return
  490. }
  491. if (this.inputMoney < 10) {
  492. this.inputMoney = 10
  493. }
  494. }
  495. },
  496. }
  497. </script>
  498. <style>
  499. @import url("jiaofei.css");
  500. </style>