lijijiaofei.js 10 KB

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