Browse Source

订单确认-收货地址

master
ZLW-PC\Administrator 7 years ago
parent
commit
36184f94e3
  1. 14
      app/Common/WechatAuth.php
  2. 426
      app/Common/WechatMenu.php
  3. 2
      app/Http/Controllers/Api/UserAddressController.php
  4. 12
      app/Http/Controllers/Weixin/CartController.php
  5. 2
      app/Http/Model/UserAddress.php
  6. 91
      resources/views/weixin/cart/cartCheckout.blade.php

14
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
*

426
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 '</pre>';
print_r($stream);
echo '</pre>';
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 = "<xml>
<ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
<FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
<CreateTime>{$createtime}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%s</FuncFlag>
</xml>";
return sprintf($textTpl,$text,$funcflag);
}
/**
* 回复图文消息
* @param array $newsData
* @return string
*/
public function makeNews($newsData=array())
{
$createtime = time();
$funcflag = $this->setFlag ? 1 : 0;
$newTplHeader = "<xml>
<ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
<FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
<CreateTime>{$createtime}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[%s]]></Content>
<ArticleCount>%s</ArticleCount><Articles>";
$newTplItem = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$newTplFoot = "</Articles>
<FuncFlag>%s</FuncFlag>
</xml>";
$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 = "<xml>
<ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
<FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
<CreateTime>{$createtime}</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[{$newsData['title']}]]></Title>
<Description><![CDATA[{$newsData['description']}]]></Description>
<MusicUrl><![CDATA[{$newsData['MusicUrl']}]]></MusicUrl>
<HQMusicUrl><![CDATA[{$newsData['HQMusicUrl']}]]></HQMusicUrl>
</Music>
<FuncFlag>%s</FuncFlag>
</xml>";
return sprintf($textTpl,'',$funcflag);
}
/**
* 得到制定分组的用户列表
* @param number $groupid
* @param number $pagesize,每页人数
* @param number $pageidx,起始位置
* @return Ambigous <boolean, string, mixed>
*/
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="<xml>
<ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName>
<FromUserName><![CDATA[{$this->toUsername}]]></FromUserName>
<CreateTime>{$this->times}</CreateTime>
<MsgType><![CDATA[{$type}]]></MsgType>";
//=================type content============
switch($type)
{
case "text" :
$con.="<Content><![CDATA[{$value_arr[0]}]]></Content>
<FuncFlag>{$o_arr}</FuncFlag>";
break;
case "news" :
$con.="<ArticleCount>{$o_arr[0]}</ArticleCount>
<Articles>";
foreach($value_arr as $id=>$v){
if($id>=$o_arr[0]) break; else null; //判断数组数不超过设置数
$con.="<item>
<Title><![CDATA[{$v[0]}]]></Title>
<Description><![CDATA[{$v[1]}]]></Description>
<PicUrl><![CDATA[{$v[2]}]]></PicUrl>
<Url><![CDATA[{$v[3]}]]></Url>
</item>";
}
$con.="</Articles>
<FuncFlag>{$o_arr[1]}</FuncFlag>";
break;
} //end switch
//=================end return============
$con.="</xml>";
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)
{

2
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);
}

12
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);
}
}

2
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;
}

91
resources/views/weixin/cart/cartCheckout.blade.php

