jiaofei.vue 12 KB

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