Files
mama-box/js/core.js

89 lines
3.6 KiB
JavaScript
Raw Permalink 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.
/* 妈妈盒子 · MPA 共享层(无框架,普通 <script>,兼容微信 webview)
window.MB:openid 解析、带 openid 的 fetch、hash 深链、缓存、UI 工具 */
(function (global) {
'use strict';
var API = '/api/mp/public/mama-box';
function readHash(name) {
try {
var h = location.hash.slice(1);
var m = h.match(new RegExp('(?:^|&)' + name + '=([^&]*)'));
if (m) return decodeURIComponent(m[1]);
m = location.search.match(new RegExp('[?&]' + name + '=([^&]*)'));
if (m) return decodeURIComponent(m[1]);
} catch (e) {}
return '';
}
var MB = {
openid: function () { return readHash('openid'); },
param: readHash,
/* 拼 hash 深链:page.html#openid=x&key=...(微信 webview 内 hash 不丢) */
buildUrl: function (page, params) {
var parts = [];
var oid = MB.openid();
if (oid) parts.push('openid=' + encodeURIComponent(oid));
var p = params || {};
Object.keys(p).forEach(function (k) { parts.push(k + '=' + encodeURIComponent(p[k])); });
return page + '.html' + (parts.length ? '#' + parts.join('&') : '');
},
go: function (page, params) { location.href = MB.buildUrl(page, params); },
/* 带 openid 的 GET,解包 {code,message,result} → result 或 null(失败回退) */
fetch: function (path, params) {
var oid = MB.openid();
var qs = [];
var p = params || {};
Object.keys(p).forEach(function (k) { qs.push(k + '=' + encodeURIComponent(p[k])); });
if (oid) qs.push('openid=' + encodeURIComponent(oid));
var url = API + path + (qs.length ? '?' + qs.join('&') : '');
return fetch(url)
.then(function (r) { return r.ok ? r.json() : null; })
.then(function (j) { return (j && j.code === 200) ? j.result : null; })
.catch(function () { return null; });
},
get: function (path, params) { return MB.fetch(path, params); },
/* 内存缓存(同一页内避免重复请求) */
cache: {},
cached: function (key, fn) {
if (key in MB.cache) return Promise.resolve(MB.cache[key]);
return fn().then(function (v) { MB.cache[key] = v; return v; });
},
pregnancy: function () { return MB.cached('pregnancy', function () { return MB.fetch('/pregnancy'); }); },
profile: function () { return MB.cached('profile', function () { return MB.fetch('/profile'); }); },
/* UI 工具 */
toast: function (msg) {
var t = document.getElementById('toast');
if (!t) return;
t.textContent = msg; t.classList.add('show');
clearTimeout(MB._tt); MB._tt = setTimeout(function () { t.classList.remove('show'); }, 1800);
},
haptic: function () { if (navigator.vibrate) navigator.vibrate(8); },
ripple: function (ev) {
var t = ev.target.closest('.ripple');
if (!t) return;
var r = t.getBoundingClientRect();
var size = Math.max(r.width, r.height) * 2.1;
var sp = document.createElement('span');
sp.className = 'rp';
sp.style.cssText = 'width:' + size + 'px;height:' + size + 'px;left:' + (ev.clientX - r.left) + 'px;top:' + (ev.clientY - r.top) + 'px';
t.appendChild(sp); setTimeout(function () { sp.remove(); }, 600);
},
/* 孕周/日期工具(对齐原型) */
WEEK: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
weekTxt: function (week, day) {
return '孕 ' + week + ' 周' + (day ? ' +' + day + ' 天' : '');
},
fmtFull: function (ms) {
var d = new Date(ms);
return d.getFullYear() + '年' + (d.getMonth() + 1) + '月' + d.getDate() + '日';
}
};
global.MB = MB;
})(window);