lijijiaofei.js 13 KB

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