Files
MBTI_wang/miniprogram/utils/resultProfileGate.js

59 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 结果页资料门禁:未完善头像+昵称+手机时,客户端展示约 30% 预览(与后端 locked 互补)
*/
const { isReportProfileComplete } = require('./phoneAuth.js')
/** @param {WechatMiniprogram.Page.TrivialInstance} page */
function setProfileGateOnPage(page) {
const fromShare = !!(page.data && page.data.fromShare)
const gate = !fromShare && !isReportProfileComplete()
page.setData({ profileGate: gate })
}
/**
* 截取约 ratio 比例的纯文本(最少 minChars尾部省略号
* @param {string} str
* @param {number} ratio 0~1
* @param {number} minChars
*/
function slicePreviewText(str, ratio, minChars) {
const s = (str && String(str).trim()) || ''
const r = typeof ratio === 'number' && ratio > 0 && ratio < 1 ? ratio : 0.3
const minC = minChars != null ? minChars : 36
if (!s) return ''
const n = Math.max(minC, Math.floor(s.length * r))
return s.length <= n ? s : s.slice(0, n) + '…'
}
/**
* @param {string[]} list
* @param {number} ratio
*/
function slicePreviewList(list, ratio) {
const arr = Array.isArray(list) ? list : []
const r = typeof ratio === 'number' && ratio > 0 && ratio < 1 ? ratio : 0.3
if (!arr.length) return []
const take = Math.max(1, Math.ceil(arr.length * r))
return arr.slice(0, take)
}
function openTimelineShareHint() {
try {
wx.showShareMenu({ withShareTicket: true, menus: ['shareAppMessage', 'shareTimeline'] })
} catch (e) {}
wx.showModal({
title: '分享到朋友圈',
content: '已为你打开分享能力:请先点右上角「···」,选择「分享到朋友圈」。好友点链接即可测试。',
showCancel: false,
confirmText: '知道了'
})
}
module.exports = {
isReportProfileComplete,
setProfileGateOnPage,
slicePreviewText,
slicePreviewList,
openTimelineShareHint
}