实现微信公众号菜单的前缀图标

新增函数unicode2utf8_2注意此函数是从网上找来, 感谢原作者

1
2
3
4
5
public function unicode2utf8_2($str) { 
$str = '{"result_str":"' . $str . '"}';
$strarray = json_decode ( $str, true );
return $strarray ['result_str'];
}

将此函数加到D:\wamp\www\weixin\Application\Admin\Controller\WeixinMenusController.class.php中,并在相关菜单后加上用此函数转换后的emoji代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Admin\Controller;
use Think\Controller;
class WeixinMenusController extends Controller {
public function set_weixin_menus() {

// 引入微信类
$IncludePath = C ( 'IncludePath' );
require_once $IncludePath . 'weixin/' . 'wechat.class.php';

// 定义类--请注意config.php文件中的参数设置
$options = array (
'token' => C ( 'weixin_Token' ),
'appid' => C ( 'weixin_AppId' ),
'appsecret' => C ( 'weixin_AppSecret' )
);
// $weObj = new Wechat ( $options );
$weObj = new \Wechat ( $options );

// 定义菜单数组
// 菜单1及其子菜单
$menusArray [0] ['name'] = '菜单1';
$menusArray [0] ['type'] = 'click';
$menusArray [0] ['key'] = 'menu_1';
// 子菜单为消息类型
$menusArray [0] ['sub_button'] [0] ['name'] = '菜单1_1';
$menusArray [0] ['sub_button'] [0] ['type'] = 'click';
$menusArray [0] ['sub_button'] [0] ['key'] = 'menu_1_1';
// 子菜单为链接--打开百度
$menusArray [0] ['sub_button'] [1] ['name'] = '菜单1_2';
$menusArray [0] ['sub_button'] [1] ['type'] = 'view';
$menusArray [0] ['sub_button'] [1] ['key'] = 'menu_1_2';
$menusArray [0] ['sub_button'] [1] ['url'] = 'http://www.baidu.com';
// 菜单2
$menusArray [1] ['name'] = '菜单2' . $this->unicode2utf8_2 ( '\ue159' );
$menusArray [1] ['type'] = 'click';
$menusArray [1] ['key'] = 'menu_2';
// 菜单3--点击打开一个QQ网页
$menusArray [2] ['name'] = '菜单3';
$menusArray [2] ['type'] = 'view';
$menusArray [2] ['key'] = 'menu_3';
$menusArray [2] ['url'] = 'http://www.qq.com';

$menusNavArray ['button'] = $menusArray;
// 调用接口完成菜单数据提交,二十四小时生效, 你可以取消息关注再重新关注,即可看到菜单效果
$weObj->createMenu ( $menusNavArray );

$message = "已向微信公众平台提交,二十四小时后生效。";
echo $message;
}
public function unicode2utf8_2($str) {
$str = '{"result_str":"' . $str . '"}';
$strarray = json_decode ( $str, true );
return $strarray ['result_str'];
}
}

将程序代码上传到你的网站所在目录, 我以temp.abis.com.cn为例
上传后,在浏览器地址栏中输入http://temp.abis.com.cn/index.php/Admin/WeixinMenus/set_weixin_menus

回车, 这时就会将你的菜单提交上去。因为微信缓存, 二十四小时后才有效,你可以取消关注,重新关注, 即可看到菜单。

下图是已经搞好的成品(并非是上面产生的菜单)