From 36184f94e3b59003453cd8a9a7d83d97cfd63406 Mon Sep 17 00:00:00 2001 From: "ZLW-PC\\Administrator" <374861669@qq.com> Date: Wed, 15 Nov 2017 17:31:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E7=A1=AE=E8=AE=A4-=E6=94=B6?= =?UTF-8?q?=E8=B4=A7=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Common/WechatAuth.php | 14 +- app/Common/WechatMenu.php | 426 +++++++++++++++++- .../Controllers/Api/UserAddressController.php | 2 +- .../Controllers/Weixin/CartController.php | 12 +- app/Http/Model/UserAddress.php | 2 +- .../views/weixin/cart/cartCheckout.blade.php | 91 +++- 6 files changed, 529 insertions(+), 18 deletions(-) diff --git a/app/Common/WechatAuth.php b/app/Common/WechatAuth.php index 107720d..af192ea 100644 --- a/app/Common/WechatAuth.php +++ b/app/Common/WechatAuth.php @@ -2,7 +2,8 @@ namespace App\Common; /** - * OAuth2.0微信授权登录实现 + * OAuth2.0微信授权登录实现/微信PC扫码授权登录 + * 微信/PC扫码登录,两种的方式是一样的,先跳转到微信网页获取code,通过code获取token,通过token获取用户信息 */ class WechatAuth { @@ -27,6 +28,17 @@ class WechatAuth return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->app_id."&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect"; } + /** + * 微信PC扫码授权登录链接 + * + * @param string $redirect_uri 回调地址,授权后重定向的回调链接地址,请使用urlEncode对链接进行处理 + * @param mixed $state 可以为空,重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 + */ + public function get_qrconnect_url($redirect_uri = '', $state = '') + { + return "https://open.weixin.qq.com/connect/qrconnect?appid".$this->app_id."&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope=snsapi_login&state=".$state."#wechat_redirect"; + } + /** * 获取授权token * diff --git a/app/Common/WechatMenu.php b/app/Common/WechatMenu.php index 2fb4c70..c4cc971 100644 --- a/app/Common/WechatMenu.php +++ b/app/Common/WechatMenu.php @@ -34,17 +34,8 @@ class WechatMenu return json_decode($token_data, true); } - //获取关注者列表 - public function get_user_list($next_openid = NULL) - { - $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token."&next_openid=".$next_openid; - $res = $this->http($url); - return json_decode($res, true); - } - /** * 自定义菜单创建 - * * @param string $jsonmenu */ public function create_menu($jsonmenu) @@ -53,6 +44,423 @@ class WechatMenu return $this->http($url, $jsonmenu); } + /** + * 查询菜单 + * @param $access_token 已获取的ACCESS_TOKEN + */ + public function getmenu($access_token) + { + # code... + $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$this->access_token; + $data = file_get_contents($url); + return $data; + } + + /** + * 删除菜单 + * @param $access_token 已获取的ACCESS_TOKEN + */ + public function delmenu($access_token) + { + # code... + $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->access_token; + $data = json_decode(file_get_contents($url),true); + if ($data['errcode']==0) + { + # code... + return true; + } + else + { + return false; + } + } + + /** + * 获取最新5天关注用户发过来的消息,消息id,用户fakeid,昵称,消息内容 + * + * 返回结构:id:msgId; fakeId; nickName; content; + * + * @return array + */ + public function newmesg() + { + $url = 'https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&token='.$this->access_token.'&lang=zh_CN&count=50&rad='.rand(10000, 99999); + $stream = $this->http($url); + + preg_match('/< type="json" id="json-msgList">(.*?)<\/>/is', $stream, $match); + $json = json_decode($match[1], true); + $returns = array(); + foreach ( $json as $val) + { + if ( $val['starred'] == '0') + { + $returns[] = $val; + } + } + + return $returns; + } + + /** + * 设置标记 + * + * @param integer $msgId 消息标记 + * @return boolean + */ + public function start($msgId) + { + $url = 'https://mp.weixin.qq.com/cgi-bin/setstarmessage?t=ajax-setstarmessage&rad='.rand(10000, 99999); + $post = 'msgid='.$msgId.'&value=1&token='.$this->access_token.'&ajax=1'; + $stream = $this->http($url, $post); + + // 是不是设置成功 + $html = preg_replace("/^.*\{/is", "{", $stream); + $json = json_decode($html, true); + + return (boolean)$json['msg'] == 'sys ok'; + } + + /** + * 发送消息 + * + * 结构 $param = array(fakeId, content, msgId); + * @param array $param + * @return boolean + */ + public function sendmesg($param) + { + $url = 'https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response'; + $post = 'error=false&tofakeid='.$param['fakeId'].'&type=1&content='.$param['content'].'&quickreplyid='.$param['msgId'].'&token='.$this->access_token.'&ajax=1'; + + $stream = $this->http($url, $post); + $this->start($param['msgId']); + + // 是不是设置成功 + $html = preg_replace("/^.*\{/is", "{", $stream); + $json = json_decode($html, true); + return (boolean)$json['msg'] == 'ok'; + } + + /** + * 主动发消息结构 + * $param = array(fakeId, content); + * @param array $param + * @return [type] [description] + */ + public function send($param) + { + $url = 'https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN'; + //$post = 'ajax=1&appmsgid='.$param['msgid'].'&error=false&fid='.$param['msgid'].'&tofakeid='.$param['fakeId'].'&token='.$this->access_token.'&type=10'; + $post = 'ajax=1&content='.$param['content'].'&error=false&tofakeid='.$param['fakeId'].'&token='.$this->access_token.'&type=1'; + $stream = $this->html($url, $post); + // 是不是设置成功 + $html = preg_replace("/^.*\{/is", "{", $stream); + $json = json_decode($html, true); + return (boolean)$json['msg'] == 'ok'; + } + + /** + * 批量发送(可能需要设置超时) + * $param = array(fakeIds, content); + * @param array $param + * @return [type] [description] + */ + public function batSend($param) + { $url = 'https://mp.weixin.qq.com/cgi-bin/masssend?t=ajax-response'; + $post = 'ajax=1&city=&content='.$param['content'].'&country=&error=false&groupid='.$param['groupid'].'&needcomment=0&province=&sex=0&token='.$this->access_token.'&type=1'; + $stream = $this->html($url, $post); + // 是不是设置成功 + $html = preg_replace("/^.*\{/is", "{", $stream); + $json = json_decode($html, true); + return (boolean)$json['msg'] == 'ok'; + } + + /** + * 新建图文消息 + */ + public function setNews($param, $post_data) + { + $url = 'https://mp.weixin.qq.com/cgi-bin/sysnotify?lang=zh_CN&f=json&begin=0&count=5'; + $post = 'ajax=1&token='.$this->access_token.''; + $stream = $this->html($url, $post); + //上传图片 + $url = 'https://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type='.$param['type'].'&token='.$this->access_token.'&t=iframe-uploadfile&lang=zh_CN&formId=1'; + $stream = $this->_uploadFile($url, $post_data); + echo ''; + print_r($stream); + echo ''; + exit; + } + + /** + * 获得用户发过来的消息(消息内容和消息类型) + */ + public function getMsg() + { + $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; + if ($this->debug) { + $this->write_log($postStr); + } + if (!empty($postStr)) { + $this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); + $this->msgtype = strtolower($this->msg['MsgType']);//获取用户信息的类型 + $this->eventkey = strtolower($this->msg['EventKey']);//获取key值 + } + } + /** + * 回复文本消息 + * @param string $text + * @return string + */ + public function makeText($text='') + { + $createtime = time(); + $funcflag = $this->setFlag ? 1 : 0; + $textTpl = " + msg['FromUserName']}]]> + msg['ToUserName']}]]> + {$createtime} + + + %s + "; + return sprintf($textTpl,$text,$funcflag); + } + /** + * 回复图文消息 + * @param array $newsData + * @return string + */ + public function makeNews($newsData=array()) + { + $createtime = time(); + $funcflag = $this->setFlag ? 1 : 0; + $newTplHeader = " + msg['FromUserName']}]]> + msg['ToUserName']}]]> + {$createtime} + + + %s"; + $newTplItem = " + <![CDATA[%s]]> + + + + "; + $newTplFoot = " + %s + "; + $content = ''; + $itemsCount = count($newsData['items']); + $itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条 + if ($itemsCount) { + foreach ($newsData['items'] as $key => $item) { + $content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picUrl'],$item['url']);//微信的信息数据 + + } + } + $header = sprintf($newTplHeader,$newsData['content'],$itemsCount); + $footer = sprintf($newTplFoot,$funcflag); + return $header . $content . $footer; + } + /** + * 回复音乐消息 + * @param array $newsData + * @return string + */ + public function makeMusic($newsData=array()) + { + $createtime = time(); + $funcflag = $this->setFlag ? 1 : 0; + $textTpl = " + msg['FromUserName']}]]> + msg['ToUserName']}]]> + {$createtime} + + + <![CDATA[{$newsData['title']}]]> + + + + + %s + "; + return sprintf($textTpl,'',$funcflag); + } + + /** + * 得到制定分组的用户列表 + * @param number $groupid + * @param number $pagesize,每页人数 + * @param number $pageidx,起始位置 + * @return Ambigous + */ + public function getfriendlist($groupid=0,$pagesize=500,$pageidx=0) + { + $url = 'https://mp.weixin.qq.com/cgi-bin/contactmanagepage?token='.$this->access_token.'&t=wxm-friend&lang=zh_CN&pagesize='.$pagesize.'&pageidx='.$pageidx.'&groupid='.$groupid; + $referer = "https://mp.weixin.qq.com/"; + $response = $this->html($url, $referer); + if (preg_match('%< id="json-friendList" type="json/text">([\s\S]*?)%', $response, $match)) + { + $tmp = json_decode($match[1], true); + } + + return $tmp; + } + + /** + * 返回给用户信息 + * + */ + public function reply($data) + { + echo $data; + } + + /** + *@param type: text 文本类型, news 图文类型 + *@param value_arr array(内容),array(ID) + *@param o_arr array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID) + */ + private function make_xml($type,$value_arr,$o_arr=array(0)) + { + //=================xml header============ + $con=" + fromUsername}]]> + toUsername}]]> + {$this->times} + "; + + //=================type content============ + switch($type) + { + case "text" : + $con.=" + {$o_arr}"; + break; + + case "news" : + $con.="{$o_arr[0]} + "; + foreach($value_arr as $id=>$v){ + if($id>=$o_arr[0]) break; else null; //判断数组数不超过设置数 + $con.=" + <![CDATA[{$v[0]}]]> + + + + "; + } + $con.=" + {$o_arr[1]}"; + break; + } //end switch + //=================end return============ + $con.=""; + + return $con; + } + + //获取关注者列表 + public function get_user_list($next_openid = null) + { + $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token."&next_openid=".$next_openid; + $res = $this->http($url); + return json_decode($res, true); + } + + //获取用户基本信息 + public function get_user_info($openid) + { + $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN"; + $res = $this->http($url); + return json_decode($res, true); + } + + //发送客服消息,已实现发送文本,其他类型可扩展 + public function send_custom_message($touser, $type, $data) + { + $msg = array('touser' =>$touser); + switch($type) + { + case 'text': + $msg['msgtype'] = 'text'; + $msg['text'] = array('content'=> urlencode($data)); + break; + } + $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token; + return $this->http($url, urldecode(json_encode($msg))); + } + + //生成参数二维码 + public function create_qrcode($scene_type, $scene_id) + { + switch($scene_type) + { + case 'QR_LIMIT_SCENE': //永久 + $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}'; + break; + case 'QR_SCENE': //临时 + $data = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}'; + break; + } + $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token; + $res = $this->http($url, $data); + $result = json_decode($res, true); + return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($result["ticket"]); + } + + //创建分组 + public function create_group($name) + { + $data = '{"group": {"name": "'.$name.'"}}'; + $url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=".$this->access_token; + $res = $this->http($url, $data); + return json_decode($res, true); + } + + //移动用户分组 + public function update_group($openid, $to_groupid) + { + $data = '{"openid":"'.$openid.'","to_groupid":'.$to_groupid.'}'; + $url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=".$this->access_token; + $res = $this->http($url, $data); + return json_decode($res, true); + } + + //上传多媒体文件 + public function upload_media($type, $file) + { + $data = array("media" => "@".dirname(__FILE__).'\\'.$file); + $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type; + $res = $this->http($url, $data); + return json_decode($res, true); + } + + private function checkSignature() + { + $signature = $_GET["signature"]; + $timestamp = $_GET["timestamp"]; + $nonce = $_GET["nonce"]; + + $token = 'weixin'; + $tmpArr = array($token, $timestamp, $nonce); + sort($tmpArr); + $tmpStr = implode( $tmpArr ); + $tmpStr = sha1( $tmpStr ); + + if( $tmpStr == $signature ) + { + return true; + } + else + { + return false; + } + } + // cURL函数简单封装 public function http($url, $data = null) { diff --git a/app/Http/Controllers/Api/UserAddressController.php b/app/Http/Controllers/Api/UserAddressController.php index a6c889b..ca6bd17 100644 --- a/app/Http/Controllers/Api/UserAddressController.php +++ b/app/Http/Controllers/Api/UserAddressController.php @@ -40,7 +40,7 @@ class UserAddressController extends CommonController $id = $request->input('id',null); $res = UserAddress::getOne(Token::$uid,$id); - if(!$res) + if($res === false) { return ReturnData::create(ReturnData::SYSTEM_FAIL); } diff --git a/app/Http/Controllers/Weixin/CartController.php b/app/Http/Controllers/Weixin/CartController.php index caddf7a..4fdc521 100644 --- a/app/Http/Controllers/Weixin/CartController.php +++ b/app/Http/Controllers/Weixin/CartController.php @@ -56,7 +56,7 @@ class CartController extends CommonController $res = curl_request($url,$postdata,'GET'); $data['payment_list'] = $res['data']['list']; - //支付方式列表 + //用户默认收货地址 $postdata = array( 'access_token' => $_SESSION['weixin_user_info']['access_token'] ); @@ -64,6 +64,16 @@ class CartController extends CommonController $res = curl_request($url,$postdata,'GET'); $data['user_default_address'] = $res['data']; + //用户收货地址列表 + //收货地址列表 + $postdata = array( + 'limit' => 100, + 'access_token' => $_SESSION['weixin_user_info']['access_token'] + ); + $url = env('APP_API_URL')."/user_address_list"; + $res = curl_request($url,$postdata,'GET'); + $data['address_list'] = $res['data']['list']; + return view('weixin.cart.cartCheckout', $data); } } \ No newline at end of file diff --git a/app/Http/Model/UserAddress.php b/app/Http/Model/UserAddress.php index b88788c..38fe811 100644 --- a/app/Http/Model/UserAddress.php +++ b/app/Http/Model/UserAddress.php @@ -63,7 +63,7 @@ class UserAddress extends BaseModel if ($address_id) { - $arr = self::where('id',$address_id)->first(); + $arr = self::where(array('id'=>$address_id,'user_id'=>$user_id))->first(); return $arr; } diff --git a/resources/views/weixin/cart/cartCheckout.blade.php b/resources/views/weixin/cart/cartCheckout.blade.php index 20a3dc0..96d238a 100644 --- a/resources/views/weixin/cart/cartCheckout.blade.php +++ b/resources/views/weixin/cart/cartCheckout.blade.php @@ -5,6 +5,8 @@ + +
返回
确认订单
@@ -13,12 +15,12 @@ @include('weixin.common.headerNav') - +
- -

-

- + +

+

请添加收货地址

+
+
    $v){ ?>
  • @@ -122,6 +131,78 @@ function update_pay_mode(sex) +
+ + + + +