lijijiaofei.js 13 KB

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