lijijiaofei.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. const app = getApp();
  2. Page({
  3. data: {
  4. images: {
  5. logo: '',
  6. background: '',
  7. card: '',
  8. tzd: "",
  9. phone: "",
  10. yl: "",
  11. jcsfcjtzd: ""
  12. },
  13. userInfo: {
  14. name: '',
  15. id: '',
  16. address: ''
  17. },
  18. billInfo: {}, // 存储从首页传递过来的账单信息
  19. quickAmounts: [10, 30, 50, 100, 200, 500],
  20. showKeyboard: false,
  21. selectedAmount: 0,
  22. customAmount: '',
  23. inputFocus: false,
  24. zdId:"",
  25. amount: 0,
  26. actualAmount: 0,
  27. balance: 0
  28. },
  29. onLoad(options) {
  30. const _this=this;
  31. // 更新用户信息
  32. this.updateUserInfo();
  33. wx.request({
  34. url: app.globalData.interfaceUrls.pendingBill + app.globalData.currentAccountInfo.usernumber,
  35. method: 'POST',
  36. header: {
  37. 'content-type': 'application/json',
  38. 'token': app.globalData.userWxInfo.token,
  39. 'source': "wc",
  40. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  41. },
  42. success(res) {
  43. wx.hideLoading();
  44. let apiReturnData = res.data;
  45. _this.setData({
  46. actualAmount: apiReturnData.data.yj, // 设置应缴金额
  47. balance: apiReturnData.data.ye, //余额
  48. zdId: apiReturnData.data.zdId // 对应的账单记录
  49. });
  50. // 计算默认实缴金额
  51. _this.calculateDefaultAmount();
  52. },
  53. fail(error) {
  54. wx.hideLoading();
  55. wx.showToast({
  56. title: '数据加载失败,请稍后再试',
  57. icon: 'none',
  58. duration: 2000
  59. });
  60. }
  61. });
  62. this.setData({
  63. images: {
  64. logo:'/static_file/logo.png',
  65. background:'/static_file/background.png',
  66. card:'/static_file/card.png',
  67. tzd:'/static_file/backgrountzdd.png',
  68. phone:'/static_file/phone.png',
  69. yl:'/static_file/background.yl',
  70. jcsfcjtzd:'/static_file/jcsfcjtzd.png',
  71. kapiantubiao:'/static_file/kapiantubiao.png'
  72. }
  73. })
  74. // 获取状态栏高度
  75. const systemInfo = wx.getSystemInfoSync();
  76. this.setData({
  77. statusBarHeight: systemInfo.statusBarHeight
  78. });
  79. this.fetchPaymentData();
  80. },
  81. onShow() {
  82. // 每次页面显示时更新用户信息
  83. this.updateUserInfo();
  84. // 获取最新余额数据
  85. const _this = this;
  86. wx.request({
  87. url: app.globalData.interfaceUrls.pendingBill + app.globalData.currentAccountInfo.usernumber,
  88. method: 'POST',
  89. header: {
  90. 'content-type': 'application/json',
  91. 'token': app.globalData.userWxInfo.token,
  92. 'source': "wc",
  93. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  94. },
  95. success(res) {
  96. wx.hideLoading();
  97. let apiReturnData = res.data;
  98. _this.setData({
  99. actualAmount: apiReturnData.data.yj, // 设置应缴金额
  100. balance: apiReturnData.data.ye, //余额
  101. zdId: apiReturnData.data.zdId // 对应的账单记录
  102. });
  103. // 重新计算默认实缴金额
  104. _this.calculateDefaultAmount();
  105. },
  106. fail(error) {
  107. wx.hideLoading();
  108. wx.showToast({
  109. title: '数据加载失败,请稍后再试',
  110. icon: 'none',
  111. duration: 2000
  112. });
  113. }
  114. });
  115. },
  116. // 更新用户信息的方法
  117. updateUserInfo() {
  118. this.setData({
  119. 'userInfo.name': app.globalData.currentAccountInfo.username,
  120. 'userInfo.id': app.globalData.currentAccountInfo.usernumber,
  121. 'userInfo.address': app.globalData.currentAccountInfo.address
  122. });
  123. },
  124. // 计算默认实缴金额
  125. calculateDefaultAmount: function() {
  126. const { actualAmount, balance } = this.data;
  127. const difference = (actualAmount - balance).toFixed(2);
  128. if (difference <= 0) {
  129. this.setData({
  130. amount: 0
  131. });
  132. } else {
  133. this.setData({
  134. amount: parseFloat(difference)
  135. });
  136. }
  137. },
  138. // 验证实缴金额
  139. validateAmount: function(amount) {
  140. const { actualAmount, balance } = this.data;
  141. const difference = actualAmount - balance;
  142. if (difference <= 0) {
  143. return amount > 0;
  144. } else {
  145. return amount >= difference;
  146. }
  147. },
  148. // 选择快捷金额
  149. selectAmount: function(e) {
  150. const amount = parseFloat(e.currentTarget.dataset.amount);
  151. this.setData({
  152. selectedAmount: amount,
  153. amount: amount,
  154. customAmount: ''
  155. });
  156. },
  157. // 立即缴费
  158. payNow: function() {
  159. const { amount, actualAmount, balance } = this.data;
  160. const _this=this;
  161. if (amount <= 0) {
  162. wx.showToast({
  163. title: '请输入缴费金额',
  164. icon: 'none'
  165. });
  166. return;
  167. }
  168. if (actualAmount > 0 && amount * 1000 + balance * 1000 < actualAmount * 1000) {
  169. // 先转整数计算,再转回浮点数
  170. const minPayment = (actualAmount * 1000 - balance * 1000) / 1000;
  171. const formattedMinPayment = minPayment.toFixed(2);
  172. wx.showToast({
  173. title: '最低缴费金额:' + formattedMinPayment + '元',
  174. icon: 'none'
  175. });
  176. return;
  177. }
  178. wx.showLoading({
  179. title: '处理中',
  180. });
  181. wx.request({
  182. url: app.globalData.interfaceUrls.prepayOrder,
  183. method: 'POST',
  184. header: {
  185. 'content-type': 'application/json',
  186. 'token': app.globalData.userWxInfo.token,
  187. 'source': "wc",
  188. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  189. },
  190. data: {
  191. openId: app.globalData.userWxInfo.openid,
  192. totalAmount: this.data.amount*1000/10,
  193. dskey: app.globalData.currentAccountInfo.dsKey,
  194. zdId: this.data.zdId,
  195. userNumber: app.globalData.currentAccountInfo.usernumber
  196. },
  197. success: res => {
  198. if(res.data.code === '200'){
  199. wx.hideLoading();
  200. console.log('预下单', res);
  201. let orderNo=res.data.data.orderNo;
  202. wx.requestPayment({
  203. timeStamp: res.data.data.paymentSign.timeStamp,
  204. nonceStr: res.data.data.paymentSign.nonceStr,
  205. package: res.data.data.paymentSign.packageVal,
  206. signType: 'RSA',
  207. paySign: res.data.data.paymentSign.paySign,
  208. success: res => {
  209. console.log('支付结果', res);
  210. wx.hideLoading();
  211. wx.navigateTo({
  212. url: '/pages/jiaofeiSuccess/jiaofeiSuccess',
  213. });
  214. // setTimeout(function(){
  215. // this.createWeChatPayMentRecord(orderNo,this.data.amount);
  216. // },200)
  217. },
  218. fail (failRes) {
  219. _this.setData({
  220. customAmount:null
  221. });
  222. wx.request({
  223. url: app.globalData.interfaceUrls.cancelPay+orderNo+"/wechat",
  224. method: 'POST',
  225. header: {
  226. 'content-type': 'application/json',
  227. 'token': app.globalData.userWxInfo.token,
  228. 'source': "wc",
  229. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  230. },
  231. data: {},
  232. success(res) { },
  233. fail(error) { }
  234. });
  235. console.log("fail",failRes);
  236. },
  237. complete: (res) => {
  238. console.log("complete",res);
  239. }
  240. })
  241. }else if(res.data.code === '500'){
  242. // 支付失败处理
  243. if (res.data.msg && res.data.msg.includes("系统维护中,请稍后缴费")) {
  244. const parts = res.data.msg.split("|");
  245. const errorMessage = parts[0]; // "不允许在线缴费"
  246. // const payStatus = parts[1];
  247. wx.showToast({
  248. title: errorMessage,
  249. icon: 'none',
  250. duration: 2000 // 持续2秒
  251. });
  252. }
  253. }
  254. }
  255. })
  256. },
  257. // 返回上一页
  258. goBack: function() {
  259. // 设置刷新标记
  260. wx.setStorageSync('needRefreshHomepage', true);
  261. wx.navigateBack();
  262. },
  263. // 切换到首页
  264. goToHome: function() {
  265. wx.switchTab({
  266. url: '/pages/homepage/homepage',
  267. });
  268. },
  269. // 切换到我的页面
  270. goToMine: function() {
  271. wx.switchTab({
  272. url: '/pages/mine/mine',
  273. });
  274. },
  275. // 添加切换水表的方法
  276. switchMeter: function() {
  277. wx.navigateTo({
  278. url: '/pages/switchMeter/switchMeter',
  279. })
  280. },
  281. navigateToHome() {
  282. wx.switchTab({
  283. url: '/pages/index/index'
  284. })
  285. },
  286. navigateToMine() {
  287. wx.switchTab({
  288. url: '/pages/mine/mine'
  289. })
  290. },
  291. // 显示键盘 - 使用微信内置键盘
  292. showCustomAmountInput: function() {
  293. // 使用微信内置的输入框
  294. wx.showModal({
  295. title: '请输入金额',
  296. placeholderText: '请输入缴费金额',
  297. editable: true,
  298. success: (res) => {
  299. if (res.confirm && res.content) {
  300. // 验证输入是否为有效数字
  301. const inputAmount = parseFloat(res.content);
  302. if (!isNaN(inputAmount) && inputAmount > 0) {
  303. this.setData({
  304. amount: inputAmount
  305. });
  306. } else {
  307. wx.showToast({
  308. title: '请输入有效金额',
  309. icon: 'none'
  310. });
  311. }
  312. }
  313. }
  314. });
  315. },
  316. // 自定义金额输入
  317. onCustomAmountInput: function(e) {
  318. let value = e.detail.value;
  319. // 只允许输入数字和小数点
  320. value = value.replace(/[^\d.]/g, '');
  321. // 处理前导零
  322. if (value.startsWith('0')) {
  323. // 如果只有一个0,保持不变
  324. if (value.length === 1) {
  325. // 保持原样
  326. } else if (value.startsWith('0.')) {
  327. // 如果是0.开头,保持原样
  328. } else {
  329. // 移除前导零,但保留单个0
  330. value = value.replace(/^0+/, '');
  331. if (value === '') value = '0';
  332. }
  333. }
  334. // 处理小数点
  335. if (value.includes('.')) {
  336. const parts = value.split('.');
  337. // 只保留第一个小数点
  338. if (parts.length > 2) {
  339. value = parts[0] + '.' + parts.slice(1).join('');
  340. }
  341. // 限制小数点后两位
  342. if (parts[1].length > 2) {
  343. value = parts[0] + '.' + parts[1].substring(0, 2);
  344. }
  345. }
  346. const amount = parseFloat(value) || 0;
  347. this.setData({
  348. customAmount: value,
  349. amount: amount,
  350. selectedAmount: 0
  351. });
  352. },
  353. // 输入数字
  354. inputNumber: function(e) {
  355. const number = e.currentTarget.dataset.number;
  356. let customAmount = this.data.customAmount;
  357. // 处理小数点
  358. if (number === '.') {
  359. if (customAmount.includes('.')) {
  360. return; // 已经有小数点了,不能再输入
  361. }
  362. if (customAmount === '') {
  363. customAmount = '0.'; // 如果直接输入小数点,前面补0
  364. } else {
  365. customAmount += '.';
  366. }
  367. } else {
  368. // 处理首位为0的情况
  369. if (customAmount === '0' && number !== '.') {
  370. customAmount = number;
  371. } else {
  372. // 限制小数点后两位
  373. if (customAmount.includes('.')) {
  374. const parts = customAmount.split('.');
  375. if (parts[1].length >= 2) {
  376. return; // 小数点后已经有两位了,不能再输入
  377. }
  378. }
  379. customAmount += number;
  380. }
  381. }
  382. // 更新金额
  383. this.setData({
  384. customAmount: customAmount,
  385. amount: parseFloat(customAmount) || 0
  386. });
  387. },
  388. // 删除数字
  389. deleteNumber: function() {
  390. let customAmount = this.data.customAmount;
  391. if (customAmount.length <= 1) {
  392. this.setData({
  393. customAmount: '',
  394. amount: 0
  395. });
  396. } else {
  397. customAmount = customAmount.substring(0, customAmount.length - 1);
  398. this.setData({
  399. customAmount: customAmount,
  400. amount: parseFloat(customAmount) || 0
  401. });
  402. }
  403. },
  404. // 确认输入
  405. confirmInput: function() {
  406. this.setData({
  407. showKeyboard: false,
  408. inputFocus: false
  409. });
  410. },
  411. // 输入框获得焦点
  412. onInputFocus: function() {
  413. this.setData({
  414. inputFocus: true,
  415. selectedAmount: null // 取消快捷金额的选中状态
  416. });
  417. },
  418. // 输入框失去焦点
  419. onInputBlur: function() {
  420. this.setData({
  421. inputFocus: false
  422. });
  423. },
  424. // 创建微信付款记录
  425. createWeChatPayMentRecord:function(orderNo,paymentAmount){
  426. wx.request({
  427. url: app.globalData.interfaceUrls.createPaymentRecord,
  428. method: 'POST',
  429. header: {
  430. 'content-type': 'application/json',
  431. 'token': app.globalData.userWxInfo.token,
  432. 'source': "wc",
  433. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  434. },
  435. data: {
  436. orderNo: orderNo,
  437. paymentAmount: paymentAmount,
  438. userNumber: app.globalData.currentAccountInfo.usernumber,
  439. dsKey: app.globalData.currentAccountInfo.dsKey
  440. },
  441. success(res) {
  442. wx.hideLoading();
  443. wx.navigateTo({
  444. url: '/pages/jiaofeiSuccess/jiaofeiSuccess',
  445. });
  446. },
  447. fail(error) {
  448. wx.hideLoading();
  449. wx.showToast({
  450. title: '数据加载失败,请稍后再试',
  451. icon: 'none',
  452. duration: 2000
  453. });
  454. }
  455. });
  456. },
  457. // 从接口获取支付数据
  458. fetchPaymentData: function() {
  459. // 这里是模拟数据,实际应用中应该调用真实接口
  460. // wx.request({
  461. // url: 'your-api-endpoint',
  462. // success: (res) => {
  463. // this.setData({
  464. // amountDue: res.data.amountDue,
  465. // balance: res.data.balance
  466. // });
  467. // }
  468. // });
  469. // this.setData({
  470. // amountDue: 150.00,
  471. // balance: 200.50
  472. // });
  473. },
  474. })