lijijiaofei.js 11 KB

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