微擎通过jssdk自定义分享设置方法
在做微信开发时自定义分享设置是一个很常见并且实力的功能需求。通常地会把设置的标题,描述,链接,图片等设置集成在参数设置里面。以下是模板层的示例代码:
<html>
<head>
<title>测试 register_jssdk</title>
{php echo register_jssdk(true);} <!-- 放到其他引入的 js 之前 -->
</head>
<body>
<script>
wx.ready(function () {
sharedata = {
title: '微信JS-SDK Demo',
desc: '微信JS-SDK,帮助第三方为用户提供更优质的移动web服务',
link: 'http://demo.open.weixin.qq.com/jssdk/',//本域链接
imgUrl: 'http://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRt8Qia4lv7k3M9J1SKqKCImxJCt7j9rHYicKDI45jRPBxdzdyREWnk0ia0N5TMnMfth7SdxtzMvVgXg/0',//本域链接
/*
success: function(){
alert('success');
},
cancel: function(){
alert('cancel');
}
*/
};
wx.onMenuShareAppMessage(sharedata);
wx.onMenuShareTimeline(sharedata);
wx.onMenuShareQQ(sharedata);
wx.onMenuShareQZone(sharedata);
wx.onMenuShareWeibo(sharedata);
//更多调用,查官方文档
});
</script>
</body>
</html>记录一个示例:template
{php echo register_jssdk();}
<script type="text/javascript">
wx.ready(function () {
sharedata = {
title: "{php echo $this->module['config']['stitle']}",
desc: "{php echo $this->module['config']['sinfo']}",
link: "{php echo $_W['siteroot'].$this->createMobileUrl('into')}",
imgUrl: "{php echo $_W['attachurl'].$this->module['config']['simg']}",
};
wx.onMenuShareAppMessage(sharedata);
wx.onMenuShareTimeline(sharedata);
wx.onMenuShareQQ(sharedata);
wx.onMenuShareQZone(sharedata);
wx.onMenuShareWeibo(sharedata);
});
</script>module.php
public function settingsDisplay($settings) {
global $_W, $_GPC;
load()->func('tpl');
//点击模块设置时将调用此方法呈现模块设置页面,$settings 为模块设置参数, 结构为数组。这个参数系统针对不同公众账号独立保存。
//在此呈现页面中自行处理post请求并保存设置参数(通过使用$this->saveSettings()来实现)
if(checksubmit('setting')) {
$this->saveSettings($_GPC['data']);
message('修改成功','refresh', 'success');
}else{
include $this->template('setting');
}
}setting.html
{template 'common/header'}
{php load()->func('tpl');}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">吸粉设置</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">二维码</label>
<div class="col-sm-10">
{php echo tpl_form_field_image('data',$this->module['config']['ewm'])}
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label"></label>
<div class="col-sm-10">
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>注意:</strong> 请将二维码图片尺寸调整为120*120后再上传,否则会错位
</div>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">分享标题</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="data" value="{php echo $this->module['config']['stitle']}"/>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">分享描述</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="data" value="{php echo $this->module['config']['sinfo']}"/>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">分享图片</label>
<div class="col-sm-10">
{php echo tpl_form_field_image('data',$this->module['config']['simg'])}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="保存" name="setting" class="btn btn-default" />
<input type="hidden" name="token" value="{$_W['token']}" />
</div>
</div>
</form>
</div>
</div>
{template 'common/footer'}需要注意的是,register_jssdk()会自动识别,或为普通公众号可以在后台设置借权操作。分享的链接与图片地址必须为本域下的,否则无法显示。
页:
[1]