appid; $secret = $this->secret; // $code = '0b1tFv100Sm91Q1kko0004vZGu0tFv12'; $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code'; list($status,$res) = $this->wx_return($url); if($status) return [true,$res['openid']]; else return [false,$res]; } public function getToken(){ $token_key = $this->appid.'_wx_token'; $token = Redis::get($token_key); if(!empty($token)){ return [true,$token]; } $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->secret; list($status,$res) = $this->wx_return($url); if($status) { Redis::setex($token_key,7100,$res['access_token']); return [true,$res['access_token']]; } else return [false,$res]; } public function getMobile($code){ list($status,$token) = $this->getToken(); $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$token; $post = [ 'post'=>json_encode([ 'code' => $code, ]), ]; $post['header'][] = "Content-Type:application/json"; list($status,$res) = $this->wx_return($url,$post); if($status) return [true,$res['phone_info']['phoneNumber']]; else return [false,$res]; } private function wx_return($url,$data=[]){ $res = $this->curlOpen($url,$data); $res = json_decode($res,true); if(isset($res['errcode'])&&$res['errcode'] !== 0) return [false,$res['errmsg']]; return [true,$res]; } //写一个单独的微信推送 /** * @param $user_id * @param $type 1审核申请 2抄送 3 审核结果 * @param $state 0申请审核1审核通过2审核拒绝 * @param $menu_id * @param $order_data * @return array */ public function wx_sendMsg($user_id,$type,$state,$menu_id,$order_data){ $openid = WxEmployeeOfficial::where('employee_id',$user_id)->value('openid'); if(empty($openid)) return [false,'not invaild openid']; $config = config('wx.msg'); switch ($type){ case '1': case '2': $menu_type = $menu_id.'_'.$type; break; case '3': $menu_type = $menu_id.'_'.$type.'_'.$state; break; default : $menu_type = ''; } if(!isset($config['wx_menu'][$menu_type])) return [false,'not invaild menu_type']; $tmp_data = $config['wx_tmp_id'][$config['wx_menu'][$menu_type]]; $detail = $tmp_data['param']; $tmp_id = $tmp_data['tmp_id']; $data = []; foreach ($detail as $k=>$v){ $data[$v] = $order_data[$k]; } $this->sendTmpMsg($openid,$tmp_id,['detail'=>$data]); return [true,'']; } public function sendTmpMsg($openid,$tempid,$data){ $reload_url = $data['reload_url']??''; list($status,$token) = $this->getToken(); if(!$status) return [false,$token]; $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token; $post = '{ "touser":"'.$openid.'", "template_id":"'.$tempid.'", "url":"'.$reload_url.'", "data":{ "first": { "value":"1", "color":"#173177" }, %s "remark":{ "value":"1", "color":"#173177" } } }'; $content = ""; foreach ($data['detail'] as $k=>$v){ $content .= '"'.$k.'": { "value":"'.$v.'", "color":"#173177" },'; } $post = sprintf($post,$content); $res = $this->curlOpen($url,['post'=>$post]); $res = json_decode($res,true); if(isset($res['errcode'])&&$res['errcode'] != 0) return [false,$res['errmsg']]; if(isset($res['errcode'])&&$res['errcode'] === 0) return [true,'']; return [false,json_encode($res)]; } }