From f55e4a95505373e3c4a708a9e5592597380dc34b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=96=E5=8F=AB?= <392494244@qq.com> Date: Thu, 7 Dec 2023 09:59:32 +0800 Subject: [PATCH] 1 --- app/common.php | 240 +++--- extend/hema/wechat/Driver.php | 1289 +++++++++++++++++---------------- extend/hema/wechat/Index.php | 79 +- 3 files changed, 823 insertions(+), 785 deletions(-) diff --git a/app/common.php b/app/common.php index 9e97169..04a1053 100755 --- a/app/common.php +++ b/app/common.php @@ -2,16 +2,18 @@ /** * 应用公共函数库文件 */ + use think\facade\Request; use hema\wechat\Driver as Wechat; use app\common\model\User as UserModel; use app\common\model\Setting as SettingModel; use app\common\model\food\ShopClerk as ShopClerkModel; + /** * 生成数字验证码 */ if (!function_exists('is_json')) { - function is_json($string) + function is_json($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); @@ -21,11 +23,11 @@ if (!function_exists('is_json')) { * 生成数字验证码 */ if (!function_exists('get_captcha')) { - function get_captcha($length=4) + function get_captcha($length = 4) { $str = ''; - for($n=0;$n<$length;$n++){ - $str .= (string)rand(0,9); + for ($n = 0; $n < $length; $n++) { + $str .= (string)rand(0, 9); } return $str; } @@ -34,16 +36,16 @@ if (!function_exists('get_captcha')) { * json_encode */ if (!function_exists('hema_json')) { - function hema_json($data) + function hema_json($data) { - return json_encode($data,JSON_UNESCAPED_UNICODE); + return json_encode($data, JSON_UNESCAPED_UNICODE); } } /** -* 验证手机号是否正确 -*/ + * 验证手机号是否正确 + */ if (!function_exists('is_phone')) { - function is_phone($phone) + function is_phone($phone) { if (!is_numeric($phone)) { return false; @@ -88,6 +90,7 @@ function base_url() } return $baseUrl; } + /** * 获取当前域名 */ @@ -96,6 +99,7 @@ function domain() $request = Request::instance(); return $request->host(); } + /** * 获取当前uploads目录访问地址 */ @@ -103,6 +107,7 @@ function uploads_url() { return base_url() . 'uploads/'; } + /** * 获取当前的应用名称 */ @@ -110,6 +115,7 @@ function app_name() { return app('http')->getName(); } + /** * 获取web根目录 */ @@ -122,6 +128,7 @@ function web_path() } return $webPath; } + /** * 获取当前url的子目录路径 */ @@ -135,32 +142,34 @@ function root_url() } return $rootUrl; } + /** * 清空缓存目录 */ function deldir($path) { //如果是目录则继续 - if(is_dir($path)){ + if (is_dir($path)) { //扫描一个文件夹内的所有文件夹和文件并返回数组 $p = scandir($path); - foreach($p as $val){ + foreach ($p as $val) { //排除目录中的.和.. - if($val !="." && $val !=".."){ + if ($val != "." && $val != "..") { //如果是目录则递归子目录,继续操作 - if(is_dir($path.$val)){ + if (is_dir($path . $val)) { //子目录中操作删除文件夹和文件 - deldir($path.$val.'/'); + deldir($path . $val . '/'); //目录清空后删除空文件夹 - @rmdir($path.$val.'/'); - }else{ + @rmdir($path . $val . '/'); + } else { //如果是文件直接删除 - unlink($path.$val); + unlink($path . $val); } } } } } + /** * 下划线转驼峰 */ @@ -169,6 +178,7 @@ function camelize($uncamelized_words, $separator = '_') $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words)); return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator); } + /** * 驼峰转下划线 */ @@ -176,6 +186,7 @@ function uncamelize($camelCaps, $separator = '_') { return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps)); } + /** * 隐藏手机号中间四位 13012345678 -> 130****5678 * @param string $phone 手机号 @@ -222,7 +233,7 @@ if (!function_exists('format_time')) { } /** * 将时间戳转换为日期时间 - * @param int $time 时间戳 + * @param int $time 时间戳 * @param string $format 日期时间格式 * @return string */ @@ -290,12 +301,12 @@ if (!function_exists('convert_left_slash')) { } /** * 将xml转为array -*/ + */ if (!function_exists('_xml_to_arr')) { - function _xml_to_arr($xml) + function _xml_to_arr($xml) { - $res = @simplexml_load_string ( $xml,NULL, LIBXML_NOCDATA ); - $res = json_decode ( json_encode ( $res), true ); + $res = @simplexml_load_string($xml, NULL, LIBXML_NOCDATA); + $res = json_decode(json_encode($res), true); return $res; } } @@ -320,6 +331,7 @@ function array_merge_multiple($array1, $array2) } return $data; } + /** * 判断是否为自定义索引数组 */ @@ -336,13 +348,13 @@ if (!function_exists('is_assoc')) { if (!function_exists('arr_sort')) { function arr_sort(array $data, string $field, string $order = 'desc') { - foreach($data as $val){ - $key_arrays[]=$val[$field]; + foreach ($data as $val) { + $key_arrays[] = $val[$field]; } - if($order == 'desc'){ - array_multisort($key_arrays,SORT_DESC,SORT_NUMERIC,$data); - }else{ - array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$data); + if ($order == 'desc') { + array_multisort($key_arrays, SORT_DESC, SORT_NUMERIC, $data); + } else { + array_multisort($key_arrays, SORT_ASC, SORT_NUMERIC, $data); } return $data; } @@ -365,9 +377,9 @@ if (!function_exists('array_sort')) { } reset($key_value); foreach ($key_value as $k => $v) { - if(is_int($k)){ + if (is_int($k)) { $new_array[] = $arr[$k]; - }else{ + } else { $new_array[$k] = $arr[$k]; } } @@ -380,26 +392,23 @@ if (!function_exists('array_sort')) { *keyid 需要判断是否重复的项目 */ if (!function_exists('array_repeat')) { - function array_repeat($array,$keyid) + function array_repeat($array, $keyid) { - $array =array_values($array); + $array = array_values($array); //提取需要判断的项目变成一维数组 - $a = array_tq($array,$keyid); - + $a = array_tq($array, $keyid); + //去除一维数组重复值 - $a =array_unique($a); + $a = array_unique($a); //提取二维数组项目值 - foreach($array[0] AS$key=>$value) - { - $akey[] =$key; + foreach ($array[0] AS $key => $value) { + $akey[] = $key; } //重新拼接二维数组 - foreach($akey AS$key=>$value) - { - $b = array_tq($array,$value); - foreach($a AS$key2=>$value2) - { - $c[$key2][$value] =$b[$key2]; + foreach ($akey AS $key => $value) { + $b = array_tq($array, $value); + foreach ($a AS $key2 => $value2) { + $c[$key2][$value] = $b[$key2]; } } return $c; @@ -407,10 +416,9 @@ if (!function_exists('array_repeat')) { } //提取二维数组项目 if (!function_exists('array_tq')) { - function array_tq($array, $aval="") + function array_tq($array, $aval = "") { - foreach($array AS $key => $value) - { + foreach ($array AS $key => $value) { $result[] = $value[$aval]; } return $result; @@ -419,29 +427,29 @@ if (!function_exists('array_tq')) { /** * POST请求 * @param [type] $url [description] - * @param array $data [description] - * @param array $headers [description] + * @param array $data [description] + * @param array $headers [description] * @return [type] [description] */ if (!function_exists('http_post')) { - function http_post($url,$data = array(),$headers=array()) + function http_post($url, $data = array(), $headers = array()) { $curl = curl_init(); - if( count($headers) >= 1 ){ + if (count($headers) >= 1) { curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); - - if (count($data) >= 1 AND !isset($data['media'])){ - $data = json_encode($data,JSON_UNESCAPED_UNICODE); - }elseif(count($data) == 0){ + + if (count($data) >= 1 AND !isset($data['media'])) { + $data = json_encode($data, JSON_UNESCAPED_UNICODE); + } elseif (count($data) == 0) { $data = '{}'; } curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); - + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); @@ -515,10 +523,10 @@ if (!function_exists('get_guid_v4')) { * 商户管理员 - 账户资金变动提醒 - 公众号 */ if (!function_exists('sand_account_change_msg')) { - function sand_account_change_msg($title,$pay,$surplus,$user_id,$applet_id = 0) - { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['balance'])){ + function sand_account_change_msg($title, $pay, $surplus, $user_id, $applet_id = 0) + { + $webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号 + if (empty($webtplmsg['balance'])) { return true; } $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 @@ -539,15 +547,15 @@ if (!function_exists('sand_account_change_msg')) { 'color' => '#173177' ], 'keyword2' => [ - 'value' => '¥'.$pay.'元',//交易金额 + 'value' => '¥' . $pay . '元',//交易金额 'color' => '#173177' ], 'keyword3' => [ - 'value' => date('Y/m/d H:i:s',time()),//交易时间 + 'value' => date('Y/m/d H:i:s', time()),//交易时间 'color' => '#173177' ], 'keyword4' => [ - 'value' => '¥'.$surplus.'元',//账户余额 + 'value' => '¥' . $surplus . '元',//账户余额 'color' => '#173177' ], 'remark' => [ @@ -556,18 +564,18 @@ if (!function_exists('sand_account_change_msg')) { ] ] ]; - return send_wechat_msg($msg,$user_id); + return send_wechat_msg($msg, $user_id); } } - + /** * 商户管理员 - 申请受理通知 - 公众号 */ if (!function_exists('sand_apply_msg')) { - function sand_apply_msg($name,$title,$user_id,$applet_id = 0) + function sand_apply_msg($name, $title, $user_id, $applet_id = 0) { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['apply'])){ + $webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号 + if (empty($webtplmsg['apply'])) { return true; } $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 @@ -592,7 +600,7 @@ if (!function_exists('sand_apply_msg')) { 'color' => '#173177' ], 'keyword3' => [ - 'value' => date('Y/m/d H:i:s',time()),//时间 + 'value' => date('Y/m/d H:i:s', time()),//时间 'color' => '#173177' ], 'remark' => [ @@ -601,18 +609,18 @@ if (!function_exists('sand_apply_msg')) { ] ] ]; - return send_wechat_msg($msg,$user_id); + return send_wechat_msg($msg, $user_id); } } - + /** * 商户管理员 - 申请状态更新通知 - 公众号 */ if (!function_exists('sand_examine_msg')) { - function sand_examine_msg($id,$title,$status,$user_id,$applet_id=0) + function sand_examine_msg($id, $title, $status, $user_id, $applet_id = 0) { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['examine'])){ + $webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号 + if (empty($webtplmsg['examine'])) { return true; } $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 @@ -637,7 +645,7 @@ if (!function_exists('sand_examine_msg')) { 'color' => '#173177' ], 'keyword3' => [ - 'value' => date('Y/m/d H:i:s',time()),//申请时间 + 'value' => date('Y/m/d H:i:s', time()),//申请时间 'color' => '#173177' ], 'keyword4' => [ @@ -650,17 +658,17 @@ if (!function_exists('sand_examine_msg')) { ] ] ]; - return send_wechat_msg($msg,$user_id); + return send_wechat_msg($msg, $user_id); } } /** * 商户管理员 - 试用申请成功通知 - 公众号 */ if (!function_exists('sand_testing_msg')) { - function sand_testing_msg($title,$time,$user_id,$applet_id = 0) + function sand_testing_msg($title, $time, $user_id, $applet_id = 0) { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['testing'])){ + $webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号 + if (empty($webtplmsg['testing'])) { return true; } $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 @@ -681,7 +689,7 @@ if (!function_exists('sand_testing_msg')) { 'color' => '#173177' ], 'keyword2' => [ - 'value' => date('Y/m/d H:i:s',$time),//有效期至 + 'value' => date('Y/m/d H:i:s', $time),//有效期至 'color' => '#173177' ], 'remark' => [ @@ -690,17 +698,17 @@ if (!function_exists('sand_testing_msg')) { ] ] ]; - return send_wechat_msg($msg,$user_id); + return send_wechat_msg($msg, $user_id); } } /** * 商户骑手 - 抢单提醒 - 公众号 */ if (!function_exists('sand_grab_msg')) { - function sand_grab_msg($order,$applet_id = 0) + function sand_grab_msg($order, $applet_id = 0) { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['grab'])){ + $webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号 + if (empty($webtplmsg['grab'])) { return true; } $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 @@ -739,10 +747,10 @@ if (!function_exists('sand_grab_msg')) { ] ]; $model = new ShopClerkModel; - $clerk = $model->getAll($order['shop_id'],30); - for($n=0;$n 0){ - send_wechat_msg($msg,$clerk[$n]['user_id']); + $clerk = $model->getAll($order['shop_id'], 30); + for ($n = 0; $n < sizeof($clerk); $n++) { + if ($clerk[$n]['user_id'] > 0) { + send_wechat_msg($msg, $clerk[$n]['user_id']); } } } @@ -751,10 +759,10 @@ if (!function_exists('sand_grab_msg')) { * 商户店长 - 退款申请通知 - 公众号 */ if (!function_exists('sand_refund_msg')) { - function sand_refund_msg($order,$applet_id = 0) + function sand_refund_msg($order, $applet_id = 0) { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['refund'])){ + $webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号 + if (empty($webtplmsg['refund'])) { return true; } $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 @@ -779,11 +787,11 @@ if (!function_exists('sand_refund_msg')) { 'color' => '#173177' ], 'keyword3' => [ - 'value' => '¥'.$order['refund_price'].'元',//退款金额 + 'value' => '¥' . $order['refund_price'] . '元',//退款金额 'color' => '#173177' ], 'keyword4' => [ - 'value' => date('Y/m/d H:i:s',time()),//退款时间 + 'value' => date('Y/m/d H:i:s', time()),//退款时间 'color' => '#173177' ], 'remark' => [ @@ -793,10 +801,10 @@ if (!function_exists('sand_refund_msg')) { ] ]; $model = new ShopClerkModel; - $clerk = $model->getAll($order['shop_id'],20); - for($n=0;$n 0){ - send_wechat_msg($msg,$clerk[$n]['user_id']); + $clerk = $model->getAll($order['shop_id'], 20); + for ($n = 0; $n < sizeof($clerk); $n++) { + if ($clerk[$n]['user_id'] > 0) { + send_wechat_msg($msg, $clerk[$n]['user_id']); } } } @@ -805,18 +813,18 @@ if (!function_exists('sand_refund_msg')) { * 商户店长 新订单提醒模板消息 - 公众号 */ if (!function_exists('sand_new_order_msg')) { - function sand_new_order_msg($order,$applet_id = 0) + function sand_new_order_msg($order, $applet_id = 0) { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['new_order'])){ + $webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号 + if (empty($webtplmsg['new_order'])) { return true; } $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 - if($order['order_mode']['value']==30){ + if ($order['order_mode']['value'] == 30) { $title = '自取'; - }elseif($order['order_mode']['value']==20){ + } elseif ($order['order_mode']['value'] == 20) { $title = '外卖'; - }else{ + } else { $title = '堂食'; } $msg = [ @@ -828,7 +836,7 @@ if (!function_exists('sand_new_order_msg')) { ], 'data' => [ 'first' => [ - 'value' => '收到一个新的'.$title.'订单', + 'value' => '收到一个新的' . $title . '订单', 'color' => '#173177' ], 'keyword1' => [ @@ -836,7 +844,7 @@ if (!function_exists('sand_new_order_msg')) { 'color' => '#173177' ], 'keyword2' => [ - 'value' => '¥'.$order['pay_price'].'元', + 'value' => '¥' . $order['pay_price'] . '元', 'color' => '#173177' ], 'keyword3' => [ @@ -850,10 +858,10 @@ if (!function_exists('sand_new_order_msg')) { ] ]; $model = new ShopClerkModel; - $clerk = $model->getAll($order['shop_id'],20); - for($n=0;$n 0){ - send_wechat_msg($msg,$clerk[$n]['user_id']); + $clerk = $model->getAll($order['shop_id'], 20); + for ($n = 0; $n < sizeof($clerk); $n++) { + if ($clerk[$n]['user_id'] > 0) { + send_wechat_msg($msg, $clerk[$n]['user_id']); } } } @@ -862,12 +870,12 @@ if (!function_exists('sand_new_order_msg')) { * 发布模板消息 - 公众号 */ if (!function_exists('send_wechat_msg')) { - function send_wechat_msg($msg, $user_id, $applet_id=0) + function send_wechat_msg($msg, $user_id, $applet_id = 0) { $user = UserModel::get($user_id); $msg['touser'] = $user['open_id']; $wx = new Wechat; - $wx->sendWechatMsg($applet_id,$msg); + $wx->sendWechatMsg($applet_id, $msg); return true; } } @@ -875,7 +883,7 @@ if (!function_exists('copydirs')) { /** * 复制文件夹 * @param string $source 源文件夹 - * @param string $dest 目标文件夹 + * @param string $dest 目标文件夹 */ function copydirs($source, $dest) { @@ -890,7 +898,7 @@ if (!function_exists('copydirs')) { ) { if ($item->isDir()) { $sontDir = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); - + if (!is_dir($sontDir)) { mkdir($sontDir, 0777, true); } @@ -903,8 +911,8 @@ if (!function_exists('copydirs')) { if (!function_exists('rmdirs')) { /** * 删除文件夹 - * @param string $dirname 目录 - * @param bool $withself 是否删除自身 + * @param string $dirname 目录 + * @param bool $withself 是否删除自身 * @return boolean */ function rmdirs($dirname, $withself = true) @@ -934,7 +942,7 @@ function write_log($values, $dir) if (is_array($values)) $values = print_r($values, true); // 日志内容 - $content = '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . $values . PHP_EOL . PHP_EOL; + $content = '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . json_encode($values, 256) . PHP_EOL . PHP_EOL; try { // 文件路径 $filePath = $dir . '/logs/'; diff --git a/extend/hema/wechat/Driver.php b/extend/hema/wechat/Driver.php index dcb0649..91c27fe 100755 --- a/extend/hema/wechat/Driver.php +++ b/extend/hema/wechat/Driver.php @@ -1,4 +1,5 @@ isp = $isp; - if($isp){ + + /** + * 构造函数 + */ + public function __construct($isp = false) + { + $this->isp = $isp; + if ($isp) { $value = Setting::getItem('wxopen'); - $value['component_access_token'] = Cache::get('component_access_token',''); - $this->config = $value; - } - } - - /** + $value['component_access_token'] = Cache::get('component_access_token', ''); + $this->config = $value; + } + } + + /** * 获取session_key - */ + */ public function getSessionKey(string $code, int $applet_id) { - if(!$applet = Applet::get($applet_id)){ + if (!$applet = Applet::get($applet_id)) { $this->error = 'applet_id不存在!'; return false; } - if($this->isp){ + if ($this->isp) { $url = 'https://api.weixin.qq.com/sns/component/jscode2session'; $queryarr = [ 'appid' => $applet['app_id'], @@ -43,17 +45,17 @@ class Driver 'grant_type' => 'authorization_code', 'component_appid' => $this->config['app_id'], 'component_access_token' => $this->config['component_access_token'] - ]; - }else{ - $url = 'https://api.weixin.qq.com/sns/jscode2session'; + ]; + } else { + $url = 'https://api.weixin.qq.com/sns/jscode2session'; $queryarr = [ 'appid' => $applet['app_id'], 'secret' => $applet['app_secret'], 'grant_type' => 'authorization_code', 'js_code' => $code - ]; + ]; } - $result = json_decode(Http::get($url, $queryarr),true); + $result = json_decode(Http::get($url, $queryarr), true); if (isset($result['errcode'])) { $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; return false; @@ -61,9 +63,9 @@ class Driver return $result; } //* ************************即将废除 - 开始************************* + /** * 获取session_key - 服务商版 - */ public function getComponentSessionKey(string $code, int $applet_id) { @@ -75,27 +77,26 @@ class Driver 'grant_type' => 'authorization_code', 'component_appid' => $this->config['app_id'], 'component_access_token' => $this->config['component_access_token'] - ]; - $result = json_decode(Http::get($url, $queryarr),true); + ]; + $result = json_decode(Http::get($url, $queryarr), true); if (isset($result['errcode'])) { $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; return false; } return $result; } - + //* ************************即将废除 - 结束************************* - - - - /************ 小程序直播 (权限集 id 为:52)**************/ - /** + + + /************ 小程序直播 (权限集 id 为:52)**************/ + /** * 申请开通直播 */ - public function applyLivelnfo($applet_id) + public function applyLivelnfo($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/business/applyliveinfo?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/business/applyliveinfo?access_token=' . $access_token; $queryarr = [ 'action' => 'apply' ]; @@ -108,7 +109,7 @@ class Driver public function registerMiniprogram($queryarr) { $url = 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=create&component_access_token=' . $this->config['component_access_token']; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } /************ 模板库管理 **************/ /** @@ -117,26 +118,27 @@ class Driver public function deleteTemplate(int $template_id) { $config = $this->config; - $url = 'https://api.weixin.qq.com/wxa/deletetemplate?access_token='.$config['component_access_token']; + $url = 'https://api.weixin.qq.com/wxa/deletetemplate?access_token=' . $config['component_access_token']; $queryarr = ['template_id' => $template_id]; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } - + /** * 获取模板列表 * $template_type 模板类型 0=普通模板 1=标准模板,为空则全部 */ - public function getTemplateList($template_type='') + public function getTemplateList($template_type = '') { $config = $this->config; - $url = 'https://api.weixin.qq.com/wxa/gettemplatelist?access_token='.$config['component_access_token']; + $url = 'https://api.weixin.qq.com/wxa/gettemplatelist?access_token=' . $config['component_access_token']; $queryarr = ['template_type' => $template_type]; - $result = json_decode(Http::get($url,$queryarr),true); - if($result['errcode']==0 AND sizeof($result['template_list']) > 0){ - return arr_sort($result['template_list'],'template_id'); + $result = json_decode(Http::get($url, $queryarr), true); + if ($result['errcode'] == 0 AND sizeof($result['template_list']) > 0) { + return arr_sort($result['template_list'], 'template_id'); } return []; } + /** * 将草稿添加到模板库 * $template_type 模板类型 0=普通模板 1=标准模板 @@ -144,38 +146,39 @@ class Driver public function addToTemplate(int $draft_id, int $template_type = 0) { $config = $this->config; - $url = 'https://api.weixin.qq.com/wxa/addtotemplate?access_token='.$config['component_access_token']; + $url = 'https://api.weixin.qq.com/wxa/addtotemplate?access_token=' . $config['component_access_token']; $queryarr = [ 'draft_id' => $draft_id, 'template_type' => $template_type ]; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } + /** * 获取草稿箱列表 */ public function getTemplatedRaftList() { $config = $this->config; - $url = 'https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token='.$config['component_access_token']; - $result = json_decode(Http::get($url),true); - if($result['errcode']==0 OR isset($result['draft_list']) > 0){ - return arr_sort($result['draft_list'],'draft_id'); + $url = 'https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token=' . $config['component_access_token']; + $result = json_decode(Http::get($url), true); + if ($result['errcode'] == 0 OR isset($result['draft_list']) > 0) { + return arr_sort($result['draft_list'], 'draft_id'); } return []; } - - /************ 小程序代码管理 (权限集 id 为:18)**************/ - /** + + /************ 小程序代码管理 (权限集 id 为:18)**************/ + /** * 上传代码并生成体验版 */ - public function commit($applet, $code, $is_live = 0) + public function commit($applet, $code, $is_live = 0) { $access_token = $this->getAccessToken($applet['applet_id']); - $url = 'https://api.weixin.qq.com/wxa/commit?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/commit?access_token=' . $access_token; $apiurl = $this->config['api_domain']; - $apiurl = explode(';',$apiurl); - $apiurl = 'https://'.$apiurl[0]; + $apiurl = explode(';', $apiurl); + $apiurl = 'https://' . $apiurl[0]; $ext = [ 'extEnable' => true, 'extAppid' => $applet['app_id'], @@ -190,7 +193,7 @@ class Driver ] ]; //如果小程序开通直播 - if($is_live == 1){ + if ($is_live == 1) { /* if($wxlive = get_addon_config('wxlive')){ $ext['plugins']['live-player-plugin'] = [ @@ -207,328 +210,348 @@ class Driver ]; return $this->result(Http::post($url, hema_json($queryarr))); } - /** + + /** * 获取体验版二维码 */ public function getTrialQRCode($applet_id) { $access_token = $this->getAccessToken($applet_id); $path = urlencode('pages/index/index'); - $url = 'https://api.weixin.qq.com/wxa/get_qrcode?access_token='.$access_token.'&path='.$path; + $url = 'https://api.weixin.qq.com/wxa/get_qrcode?access_token=' . $access_token . '&path=' . $path; $result = Http::get($url); - $path = 'temp'; - if(!file_exists('./'.$path)){ - mkdir($path,0777,true); - } - //获取的二维码数据存储到指定的文件 - file_put_contents('./'.$path . '/test_code_'. $applet_id .'.png',$result); - return '/'.$path.'/test_code_'.$applet_id.'.png'; + $path = 'temp'; + if (!file_exists('./' . $path)) { + mkdir($path, 0777, true); + } + //获取的二维码数据存储到指定的文件 + file_put_contents('./' . $path . '/test_code_' . $applet_id . '.png', $result); + return '/' . $path . '/test_code_' . $applet_id . '.png'; } - /** + + /** * 提交代码审核 */ - public function submitAudit($applet_id,$speedup=0) + public function submitAudit($applet_id, $speedup = 0) { $access_token = $this->getAccessToken($applet_id); $queryarr = [ 'privacy_api_not_use' => false ]; - $url = 'https://api.weixin.qq.com/wxa/submit_audit?access_token='.$access_token; - $result = json_decode(Http::post($url,hema_json($queryarr)),true); - if($result['errcode'] != 0){ + $url = 'https://api.weixin.qq.com/wxa/submit_audit?access_token=' . $access_token; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + if ($result['errcode'] != 0) { $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; - return false; + return false; } //是否加急审核 - if($speedup == 1){ - if(!$this->speedupCodeAudit($applet_id,$result['auditid'])){ + if ($speedup == 1) { + if (!$this->speedupCodeAudit($applet_id, $result['auditid'])) { return false; } } return $result; } - /** + + /** * 查询审核单状态 */ - public function getAuditStatus($applet_id,$auditid) + public function getAuditStatus($applet_id, $auditid) { $access_token = getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/get_auditstatus?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/get_auditstatus?access_token=' . $access_token; $queryarr = [ 'auditid' => $auditid ]; return $this->result(Http::post($url, json_encode($queryarr))); } - /** + + /** * 撤回代码审核 * 单个帐号每天审核撤回次数最多不超过 5 次(每天的额度从0点开始生效),一个月不超过 10 次 - */ + */ public function undoAudit($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/undocodeaudit?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/undocodeaudit?access_token=' . $access_token; return $this->result(Http::get($url)); } - /** + + /** * 发布已通过审核的小程序 */ public function release($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/release?access_token='.$access_token; - return $this->result(Http::post($url,'{}')); + $url = 'https://api.weixin.qq.com/wxa/release?access_token=' . $access_token; + return $this->result(Http::post($url, '{}')); } - /** + + /** * 小程序版本回退 - */ - public function revertCodeRelease($applet_id,$version='') + */ + public function revertCodeRelease($applet_id, $version = '') { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/revertcoderelease?app_version='.$version.'&access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/revertcoderelease?app_version=' . $version . '&access_token=' . $access_token; return $this->result(Http::get($url)); } + /** * 获取可回退的小程序版本 - */ + */ public function getHistoryVersion($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/revertcoderelease?action=get_history_version&access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/revertcoderelease?action=get_history_version&access_token=' . $access_token; return $this->result(Http::get($url)); } - /** + + /** * 设置小程序服务状态 - */ - public function setVisitStatus($applet_id,$action) + */ + public function setVisitStatus($applet_id, $action) { $access_token = $this->getAccessToken($applet_id); $queryarr = [ 'action' => $action ]; - $url = 'https://api.weixin.qq.com/wxa/change_visitstatus?access_token='.$access_token; - return $this->result(Http::post($url,hema_json($queryarr))); + $url = 'https://api.weixin.qq.com/wxa/change_visitstatus?access_token=' . $access_token; + return $this->result(Http::post($url, hema_json($queryarr))); } - /** + + /** * 查询小程序服务状态 - */ + */ public function getVisitStatus($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/getvisitstatus?access_token='.$access_token; - return $this->result(Http::post($url,'{}')); + $url = 'https://api.weixin.qq.com/wxa/getvisitstatus?access_token=' . $access_token; + return $this->result(Http::post($url, '{}')); } - /** + + /** * 设置最低基础库版本 - */ - public function setSupportVersion($applet_id,$version) + */ + public function setSupportVersion($applet_id, $version) { $access_token = $this->getAccessToken($applet_id); $queryarr = [ 'version' => $version ]; - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/setweappsupportversion?access_token='.$access_token; - return $this->result(Http::post($url,hema_json($queryarr))); + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/setweappsupportversion?access_token=' . $access_token; + return $this->result(Http::post($url, hema_json($queryarr))); } - /** + + /** * 查询服务商审核额度 - */ + */ public function setCodeAuditQuota($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/queryquota?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/queryquota?access_token=' . $access_token; return $this->result(Http::get($url)); } - /** + + /** * 加急代码审核 - */ - public function speedupCodeAudit($applet_id,$auditid) + */ + public function speedupCodeAudit($applet_id, $auditid) { $access_token = $this->getAccessToken($applet_id); $queryarr = [ 'auditid' => $auditid ]; - $url = 'https://api.weixin.qq.com/wxa/speedupaudit?access_token='.$access_token; - return $this->result(Http::post($url,hema_json($queryarr))); + $url = 'https://api.weixin.qq.com/wxa/speedupaudit?access_token=' . $access_token; + return $this->result(Http::post($url, hema_json($queryarr))); } - /** + + /** * 查询小程序版本信息 - */ + */ public function getVersionInfo($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/getversioninfo?access_token='.$access_token; - return $this->result(Http::post($url,'{}')); + $url = 'https://api.weixin.qq.com/wxa/getversioninfo?access_token=' . $access_token; + return $this->result(Http::post($url, '{}')); } - /** + + /** * 查询最新一次审核单状态 - */ + */ public function getLatestAuditStatus($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=' . $access_token; return $this->result(Http::get($url)); } - /** + + /** * 获取隐私接口检测结果 - */ + */ public function getCodePrivacyInfo($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/security/get_code_privacy_info?access_token='.$access_token; - $result = json_decode(Http::get($url),true); - if($result['errcode'] != 0){ + $url = 'https://api.weixin.qq.com/wxa/security/get_code_privacy_info?access_token=' . $access_token; + $result = json_decode(Http::get($url), true); + if ($result['errcode'] != 0) { $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; - return false; + return false; } $res = true; $error = ''; - if(sizeof($result['without_auth_list']) > 0){ + if (sizeof($result['without_auth_list']) > 0) { $error = '没权限的隐私接口:'; - foreach ($result['without_auth_list'] as $item){ + foreach ($result['without_auth_list'] as $item) { $error = $error . $item . ','; } $res = false; } - if(sizeof($result['without_conf_list']) > 0){ - if(empty($error)){ + if (sizeof($result['without_conf_list']) > 0) { + if (empty($error)) { $error = '没配置的隐私接口:'; - }else{ - $error = $error . '没配置的隐私接口:'; + } else { + $error = $error . '没配置的隐私接口:'; } - foreach ($result['without_auth_list'] as $item){ + foreach ($result['without_auth_list'] as $item) { $error = $error . $item . ','; } $res = false; } - if(!empty($error)){ + if (!empty($error)) { $error = $error . '请完成配置'; } - if($res){ - return $result; + if ($res) { + return $result; } $this->error = $error; - return false; + return false; } - /************ 扫普通二维码打开小程序 (权限集 id 为:3、18)**************/ - /** + /************ 扫普通二维码打开小程序 (权限集 id 为:3、18)**************/ + /** * 获取已设置的二维码规则 - */ - public function getJumpQRCode($applet_id,$appid='') + */ + public function getJumpQRCode($applet_id, $appid = '') { - if(empty($appid)){ + if (empty($appid)) { $queryarr = '{}'; $access_token = $this->getAccessToken($applet_id); - }else{ + } else { $queryarr = [ 'appid' => $appid ]; - $queryarr = json_encode($queryarr,JSON_UNESCAPED_UNICODE); - $access_token = $this->getAccessToken($applet_id,2); + $queryarr = json_encode($queryarr, JSON_UNESCAPED_UNICODE); + $access_token = $this->getAccessToken($applet_id, 2); } - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpget?access_token='.$access_token; - $result = json_decode(Http::post($url,$queryarr),true); + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpget?access_token=' . $access_token; + $result = json_decode(Http::post($url, $queryarr), true); return $result; } + /** * 增加或修改二维码规则 - */ - public function addJumpQRCode($applet_id,$queryarr) + */ + public function addJumpQRCode($applet_id, $queryarr) { - if(isset($queryarr['appid'])){ - $access_token = $this->getAccessToken($applet_id,2); - }else{ + if (isset($queryarr['appid'])) { + $access_token = $this->getAccessToken($applet_id, 2); + } else { $access_token = $this->getAccessToken($applet_id); } //获取效验文件 - $path = str_replace(base_url(),'',$queryarr['prefix']); + $path = str_replace(base_url(), '', $queryarr['prefix']); $path = './' . $path; - if(!$this->downloadQRCodeText($applet_id,$access_token,$path)){ - return false;//获取效验文件失败 + if (!$this->downloadQRCodeText($applet_id, $access_token, $path)) { + return false;//获取效验文件失败 } - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpadd?access_token='.$access_token; - $result = json_decode(Http::post($url,hema_json($queryarr)),true); - if($result['errcode'] != 0){ + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpadd?access_token=' . $access_token; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + if ($result['errcode'] != 0) { $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; - return false; + return false; } rmdirs($path);//添加成功,删除效验文件 return $result; } + /** * 发布已设置的二维码规则 - */ - public function publishJumpQRCode($applet_id,$prefix,$parameter='') + */ + public function publishJumpQRCode($applet_id, $prefix, $parameter = '') { - if(empty($parameter)){ + if (empty($parameter)) { $access_token = $this->getAccessToken($applet_id); - }else{ + } else { $prefix = $prefix . '/' . $parameter; - $access_token = $this->getAccessToken($applet_id,2); + $access_token = $this->getAccessToken($applet_id, 2); } - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumppublish?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumppublish?access_token=' . $access_token; $queryarr = [ 'prefix' => $prefix ]; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } + /** * 删除已设置的二维码规则 - */ - public function deleteJumpQRCode($applet_id,$prefix,$appid='') + */ + public function deleteJumpQRCode($applet_id, $prefix, $appid = '') { $queryarr = [ 'prefix' => $prefix ]; - if(empty($appid)){ + if (empty($appid)) { $access_token = $this->getAccessToken($applet_id); - }else{ + } else { $queryarr['appid'] = $appid; - $access_token = $this->getAccessToken($applet_id,2); + $access_token = $this->getAccessToken($applet_id, 2); } - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpdelete?access_token='.$access_token; - return $this->result(Http::post($url,hema_json($queryarr))); + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpdelete?access_token=' . $access_token; + return $this->result(Http::post($url, hema_json($queryarr))); } + /** * 获取校验文件名称及内容 - */ - public function downloadQRCodeText($applet_id,$access_token,$path) + */ + public function downloadQRCodeText($applet_id, $access_token, $path) { - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpdownload?access_token='.$access_token; - $result = json_decode(Http::post($url,'{}'),true); + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpdownload?access_token=' . $access_token; + $result = json_decode(Http::post($url, '{}'), true); if ($result['errcode'] != 0) { $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; return false; } - if(!file_exists($path)){ - mkdir($path,0777,true); + if (!file_exists($path)) { + mkdir($path, 0777, true); } $path = $path . $result['file_name']; - file_put_contents($path,$result['file_content']); + file_put_contents($path, $result['file_content']); return true; } - + /************ 地理位置接口申请 (权限集 id 为:18)**************/ - /** + /** * 获取地理位置接口列表 - */ + */ public function getPrivacyInterface($applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/security/get_privacy_interface?access_token='.$access_token; - $result = json_decode(Http::get($url),true); + $url = 'https://api.weixin.qq.com/wxa/security/get_privacy_interface?access_token=' . $access_token; + $result = json_decode(Http::get($url), true); return $result; } + /** * 申请地理位置接口 - */ - public function applyPrivacyInterface($applet_id,$queryarr=[]) + */ + public function applyPrivacyInterface($applet_id, $queryarr = []) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/security/apply_privacy_interface?access_token='.$access_token; - return $this->result(Http::post($url,hema_json($queryarr))); + $url = 'https://api.weixin.qq.com/wxa/security/apply_privacy_interface?access_token=' . $access_token; + return $this->result(Http::post($url, hema_json($queryarr))); } - - + + /** * 启动ticket推送服务 */ @@ -542,73 +565,78 @@ class Driver Http::post($url, json_encode($queryarr)); return true; } + /** * 获取不限制的小程序码 */ - public function getUnlimitedQRCode(int $applet_id,$file_path, string $scene = '',$page = 'pages/index/index') + public function getUnlimitedQRCode(int $applet_id, $file_path, string $scene = '', $page = 'pages/index/index') { $access_token = $this->getAccessToken($applet_id); - - - $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token; + + + $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token; $queryarr = [ 'scene' => $scene, 'page' => $page ]; - $result = Http::post($url,json_encode($queryarr)); - - file_put_contents(public_path().$file_path,$result); + $result = Http::post($url, json_encode($queryarr)); + + file_put_contents(public_path() . $file_path, $result); //获取的二维码数据存储到指定的文件 return $file_path; } + /** * 公众号用户登录 - 获取用户资料 */ public function getTicket($applet_id) { - if(!$ticket = Cache::get('jsapi_ticket_'.$applet_id)){ + if (!$ticket = Cache::get('jsapi_ticket_' . $applet_id)) { $access_token = $this->getAccessToken($applet_id); $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; $queryarr = [ 'access_token' => $access_token, 'type' => 'jsapi' ]; - $result = json_decode(Http::get($url, $queryarr),true); - if($result['errcode']!=0){ + $result = json_decode(Http::get($url, $queryarr), true); + if ($result['errcode'] != 0) { return false; } $ticket = $result['ticket']; - Cache::set('jsapi_ticket_'.$applet_id, $ticket, 5000); + Cache::set('jsapi_ticket_' . $applet_id, $ticket, 5000); } return $ticket; } - /** - * 查询昵称设置状态 - */ - public function querynickname(){ - $audit_id='454738159'; - $access_token = getAccessToken(); - $url = 'https://api.weixin.qq.com/wxa/api_wxa_querynickname?access_token='.$access_token; - $data = ['audit_id' => $audit_id]; - $result = http_post($url,$data); - return $result; - } - - /** - * 获取审核时可填写的类目信息 - */ - public function getshowwxaitem() - { - $access_token = getAccessToken(); - $url = 'https://api.weixin.qq.com/wxa/getshowwxaitem?access_token='.$access_token; - return curl($url); - } - /** + + /** + * 查询昵称设置状态 + */ + public function querynickname() + { + $audit_id = '454738159'; + $access_token = getAccessToken(); + $url = 'https://api.weixin.qq.com/wxa/api_wxa_querynickname?access_token=' . $access_token; + $data = ['audit_id' => $audit_id]; + $result = http_post($url, $data); + return $result; + } + + /** + * 获取审核时可填写的类目信息 + */ + public function getshowwxaitem() + { + $access_token = getAccessToken(); + $url = 'https://api.weixin.qq.com/wxa/getshowwxaitem?access_token=' . $access_token; + return curl($url); + } + + /** * code换取token - 微信扫码登录 */ function getWebToken(string $code) { - $values = Setting::getItem('wxweb',0); + $values = Setting::getItem('wxweb', 0); $url = 'https://api.weixin.qq.com/sns/oauth2/access_token'; $queryarr = [ 'appid' => $values['app_id'], @@ -616,14 +644,14 @@ class Driver 'grant_type' => 'authorization_code', 'code' => $code ]; - $result = json_decode(Http::get($url,$queryarr),true); - if(isset($result['errcode']) AND $result['errcode'] != 0){ + $result = json_decode(Http::get($url, $queryarr), true); + if (isset($result['errcode']) AND $result['errcode'] != 0) { $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; - return false; - } - return $result; + return false; + } + return $result; } - + /** * 用户登录 - 获取用户资料 (公众号,web应用) */ @@ -635,41 +663,41 @@ class Driver 'openid' => $openid, 'lang' => 'zh_CN' ]; - $result = json_decode(Http::get($url, $queryarr),true); - if(isset($result['errcode']) AND $result['errcode'] != 0){ + $result = json_decode(Http::get($url, $queryarr), true); + if (isset($result['errcode']) AND $result['errcode'] != 0) { $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; - return false; - } - return $result; - } - - /** - * 获取授权应用的帐号基本信息 - */ - public function getAppInfo(string $auth_appid) - { - $config = $this->config; - $url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$config['component_access_token']; - $queryarr = [ - 'component_appid' => $config['app_id'], - 'authorizer_appid' => $auth_appid - ]; - $result = json_decode(Http::post($url, json_encode($queryarr)),true); - if(isset($result['errcode']) AND $result['errcode'] != 0){ - $this->error = 'code:'.$result['errcode'].',msg:'.$result['errmsg'];//获取失败 return false; } return $result; } - - /** + + /** + * 获取授权应用的帐号基本信息 + */ + public function getAppInfo(string $auth_appid) + { + $config = $this->config; + $url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=' . $config['component_access_token']; + $queryarr = [ + 'component_appid' => $config['app_id'], + 'authorizer_appid' => $auth_appid + ]; + $result = json_decode(Http::post($url, json_encode($queryarr)), true); + if (isset($result['errcode']) AND $result['errcode'] != 0) { + $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg'];//获取失败 + return false; + } + return $result; + } + + /** * 修改小程序头像 */ - public function modifyHeadImage(string $media_id,int $applet_id) + public function modifyHeadImage(string $media_id, int $applet_id) { $access_token = $this->getAccessToken($applet_id); //执行修改头像 - $url = 'https://api.weixin.qq.com/cgi-bin/account/modifyheadimage?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/cgi-bin/account/modifyheadimage?access_token=' . $access_token; $queryarr = [ 'head_img_media_id' => $media_id, 'x1' => 0, @@ -679,84 +707,84 @@ class Driver ]; return $this->result(Http::post($url, hema_json($queryarr))); } - - /** - * 上传临时素材 - */ - public function upTempMaterial(string $file_url, int $applet_id, string $type='image') - { + + /** + * 上传临时素材 + */ + public function upTempMaterial(string $file_url, int $applet_id, string $type = 'image') + { $access_token = $this->getAccessToken($applet_id); $real_path = web_path() . 'temp/' . time() . '.jpg'; $temp_file = file_get_contents($file_url); //获取网络图片 - file_put_contents($real_path,$temp_file); //存放临时图片 - $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$access_token.'&type='.$type; - $queryarr['media'] = curl_file_create($real_path,'image/jpeg',$file_url);//获取要上传的二进制文件 - $result = json_decode(http_post($url,$queryarr),true); + file_put_contents($real_path, $temp_file); //存放临时图片 + $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $access_token . '&type=' . $type; + $queryarr['media'] = curl_file_create($real_path, 'image/jpeg', $file_url);//获取要上传的二进制文件 + $result = json_decode(http_post($url, $queryarr), true); unlink($real_path);//删除临时图片 - if(isset($result['media_id'])){ + if (isset($result['media_id'])) { return $result['media_id']; //返回的临时素材(media_id) - }else{ - $this->error = 'code:'.$result['errcode'].',msg:'.$result['errmsg'];//获取失败 + } else { + $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg'];//获取失败 return false; } } - - /** - * 获取小程序设置信息 - */ + + /** + * 获取小程序设置信息 + */ public function getInfor(int $applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo?access_token=' . $access_token; return $this->result(Http::get($url)); } - - /** - * 获取预授权码 - 生成授权页面 - * $type,1=授权公众号,2=授权小程序,3=两者都有 - */ + + /** + * 获取预授权码 - 生成授权页面 + * $type,1=授权公众号,2=授权小程序,3=两者都有 + */ public function authUrl(int $applet_id = 0, int $type = 3) { $config = $this->config; //获取第三方配置 $url = '#'; - $redirect_uri = 'https://'.$config['authorize_domain'].'/applet/auth/'; - if($type==1){ - $redirect_uri .= 'wechat/applet_id/'.$applet_id; - }else{ - $redirect_uri .= 'wxapp/applet_id/'.$applet_id; + $redirect_uri = 'https://' . $config['authorize_domain'] . '/applet/auth/'; + if ($type == 1) { + $redirect_uri .= 'wechat/applet_id/' . $applet_id; + } else { + $redirect_uri .= 'wxapp/applet_id/' . $applet_id; } - $url = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token='.$config['component_access_token']; + $url = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=' . $config['component_access_token']; $queryarr = [ 'component_appid' => $config['app_id'] ]; - $result = json_decode(Http::post($url, json_encode($queryarr)),true);//返回"pre_auth_code": "预授权码","expires_in": 有效期(600秒) - if(isset($result['pre_auth_code'])){ - $url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid='.$config['app_id'].'&pre_auth_code='.$result['pre_auth_code'].'&redirect_uri='.$redirect_uri.'&auth_type='.$type; + $result = json_decode(Http::post($url, json_encode($queryarr)), true);//返回"pre_auth_code": "预授权码","expires_in": 有效期(600秒) + if (isset($result['pre_auth_code'])) { + $url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=' . $config['app_id'] . '&pre_auth_code=' . $result['pre_auth_code'] . '&redirect_uri=' . $redirect_uri . '&auth_type=' . $type; } return $url; } - - - /** + + + /** * 设置服务器域名 */ public function setServeDomain(int $applet_id = 0, string $apiurl = '', string $access_token = '') { $config = $this->config; - if(empty($access_token)){ + if (empty($access_token)) { $access_token = $this->getAccessToken($applet_id); } - if(empty($apiurl)){ + if (empty($apiurl)) { $apiurl = $config['api_domain']; } - $domain = explode(';',$apiurl); + $domain = explode(';', $apiurl); $requestdomain = [];//request 合法域名 $wsrequestdomain = [];//socket 合法域名 - for($n=0;$n 'set', 'requestdomain' => $requestdomain, @@ -764,78 +792,80 @@ class Driver 'uploaddomain' => $requestdomain, 'downloaddomain' => $requestdomain ]; - $result = json_decode(Http::post($url, hema_json($queryarr)),true); - if($result['errcode'] != 0){ - $this->error = 'code:'.$result['errcode'].',msg:'.$result['errmsg']; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + if ($result['errcode'] != 0) { + $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; return false; } $result['apiurl'] = $apiurl; return $result; } - + /** * 设置业务域名 */ public function setWebDomain(int $applet_id = 0, string $apiurl = '') { - $config = $this->config; - if(empty($access_token)){ + $config = $this->config; + if (empty($access_token)) { $access_token = $this->getAccessToken($applet_id); } - if(empty($apiurl)){ + if (empty($apiurl)) { $apiurl = $config['api_domain']; } - $domain = explode(';',$apiurl); + $domain = explode(';', $apiurl); $webviewdomain = []; - foreach ($domain as $vo){ - $webviewdomain[] = 'https://'.$vo; + foreach ($domain as $vo) { + $webviewdomain[] = 'https://' . $vo; } - $url = 'https://api.weixin.qq.com/wxa/setwebviewdomain?access_token='.$access_token; - $queryarr = [ - 'action' => 'set', - 'webviewdomain' => $webviewdomain - ]; - return $this->result(Http::post($url, hema_json($queryarr))); + $url = 'https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=' . $access_token; + $queryarr = [ + 'action' => 'set', + 'webviewdomain' => $webviewdomain + ]; + return $this->result(Http::post($url, hema_json($queryarr))); } + /** * 设置功能介绍 */ public function setSignature(int $applet_id = 0, string $signature = '', string $access_token = '') { - if(empty($access_token)){ + if (empty($access_token)) { $access_token = $this->getAccessToken($applet_id); } - if(empty($signature)){ + if (empty($signature)) { $signature = '一个值得信赖的小程序'; } - $url = 'https://api.weixin.qq.com/cgi-bin/account/modifysignature?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/cgi-bin/account/modifysignature?access_token=' . $access_token; $queryarr = ['signature' => $signature]; - $result = json_decode(Http::post($url, hema_json($queryarr)),true); - if($result['errcode'] != 0){ - $this->error = 'code:'.$result['errcode'].',msg:'.$result['errmsg']; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + if ($result['errcode'] != 0) { + $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; return false; } $result['signature'] = $signature; return $result; } - - /** - * 微信认证名称检测 - */ + + /** + * 微信认证名称检测 + */ public function checkWxVerifyNickName(int $applet_id, string $nick_name) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/cgi-bin/wxverify/checkwxverifynickname?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/cgi-bin/wxverify/checkwxverifynickname?access_token=' . $access_token; $queryarr = ['nick_name' => $nick_name]; return $this->result(Http::post($url, hema_json($queryarr))); } + /** * 设置小程序昵称 */ public function setNickName(int $applet_id, string $nick_name, string $license, string $other1) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/setnickname?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxa/setnickname?access_token=' . $access_token; $queryarr = [ 'nick_name' => $nick_name, 'license' => $license, @@ -843,36 +873,36 @@ class Driver ]; return $this->result(Http::post($url, hema_json($queryarr))); } - - /** + + /** * 删除类目 */ public function deleteCategory(int $applet_id, int $first, int $second) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/deletecategory?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/deletecategory?access_token=' . $access_token; $queryarr = [ 'first' => $first, 'second' => $second ]; return $this->result(Http::post($url, hema_json($queryarr))); } - - /** - * 获取已设置的所有类目 - */ + + /** + * 获取已设置的所有类目 + */ public function getCategory(int $applet_id) { $access_token = $this->getAccessToken($applet_id); - if($this->isp){ - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/getcategory?access_token='.$access_token; - }else{ - $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token='.$access_token; + if ($this->isp) { + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/getcategory?access_token=' . $access_token; + } else { + $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token=' . $access_token; } - $result = json_decode(Http::get($url),true); + $result = json_decode(Http::get($url), true); return $result; } - + /** * 获取可以设置的所有类目 */ @@ -880,57 +910,57 @@ class Driver { //同步微信端线下类目 $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/getallcategories?access_token='.$access_token; - $result = json_decode(Http::get($url),true); - if($result['errcode']==0){ + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/getallcategories?access_token=' . $access_token; + $result = json_decode(Http::get($url), true); + if ($result['errcode'] == 0) { $category = $result['categories_list']['categories']; $new = []; //筛选后的类目 //遍历一级类目 - foreach($category[0]['children'] as $value){ + foreach ($category[0]['children'] as $value) { //查找一级类目 - for($n=1;$ngetAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/addcategory?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/addcategory?access_token=' . $access_token; $queryarr = [ 'categories' => [ [ @@ -945,29 +975,29 @@ class Driver ]; return $this->result(Http::post($url, hema_json($queryarr))); } - - /** - * 附近小程序 - 查询地点类目信息 - */ + + /** + * 附近小程序 - 查询地点类目信息 + */ public function getStoreWxaAttr(int $applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/getstorewxaattr?access_token='.$access_token; - $result = json_decode(Http::get($url),true); + $url = 'https://api.weixin.qq.com/wxa/getstorewxaattr?access_token=' . $access_token; + $result = json_decode(Http::get($url), true); $list = []; - if($result['errcode'] == 0 AND $result['is_exist'] == 1){ + if ($result['errcode'] == 0 AND $result['is_exist'] == 1) { $category = $this->getMerchantCategory($applet_id);//拉取可设置类目 - if(isset($result['store_wxa_attr']['weapp_category'])){ + if (isset($result['store_wxa_attr']['weapp_category'])) { foreach ($result['store_wxa_attr']['weapp_category']['categories'] as $vo) { $first_name = ''; $second_name = ''; //获取一级类目名称 foreach ($category as $first) { - if($first['id'] == $vo['first']){ + if ($first['id'] == $vo['first']) { $first_name = $first['name']; //获取二级类目名称 foreach ($first['children'] as $second) { - if($second['id'] == $vo['second']){ + if ($second['id'] == $vo['second']) { $second_name = $second['name']; break; } @@ -988,161 +1018,168 @@ class Driver } return $list; } + /** - * 附近小程序 - 申请附近地点类目 - */ + * 附近小程序 - 申请附近地点类目 + */ public function nearbyApplyCategory(array $queryarr, int $applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/nearbyapplycategory?access_token='.$access_token; - $this->result(Http::post($url,hema_json($queryarr))); + $url = 'https://api.weixin.qq.com/wxa/nearbyapplycategory?access_token=' . $access_token; + $this->result(Http::post($url, hema_json($queryarr))); } + /** - * 附近小程序 - 拉取门店小程序类目 - */ + * 附近小程序 - 拉取门店小程序类目 + */ public function getMerchantCategory(int $applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/get_merchant_category?access_token='.$access_token; - $result = json_decode(Http::get($url),true); - if($result['errcode']==0){ + $url = 'https://api.weixin.qq.com/wxa/get_merchant_category?access_token=' . $access_token; + $result = json_decode(Http::get($url), true); + if ($result['errcode'] == 0) { $category = $result['data']['all_category_info']['categories']; $new = []; //筛选后的类目 //遍历一级类目 - foreach($category[0]['children'] as $value){ + foreach ($category[0]['children'] as $value) { //查找一级类目 - for($n=1;$ngetAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/getnearbypoilist?page=1&page_rows=20&access_token='.$access_token; - $result = json_decode(Http::get($url),true); + $url = 'https://api.weixin.qq.com/wxa/getnearbypoilist?page=1&page_rows=20&access_token=' . $access_token; + $result = json_decode(Http::get($url), true); return $result; } + /** - * 附近小程序 - 添加附近门店 - */ + * 附近小程序 - 添加附近门店 + */ public function addNearbyPoi(array $queryarr, int $applet_id) { - $queryarr['hour'] = str_replace(' ','',$queryarr['hour']); + $queryarr['hour'] = str_replace(' ', '', $queryarr['hour']); $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/addnearbypoi?access_token='.$access_token; - return $this->result(Http::post($url,hema_json($queryarr))); + $url = 'https://api.weixin.qq.com/wxa/addnearbypoi?access_token=' . $access_token; + return $this->result(Http::post($url, hema_json($queryarr))); } + /** - * 附近小程序 - 删除附近门店 - */ + * 附近小程序 - 删除附近门店 + */ public function delNearbyPoi(int $applet_id, string $poi_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/delnearbypoi?access_token='.$access_token; - $queryarr =[ + $url = 'https://api.weixin.qq.com/wxa/delnearbypoi?access_token=' . $access_token; + $queryarr = [ 'poi_id' => $poi_id //门店的 poi_id ]; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } - + /** - * 附近小程序 - 展示/取消展示附近门店(小程序) - */ + * 附近小程序 - 展示/取消展示附近门店(小程序) + */ public function setNearbyPoiShowStatus(int $applet_id, $poi_id, $status) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/setnearbypoishowstatus?access_token='.$access_token; - $queryarr =[ + $url = 'https://api.weixin.qq.com/wxa/setnearbypoishowstatus?access_token=' . $access_token; + $queryarr = [ 'poi_id' => $poi_id, //门店的 poi_id 'status' => $status //0:取消展示;1:展示 ]; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } - + /** - * 配置小程序用户隐私保护指引 - */ - public function setPrivacySetting($applet_id,$queryarr) + * 配置小程序用户隐私保护指引 + */ + public function setPrivacySetting($applet_id, $queryarr) { $access_token = $this->getAccessToken($applet_id); $url = 'https://api.weixin.qq.com/cgi-bin/component/setprivacysetting?access_token=' . $access_token; $queryarr['owner_setting']['notice_method'] = '小程序弹窗'; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } - + /** - * 查询小程序用户隐私保护指引 - */ + * 查询小程序用户隐私保护指引 + */ public function getPrivacySetting($applet_id) { $access_token = $this->getAccessToken($applet_id); $url = 'https://api.weixin.qq.com/cgi-bin/component/getprivacysetting?access_token=' . $access_token; - return $this->result(Http::post($url,"{}")); + return $this->result(Http::post($url, "{}")); } - + /** - * 获取体验用户列表 - */ + * 获取体验用户列表 + */ public function getTestUser(int $applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/memberauth?access_token='.$access_token; - $queryarr =[ + $url = 'https://api.weixin.qq.com/wxa/memberauth?access_token=' . $access_token; + $queryarr = [ 'action' => 'get_experiencer' //固定值 ]; - return json_decode(Http::post($url,hema_json($queryarr)),true); + return json_decode(Http::post($url, hema_json($queryarr)), true); } + /** - * 添加体验用户 - */ + * 添加体验用户 + */ public function addTestUser(int $applet_id, string $wechatid) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/bind_tester?access_token='.$access_token; - $queryarr =[ + $url = 'https://api.weixin.qq.com/wxa/bind_tester?access_token=' . $access_token; + $queryarr = [ 'wechatid' => $wechatid //微信号 ]; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } + /** - * 删除体验用户 - */ + * 删除体验用户 + */ public function delTestUser(int $applet_id, string $userstr) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxa/unbind_tester?access_token='.$access_token; - $queryarr =[ + $url = 'https://api.weixin.qq.com/wxa/unbind_tester?access_token=' . $access_token; + $queryarr = [ 'userstr' => $userstr //唯一识别码 ]; - return $this->result(Http::post($url,hema_json($queryarr))); + return $this->result(Http::post($url, hema_json($queryarr))); } - + /** * 上传图文消息内的图片 - 到微信端(永久素材) - 小程序端 * 图片仅支持jpg/png格式,大小必须在1MB以下 @@ -1152,40 +1189,41 @@ class Driver $access_token = $this->getAccessToken($applet_id); $real_path = web_path() . 'temp/' . time() . '.jpg'; $temp_file = file_get_contents($file_url);//获取网络图片 - file_put_contents($real_path,$temp_file); //存放临时图片 + file_put_contents($real_path, $temp_file); //存放临时图片 //上传到微信服务器 - $url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token='.$access_token; - $queryarr['media'] = curl_file_create($real_path,'image/jpeg',$file_url);//获取要上传的二进制文件 - $result = json_decode(Http::post($url,$queryarr),true); + $url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=' . $access_token; + $queryarr['media'] = curl_file_create($real_path, 'image/jpeg', $file_url);//获取要上传的二进制文件 + $result = json_decode(Http::post($url, $queryarr), true); unlink($real_path);//删除临时图片 - if(!isset($result['url'])){ + if (!isset($result['url'])) { $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; return false;//上传错误,一般是图片不符合要求 } return $result['url']; } + /** * 使用授权码获取授权信息 - */ + */ public function getAuth(string $auth_code) { $config = $this->config; - $url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$config['component_access_token']; + $url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' . $config['component_access_token']; $queryarr = [ 'component_appid' => $config['app_id'], 'authorization_code' => $auth_code ]; - $result = json_decode(Http::post($url, hema_json($queryarr)),true); - if(!isset($result['authorization_info'])){ + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + if (!isset($result['authorization_info'])) { $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; return false; } return $result['authorization_info']; } - + /** * 获取 component_access_token - */ + */ public function getComponentToken(string $ticket) { $config = $this->config; @@ -1195,19 +1233,20 @@ class Driver 'component_appsecret' => $config['app_secret'], 'component_verify_ticket' => $ticket, ]; - $result = json_decode(Http::post($url, hema_json($queryarr)),true); - if(isset($result['component_access_token'])){ - return $result; - } - $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + if (isset($result['component_access_token'])) { + return $result; + } + $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; return false; } + /************ 公众号 - 生成二维码 (帐号管理-生成带参数的二维码)**************/ - - public function qrcodeCreate($applet_id=0,$action_name='QR_STR_SCENE',$scene='login',$expire_seconds=7200) + + public function qrcodeCreate($applet_id = 0, $action_name = 'QR_STR_SCENE', $scene = 'login', $expire_seconds = 7200) { - $access_token = $this->getAccessToken($applet_id,2); - + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $access_token; /** * 二维码类型 @@ -1216,37 +1255,37 @@ class Driver */ $queryarr['action_name'] = $action_name; //设置临时二维码过期时间 - if($action_name=='QR_STR_SCENE' or $action_name=='QR_SCENE'){ + if ($action_name == 'QR_STR_SCENE' or $action_name == 'QR_SCENE') { $queryarr['expire_seconds'] = $expire_seconds; } - if($action_name=='QR_STR_SCENE' or $action_name=='QR_LIMIT_STR_SCENE'){ + if ($action_name == 'QR_STR_SCENE' or $action_name == 'QR_LIMIT_STR_SCENE') { $queryarr['action_info']['scene']['scene_str'] = $scene; - }else{ + } else { $queryarr['action_info']['scene']['scene_id'] = (int)$scene; } - $result = json_decode(Http::post($url, hema_json($queryarr)),true); - - if(isset($result['ticket'])){ - $rs = Http::get('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($result['ticket'])); - $path = 'temp'; - if(!file_exists('./'.$path)){ - mkdir($path,0777,true); - } - //获取的二维码数据存储到指定的文件 - file_put_contents('./'.$path . '/hemaphp_login_qrcode.png',$rs); - return $result['ticket']; - } - $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + + if (isset($result['ticket'])) { + $rs = Http::get('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($result['ticket'])); + $path = 'temp'; + if (!file_exists('./' . $path)) { + mkdir($path, 0777, true); + } + //获取的二维码数据存储到指定的文件 + file_put_contents('./' . $path . '/hemaphp_login_qrcode.png', $rs); + return $result['ticket']; + } + $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; return false; } - + /** * 公众号网页授权 - H5用户登录 code 换取 access_token * $appid 公众号的APPID */ - public function oauth2($code,$appid,$applet_id) + public function oauth2($code, $appid, $applet_id) { - $access_token = $this->getAccessToken($applet_id,2); + $access_token = $this->getAccessToken($applet_id, 2); //通过 code 换取 access_token $url = 'https://api.weixin.qq.com/sns/oauth2/component/access_token'; $queryarr = [ @@ -1256,11 +1295,11 @@ class Driver 'component_appid' => $this->config['app_id'], 'component_access_token' => $this->config['component_access_token'] ]; - $result = json_decode(Http::get($url, $queryarr),true); + $result = json_decode(Http::get($url, $queryarr), true); if (isset($result['errcode'])) { - $this->error = 'code:'.$result['errcode'].',msg:' . $result['errmsg']; + $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; return false; - }else{ + } else { //拉取用户信息(需 scope 为 snsapi_userinfo) $url = 'https://api.weixin.qq.com/sns/userinfo?access_token'; $queryarr = [ @@ -1268,115 +1307,119 @@ class Driver 'openid' => $result['openid'], 'lang' => 'zh_CN' ]; - $result = json_decode(Http::get($url, $queryarr),true); + $result = json_decode(Http::get($url, $queryarr), true); if (isset($result['errcode'])) { - $this->error = 'code:'.$result['errcode'].',msg:' . $result['errmsg']; + $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; return false; } } return $result; } + /** - * 公众号获取粉丝列表 - */ + * 公众号获取粉丝列表 + */ public function getFans(int $applet_id) { - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$access_token.'&next_openid='; - $result = json_decode(Http::get($url),true); + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' . $access_token . '&next_openid='; + $result = json_decode(Http::get($url), true); return $result; } + /** * 上传图文消息内的图片 - 到微信端(永久素材) - 公众号端 * 图片仅支持jpg/png格式,大小必须在1MB以下 */ public function upWechatUrl(array $img, int $applet_id = 0) { - $access_token = $this->getAccessToken($applet_id,2); - for($n=0;$ngetAccessToken($applet_id, 2); + for ($n = 0; $n < sizeof($img); $n++) { $real_path = web_path() . 'uploads/' . $img[$n]['file_path']; //上传到微信服务器 - $url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token='.$access_token; - $queryarr['media'] = curl_file_create($real_path,'image/jpeg',$img[$n]['file_path']);//获取要上传的二进制文件 - $result = json_decode(Http::post($url,$queryarr),true); - if(!isset($result['url'])){ + $url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=' . $access_token; + $queryarr['media'] = curl_file_create($real_path, 'image/jpeg', $img[$n]['file_path']);//获取要上传的二进制文件 + $result = json_decode(Http::post($url, $queryarr), true); + if (!isset($result['url'])) { return false;//上传错误,一般是图片不符合要求 } $img[$n]['url'] = $result['url']; } return $img; } + /** * 上传素材文件 - 到微信端 */ public function addMaterial(int $applet_id, string $file_path = '', int $file_type = 10, string $name = '', string $introduction = '') { // 验证文件并上传 - if($file_type==10){ //图片(image): 2M,支持bmp/png/jpeg/jpg/gif格式 + if ($file_type == 10) { //图片(image): 2M,支持bmp/png/jpeg/jpg/gif格式 $type = 'image'; $mimetype = 'image/jpeg'; } - if($file_type==20){ //语音(voice):2M,播放长度不超过60s,mp3/wma/wav/amr格式 + if ($file_type == 20) { //语音(voice):2M,播放长度不超过60s,mp3/wma/wav/amr格式 $type = 'voice'; $mimetype = 'audio/mpeg'; } - if($file_type==30){ //视频(video):10MB,支持MP4格式 + if ($file_type == 30) { //视频(video):10MB,支持MP4格式 $type = 'video'; $mimetype = 'video/mp4'; - $queryarr['description'] = '{"title":"'.$name.'","introduction":"'.$introduction.'"}'; + $queryarr['description'] = '{"title":"' . $name . '","introduction":"' . $introduction . '"}'; } - $access_token = $this->getAccessToken($applet_id,2); + $access_token = $this->getAccessToken($applet_id, 2); $real_path = web_path() . 'uploads/' . $file_path; //上传到微信服务器 - $url = 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token='.$access_token.'&type='.$type; - $queryarr['media'] = curl_file_create($real_path,$mimetype,$file_path);//获取要上传的二进制文件 - $result = json_decode(http_post($url,$queryarr),true); + $url = 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=' . $access_token . '&type=' . $type; + $queryarr['media'] = curl_file_create($real_path, $mimetype, $file_path);//获取要上传的二进制文件 + $result = json_decode(http_post($url, $queryarr), true); return $result; //$result['media_id']; } + /** * 删除素材文件 - 到微信端 */ public function delMaterial(string $media_id, int $applet_id = 0) { - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/material/del_material?access_token='.$access_token; + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=' . $access_token; $queryarr = [ 'media_id' => $media_id ]; - $result = json_decode(Http::post($url,json_encode($queryarr)),true); + $result = json_decode(Http::post($url, json_encode($queryarr)), true); return $result; } - + /** * 新增草稿 - 图文消息 */ public function addDraft(array $data, int $applet_id = 0) { $queryarr['articles'] = array(); - for($n=0;$n $data[$n]['title'], 'author' => $data[$n]['author'], 'digest' => $data[$n]['digest'], - 'content' => str_ireplace('"','\'',$data[$n]['wx_content']), + 'content' => str_ireplace('"', '\'', $data[$n]['wx_content']), 'content_source_url' => base_url(), 'thumb_media_id' => $data[$n]['media_id'] ]); } - $access_token = $this->getAccessToken($applet_id,2); + $access_token = $this->getAccessToken($applet_id, 2); //上传到微信服务器 - $url = 'https://api.weixin.qq.com/cgi-bin/draft/add?access_token='.$access_token; - $result = json_decode(Http::post($url,hema_json($queryarr)),true); + $url = 'https://api.weixin.qq.com/cgi-bin/draft/add?access_token=' . $access_token; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); return $result; } - + /** * 修改草稿 - 图文消息 */ public function editDraft(array $data, int $media_id, int $applet_id = 0) { - $access_token = $this->getAccessToken($applet_id,2); - for($n=0;$ngetAccessToken($applet_id, 2); + for ($n = 0; $n < sizeof($data); $n++) { $queryarr = [ 'media_id' => $media_id, 'index' => $data[$n]['ids'], @@ -1384,41 +1427,42 @@ class Driver 'title' => $data[$n]['title'], 'author' => $data[$n]['author'], 'digest' => $data[$n]['digest'], - 'content' => str_ireplace('"','\'',$data[$n]['wx_content']), + 'content' => str_ireplace('"', '\'', $data[$n]['wx_content']), 'content_source_url' => base_url(), 'thumb_media_id' => $data[$n]['media_id'] ] ]; //上传到微信服务器 - $url = 'https://api.weixin.qq.com/cgi-bin/draft/update?access_token='.$access_token; - $result = json_decode(Http::post($url,hema_json($queryarr)),true); - if($result['errcode']!=0){ - $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; - return false; - } + $url = 'https://api.weixin.qq.com/cgi-bin/draft/update?access_token=' . $access_token; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + if ($result['errcode'] != 0) { + $this->error = 'code:' . $result['errcode'] . ',msg:' . $result['errmsg']; + return false; + } } return true; } - + /** * 删除草稿 - 图文消息 */ public function delDraft(string $media_id, int $applet_id = 0) { - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/draft/delete?access_token='.$access_token; + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/draft/delete?access_token=' . $access_token; $queryarr = [ 'media_id' => $media_id ]; - return json_decode(Http::post($url,json_encode($queryarr)),true); + return json_decode(Http::post($url, json_encode($queryarr)), true); } + /** * 根据OpenID列表群发公众号信息 */ public function sendMass($msg, $open_id, int $applet_id = 0) { //图文消息 - if($msg['msg_type']['value']=='news'){ + if ($msg['msg_type']['value'] == 'news') { $queryarr = [ 'mpnews' => [ 'media_id' => $msg['content'] @@ -1428,7 +1472,7 @@ class Driver ]; } //文本消息 - if($msg['msg_type']['value']=='text'){ + if ($msg['msg_type']['value'] == 'text') { $queryarr = [ 'msgtype' => 'text', 'text' => [ @@ -1437,7 +1481,7 @@ class Driver ]; } //语音消息 - if($msg['msg_type']['value']=='voice'){ + if ($msg['msg_type']['value'] == 'voice') { $queryarr = [ 'voice' => [ 'media_id' => $msg['content'] @@ -1446,7 +1490,7 @@ class Driver ]; } //图片消息 - if($msg['msg_type']['value']=='image'){ + if ($msg['msg_type']['value'] == 'image') { $queryarr = [ 'images' => [ 'media_ids' => [ @@ -1460,7 +1504,7 @@ class Driver ]; } //视频消息 - if($msg['msg_type']['value']=='video'){ + if ($msg['msg_type']['value'] == 'video') { $queryarr = [ 'mpvideo' => [ 'media_id' => $msg['content'], @@ -1471,7 +1515,7 @@ class Driver ]; } //卡券消息 - if($msg['msg_type']['value']=='wxcard'){ + if ($msg['msg_type']['value'] == 'wxcard') { $queryarr = [ 'wxcard' => [ 'card_id' => $msg['content'] @@ -1480,67 +1524,69 @@ class Driver ]; } $queryarr['touser'] = $open_id; - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.$access_token; - $result = json_decode(Http::post($url,json_encode($queryarr)),true); + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=' . $access_token; + $result = json_decode(Http::post($url, json_encode($queryarr)), true); return $result; } + /** * 删除公众号群发记录 */ - public function deleteMass(int $msg_id,int $applet_id = 0) + public function deleteMass(int $msg_id, int $applet_id = 0) { - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token='.$access_token; + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=' . $access_token; $queryarr = ['msg_id' => $msg_id]; - $result = json_decode(Http::post($url,json_encode($queryarr)),true); + $result = json_decode(Http::post($url, json_encode($queryarr)), true); return $result; } + /** * 向公众号发布预览消息 */ public function previewMsg(array $data, string $open_id, int $applet_id = 0) { //图文消息 - if($data['msg_type']=='news'){ + if ($data['msg_type'] == 'news') { $queryarr = [ 'mpnews' => ['media_id' => $data['content']], 'msgtype' => 'mpnews' ]; } //文本消息 - if($data['msg_type']=='text'){ + if ($data['msg_type'] == 'text') { $queryarr = [ 'text' => ['content' => $data['content']], 'msgtype' => 'text' ]; } //语音消息 - if($data['msg_type']=='voice'){ + if ($data['msg_type'] == 'voice') { $queryarr = [ 'voice' => ['media_id' => $data['content']], 'msgtype' => 'voice' ]; } //图片消息 - if($data['msg_type']=='image'){ + if ($data['msg_type'] == 'image') { $queryarr = [ 'image' => ['media_id' => $data['content']], 'msgtype' => 'image' ]; } //视频消息 - if($data['msg_type']=='video'){ + if ($data['msg_type'] == 'video') { $queryarr = [ 'mpvideo' => ['media_id' => $data['content']], 'msgtype' => 'mpvideo' ]; } //卡券消息 - if($data['msg_type']=='wxcard'){ + if ($data['msg_type'] == 'wxcard') { $queryarr = '{ "wxcard":{ - "card_id":"'.$data['content'].'", + "card_id":"' . $data['content'] . '", "card_ext": "{ "code":"", "openid":"", @@ -1563,69 +1609,71 @@ class Driver ]; } $queryarr['touser'] = $open_id; - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.$access_token; - $result = json_decode(Http::post($url,json_encode($queryarr)),true); + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=' . $access_token; + $result = json_decode(Http::post($url, json_encode($queryarr)), true); return $result; } + /** - * 公众号自定义菜单 - 同步到微信端 - */ + * 公众号自定义菜单 - 同步到微信端 + */ public function creatMenu(array $menu, int $applet_id = 0) { - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token; + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $access_token; $queryarr = [ - 'button' => $menu + 'button' => $menu ]; - $result = json_decode(Http::post($url, hema_json($queryarr)),true); - return $result; + $result = json_decode(Http::post($url, hema_json($queryarr)), true); + return $result; } + /** * 发布模板消息 - 公众号 */ function sendWechatMsg(int $applet_id = 0, array $queryarr = []) { - if(sizeof($queryarr) == 0){ + if (sizeof($queryarr) == 0) { return true; } - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token; - $result = json_decode(Http::post($url,json_encode($queryarr)),true); - return true; + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token; + $result = json_decode(Http::post($url, json_encode($queryarr)), true); + return true; } - + /** * 获取公众号粉丝用户基本信息(包括UnionID机制)- 公众号 - */ + */ public function getWechatUserInfo(string $openid, int $applet_id) { - $access_token = $this->getAccessToken($applet_id,2); + $access_token = $this->getAccessToken($applet_id, 2); $url = 'https://api.weixin.qq.com/cgi-bin/user/info'; $queryarr = [ 'access_token' => $access_token, 'openid' => $openid, 'lang' => 'zh_CN' ]; - $result = json_decode(Http::get($url,$queryarr),true); - if(!isset($result['openid'])){ + $result = json_decode(Http::get($url, $queryarr), true); + if (!isset($result['openid'])) { $this->error = 'code:' . $result['errcode'] . ' msg:' . $result['errmsg']; return false; } $data['open_id'] = $result['openid']; - if(isset($result['unionid'])){ + if (isset($result['unionid'])) { $data['union_id'] = $result['unionid']; } return $data; } - + /** * 发送客服消息模板 - */ + */ public function sendServiceMsg(array $msg, string $touser, int $applet_id) { //文本消息 - if($msg['type']=='text'){ + if ($msg['type'] == 'text') { $queryarr = [ 'msgtype' => 'text', 'text' => [ @@ -1634,7 +1682,7 @@ class Driver ]; } //图片消息 - if($msg['type']=='image'){ + if ($msg['type'] == 'image') { $queryarr = [ 'msgtype' => 'image', 'image' => [ @@ -1643,7 +1691,7 @@ class Driver ]; } //图文消息(点击跳转到图文消息页面) 图文消息条数限制在1条以内 - if($msg['type']=='news'){ + if ($msg['type'] == 'news') { $queryarr = [ 'msgtype' => 'mpnews', 'mpnews' => [ @@ -1652,7 +1700,7 @@ class Driver ]; } //发送图文消息(点击跳转到外链) 图文消息条数限制在1条以内 - if($msg['type']=='news'){ + if ($msg['type'] == 'news') { $queryarr = [ 'msgtype' => 'news', 'news' => [ @@ -1666,7 +1714,7 @@ class Driver ]; } //语音消息 - if($msg['type']=='voice'){ + if ($msg['type'] == 'voice') { $queryarr = [ 'msgtype' => 'voice', 'voice' => [ @@ -1675,7 +1723,7 @@ class Driver ]; } //视频消息 - if($msg['type']=='video'){ + if ($msg['type'] == 'video') { $queryarr = [ 'msgtype' => 'video', 'video' => [ @@ -1687,7 +1735,7 @@ class Driver ]; } //音乐消息 - if($msg['type']=='music'){ + if ($msg['type'] == 'music') { $queryarr = [ 'msgtype' => 'music', 'video' => [ @@ -1700,7 +1748,7 @@ class Driver ]; } //发送小程序卡片(要求小程序与公众号已关联) - if($msg['type']=='wxapp'){ + if ($msg['type'] == 'wxapp') { $queryarr = [ 'msgtype' => 'miniprogrampage', 'miniprogrampage' => [ @@ -1712,90 +1760,66 @@ class Driver ] ]; } - if(isset($queryarr)){ + if (isset($queryarr)) { $queryarr['touser'] = $touser; - $access_token = $this->getAccessToken($applet_id,2); - $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token; + $access_token = $this->getAccessToken($applet_id, 2); + $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token; return $this->result(Http::post($url, hema_json($queryarr))); } $this->error = '参数错误'; return false; } + /** * 获取令牌 - 开放平台 * $type 请求类型 1小程序,2公众号 - */ + */ function getAccessToken(int $applet_id = 0, int $type = 1) { - $config = $this->config; //获取第三方配置 - - $access_token = ''; - - if($type==1){ - if($applet = Applet::getApplet([ 'applet_id' => $applet_id,'status' => 1 ])) - { - $access_token=$applet['access_token']; - - if(empty($access_token)){ - $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3668302906e3894f&secret=3ef948febd085fb89908f22b4a79c52f' ; - $result = json_decode(Http::get($url), true); - - if(isset($result['access_token'])){ + + if ($applet = Applet::getApplet(['applet_id' => $applet_id, 'status' => 1])) { + $access_token = $applet['access_token']; + + if (empty($access_token)) { + + if (time() > $applet['expires_in']) { + $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3668302906e3894f&secret=3ef948febd085fb89908f22b4a79c52f'; + $result = json_decode(Http::get($url), true); + + if (isset($result['access_token'])) { $access_token = $result['access_token']; $applet->access_token = $result['access_token']; - $applet->expires_in = time()+3600;//2个小时候过期,这里设置1小时获取一次 + $applet->expires_in = time() + 3600;//2个小时候过期,这里设置1小时获取一次 $applet->save();//保存最新的令牌access_token和过期时间 } + } else { + return $access_token; } - - } - }else{ - //公众号 - if($wechat = Wechat::getWechat([ - 'applet_id' => $applet_id, - 'status' => 1 - ])){ - $access_token=$wechat['access_token']; - if($wechat['expires_in'] < time()){ - //重新获取 - $url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token='.$config['component_access_token']; - $queryarr = [ - 'component_appid' => $config['app_id'], - 'authorizer_appid' => $wechat['app_id'], - 'authorizer_refresh_token' => $wechat['authorizer_refresh_token'] - ]; - $result = json_decode(Http::post($url, json_encode($queryarr)),true); - if(isset($result['authorizer_access_token'])){ - $access_token = $result['authorizer_access_token']; - $wechat->access_token = $result['authorizer_access_token']; - $wechat->expires_in = time()+3600; //2个小时候过期,这里设置1小时获取一次 - $wechat->save();//保存最新的令牌access_token和过期时间 - } - } } + } - - return $access_token; } + /** * 请求数据验证 **/ private function result($result) { - $result = json_decode($result,true); - if(isset($result['errcode']) and $result['errcode']!=0){ - $this->error = '错误代码:' . $result['errcode'] . ',错误信息:' . $result['errmsg']; - return false; - } - return $result; + $result = json_decode($result, true); + if (isset($result['errcode']) and $result['errcode'] != 0) { + $this->error = '错误代码:' . $result['errcode'] . ',错误信息:' . $result['errmsg']; + return false; + } + return $result; } + public function getError() { return $this->error; } - + //订阅消息接口 //#################################################################### /** @@ -1804,45 +1828,48 @@ class Driver public function getMessageTemplateList(int $applet_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=' . $access_token; return $this->result(Http::get($url)); } + /** * 删除帐号下的模板 */ public function deleteMessageTemplate(int $applet_id, string $tpl_id) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token=' . $access_token; $queryarr = [ 'priTmplId' => $tpl_id ]; - return $this->result(Http::post($url, hema_json($queryarr),[],['content-type: application/json'])); + return $this->result(Http::post($url, hema_json($queryarr), [], ['content-type: application/json'])); } + /** * 添加帐号下的模板 */ public function addMessageTemplate(int $applet_id, string $tid, array $kidlist, string $desc) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$access_token; + $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=' . $access_token; $queryarr = [ 'tid' => $tid, 'kidList' => $kidlist, 'sceneDesc' => $desc ]; - return $this->result(Http::post($url, hema_json($queryarr),[],['content-type: application/json'])); + return $this->result(Http::post($url, hema_json($queryarr), [], ['content-type: application/json'])); } - /** + + /** * 发送订阅消息 */ public function sendMessage(int $applet_id, array $queryarr) { $access_token = $this->getAccessToken($applet_id); - $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token; - $result = json_decode(Http::post($url,json_encode($queryarr)),true); - write_log($result,__DIR__); - return true; + $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token; + $result = json_decode(Http::post($url, json_encode($queryarr)), true); + write_log($result, __DIR__); + return true; } //#################################################################### } \ No newline at end of file diff --git a/extend/hema/wechat/Index.php b/extend/hema/wechat/Index.php index f294b71..cd632e3 100644 --- a/extend/hema/wechat/Index.php +++ b/extend/hema/wechat/Index.php @@ -7,9 +7,9 @@ use app\applet\model\User as UserModel; use app\common\model\Setting; use app\common\model\Applet; use app\common\model\Wechat; -use Requests; use think\facade\Cache; use hema\Http; +use think\facade\Db; class Index { @@ -29,62 +29,65 @@ class Index ]; - } public function index() { - if (isset($_GET['echostr'])) { echo $_GET["echostr"]; exit; } - //接收微信推送数据 - $nonce = empty ($_GET ['nonce']) ? "" : trim($_GET ['nonce']); - $signature = empty ($_GET['signature']) ? "" : trim($_GET ['signature']); - $timeStamp = empty ($_GET ['timestamp']) ? "" : trim($_GET ['timestamp']); - $msg_signature = empty ($_GET['msg_signature']) ? "" : trim($_GET ['msg_signature']); - $encryptMsg = file_get_contents('php://input'); + $data = file_get_contents('php://input'); - write_log($encryptMsg, __DIR__); - - die('success'); - - - $pc = new WxBizMsgCrypt();//创建解密类 - $msg = ''; - $errCode = $pc->decryptMsg($msg_signature, $timeStamp, $nonce, $encryptMsg, $msg); - if ($errCode == 0) { - $data = _xml_to_arr($msg); //XML转换为数组 - - //首次关注公众号 - if ($data['MsgType'] == 'event') { +// $data = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA); + if (!empty($data)) { + $data = json_decode($data, true); + if (!empty($data)) { + //首次关注 if ($data['Event'] == 'subscribe') { - $wx = new Driver; - if ($wechat_user = $wx->getWechatUserInfo($data['FromUserName'], 10001)) { - //用户操作 - $model = new UserModel; - if ($user = $model->subscribe($wechat_user, $this->wechat['applet_id'])) { - - } + $user = Db::name('food_user')->where('open_id', $data['FromUserName'])->value('is_new'); + if ($user !== 1) { + //不是新关注 + die('success'); } else { - //返回文本提醒 - $wx->sendServiceMsg([ - 'type' => 'text', - 'content' => $wx->getError() - ], $data['FromUserName'], $this->wechat['applet_id']); - } - //是否设置了关注回复 - if ($subscribe = SettingModel::getItem('subscribe', $this->wechat['applet_id'])) { - $this->replyMsg($subscribe, $data);//回复信息 + + //插入一条新用户数据 + $data = [ + 'open_id' => $data['FromUserName'], + 'platform' => 10, + 'applet_id' => 10001, + 'create_time' => time() + ]; + Db::name('food_user')->insert($data); + //发送公众号消息 + + $wx = new Driver; + $access_token = $wx->getAccessToken(10001); + $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token; + $queryarr = [ + 'msgtype' => 'news', + 'news' => [ + 'articles' => [ + 'title' => '您有一张新的优惠券,请及时领取', + 'picurl' => '', + 'url' => 'https://www.baidu.com', + 'description' => '黄辛一新人优惠,首次关注赠送优惠券' + ] + ] + ]; + $queryarr['touser'] = $data['FromUserName']; + Http::post($url, hema_json($queryarr)); + } + die('success'); } } } + }