jiaofei.vue 9.5 KB

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