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")); }
/** * 发送模板消息 * * $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); }
/** * 发起打印请求 * * @param $content * @param $machine_code * @param $mkey * @param $time */ public function start_print($content, $machine_code, $mkey, $time) { $params = ['partner' => $this->user_id, 'machine_code' => $machine_code, 'time' => $time, 'content' => $content]; $sign = "{$this->api_key}machine_code{$machine_code}partner{$this->user_id}time{$time}{$mkey}"; $params['sign'] = md5($sign); $params['sign'] = strtoupper($params['sign']); $result = \handler\common\UrlTool::request($this->domain, 'POST', $params); $result = json_decode($result->body); if ($result->state == 1) { return true; } $errmsg = [2 => '提交时间超时.验证你所提交的时间戳超过3分钟事拒绝接受', 3 => '参数有误', 4 => 'sign加密检验失败']; $this->msg = ['status' => 'err', 'msg' => $errmsg[$result->state], 'errcode' => 90100]; return false; }
/** * 获取JSAPI Ticket * @param $access_token 公众号的access_token */ public static function generate_jssdk_ticket($access_token) { $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi"; $result = \handler\common\UrlTool::request($url, 'GET', null, true); $obj = json_decode($result->body); if (isset($obj->errcode) && $obj->errcode > 0) { \Log::error("在获取临时jsapi_ticket时发生异常.异常信息:{$obj->errmsg};异常代码:{$obj->errcode}"); return false; } return ['ticket' => $obj->ticket, 'valid' => time() + ($obj->expires_in - 100)]; }
/** * 网页授权获取用户基本信息回调处理方法 * * @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); }