lijijiaofei.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: null,
  28. customAmount: '',
  29. inputFocus: false,
  30. },
  31. onLoad(options) {
  32. // 接收从homepage传递的账单信息
  33. if (options.billInfo) {
  34. const billInfo = JSON.parse(decodeURIComponent(options.billInfo));
  35. this.setData({
  36. amount: billInfo.amount, // 默认实缴金额为应缴金额
  37. actualAmount: billInfo.amountDue, // 设置应缴金额
  38. balance: billInfo.balance //余额
  39. });
  40. }
  41. this.setData({
  42. images: {
  43. logo:'/static_file/logo.png',
  44. background:'/static_file/background.png',
  45. card:'/static_file/card.png',
  46. tzd:'/static_file/backgrountzdd.png',
  47. phone:'/static_file/phone.png',
  48. yl:'/static_file/background.yl',
  49. jcsfcjtzd:'/static_file/jcsfcjtzd.png',
  50. kapiantubiao:'/static_file/kapiantubiao.png'
  51. }
  52. })
  53. // 获取状态栏高度
  54. const systemInfo = wx.getSystemInfoSync();
  55. this.setData({
  56. statusBarHeight: systemInfo.statusBarHeight
  57. });
  58. this.fetchPaymentData();
  59. },
  60. // 选择快捷金额
  61. selectAmount: function(e) {
  62. const amount = e.currentTarget.dataset.amount;
  63. this.setData({
  64. amount: parseFloat(amount),
  65. selectedAmount: parseFloat(amount),
  66. customAmount: '' // 清空自定义金额
  67. });
  68. },
  69. // 立即缴费
  70. payNow: function() {
  71. wx.showLoading({
  72. title: '处理中',
  73. });
  74. wx.request({
  75. url: app.globalData.interfaceUrls.prepayOrder,
  76. method: 'POST',
  77. header: {
  78. 'content-type': 'application/json',
  79. 'token': app.globalData.userWxInfo.token,
  80. 'source': "wc",
  81. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  82. },
  83. data: {
  84. openId: app.globalData.userWxInfo.openid,
  85. totalAmount: 1,
  86. description: '测试订单',
  87. },
  88. success: res => {
  89. wx.hideLoading();
  90. console.log('预下单', res);
  91. wx.requestPayment({
  92. timeStamp: res.data.data.timeStamp,
  93. nonceStr: res.data.data.nonceStr,
  94. package: res.data.data.packageVal,
  95. signType: 'RSA',
  96. paySign: res.data.data.paySign,
  97. success: res => {
  98. console.log('支付结果', res)
  99. },
  100. complete: () => {
  101. }
  102. })
  103. }
  104. })
  105. // 模拟支付
  106. // setTimeout(() => {
  107. // wx.hideLoading();
  108. // wx.showToast({
  109. // title: '缴费成功',
  110. // icon: 'success',
  111. // duration: 2000,
  112. // success: () => {
  113. // // 支付成功后返回首页
  114. // setTimeout(() => {
  115. // wx.navigateBack();
  116. // }, 2000);
  117. // }
  118. // });
  119. // }, 1500);
  120. },
  121. // 返回上一页
  122. goBack: function() {
  123. wx.navigateBack();
  124. },
  125. // 切换到首页
  126. goToHome: function() {
  127. wx.switchTab({
  128. url: '/pages/homepage/homepage',
  129. });
  130. },
  131. // 切换到我的页面
  132. goToMine: function() {
  133. wx.switchTab({
  134. url: '/pages/mine/mine',
  135. });
  136. },
  137. // 添加切换水表的方法
  138. switchMeter: function() {
  139. wx.navigateTo({
  140. url: '/pages/switchMeter/switchMeter',
  141. })
  142. },
  143. navigateToHome() {
  144. wx.switchTab({
  145. url: '/pages/index/index'
  146. })
  147. },
  148. navigateToMine() {
  149. wx.switchTab({
  150. url: '/pages/mine/mine'
  151. })
  152. },
  153. // 显示键盘 - 使用微信内置键盘
  154. showCustomAmountInput: function() {
  155. // 使用微信内置的输入框
  156. wx.showModal({
  157. title: '请输入金额',
  158. placeholderText: '请输入缴费金额',
  159. editable: true,
  160. success: (res) => {
  161. if (res.confirm && res.content) {
  162. // 验证输入是否为有效数字
  163. const inputAmount = parseFloat(res.content);
  164. if (!isNaN(inputAmount) && inputAmount > 0) {
  165. this.setData({
  166. amount: inputAmount
  167. });
  168. } else {
  169. wx.showToast({
  170. title: '请输入有效金额',
  171. icon: 'none'
  172. });
  173. }
  174. }
  175. }
  176. });
  177. },
  178. // 输入数字
  179. inputNumber: function(e) {
  180. const number = e.currentTarget.dataset.number;
  181. let amount = this.data.amount;
  182. // 处理小数点
  183. if (number === '.' && amount.includes('.')) {
  184. return;
  185. }
  186. // 限制小数点后两位
  187. if (amount.includes('.') && amount.split('.')[1].length >= 2 && number !== '.') {
  188. return;
  189. }
  190. // 更新金额
  191. this.setData({
  192. amount: amount === '0' ? number : amount + number
  193. });
  194. },
  195. // 删除数字
  196. deleteNumber: function() {
  197. let amount = this.data.amount;
  198. if (amount.length <= 1) {
  199. this.setData({
  200. amount: '0'
  201. });
  202. } else {
  203. this.setData({
  204. amount: amount.substring(0, amount.length - 1)
  205. });
  206. }
  207. },
  208. // 确认输入
  209. confirmInput: function() {
  210. this.setData({
  211. showKeyboard: false
  212. });
  213. },
  214. // 添加自定义金额输入处理函数
  215. onCustomAmountInput: function(e) {
  216. let value = e.detail.value;
  217. // 只允许数字和小数点,且小数点后最多两位
  218. if (value) {
  219. // 移除非数字和小数点字符
  220. value = value.replace(/[^\d.]/g, '');
  221. // 确保只有一个小数点
  222. const parts = value.split('.');
  223. if (parts.length > 2) {
  224. value = parts[0] + '.' + parts.slice(1).join('');
  225. }
  226. // 限制小数点后最多两位
  227. if (parts.length === 2 && parts[1].length > 2) {
  228. value = parts[0] + '.' + parts[1].substring(0, 2);
  229. }
  230. // 限制总长度小于8位(包含小数点)
  231. if (value.length > 7) {
  232. value = value.substring(0, 7);
  233. }
  234. }
  235. this.setData({
  236. customAmount: value,
  237. selectedAmount: 0, // 清除其他金额选择
  238. amount: value || '0.00' // 更新显示金额
  239. });
  240. },
  241. // 输入框获得焦点
  242. onInputFocus: function() {
  243. this.setData({
  244. inputFocus: true,
  245. selectedAmount: null // 取消快捷金额的选中状态
  246. });
  247. },
  248. // 输入框失去焦点
  249. onInputBlur: function() {
  250. this.setData({
  251. inputFocus: false
  252. });
  253. },
  254. // 从接口获取支付数据
  255. fetchPaymentData: function() {
  256. // 这里是模拟数据,实际应用中应该调用真实接口
  257. // wx.request({
  258. // url: 'your-api-endpoint',
  259. // success: (res) => {
  260. // this.setData({
  261. // amountDue: res.data.amountDue,
  262. // balance: res.data.balance
  263. // });
  264. // }
  265. // });
  266. // this.setData({
  267. // amountDue: 150.00,
  268. // balance: 200.50
  269. // });
  270. },
  271. })