diff --git a/app/cart/controller/ZjCartController.php b/app/cart/controller/ZjCartController.php index 591c121..ec06241 100644 --- a/app/cart/controller/ZjCartController.php +++ b/app/cart/controller/ZjCartController.php @@ -111,16 +111,45 @@ class ZjCartController extends HomeBaseController ->field('c.num,g.name,g.price,g.price_num,g.price_cash,g.thumb,g.is_type,g.intro,t.cid as caid') ->where(['c.uid' => session('user.id'), 'c.id' => ['in', session('cart.id')]]) ->select(); + $total_num = 0; + $total_price = 0; foreach ($data as $k => $item) { if ($item['caid'] == 1) { $item['integral'] = '购买可获得' . $item['price'] * 2 . '积分'; } else { $item['integral'] = '该商品无积分奖励'; } + $total_num += $item['num']; + $total_price += $item['num'] * $item['price']; $data[$k] = $item; } + $address = Db::name('zj_user_place') + ->field('id,name,province,city,county,mobile,place,is_sta') + ->where(['uid' => session('user.id')]) + ->select() + ->toArray(); + $default_name = ''; + $default_mobile = ''; + $default_address = ''; + if (!empty($address)) { + foreach ($address as $item) { + if ($item['is_sta'] == 2) { + $default_name = $item['name']; + $default_mobile = $item['mobile']; + $default_address = $item['province'] . $item['city'] . $item['county'] . $item['place']; + break; + } + } + } return $this->fetch('order_confirm', [ - 'data' => $data + 'data' => $data, + 'address' => $address, + 'default_name' => $default_name, + 'default_mobile' => $default_mobile, + 'default_address' => $default_address, + 'total_num' => $total_num, + 'total_price' => $total_price, + 'caid' => $data[0]['caid'] ]); } diff --git a/app/index/controller/IndexController.php b/app/index/controller/IndexController.php index 86b5327..402bb7f 100644 --- a/app/index/controller/IndexController.php +++ b/app/index/controller/IndexController.php @@ -10,6 +10,37 @@ class IndexController extends HomeBaseController function _initialize() { parent::_initialize(); // TODO: Change the autogenerated stub + if (cmf_is_wechat()) { + if (empty(session('user.id'))) { + require_once EXTEND_PATH . '/WeChatCommon.php'; + $wx = new \WeChatCommon(); + if (request()->param('code') == NULL) { + $wx->code(); + } else { + $code = request()->param('code'); + $userInfo = $wx->getInfo($code); + if(Db::name('user')->where(['openid'=>$userInfo['openid']])->count() == 0) { + //注册新用户 + $data = [ + 'user_type' => 2, + 'create_time' => time(), + 'user_nickname' => $userInfo['nickname'], + 'sex' => $userInfo['sex'], + 'avatar' => $userInfo['headimgurl'] + ]; + if(Db::name('user')->insert($data)) { + $userId = Db::name('user')->getLastInsID(); + session('user.id', $userId); + } + }else { + $userId = Db::name('user')->where(['openid'=>$userInfo['openid']])->value('id'); + session('user.id', $userId); + } + } + } + }else { + $this->error('请从手机微信浏览器打开'); + } session('user.id', 8); } diff --git a/app/pay/controller/PayController.php b/app/pay/controller/PayController.php new file mode 100644 index 0000000..cc52b99 --- /dev/null +++ b/app/pay/controller/PayController.php @@ -0,0 +1,42 @@ +<?php +namespace app\pay\controller; +use cmf\controller\HomeBaseController; + +class PayController extends HomeBaseController +{ + + function _initialize() + { + parent::_initialize(); // TODO: Change the autogenerated stub + if(empty(session('user.id'))) { + $this->error('登录失败'); + } + } + + //提交订单 + public function done() + { + $request = request(); + if($request->isAjax()) { + $order = [ + 'order_num' => date('YmdHis').rand(100000, 999999), + 'step' => 1, + 'uid' => session('user.id'), + 'site' => '收货地址', + 'name' => '收货人姓名', + 'mobile' => '电话', + 'remark' => $request->param('remark'), + 'create_time' => time(), + 'whole' => '订单总金额', + 'whole_num' => '订单总积分', + ]; + } + } + + //微信支付 + public function wxPay() + { + + } + +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index 7b79c6e..c02761e 100644 --- a/public/index.php +++ b/public/index.php @@ -10,7 +10,7 @@ // [ 入口文件 ] // 调试模式开关 -define("APP_DEBUG", true); +define("APP_DEBUG", false); // 定义CMF根目录,可更改此目录 define('CMF_ROOT', __DIR__ . '/../'); diff --git a/public/themes/simpleboot3/cart/zj_cart/cart.html b/public/themes/simpleboot3/cart/zj_cart/cart.html index 0ecbbf8..656e8a1 100644 --- a/public/themes/simpleboot3/cart/zj_cart/cart.html +++ b/public/themes/simpleboot3/cart/zj_cart/cart.html @@ -37,7 +37,7 @@ </div> <!--确认删除--> -<div class="deletewrap" style="display:none"> +<div class="deletewrap" style="display:none" id="del"> <div class="deletepop"> <p class="suredelete">确认删除</p> <p class="certainornot">确定删除该商品吗?</p> @@ -56,16 +56,11 @@ <p class="suredelete">您好,不同专区的商品</p> <p class="certainornot">不可混合支付请重新选择</p> <div class="certain"> - <p class="cancel"><font color="red">确认</font></p> + <p class="cancel warning"><font color="red">确认</font></p> </div> </div> </div> -<script> - $(".cancel").click(function () { - $("#waring").hide(); - }) -</script> <div class="container"> <!--购物车为空--> @@ -176,6 +171,11 @@ </body> <script src="__INDEX__/js/jquery.min.js"></script> <script> + + $(".warning").click(function () { + $("#warning").hide(); + }) + if ({$you}==1){ $('.havegood').remove() $('.nogood').css('display','block') @@ -264,7 +264,7 @@ //删除 $(".delect").click(function(){ if ($('.one').filter('.icon-xuanzhong').length>0){ - $(".deletewrap").show(); + $("#del").show(); } }) //确认删除 @@ -286,7 +286,7 @@ }); //取消删除 $(".cancel").click(function(){ - $(".deletewrap").hide(); + $("#del").hide(); }) //结算 $(".settle").click(function(){ @@ -325,5 +325,9 @@ $(".membercenter").click(function(){ window.location.href="{:url('user/Center/index')}" }) + + + + </script> </html> diff --git a/public/themes/simpleboot3/cart/zj_cart/order_confirm.html b/public/themes/simpleboot3/cart/zj_cart/order_confirm.html index 534bfd3..aa7d156 100644 --- a/public/themes/simpleboot3/cart/zj_cart/order_confirm.html +++ b/public/themes/simpleboot3/cart/zj_cart/order_confirm.html @@ -5,6 +5,7 @@ <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <link rel="stylesheet" href="https://at.alicdn.com/t/font_834805_0ml90wdq5hzm.css"> + <link rel="stylesheet" href="https://at.alicdn.com/t/font_834805_bx3vyrf79mj.css"> <link rel="stylesheet" href="__INDEX__/css/base.css"> <link rel="stylesheet" href="__INDEX__/css/order.css"> <link rel="stylesheet" href="__INDEX__/css/swiper.min.css"> @@ -21,6 +22,49 @@ padding-top: 0.05rem; } + .addresswrapper{ + width: 100%; + height: 100%; + /*background-color: rgba(0,0,0,0.5);*/ + background: #fff; + position: fixed; + top: 0; + left: 0; + right: 0; + z-index: 5; + overflow: scroll; + } + .addaddress{ + display:flex; + } + .addressinfo{ + font-size: 0.26rem; + color:#1A1A1A; + margin-left: 0.25rem; + } + .xuan{ + color:#999999; + } + .addaddress{ + padding: 0.38rem 0.23rem; + border-bottom: 1px solid #f5f5f5; + } + .addresspoup{ + + + } + .icon-xuanze-fangkuang{ + color:#FF0800; + } + .address_text{ + display:flex; + } + .address_num{ + margin-left: 0.27rem; + } + .address_place{ + margin-top: 0.2rem; + } </style> </head> <body> @@ -47,6 +91,24 @@ <div class="surepay">确认支付</div> </div> </div> +<!--选择地址--> +<div class="addresswrapper" style="display:none"> + <div class="addresspoup"> + <volist name="address" id="vo"> + <div class="addaddress"> + <p class="iconfont icon-xuanzekuang xuan" data-id="{$vo.id}"></p> + <p class="iconfont icon-xuanze-fangkuang" style="display:none"></p> + <div class="addressinfo"> + <div class="address_text"> + <p class="address_name" id="name_{$vo.id}">{$vo.name}</p> + <p class="address_num" id="num_{$vo.id}">{$vo.mobile}</p> + </div> + <p class="address_place" id="place_{$vo.id}">{$vo.province}{$vo.city}{$vo.county}{$vo.place}</p> + </div> + </div> + </volist> + </div> +</div> <!--确认支付--> <div class="paywrapper" style="display:none"> <div class="paypop"> @@ -63,11 +125,11 @@ <div class="container"> <div class="people_info"> <div class="people_info_list"> - <p class="people_name">向昱筱</p> - <p class="people_phone">13252648165</p> + <p class="people_name">{$default_name}</p> + <p class="people_phone">{$default_mobile}</p> </div> <p class="iconfont icon-jinru row"> </p> - <p class="people_place">天津市和平区气象台路1088号中环公寓</p> + <p class="people_place">{$default_address}</p> </div> <!--订单信息--> <div class="order_info"> @@ -99,7 +161,7 @@ <div class=" dispatch"> <div class="dispatch_num"> <p class="buy_num">购买数量</p> - <p class="num">x2</p> + <p class="num">x{$total_num}</p> </div> <div class="dispatch_type"> <p class="type">配送方式</p> @@ -114,16 +176,13 @@ <div class="pay_type_list"> <p class="pay_name">微信支付</p> <p class="iconfont icon-xuanzhong"></p> - <p class="iconfont icon-not_Selected-copy" style="display:none"></p> </div> <div class="pay_type_list"> <p class="pay_name">组合支付(现金金额不少于500)</p> - <p class="iconfont icon-xuanzhong" style="display: none;"></p> <p class="iconfont icon-not_Selected-copy"></p> </div> <div class="pay_type_list"> <p class="pay_name">积分(2000积分)</p> - <p class="iconfont icon-xuanzhong" style="display: none;"></p> <p class="iconfont icon-not_Selected-copy"></p> </div> </div> @@ -133,7 +192,7 @@ <div class="total"> <div class="totalcalcute"> <p class="totalsum">合计</p> - <p class="totalmoney">¥60</p> + <p class="totalmoney">¥{$total_price}</p> </div> </div> <!--买家留言--> @@ -145,7 +204,7 @@ <div class="foot"> <div class="buytotal"> <p class="buy_total_price">合计</p> - <p class="buy_total_money">¥800</p> + <p class="buy_total_money">¥{$total_price}</p> </div> <p class="payorder">支付订单</p> </div> @@ -153,11 +212,11 @@ </body> <script src="__INDEX__/js/jquery.min.js"></script> <script> - $(".icon-not_Selected-copy").click(function(){ - $(this).hide(); - $(this).siblings(".icon-xuanzhong").show(); - $(this).parents(".pay_type_list").siblings(".pay_type_list").children(".icon-xuanzhong").hide(); - $(this).parents(".pay_type_list").siblings(".pay_type_list").children(".icon-not_Selected-copy").show(); + $(".iconfont").click(function(){ + $(this).removeClass("icon-not_Selected-copy"); + $(this).addClass("icon-xuanzhong"); + $(this).parents(".pay_type_list").siblings(".pay_type_list").children(".iconfont").removeClass("icon-xuanzhong"); + $(this).parents(".pay_type_list").siblings(".pay_type_list").children(".iconfont").addClass("icon-not_Selected-copy"); }) $(".payorder").click(function(){ $(".wrapper").css("display","block") @@ -170,5 +229,25 @@ $(".wrapper").click(function(){ $(this).css("display","none") }) + + //选择地址 + $(".people_info").click(function(){ + $(".addresswrapper").css("display","block") + }) + var address_id = ''; + $(".xuan").click(function(){ + $(".addresswrapper").css("display","none") + $(this).css("display","none"); + $(this).siblings(".icon-xuanze-fangkuang").css("display","block"); + $(this).parents(".addaddress").siblings(".addaddress").children(".xuan").css("display","block"); + $(this).parents(".addaddress").siblings(".addaddress").children(".icon-xuanze-fangkuang").css("display","none") + var id=$(this).attr('data-id'); + $(".people_name").html($("#name_"+id).html()); + $(".people_phone").html($("#num_"+id).html()); + $(".people_place").html($("#place_"+id).html()); + address_id = id; + }) + + </script> </html> diff --git a/simplewind/extend/Payment.php b/simplewind/extend/Payment.php new file mode 100644 index 0000000..ac9c63a --- /dev/null +++ b/simplewind/extend/Payment.php @@ -0,0 +1,183 @@ +<?php + +class Payment { + + protected $attach; + protected $openid; + protected $body; + protected $total_fee; + + function __construct($attach=null, $openid=null, $body=null, $total_fee=null) { + $this->attach = $attach; + $this->openid = $openid; + $this->body = $body; + $this->total_fee = $total_fee; + } + + /** + * 对外暴露的支付接口 + * @return array + */ + public function pay() { + return $this->weixinPay(); + } + + private function weixinPay() { + //统一下单接口 + $unifiedorder = $this->unifiedorder(); + $parameters = array( + 'appId' => config('AppID'), + 'timeStamp' => '' . time() . '', //时间戳 + 'nonceStr' => $this->createNoncestr(), //随机串 + 'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包 + 'signType' => 'MD5'//签名方式 + ); + //签名 + $parameters['paySign'] = $this->getSign($parameters); + return $parameters; + } + + //统一下单接口 + private function unifiedorder() { + $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; + $parameters = array( + 'appid' => config('AppID'), + 'mch_id' => config('MchId'), + 'nonce_str' => $this->createNoncestr(), + 'body' => $this->body, + 'out_trade_no' => config('MchId').time(), + 'total_fee' => $this->total_fee, + 'spbill_create_ip' => '114.215.223.17', //终端IP + 'notify_url' => '', + 'openid' => $this->openid, + 'trade_type' => 'JSAPI',//交易类型 + 'attach' => $this->attach + ); + //统一下单签名 + $parameters['sign'] = $this->getSign($parameters); + $xmlData = $this->arrayToXml($parameters); + $return = $this->xmlToArray($this->postXmlCurl($xmlData, $url, 60)); + return $return; + } + + //作用:生成签名 + private function getSign($Obj) { + foreach ($Obj as $k => $v) { + $Parameters[$k] = $v; + } + //签名步骤一:按字典序排序参数 + ksort($Parameters); + $String = $this->formatBizQueryParaMap($Parameters, false); + //签名步骤二:在string后加入KEY + $String = $String . "&key=" . config('Key'); + //签名步骤三:MD5加密 + $String = md5($String); + //签名步骤四:所有字符转为大写 + $result_ = strtoupper($String); + return $result_; + } + + private static function postXmlCurl($xml, $url, $second = 30) { + $ch = curl_init(); + //设置超时 + curl_setopt($ch, CURLOPT_TIMEOUT, $second); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验 + //设置header + curl_setopt($ch, CURLOPT_HEADER, FALSE); + //要求结果为字符串且输出到屏幕上 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + //post提交方式 + curl_setopt($ch, CURLOPT_POST, TRUE); + curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); + curl_setopt($ch, CURLOPT_TIMEOUT, 40); + set_time_limit(0); + //运行curl + $data = curl_exec($ch); + //返回结果 + if ($data) { + curl_close($ch); + return $data; + } else { + $error = curl_errno($ch); + curl_close($ch); + throw new WxPayException("curl出错,错误码:$error"); + } + } + + //数组转换成xml + private function arrayToXml($arr) { + $xml = "<root>"; + foreach ($arr as $key => $val) { + if (is_array($val)) { + $xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">"; + } else { + $xml .= "<" . $key . ">" . $val . "</" . $key . ">"; + } + } + $xml .= "</root>"; + return $xml; + } + + + //xml转换成数组 + private function xmlToArray($xml) { + //禁止引用外部xml实体 + libxml_disable_entity_loader(true); + $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); + $val = json_decode(json_encode($xmlstring), true); + return $val; + } + + //作用:产生随机字符串,不长于32位 + private function createNoncestr($length = 32) { + $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; + $str = ""; + for ($i = 0; $i < $length; $i++) { + $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); + } + return $str; + } + + ///作用:格式化参数,签名过程需要使用 + private function formatBizQueryParaMap($paraMap, $urlencode) { + $buff = ""; + ksort($paraMap); + foreach ($paraMap as $k => $v) { + if ($urlencode) { + $v = urlencode($v); + } + $buff .= $k . "=" . $v . "&"; + } + $reqPar=''; + if (strlen($buff) > 0) { + $reqPar = substr($buff, 0, strlen($buff) - 1); + } + return $reqPar; + } + + public function handleNotify() { + //$postXml = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信参数 + $postXml=file_get_contents("php://input"); + if (empty($postXml)) { + return false; + }else { + $data = $this->xmlToArray($postXml); + if($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') { + $data = (array)simplexml_load_string($postXml, 'SimpleXMLElement', LIBXML_NOCDATA); + $signA = "appid=".$data['appid']."&attach=".$data['attach']."&bank_type=".$data['bank_type']."&cash_fee=".$data['cash_fee']."&fee_type=".$data['fee_type']."&is_subscribe=".$data['is_subscribe']."&mch_id=".$data['mch_id']."&nonce_str=".$data['nonce_str']."&openid=". $data['openid']."&out_trade_no=".$data['out_trade_no']."&result_code=".$data['result_code']."&return_code=".$data['return_code']."&time_end=".$data['time_end']."&total_fee=".$data['total_fee']."&trade_type=".$data['trade_type']."&transaction_id=".$data['transaction_id']."&key=".config('Key'); + $sign = strtoupper(MD5($signA)); + if($sign == $data['sign']) { + return $data['attach']; + }else { + return false; + } + }else { + return false; + } + } + } + +} \ No newline at end of file diff --git a/simplewind/extend/WeChatCommon.php b/simplewind/extend/WeChatCommon.php new file mode 100644 index 0000000..174c365 --- /dev/null +++ b/simplewind/extend/WeChatCommon.php @@ -0,0 +1,239 @@ +<?php + +class WeChatCommon { + + /** + * 判断是否已关注公众号 + */ + public function isAuth() { + $access_token = $this->getAccessToken(); + $subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".session('openid'); + $subscribe = json_decode(file_get_contents($subscribe_msg)); + $gzxx = $subscribe->subscribe; + if($gzxx === 1){ + return true; + }else { + return false; + } + } + + /** + * 获取code + */ + public function code(){ + $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".config('AppID')."&redirect_uri=http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; + header('location:'.$url); + } + + /** + * 获取openid + * @param $code + * @return mixed + */ + public function getInfo($code){ + $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('AppID').'&secret='.config('AppSecret').'&code='.$code .'&grant_type=authorization_code'; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $get_token_url); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + $res = curl_exec($ch); + curl_close($ch); + $json_obj = json_decode($res, true); + return $json_obj; + + } + + /** + * 获取access_token,全局缓存7200s + * @return mixed + */ + public function getAccessToken(){ + $data=cache('Vendor/access_token'); + if(!empty($data) && ((time()-$data['time']) < 7000)) { + return $data['access_token']; + } + else { + $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('AppID')."&secret=".config('AppSecret').""; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $output = curl_exec($ch); + curl_close($ch); + $jsoninfo = json_decode($output, true); + $access_token = $jsoninfo["access_token"]; + $time=time(); + $data=array( + 'access_token' => $access_token, + 'time' => $time + ); + cache('Vendor/access_token', $data); + return $access_token; + } + } + + /** + * 获取用户信息(头像、昵称等) + * @return array + */ + public function getUserInfo($openid){ + $url='https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->getAccessToken().'&openid='.$openid; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $output = curl_exec($ch); + curl_close($ch); + $jsoninfo = json_decode($output, true); + $data=array($jsoninfo['nickname'], $jsoninfo['headimgurl']); + return $data; + } + + /** + * 下载网址内容,配合getUserInfo使用 + * @param $url + * @param $filename + * @return mixed + */ + public function curl_file_get_contents($url,$filename){ + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_TIMEOUT, 2); + curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); + curl_setopt($ch, CURLOPT_REFERER,_REFERER_); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $r = curl_exec($ch); + curl_close($ch); + file_put_contents('data/upload/headimg/'.$filename, $r); + return $filename; + } + + /** + * 获取js-sdkp票据,全局缓存7200s + * @return mixed + */ + public function get_jsapi_ticket(){ + $ticket=cache('Vendor/ticket'); + if(!empty($ticket) && ((time()-$ticket['time']) < 7000)) { + return $ticket['ticket']; + }else { + $url='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->getAccessToken().'&type=jsapi'; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + $res = curl_exec($ch); + curl_close($ch); + $json_obj = json_decode($res, true); + $jsapi_ticket = $json_obj['ticket']; + $time=time(); + $data=array( + 'ticket' => $jsapi_ticket, + 'time' => $time + ); + cache('Vendor/ticket', $data); + return $jsapi_ticket; + } + } + + /** + * JS_SDK + * @return array + */ + public function js_sdk(){ + $timestamp=time(); + $string='jsapi_ticket='.$this->get_jsapi_ticket().'&noncestr='.config('nonceStr').'×tamp='.$timestamp.'&url='.'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; + $signature=sha1($string); + return array( + 'appId' => config('AppId'), + 'timestamp' => $timestamp, + 'nonceStr' => config('nonceStr'), + 'signature' => $signature, + ); + } + + /** + * 创建菜单 + * @return mixed|string + */ + public function creatMenu(){ + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->getAccessToken()); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_AUTOREFERER, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $this->menuItem()); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $tmpInfo = curl_exec($ch); + if (curl_errno($ch)) { + return curl_error($ch); + } + curl_close($ch); + return $tmpInfo; + } + + /** + * 菜单内容JSON + * @return string + */ + public function menuItem(){ + $data = '{ + "button":[{ + "type":"view", + "name":"登录/注册", + "url":"http://hospital.wx.bronet.cn/index.php/UserCenter/login" + }], + "button":[{ + "type":"view", + "name":"医生预约", + "url":"http://hospital.wx.bronet.cn/index.php/DoctorAppointment/index" + }], + "button":[{ + "type":"view", + "name":"个人中心", + "url":"http://hospital.wx.bronet.cn/index.php/UserCenter/index" + }] + }'; + return $data; + } + + /** + * 上传永久素材 + */ + public function eternalMaterial(){ + $file_info = array('filename' => '/public/images/soul_of_cinder.png', //国片相对于网站根目录的路径 + 'content-type' => 'image/jpg/png', //文件类型 + 'filelength' => '71' //图文大小 + ); + $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->getAccessToken()."&type=png"; + $ch1 = curl_init(); + $timeout = 5; + $real_path = "{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}"; + //$real_path=str_replace("/", "//", $real_path); + $data = array("media" => "@{$real_path}", 'form-data' => $file_info); + curl_setopt($ch1, CURLOPT_URL, $url); + curl_setopt($ch1, CURLOPT_POST, 1); + curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout); + curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch1, CURLOPT_POSTFIELDS, $data); + $result = curl_exec($ch1); + curl_close($ch1); + if (curl_errno() == 0) { + $result = json_decode($result, true); + var_dump($result); + return $result['media_id']; + } else { + return false; + } + } + +} \ No newline at end of file