lijijiaofei.js 7.1 KB

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