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_login() { if (\Auth::check()) { $redirect = "/admin"; if (isset($data['to_url'])) { $redirect = $data['to_url']; } \Response::redirect($redirect); } \View::set_global(array('menu' => 'admin-home', 'title' => '登录系统', 'action' => 'login')); if (\Input::method() == 'POST') { if (\Auth::login()) { if (\Auth::get_user()->username == 'admin') { \Response::redirect('/admin'); } $employee = \Model_Employee::query()->where('parent_id', \Auth::get_user()->id)->get_one(); if (!$employee) { \Session::set_flash('msg', ['status' => 'err', 'msg' => '非法登录,多次尝试登录,您的帐户将被封锁!', 'title' => '警告', 'sub_title' => '非法登录', 'icon' => 'exclamation-circle', 'color' => '#d9534f']); return $this->not_login_alert(); } // 保存会话信息: 当前登录人员的身份、所属商户、微信公众号信息 \Session::set('seller', $employee->seller); \Session::set('people', $employee->people); \Session::set('employee', $employee); // 查询当前商户默认公众号信息 $accounts = \Model_WXAccount::query()->where(['seller_id' => $employee->seller->id])->get(); $account = false; if (count($accounts) > 1) { foreach ($accounts as $item) { if ($account->is_default == 1) { $account = $item; break; } } } else { $account = current($accounts); } \Session::set('WXAccount', $account); //获取API访问令牌 $result = \handler\common\UrlTool::request(\Config::get('base_url') . 'api/token.json?user_id=' . \Auth::get_user()->id); $token = json_decode($result->body); \Session::set('access_token', $token->access_token); $redirect = "/admin"; if (isset($data['to_url'])) { $redirect = $data['to_url']; } \Response::redirect($redirect); } \Session::set_flash('msg', array('status' => 'err', 'msg' => '登录失败', 'errcode' => 20)); } return \Response::forge(\View::forge("ace/login")); }
public function action_order_create() { $params = ['first' => ['value' => '订单支付成功', 'color' => '#D02090'], 'keyword1' => ['value' => date('YmdHis'), 'color' => '#D02090'], 'keyword2' => ['value' => '菊花茶,西湖龙井,豪华果盘,精品果盘,百威,哈尔滨啤酒,威尔士香槟', 'color' => '#D02090'], 'keyword3' => ['value' => 100, 'color' => '#D02090'], 'remark' => ['value' => '点击查看订单已使用状态', 'color' => '#D02090']]; $account = \Model_WXAccount::find(1); $to_openid = 'oqTo9uJao4vdZy5EZH8yQgL_0SY0'; $tmpl_id = 'tQ46mymM617VOKpNv6rbg5hBQpXIle8EC64n-ozbSSw'; $tmpl = new \handler\mp\TemplateMsg($account, $to_openid, $tmpl_id, 'http://www.baidu.com'); $result = $tmpl->send($params); if ($result->errcode != 0) { die('模板消息发送失败'); } die('消息发送成功'); }
public function action_menus() { $params = array('title' => "菜单项", 'menu' => 'wechat-menu', 'action_name' => "自定义菜单"); $account = \Model_WXAccount::find(\Session::get('WXAccount')->id); if (!$account) { if (\Input::is_ajax()) { die(json_encode(array('status' => 'err', 'msg' => '您还未绑定有效公众帐户', 'errcode' => 10))); } die('您还未绑定有效公众帐户'); } $params['items'] = isset($account->menu) && $account->menu ? json_decode($account->menu) : ''; \View::set_global($params); return \View::forge("ace/mp/coustom_menu/moblie"); }
public static function createAccount($data) { if (!isset($data['is_subscribe_create_user'])) { $data['is_subscribe_create_user'] = false; } if (!isset($data['is_subscribe_create_member'])) { $data['is_subscribe_create_member'] = false; } if (!isset($data['create_user_default_group'])) { $data['create_user_default_group'] = 1; } $account = \Model_WXAccount::forge($data); if (!$account->save()) { return false; } return $account; }
/** * 获取token */ public function action_index() { $data = ['status' => 'err', 'msg' => '缺少必要参数', 'errcode' => 90001]; $user_id = \Input::get('user_id', false); $wechat_id = \Input::get('wechat_id', false); $open_id = \Input::get('open_id', false); $store_id = \Input::get('store_id', false); $wx_account_id = \Input::get('wx_account_id', false); if (!$user_id || !$wechat_id || !$open_id || !$store_id || !$wx_account_id) { return $this->response($data, 403); } $user = false; $wechat = false; $openid = false; $store = false; $account = false; if ($user_id) { $user = \Model_User::find($user_id); } if ($wechat_id) { $wechat = \Model_Wechat::find($wechat_id); } if ($open_id) { $openid = \Model_WechatOpenid::find($open_id); } if ($store_id) { $store = \Model_Store::find($store_id); } if ($wx_account_id) { $account = \Model_WXAccount::find($wx_account_id); } if (!$user || !$wechat || !$openid || !$store || !$account) { return $this->response($data, 403); } $params = ['user_id' => $user_id, 'store_id' => $store_id, 'wechat_id' => $wechat_id, 'openid_id' => $open_id, 'wx_account_id' => $wx_account_id]; $token = \Model_ApiToken::forge(['token' => md5("{$user_id}{$wechat_id}{$open_id}{$store_id}{$wx_account_id}" . time()), 'expire_at' => time() + 7200, 'data' => serialize((object) $params)]); $token->save(); $data = ['status' => 'succ', 'msg' => 'ok', 'errcode' => 0, 'expires_in' => 7200, 'access_token' => base64_encode($token->token)]; $this->response($data, 200); }
public function auth() { $flag = false; if (!\Input::get('access_token', false)) { return $flag; } $token = \Model_ApiToken::query()->where('token', base64_decode(\Input::get('access_token')))->get_one(); if (!$token) { return false; } else { if ($token->expire_at < time()) { return false; } } $data = unserialize($token->data); $this->user = \Model_User::find($data->user_id); if (\Input::param('store_id', false)) { $this->store = \Model_Store::find(\Input::param('store_id')); $this->seller = $this->wx_account->seller; } if (\Input::param('wechat_id', false)) { $this->wechat = \Model_Wechat::find(\Input::param('wechat_id')); } if (\Input::param('openid_id', false)) { $this->store = \Model_WechatOpenid::find(\Input::param('openid_id')); } if (\Input::param('account_id', false)) { $this->wx_account = \Model_WXAccount::find(\Input::param('account_id')); $this->seller = $this->wx_account->seller; } if (\Input::param('seller_id', false)) { $this->seller = \Model_Seller::find(\Input::param('seller_id')); } //解析access_token,并查询access_token有效期 //有效返回true否则返回false return $this->user ? true : false; }
/** * 生成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; }
/** * 发起微信扫码支付 * * 调用示例:/services/wxpay_qrcode?account_id=1&goods_id=2 */ public function action_wxpay_qrcode() { $msg = false; if (!\Input::get('account_id', false) && !\Session::get($this->SESSION_WXACCOUNT_KEY, false)) { $msg = ['status' => 'err', 'msg' => '缺少微信公众号ID', 'errcode' => 0, 'title' => '错误']; } else { if (!\Input::get('goods_id', false)) { $msg = ['status' => 'err', 'msg' => '缺少商品ID', 'errcode' => 0, 'title' => '错误']; } } if ($msg) { \Session::set_flash('msg', $msg); return \Response::forge(\View::forge('message/moblie')); } $account = false; if (\Input::get('account_id', false)) { $account = \Model_WXAccount::find(\Input::get('account_id')); } else { $account = \Session::get($this->SESSION_WXACCOUNT_KEY); } $config = \Model_AccessConfig::query()->where(['seller_id' => $account->seller_id, 'access_type' => 'wxpay'])->get_one(); $params = ['appid' => $account->app_id, 'mch_id' => $config->access_id, 'product_id' => \Input::get('goods_id'), 'time_stamp' => time(), 'nonce_str' => \Str::random('alnum', 16)]; ksort($params); reset($params); $signStr = handler\common\UrlTool::createLinkstring($params); $signStr = "{$signStr}&key={$config->access_key}"; $params['sign'] = strtoupper(md5($signStr)); $url = "weixin://wxpay/bizpayurl?" . handler\common\UrlTool::createLinkstring($params); $url = urlencode($url); die($url); }
/** * 发货操作 * @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; }
/** * 网页授权获取用户基本信息回调处理方法 * * @access public * @return Response */ public function action_oauth2_callback() { $params = \Input::get(); if (!\Input::get('code', false)) { \Session::set_flash('msg', ['status' => 'err', 'msg' => '你拒绝授权,系统无法确认您的身份!系统中止!', 'title' => '错误']); return $this->show_message(); } $this->account = \Session::get('WXAccount', \Model_WXAccount::find(1)); $url = handler\mp\Tool::createOauthUrlForOpenid($this->account->app_id, $this->account->app_secret, $params['code']); $result = \handler\common\UrlTool::request($url, 'GET', null, true); $result = json_decode($result->body); if (!isset($result->openid) || !$result->openid) { \Session::set_flash('msg', ['status' => 'err', 'msg' => '未获取到OpenId!', 'title' => '错误']); return $this->show_message(); } //跳转参数加openid $to_url = \Input::get('to_url', '/'); $addspan = strpos($to_url, '?') !== false ? '&' : '?'; $to_url = "{$to_url}{$addspan}openid={$result->openid}"; //获取openid对象 $wechatOpenID = \Model_WechatOpenid::query()->where(['openid' => $result->openid])->get_one(); //openid存在,不需要创建 if ($wechatOpenID) { \Response::redirect($to_url); return; } //拉取用户信息 $url = handler\mp\Tool::createOauthUrlForUserinfo($result->access_token, $result->openid); $result = \handler\common\UrlTool::request($url, 'GET', null, true); $result = json_decode($result->body); if (isset($result->errcode)) { \Session::set_flash('msg', ['status' => 'err', 'msg' => $result->errmsg, 'title' => '错误']); return $this->show_message(); } //查询微信用户信息是否存在 $wechat = \Model_Wechat::query()->where(['nickname' => $result->nickname, 'sex' => $result->sex, 'city' => $result->city, 'province' => $result->province, 'country' => $result->country, 'headimgurl' => $result->headimgurl])->get_one(); //存在则直接赋值微信信息记录 if ($wechat) { $wechatOpenID->wechat_id = $wechatOpenID->id; return; } //创建openid数据及微信信息 $wechatOpenID = handler\mp\Account::createWechatAccount($result->openid, $this->account); if (!$wechatOpenID) { \Session::set_flash('msg', ['status' => 'err', 'msg' => '微信信息保存失败! 缺少必要信息,系统终止!', 'title' => '错误']); return $this->show_message(); } $wechat = $wechatOpenID->wechat; # 保存拉取到的用户信息 $wechat->nickname = $result->nickname; $wechat->sex = $result->sex; $wechat->city = $result->city; $wechat->province = $result->province; $wechat->country = $result->country; $wechat->headimgurl = $result->headimgurl; $wechat->language = isset($result->language) ? $result->language : ''; $wechat->subscribe_time = isset($result->subscribe_time) ? $result->subscribe_time : 0; $wechat->subscribe = isset($result->subscribe) ? $result->subscribe : 0; $wechat->save(); \Response::redirect($to_url); }
/** * 同步素材 */ public function action_syn() { $id = \Input::get('account_id'); $id = $id ? $id : \Session::get('WXAccount')->id; $account = \Model_WXAccount::find($id); $account->checkToken(); $result = \handler\mp\Api::syn_material($account->temp_token); if ($result !== true) { die(json_encode($result)); } }
public function action_exist($filed = 'open_id', $value = '') { $account = \Model_WXAccount::query()->where($filed, $value)->get(); if (!$account) { die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => false))); } die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => true))); }