lijijiaofei.js 13 KB

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