public function before() { parent::before(); $flag = $this->getNotOpenidAllowed(); if ($flag) { return; } if (!\Session::get('wechat', false) && !\Input::get('openid', false)) { //获取到openid之后跳转的参数列表 //$params = \handler\mp\UrlTool::createLinkstring(\Input::get()); //本站域名 $baseUrl = \Config::get('base_url'); $url = $baseUrl . \Input::server('REQUEST_URI'); $toUrl = urlencode($url); $callback = "{$baseUrl}wxapi/oauth2_callback?to_url={$toUrl}"; $account = \Session::get('WXAccount', \Model_WXAccount::find(1)); $url = \handler\mp\Tool::createOauthUrlForCode($account->app_id, $callback); \Response::redirect($url); } else { if (!\Session::get('wechat', false)) { $wxopenid = \Model_WechatOpenid::query()->where(['openid' => \Input::get('openid')])->get_one(); if (!$wxopenid) { \Session::set_flash('msg', ['status' => 'err', 'msg' => '未找到您的微信信息,无法确认您的身份! 系统无法为您提供服务!', 'title' => '拒绝服务']); return $this->show_mesage(); } \Session::set('wechat', $wxopenid->wechat); \Session::set('OpenID', $wxopenid); \Auth::force_login($wxopenid->wechat->user_id); } else { if (!\Auth::check() && \Session::get('wechat')->user_id) { \Auth::force_login(\Session::get('wechat')->user_id); } } } }
/** * 处理微信服务器推送的请求 * */ public function action_action($appid = false) { if (!$appid) { die(json_decode(['status' => 'err', 'msg' => '非法请求', 'errcode' => 2010])); } if (is_numeric($appid)) { $this->account = \Model_WXAccount::find($appid); } else { if (is_string($appid)) { $this->account = \Model_WXAccount::query()->where('app_id', $appid)->get_one(); } } if (!$this->account) { die(json_decode(['status' => 'err', 'msg' => '该公众号不存在', 'errcode' => 2011])); } \Session::set($this->SESSION_WXACCOUNT_KEY, $this->account); //检验消息合法性 if (!\handler\mp\Tool::checkSignature(\Input::get('signature', false), \Input::get('timestamp', false), \Input::get('nonce', false), $this->account->token)) { \Log::error('WXApi.php check signature account:' . json_encode($this->account->to_array())); die(''); } //接入请求 if (\Input::get('echostr', false)) { if ($this->account->status != 'NONE') { \Log::error('account status error'); die; } else { die(\Input::get('echostr')); } } $this->handler(); }
/** * 发送模板消息 * * $data = [ * 'first' => [ * 'value' => '', * 'color' => '', * ], * 'keynote1' => [ * 'value' => '', * 'color': => '', * ], * 'remark' => [ * 'value' => '', * 'color' => '' * ] * ] * * @param $data 模板数据 * @return mixed 返回发送结果 */ public function send($data) { $params = ['touser' => $this->to, 'template_id' => $this->template_id, 'url' => $this->url, 'data' => $data]; if ($this->account->temp_token_valid < time()) { $result = \handler\mp\Tool::generate_token($this->account->app_id, $this->account->app_secret); $this->account->temp_token = $result['token']; $this->account->temp_token_valid = $result['valid']; $this->account->save(); } $url = "{$this->domain}/cgi-bin/message/template/send?access_token={$this->account->temp_token}"; $result = \handler\common\UrlTool::request($url, 'POST', json_encode($params), true); return json_decode($result); }
/** * 发起微信支付(公众号JSSDK支付) */ public function action_wxpay() { $this->account = \Session::get('WXAccount', \Model_WXAccount::find(\Input::get('account_id', 1))); if (!\Input::get('openid', false)) { //本站域名 $baseUrl = \Config::get('base_url'); $request_uri = \Input::server('REQUEST_URI', ''); if ($request_uri) { $request_uri = substr($request_uri, 1); } $toUrl = urlencode("{$baseUrl}{$request_uri}"); $callback = "{$baseUrl}wxapi/oauth2_callback?to_url={$toUrl}"; $url = \handler\mp\Tool::createOauthUrlForCode($this->account->app_id, $callback); \Response::redirect($url); } $msg = false; if (!\Input::get('order_id', false)) { $msg = ['status' => 'err', 'msg' => '缺少订单ID', 'errcode' => 0, 'title' => '错误']; } else { if (!$this->account) { $msg = ['status' => 'err', 'msg' => '缺少微信公众号ID', 'errcode' => 0, 'title' => '错误']; } } if ($msg) { \Session::set_flash('msg', $msg); return \Response::forge(\View::forge('message/moblie')); } //订单openid赋值 $order = \Model_Order::find(\Input::get('order_id')); if (!$order->buyer_openid) { $openID = \Model_WechatOpenid::query()->where(['openid' => \Input::get('openid')])->get_one(); if ($openID->wechat->user_id == $order->buyer_id) { $order->buyer_openid = \Input::get('openid'); $order->save(); } } //查询收款帐户 $access = \Model_AccessConfig::query()->where('access_type', 'wxpay')->where('seller_id', $order->from_id)->where('enable', 'ENABLE')->get_one(); $result = \handler\mp\Tool::wxpay_order($this->account, $order, $access, \Input::get('openid')); $params = array('appId' => $this->account->app_id, 'timeStamp' => strval(time()), 'nonceStr' => \Str::random('alnum', 16), 'package' => "prepay_id={$result['prepay_id']}", 'signType' => "MD5"); $params['paySign'] = \handler\mp\Tool::getWxPaySign($params, $access->access_key); $params['to_url'] = "/order/home/delivery/{$order->id}"; return \Response::forge(\View::forge('pay/wxpay', $params)); }
/** * 生成JsSdk配置 * * @param $appid 公众号应用ID * @param $ticket 公众号JSApi ticket * @param $url 当前完整URL * @return array 返回完整配置 */ public static function getJssdkConfig($id = 0) { $account = \Session::get('WXAccount', false); if ($id) { $account = \Model_WXAccount::find($id); } //判断ticket是否过期 if (!$account->wechat_ticket_valid || $account->wechat_ticket_valid < time()) { if ($account->temp_token_valid < time()) { $result = \handler\mp\Tool::generate_token($account->app_id, $account->app_secret); $account->temp_token = $result['token']; $account->temp_token_valid = $result['valid']; } $result = \handler\mp\Tool::generate_jssdk_ticket($account->temp_token); $account->wechat_ticket = $result['ticket']; $account->wechat_ticket_valid = $result['valid']; $account->save(); } $url = "http://" . \Input::server('HTTP_HOST') . \Input::server('REQUEST_URI'); //参与签名的参数 $timestamp = time(); $params = array('noncestr' => \Str::random('alnum', 16), 'jsapi_ticket' => $account->wechat_ticket, 'timestamp' => $timestamp, 'url' => $url); //排序 ksort($params); //生成签名 $signature = sha1(\handler\common\UrlTool::createLinkstring($params)); //配置文件 $config = ['debug' => false, 'appId' => $account->app_id, 'timestamp' => $timestamp, 'nonceStr' => $params['noncestr'], 'signature' => $signature, 'jsApiList' => ['onMenuShareTimeline', 'onMenuShareAppMessage', 'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'translateVoice', 'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard']]; return $config; }
/** * 发货操作 * @param int $id 订单ID */ public static function delivery($id = 0) { $msg = false; $order = \Model_Order::find($id); if (!$order) { $msg = ['status' => 'err', 'msg' => '未找到订单,发货失败', 'title' => '错误']; } else { if (in_array($order->order_status, ['NONE', 'WAIT_PAYMENT'])) { $msg = ['status' => 'err', 'msg' => '订单未付款,发货失败', 'title' => '错误']; } else { if ($order->order_status != 'PAYMENT_SUCCESS') { $msg = ['status' => 'err', 'msg' => '订单状态异常,发货失败', 'title' => '错误']; } } } if ($msg) { \Session::set_flash('msg', $msg); return false; } //微信发货 $account = \Session::get('WXAccount', false); if (!$account) { $account = \Model_WXAccount::find(1); } if ($account->temp_token_valid < time()) { $result = \handler\mp\Tool::generate_token($account->app_id, $account->app_secret); $account->temp_token = $result['token']; $account->temp_token_valid = $result['valid']; $account->save(); } $delivery_count = 0; foreach ($order->details as $detail) { $sn = \Model_GoodsAccount::query()->where(['goods_id' => $detail->goods_id, 'status' => 'NONE'])->get_one(); if (!$sn) { $delivery_count++; continue; } if ($order->buyer_openid) { $remark = "订单号:{$order->order_no}\n用户名:{$sn->account}\n密码:{$sn->password}"; $data = \handler\mp\TemplateMsg::get_buy_goods_success($detail->goods->name, $remark); $params = \handler\mp\TemplateMsg::get_base_params($order->buyer_openid, "ARlIzufqpUc8tvCTAVswkny-_AYwYatkxiw42MOa_uA", "http://mall.doujao.com", $data); $flag = \handler\mp\TemplateMsg::send_msg($account->temp_token, $params); if ($flag) { $sn->status = 'USED'; $sn->order_id = $order->id; $sn->save(); } $detail->is_delivery = 1; $detail->save(); } } if ($delivery_count > 0) { \Session::set_flash('msg', ['status' => 'err', 'msg' => "{$delivery_count}件商品发货失败.原因:库存不足!请联系客服.", 'title' => '错误']); return false; } $order->order_status = 'FINISH'; $order->save(); return true; }
$account = \Session::get('WXAccount', false); if ($account) { ?> <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script type="text/javascript"> $(function(){ load_wx(); }); function load_wx(){ if(typeof(wx) == 'undefined'){ return; } <?php $params = \handler\mp\Tool::getJssdkConfig(); ?> wx.config(<?php echo json_encode($params); ?> ); wx.ready(function(){ wx.onMenuShareTimeline({ title: _share_title, link: _share_url, imgUrl: _share_img, success: function(){ if(wechat_share_timeline === 'function'){ wechat_share_timeline(); }