コード例 #1
0
ファイル: account.php プロジェクト: wxl2012/wx
 /**
  * 根据微信推送的包,创建相关帐户信息
  *
  * @param $openid 微信服务器推送的微信粉丝OpenId
  * @param $account 接受微信服务器推送数据的公众号实体对象
  * @return 创建成功返回微信OpenId数据对象,否则返回False
  */
 public static function createWechatAccount($openid, $account = [])
 {
     //创建微信信息
     $wechat = \Model_Wechat::forge(['nickname' => $openid]);
     //是否创建用户登录信息
     if (isset($account->is_subscribe_create_user) && $account->is_subscribe_create_user) {
         $params = ['username' => "wx_{$openid}", 'password' => "w{$account->id}#{$openid}", 'email' => "wx_{$openid}@{$account->id}.com", 'group_id' => $account->create_user_default_group];
         $user_id = \Model_User::createUser($params);
         $wechat->user_id = $user_id;
         $params = ['user_id' => $user_id];
         $people = \Model_People::forge($params);
         $people->save();
         //是否创建会员信息
         if (isset($account->is_subscribe_create_member) && $account->is_subscribe_create_member) {
             $params = ['no' => "{$account->seller_id}{$wechat->user_id}" . time(), 'user_id' => $wechat->user_id];
             $member = \Model_Member::forge($params);
             $member->save();
         }
     }
     //创建微信OpenID记录
     $params = ['openid' => $openid, 'account_id' => $account->id];
     $wechatOpenid = \Model_WechatOpenid::forge($params);
     $wechat->ids = [$wechatOpenid];
     $wechat->save();
     return $wechatOpenid;
 }
コード例 #2
0
ファイル: wxcontroller.php プロジェクト: wxl2012/wx
 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);
             }
         }
     }
 }
コード例 #3
0
ファイル: request.php プロジェクト: wxl2012/wx
 /**
  * 初始化微信粉丝帐户
  *
  */
 public function init_wechat()
 {
     $openid = \Model_WechatOpenid::getItem($this->data->FromUserName);
     if (!$openid) {
         $openid = \handler\mp\Account::createWechatAccount($this->data->FromUserName, $this->account);
     }
     $this->wechat = $openid->wechat;
 }
コード例 #4
0
ファイル: gateway.php プロジェクト: wxl2012/wx
 /**
  * 发起微信支付(公众号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));
 }
コード例 #5
0
ファイル: token.php プロジェクト: wxl2012/wx
 /**
  * 获取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);
 }
コード例 #6
0
ファイル: basecontroller.php プロジェクト: wxl2012/wx
 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;
 }
コード例 #7
0
ファイル: wechatopenid.php プロジェクト: wxl2012/wx
 /**
  * 获取微信信息
  *
  * @param $openid 微信粉丝ID
  */
 public static function getItem($openid)
 {
     $wechat = \Model_WechatOpenid::query()->where('openid', $openid)->get_one();
     return $wechat;
 }
コード例 #8
0
ファイル: wxapi.php プロジェクト: wxl2012/wx
 /**
  * 网页授权获取用户基本信息回调处理方法
  *
  * @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);
 }