jiaofei.vue 12 KB

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