Exemple #1
0
 /**
  * 根据微信推送的包,创建相关帐户信息
  *
  * @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;
 }
Exemple #2
0
 /**
  * 获取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);
 }
Exemple #3
0
 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;
 }
Exemple #4
0
 /**
  * 网页授权获取用户基本信息回调处理方法
  *
  * @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);
 }
Exemple #5
0
 /**
  * 获取当前用户的推荐人的微信OPENID
  * @return bool
  */
 private function getParentWechatOpenid()
 {
     return 'oqTo9uJao4vdZy5EZH8yQgL_0SY0';
     //获取上级用户
     $members = \Model_MemberRecommendRelation::parentMember(\Auth::get_user()->id);
     if (!$members) {
         return false;
     }
     $member = current($members);
     $to_openid = false;
     //获取上级用户的微信信息
     $wechat = \Model_Wechat::query()->where(['user_id' => $member->master_id])->get_one();
     //获取上级用户的微信OPENID
     foreach ($wechat->ids as $openid) {
         if ($openid->account_id == \Session::get('WXAccount')->id) {
             $to_openid = $openid->openid;
         }
     }
     return $to_openid;
 }