Skip to content

Commit 3a826ba

Browse files
committed
add menu & template
1 parent 681542c commit 3a826ba

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

src/Wechat.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ class Wechat {
2626
const USER_INFO_URI = 'cgi-bin/user/info';
2727
const USER_LIST_URI = 'cgi-bin/user/get';
2828
const USER_LIST_INFO_URI = 'cgi-bin/user/info/batchget';
29+
2930

31+
const MENU_INFO = 'cgi-bin/get_current_selfmenu_info';
32+
const MENU_CREATE = 'cgi-bin/menu/create';
33+
const TEMPLATE_MESSAGE_SEND = 'cgi-bin/message/template/send';
34+
3035
const AUTH_URI = 'connect/oauth2/authorize';
3136
const AUTH_ACCESS_TOKEN_URI = 'sns/oauth2/access_token';
3237
const AUTH_USER_URI = 'sns/userinfo';
@@ -218,6 +223,73 @@ public function get_auth_user($openid , $access_token , $lang = 'zh_CN'){
218223

219224
return $this->return_data($data);
220225
}
226+
227+
/**
228+
* 查询自定义菜单
229+
* @return boolean|\Org\Com\unknown
230+
*/
231+
function get_menu_info(){
232+
233+
$url = self::API_URI . self::MENU_INFO;
234+
$param['access_token'] = $this->access_token;
235+
$menu_url = $this->create_url($url, $param);
236+
237+
$data = $this->http_get($menu_url);
238+
239+
return $this->return_data($data);
240+
}
241+
242+
/**
243+
* 创建自定义菜单
244+
* @param unknown $menu
245+
* @return boolean|\niklaslu\unknown
246+
*/
247+
function create_menu($menu){
248+
249+
$url = self::API_URI . self::MENU_CREATE;
250+
$param['access_token'] = $this->access_token;
251+
$menu_url = $this->create_url($url, $param);
252+
253+
$fields = self::json_encode($menu);
254+
$data = $this->http_post($menu_url, $fields);
255+
return $this->return_data($data);
256+
257+
}
258+
259+
/**
260+
* 发送模板信息
261+
* @param unknown $openid
262+
* @param unknown $template_id
263+
* @param unknown $template_url
264+
* @param unknown $datas
265+
* @return boolean|\niklaslu\unknown
266+
*/
267+
public function send_template_msg($openid , $template_id , $template_url , $datas = NULL){
268+
269+
$url = self::API_URI . self::TEMPLATE_MESSAGE_SEND;
270+
$param['access_token'] = $this->get_access_token();
271+
272+
$template_send_url = $this->create_url($url, $param);
273+
274+
$fields['touser'] = $openid;
275+
$fields['template_id'] = $template_id;
276+
$fields['url'] = $template_url;
277+
$data = array();
278+
foreach ($datas as $k=>$v){
279+
$d['value'] = $v;
280+
$d['color'] = '#173177';
281+
282+
$data[$k] = $d;
283+
}
284+
if ($data){
285+
$fields['data'] = $data;
286+
}
287+
288+
$fields = json_encode($fields , true);
289+
$res = $this->http_post($template_send_url, $fields);
290+
291+
return $this->return_data($res);
292+
}
221293
/**
222294
* 返回data
223295
* @param array $data
@@ -325,6 +397,55 @@ public function http_post($url, $fields, $data_type='json') {
325397
}
326398
}
327399

400+
/**
401+
* 微信api不支持中文转义的json结构
402+
* @param array $arr
403+
*/
404+
static function json_encode($arr) {
405+
if (count($arr) == 0) return "[]";
406+
$parts = array ();
407+
$is_list = false;
408+
//Find out if the given array is a numerical array
409+
$keys = array_keys ( $arr );
410+
$max_length = count ( $arr ) - 1;
411+
if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
412+
$is_list = true;
413+
for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
414+
if ($i != $keys [$i]) { //A key fails at position check.
415+
$is_list = false; //It is an associative array.
416+
break;
417+
}
418+
}
419+
}
420+
foreach ( $arr as $key => $value ) {
421+
if (is_array ( $value )) { //Custom handling for arrays
422+
if ($is_list)
423+
$parts [] = self::json_encode ( $value ); /* :RECURSION: */
424+
else
425+
$parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
426+
} else {
427+
$str = '';
428+
if (! $is_list)
429+
$str = '"' . $key . '":';
430+
//Custom handling for multiple data types
431+
if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
432+
$str .= $value; //Numbers
433+
elseif ($value === false)
434+
$str .= 'false'; //The booleans
435+
elseif ($value === true)
436+
$str .= 'true';
437+
else
438+
$str .= '"' . addslashes ( $value ) . '"'; //All other things
439+
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
440+
$parts [] = $str;
441+
}
442+
}
443+
$json = implode ( ',', $parts );
444+
if ($is_list)
445+
return '[' . $json . ']'; //Return numerical JSON
446+
return '{' . $json . '}'; //Return associative JSON
447+
}
448+
328449
public function getCacheAccessToken(){
329450

330451

0 commit comments

Comments
 (0)