lijijiaofei.js 13 KB

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