137 lines
3.6 KiB
JavaScript
137 lines
3.6 KiB
JavaScript
/* global wx */
|
|
|
|
const SUBSCRIBE_RESULT_STORAGE_KEY = 'genarrative:wechat-subscribe-result';
|
|
|
|
function appendSubscribeResult(url, result) {
|
|
const hashIndex = String(url || '').indexOf('#');
|
|
const baseUrl =
|
|
hashIndex >= 0 ? String(url).slice(0, hashIndex) : String(url || '');
|
|
const rawHash = hashIndex >= 0 ? String(url).slice(hashIndex + 1) : '';
|
|
const nextHash = rawHash
|
|
.split('&')
|
|
.filter((part) => part && !part.startsWith('wx_subscribe_result='))
|
|
.concat(`wx_subscribe_result=${encodeURIComponent(result)}`)
|
|
.join('&');
|
|
return `${baseUrl}#${nextHash}`;
|
|
}
|
|
|
|
function buildSubscribeResultValue(requestId, status, reason) {
|
|
const segments = [requestId, status];
|
|
if (reason) {
|
|
segments.push(encodeURIComponent(reason));
|
|
}
|
|
return segments.join(':');
|
|
}
|
|
|
|
function notifyPreviousWebView(requestId, status, reason) {
|
|
const result = buildSubscribeResultValue(requestId, status, reason);
|
|
wx.setStorageSync(SUBSCRIBE_RESULT_STORAGE_KEY, result);
|
|
}
|
|
|
|
function resolveSubscribeStatus(result, templateId) {
|
|
return result && result[templateId] === 'accept'
|
|
? 'success'
|
|
: 'skip';
|
|
}
|
|
|
|
function createSubscribeMessagePage(pageContext, options = {}) {
|
|
const templateId = String(options.templateId || '').trim();
|
|
const notifyPageResult = (methodThis, status, reason) => {
|
|
const page = pageContext ?? methodThis;
|
|
const requestId = page.requestId || '';
|
|
if (!requestId || page.hasNotifiedSubscribeResult) {
|
|
return;
|
|
}
|
|
page.hasNotifiedSubscribeResult = true;
|
|
notifyPreviousWebView(requestId, status, reason);
|
|
};
|
|
|
|
return {
|
|
data: {
|
|
title: '接收生成结果通知',
|
|
errorMessage: '',
|
|
requesting: false,
|
|
},
|
|
|
|
onLoad(query) {
|
|
const page = pageContext ?? this;
|
|
page.requestId = String(query.requestId || '');
|
|
page.hasNotifiedSubscribeResult = false;
|
|
page.autoRequest = query.autoRequest === '1' || query.autoRequest === true;
|
|
if (page.autoRequest) {
|
|
setTimeout(() => {
|
|
if (!page.hasNotifiedSubscribeResult) {
|
|
this.requestSubscribe.call(page);
|
|
}
|
|
}, 0);
|
|
}
|
|
},
|
|
|
|
notifyResult(status, reason) {
|
|
notifyPageResult(this, status, reason);
|
|
},
|
|
|
|
requestSubscribe() {
|
|
const page = pageContext ?? this;
|
|
const requestId = page.requestId || '';
|
|
if (!requestId) {
|
|
page.setData({
|
|
errorMessage: '缺少订阅请求参数。',
|
|
});
|
|
return;
|
|
}
|
|
if (!templateId) {
|
|
notifyPageResult(this, 'skip', 'missing_template_id');
|
|
wx.navigateBack();
|
|
return;
|
|
}
|
|
if (typeof wx.requestSubscribeMessage !== 'function') {
|
|
notifyPageResult(this, 'skip', 'unsupported');
|
|
wx.navigateBack();
|
|
return;
|
|
}
|
|
|
|
page.setData({
|
|
requesting: true,
|
|
errorMessage: '',
|
|
});
|
|
wx.requestSubscribeMessage({
|
|
tmplIds: [templateId],
|
|
success(result) {
|
|
notifyPageResult(
|
|
page,
|
|
resolveSubscribeStatus(result, templateId),
|
|
'',
|
|
);
|
|
wx.navigateBack();
|
|
},
|
|
fail(error) {
|
|
notifyPageResult(
|
|
page,
|
|
'skip',
|
|
error && error.errMsg ? error.errMsg : 'failed',
|
|
);
|
|
wx.navigateBack();
|
|
},
|
|
});
|
|
},
|
|
|
|
handleSkip() {
|
|
notifyPageResult(this, 'skip', 'user_skip');
|
|
wx.navigateBack();
|
|
},
|
|
|
|
onUnload() {
|
|
notifyPageResult(this, 'skip', 'page_unload');
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
SUBSCRIBE_RESULT_STORAGE_KEY,
|
|
appendSubscribeResult,
|
|
buildSubscribeResultValue,
|
|
createSubscribeMessagePage,
|
|
resolveSubscribeStatus,
|
|
};
|