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. 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 (res) {
  220. console.log("fail",res);
  221. },
  222. complete: (res) => {
  223. console.log("complete",res);
  224. }
  225. })
  226. }else if(res.data.code === '500'){
  227. // 支付失败处理
  228. if (res.data.msg && res.data.msg.includes("系统维护中,请稍后缴费")) {
  229. const parts = res.data.msg.split("|");
  230. const errorMessage = parts[0]; // "不允许在线缴费"
  231. // const payStatus = parts[1];
  232. wx.showToast({
  233. title: errorMessage,
  234. icon: 'none',
  235. duration: 2000 // 持续2秒
  236. });
  237. }
  238. }
  239. }
  240. })
  241. },
  242. // 返回上一页
  243. goBack: function() {
  244. // 设置刷新标记
  245. wx.setStorageSync('needRefreshHomepage', true);
  246. wx.navigateBack();
  247. },
  248. // 切换到首页
  249. goToHome: function() {
  250. wx.switchTab({
  251. url: '/pages/homepage/homepage',
  252. });
  253. },
  254. // 切换到我的页面
  255. goToMine: function() {
  256. wx.switchTab({
  257. url: '/pages/mine/mine',
  258. });
  259. },
  260. // 添加切换水表的方法
  261. switchMeter: function() {
  262. wx.navigateTo({
  263. url: '/pages/switchMeter/switchMeter',
  264. })
  265. },
  266. navigateToHome() {
  267. wx.switchTab({
  268. url: '/pages/index/index'
  269. })
  270. },
  271. navigateToMine() {
  272. wx.switchTab({
  273. url: '/pages/mine/mine'
  274. })
  275. },
  276. // 显示键盘 - 使用微信内置键盘
  277. showCustomAmountInput: function() {
  278. // 使用微信内置的输入框
  279. wx.showModal({
  280. title: '请输入金额',
  281. placeholderText: '请输入缴费金额',
  282. editable: true,
  283. success: (res) => {
  284. if (res.confirm && res.content) {
  285. // 验证输入是否为有效数字
  286. const inputAmount = parseFloat(res.content);
  287. if (!isNaN(inputAmount) && inputAmount > 0) {
  288. this.setData({
  289. amount: inputAmount
  290. });
  291. } else {
  292. wx.showToast({
  293. title: '请输入有效金额',
  294. icon: 'none'
  295. });
  296. }
  297. }
  298. }
  299. });
  300. },
  301. // 自定义金额输入
  302. onCustomAmountInput: function(e) {
  303. let value = e.detail.value;
  304. // 只允许输入数字和小数点
  305. value = value.replace(/[^\d.]/g, '');
  306. // 处理前导零
  307. if (value.startsWith('0')) {
  308. // 如果只有一个0,保持不变
  309. if (value.length === 1) {
  310. // 保持原样
  311. } else if (value.startsWith('0.')) {
  312. // 如果是0.开头,保持原样
  313. } else {
  314. // 移除前导零,但保留单个0
  315. value = value.replace(/^0+/, '');
  316. if (value === '') value = '0';
  317. }
  318. }
  319. // 处理小数点
  320. if (value.includes('.')) {
  321. const parts = value.split('.');
  322. // 只保留第一个小数点
  323. if (parts.length > 2) {
  324. value = parts[0] + '.' + parts.slice(1).join('');
  325. }
  326. // 限制小数点后两位
  327. if (parts[1].length > 2) {
  328. value = parts[0] + '.' + parts[1].substring(0, 2);
  329. }
  330. }
  331. const amount = parseFloat(value) || 0;
  332. this.setData({
  333. customAmount: value,
  334. amount: amount,
  335. selectedAmount: 0
  336. });
  337. },
  338. // 输入数字
  339. inputNumber: function(e) {
  340. const number = e.currentTarget.dataset.number;
  341. let customAmount = this.data.customAmount;
  342. // 处理小数点
  343. if (number === '.') {
  344. if (customAmount.includes('.')) {
  345. return; // 已经有小数点了,不能再输入
  346. }
  347. if (customAmount === '') {
  348. customAmount = '0.'; // 如果直接输入小数点,前面补0
  349. } else {
  350. customAmount += '.';
  351. }
  352. } else {
  353. // 处理首位为0的情况
  354. if (customAmount === '0' && number !== '.') {
  355. customAmount = number;
  356. } else {
  357. // 限制小数点后两位
  358. if (customAmount.includes('.')) {
  359. const parts = customAmount.split('.');
  360. if (parts[1].length >= 2) {
  361. return; // 小数点后已经有两位了,不能再输入
  362. }
  363. }
  364. customAmount += number;
  365. }
  366. }
  367. // 更新金额
  368. this.setData({
  369. customAmount: customAmount,
  370. amount: parseFloat(customAmount) || 0
  371. });
  372. },
  373. // 删除数字
  374. deleteNumber: function() {
  375. let customAmount = this.data.customAmount;
  376. if (customAmount.length <= 1) {
  377. this.setData({
  378. customAmount: '',
  379. amount: 0
  380. });
  381. } else {
  382. customAmount = customAmount.substring(0, customAmount.length - 1);
  383. this.setData({
  384. customAmount: customAmount,
  385. amount: parseFloat(customAmount) || 0
  386. });
  387. }
  388. },
  389. // 确认输入
  390. confirmInput: function() {
  391. this.setData({
  392. showKeyboard: false,
  393. inputFocus: false
  394. });
  395. },
  396. // 输入框获得焦点
  397. onInputFocus: function() {
  398. this.setData({
  399. inputFocus: true,
  400. selectedAmount: null // 取消快捷金额的选中状态
  401. });
  402. },
  403. // 输入框失去焦点
  404. onInputBlur: function() {
  405. this.setData({
  406. inputFocus: false
  407. });
  408. },
  409. // 创建微信付款记录
  410. createWeChatPayMentRecord:function(orderNo,paymentAmount){
  411. wx.request({
  412. url: app.globalData.interfaceUrls.createPaymentRecord,
  413. method: 'POST',
  414. header: {
  415. 'content-type': 'application/json',
  416. 'token': app.globalData.userWxInfo.token,
  417. 'source': "wc",
  418. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  419. },
  420. data: {
  421. orderNo: orderNo,
  422. paymentAmount: paymentAmount,
  423. userNumber: app.globalData.currentAccountInfo.usernumber,
  424. dsKey: app.globalData.currentAccountInfo.dsKey
  425. },
  426. success(res) {
  427. wx.hideLoading();
  428. wx.navigateTo({
  429. url: '/pages/jiaofeiSuccess/jiaofeiSuccess',
  430. });
  431. },
  432. fail(error) {
  433. wx.hideLoading();
  434. wx.showToast({
  435. title: '数据加载失败,请稍后再试',
  436. icon: 'none',
  437. duration: 2000
  438. });
  439. }
  440. });
  441. },
  442. // 从接口获取支付数据
  443. fetchPaymentData: function() {
  444. // 这里是模拟数据,实际应用中应该调用真实接口
  445. // wx.request({
  446. // url: 'your-api-endpoint',
  447. // success: (res) => {
  448. // this.setData({
  449. // amountDue: res.data.amountDue,
  450. // balance: res.data.balance
  451. // });
  452. // }
  453. // });
  454. // this.setData({
  455. // amountDue: 150.00,
  456. // balance: 200.50
  457. // });
  458. },
  459. })