lijijiaofei.js 12 KB

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