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