lijijiaofei.js 11 KB

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