lijijiaofei.js 13 KB

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