@ -5,6 +5,8 @@
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/weixin/mobile.js"></script>
<link href="<?php echo env('APP_URL'); ?>/css/font-awesome.min.css" type="text/css" rel="stylesheet">
<meta name="keywords" content="关键词"><meta name="description" content="描述"></head><body style="background-color:#f1f1f1;">
<!-- 订单确认信息-start -->
<div id="checkout_info">
<div class="classreturn loginsignup">
<div class="ds-in-bl return"><a href="javascript:history.back(-1);"><img src="<?php echo env('APP_URL'); ?>/images/weixin/return.png" alt="返回"></a></div>
<div class="ds-in-bl tit center"><span>确认订单</span></div>
@ -13,12 +15,12 @@
@include('weixin.common.headerNav')
<a href="/v2/index.php?m=default&amp;c=flow&amp;a=consignee_list&amp;u=53657">
<a href="javascript:;" onclick="selectaddress();">
<div class="checkout-addr">
<!-- <p class="des">请添加收货地址</p> -->
<p class="title"><span class="name"><?php echo $user_default_address['name']; ?></span> <span class="tel"><?php echo $user_default_address['mobile']; ?></span></p>
<p class="des"><?php echo $user_default_address['province_name']; ?><?php echo $user_default_address['city_name']; ?><?php echo $user_default_address['district_name']; ?> <?php echo $user_default_address['address']; ?></p>
<i></i>
<input name="default_address_id" type="hidden" id="default_address_id" value="<?php if($user_default_address){echo $user_default_address['id'];} ?>">
<p class="title"><span class="name" id="default_consignee"><?php if($user_default_address){echo $user_default_address['name'];} ?></span> <span class="tel" id="default_phone"><?php if($user_default_address){echo $user_default_address['mobile'];} ?></span></p>
<p class="des" id="default_address"><?php if($user_default_address){ ?><?php echo $user_default_address['province_name']; ?><?php echo $user_default_address['city_name']; ?><?php echo $user_default_address['district_name']; ?> <?php echo $user_default_address['address']; ?><?php }else{ ?>请添加收货地址<?php } ?></p>
<i></i>
</div>
</a>
<style>
@ -26,6 +28,13 @@
.checkout-addr p{margin-right:20px;}.checkout-addr .title{font-size:18px;color:#353535;}.checkout-addr .des{color:#9b9b9b;}
.checkout-addr i{position: absolute;top: 50%;right:12px;margin-top:-6px;color:#bbb;display:inline-block;border-right:2px solid;border-bottom:2px solid;width:12px;height:12px;transform:rotate(-45deg);}
</style>
<script>
function selectaddress()
{
$('#addressList').show();
$('#checkout_info').hide();
}
</script>
<ul class="goodslist">
<?php if($list){foreach($list as $k=>$v){ ?>
<li>
@ -122,6 +131,78 @@ function update_pay_mode(sex)
</style>
<div class="setting"><div class="close"><a href="<?php echo route('weixin_user_logout'); ?>" id="logout">提交</a></div></div>
</div>
<!-- 订单确认信息-end -->
<!-- 收货地址选择-start -->
<div id="addressList" style="display:none;">
<div class="classreturn loginsignup">
<div class="ds-in-bl return"><a href="javascript:addressback();"><img src="<?php echo env('APP_URL'); ?>/images/weixin/return.png" alt="返回"></a></div>
<div class="ds-in-bl tit center"><span>选择收货地址</span></div>
</div>
<script>
function addressback()
{
$('#checkout_info').show();
$('#addressList').hide();
}
function defaultback(id)
{
setdefault(id);
addressback();
//var url = "";
//location.href = url;
}
function setdefault(id)
{
var access_token = '<?php echo $_SESSION['weixin_user_info']['access_token']; ?>';
var url = '<?php echo env('APP_API_URL').'/user_address_setdefault'; ?>';
$.post(url,{access_token:access_token,id:id},function(res)
{
if (res.code == 0)
{
//订单确认页面
$("#default_address_id").val(id);
$("#default_consignee").html($("#consignee"+id).html());
$("#default_phone").html($("#con_phone"+id).html());
$("#default_address").html($("#con_address"+id).html());
}
else
{
//提示
layer.open({
content: res.msg
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
}
}, 'json');
}
</script>
<div class="address_list mt10">
<style>
.address_list .flow-have-adr{padding:15px;margin-bottom:10px;background-color:#fff;}
.address_list .ect-colory{color:#e23435;}
.address_list .f-h-adr-title label{font-size:18px;color:#000;margin-right:5px;}
.address_list .f-h-adr-con{color:#777;margin-top:5px;margin-bottom:5px;}
.bottoma{display:block;font-size:18px;padding:10px;color:white;background-color: #f23030;text-align:center;}
</style>
<?php if($address_list){foreach($address_list as $k=>$v){ ?>
<div class="flow-have-adr" onclick="defaultback('<?php echo $v['id']; ?>')">
<p class="f-h-adr-title"><label id="consignee<?php echo $v['id']; ?>"><?php echo $v['name']; ?></label><span class="ect-colory fr" id="con_phone<?php echo $v['id']; ?>"><?php echo $v['mobile']; ?></span></p>
<p class="f-h-adr-con"><span class="ect-colory"><?php if($v['is_default']==1){ ?>[默认地址]<?php } ?></span><span id="con_address<?php echo $v['id']; ?>"><?php echo $v['province_name'].$v['city_name'].$v['district_name'].' '.$v['address']; ?></span></p>
</div>
<?php }}else{ ?>
<?php } ?>
</div>
</div>
<!-- 收货地址选择-end -->
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/layer/mobile/layer.js"></script>
<script>

Loading…
Cancel
Save