// pages/usercenter/balance/index.js import { fetchAccountInfo, fetchAccountLogs, accountApplyWithdraw, fetchTransferAuthorization } from '../../../services/usercenter/fetchBalance'; Page({ /** * 页面的初始数据 */ data: { totalAmount: 0, accountInfo: null, tab: 'all', list: [], page: 1, pageSize: 10, loadMoreStatus: 0, showWithdrawDialog: false, withdrawAmount: '', withdrawName: '', // 提现规则:默认折叠,仅展示核心摘要 rulesExpanded: false, briefRules: ['单笔 0-200 元', '每日限 10 次', '单日限额 2000 元', '1-3 个工作日到账'], ruleList: [ { title: '单笔额度', desc: '单笔 0-200 元,以「可提现余额」为准' }, { title: '提现次数', desc: '每个自然日最多可发起 10 次提现申请' }, { title: '单日限额', desc: '同一自然日累计提现金额不超过 2000 元,超出请次日再试' }, { title: '到账方式', desc: '转账至本人微信零钱,需填写与微信实名一致的姓名' }, { title: '免确认收款', desc: '提现需完成免确认收款授权,授权后审核通过将直接到账零钱' }, { title: '到账时间', desc: '审核通过后 1-3 个工作日内到账,节假日顺延' }, { title: '审核说明', desc: '工作日统一审核处理,审核期间该笔金额将暂时冻结' }, { title: '驳回处理', desc: '被驳回的提现金额将自动退回可提现余额,可重新发起' }, ], }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.loadAccountInfo(); this.loadList(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { if (this.data.loadMoreStatus === 0) { this.setData({ loadMoreStatus: 1 }); this.loadList(); } }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, // Tab切换 onTabChange(e) { const value = e.currentTarget.dataset.value; if (value === this.data.tab) return; this.setData({ tab: value, list: [], page: 1, loadMoreStatus: 'more' }, () => { this.loadList(); }); }, loadAccountInfo() { fetchAccountInfo().then((res) => { console.log(res) let accountInfo = res.data; this.setData({ accountInfo, totalAmount: (parseFloat(accountInfo.amount) + parseFloat(accountInfo.freeze)).toFixed(2) }); }); }, // 加载明细数据(模拟) loadList() { let { page, pageSize, list, tab } = this.data; let params = { tab: tab, page: page, accountType: 'amount', pageSize: pageSize, }; fetchAccountLogs(params).then((res) => { console.log(res) let newList = res.data.data; let hasMore = res.data.current_page < res.data.last_page; this.setData({ list: list.concat(newList), page: page + 1, loadMoreStatus: hasMore ? 0 : 2 }); }); }, // 提现弹窗 onWithdraw() { this.setData({ showWithdrawDialog: true }); }, // 展开/收起提现规则 onToggleRules() { this.setData({ rulesExpanded: !this.data.rulesExpanded }); }, onWithdrawAmountInput(e) { this.setData({ withdrawAmount: e.detail.value }); }, onWithdrawNameInput(e) { this.setData({ withdrawName: e.detail.value }); }, onConfirmWithdraw() { //提交前提现先确保完成免确认收款授权(授权后审核通过即可免确认直接到账零钱) this.ensureTransferAuthorized() .then(() => this.submitWithdraw()) .catch((msg) => { const title = msg && msg.message ? msg.message : (typeof msg === 'string' && msg ? msg : '未完成授权,已取消提现'); wx.showToast({ title: title, icon: 'none', duration: 2500 }); }); }, // 确保完成免确认收款授权:已授权直接通过;未授权拉起微信授权页 ensureTransferAuthorized() { return fetchTransferAuthorization().then((res) => { if (res.code != 1) { throw new Error(res.msg || '获取授权状态失败'); } const data = res.data || {}; if (data.authorized) { return; } //package_info 仅在授权状态为 WAIT_USER_CONFIRM(待用户确认)时才返回,缺失则无法拉起 if (!data.package_info || !data.mch_id || !data.app_id) { throw new Error('免确认收款授权参数缺失,请稍后重试'); } //文档要求:调起前须判断微信客户端/基础库是否支持 requestMerchantTransfer if (!wx.canIUse('requestMerchantTransfer')) { throw new Error('当前微信版本过低,请更新至最新版本后再授权'); } //拉起授权用的 appId 必须与发起授权接口传入的 appid 为同一个 let appId = data.app_id; const miniProgram = wx.getAccountInfoSync ? (wx.getAccountInfoSync().miniProgram || {}) : {}; if (miniProgram.appId && miniProgram.appId !== data.app_id) { throw new Error('授权商户AppID与当前小程序不一致,无法拉起授权'); } if (miniProgram.appId) { appId = miniProgram.appId; } return this.requestMerchantTransfer({ mchId: String(data.mch_id), appId: appId, package: data.package_info, }); }); }, // 拉起微信免确认收款授权页(requestMerchantTransfer) requestMerchantTransfer(params) { return new Promise((resolve, reject) => { //在用户点击手势链路中调起:先二次确认,避免异步请求后直接调起被客户端拒绝 wx.showModal({ title: '免确认收款授权', content: '提现前需先开通免确认收款授权,点击「去授权」在微信授权页完成确认后即可提现。', confirmText: '去授权', cancelText: '取消', success: (modalRes) => { if (!modalRes.confirm) { reject('已取消授权,无法提现'); return; } try { wx.requestMerchantTransfer(Object.assign({}, params, { success: (res) => { //success 仅代表授权页展示成功,不代表用户已确认授权,最终以微信异步回调为准 console.log('requestMerchantTransfer success', res); const errMsg = (res && (res.err_msg || res.errMsg)) || ''; if (errMsg && errMsg !== 'requestMerchantTransfer:ok') { reject(errMsg === 'requestMerchantTransfer:cancel' ? '已取消授权,无法提现' : '拉起授权失败:' + errMsg); return; } wx.showToast({ title: '请在授权页完成确认', icon: 'none', duration: 2000 }); resolve(); }, fail: (err) => { console.log('requestMerchantTransfer fail', err); const errMsg = (err && (err.errMsg || err.err_msg)) || ''; reject(errMsg ? '拉起授权失败:' + errMsg : '拉起授权失败,请更新微信后重试'); }, })); } catch (e) { reject('当前微信版本不支持免确认收款授权,请更新后重试'); } }, fail: () => reject('已取消授权,无法提现'), }); }); }, // 原提现提交逻辑 submitWithdraw() { const { withdrawName, withdrawAmount } = this.data; let params = { 'name': withdrawName, 'amount': withdrawAmount, }; accountApplyWithdraw(params).then((res) => { console.log(res) if (res.code == 1) { this.setData({ showWithdrawDialog: false, withdrawAmount: '', withdrawName: '' }); wx.showModal({ title: '提示', showCancel: false, content: res.msg, success: (re) => { if (re.confirm) { //提现之后,刷新数据 this.loadAccountInfo(); this.loadList(); console.log('用户点击确定'); } } }); } else { wx.showToast({ title: res.msg, icon: 'none' }); } }); }, onCancelWithdraw() { this.setData({ showWithdrawDialog: false }); } })