重新整理做过的功能,以及过程遇到问题,温故而知新
一 准备工作 (a.接入 b.配置)
a) 修改/添加 业务域名、网页授权域名; 授权回调域名 不加 http:// 协议头
绑定时微信要求将 *.txt文件上传根目录,需要将文件 添加到我项目.htaccess许可中 确保文件可被访问,否则绑定不通过
b) 公众平台 参数
appid
secret二 功能
流程
结合项目实现以下功能:
a) 首次授权获取userData存库 openid/unionid 关系对应
b) openid/unionid session缓存c) session有效期内 延时d) access_token缓存、刷新;简单画一个流程图:
access_token 缓存/刷新 感觉没必要;因为openid过期,也要重新获取的; code获取需要重定向,所以可以写在父类;
ci框架
class WX_Controller extends CI_Controller{ public $wx_code; public $wx_openid; public $wx_unionid; public $access_token; public function __construct() { parent::__construct(); $this->load->library('session'); // 存在session缓存 刷新 缓存时间 if (isset($_SESSION['openid'])) { $this->session->mark_as_temp('openid', WX_USER_TEMPTIME); $this->session->mark_as_temp('unionid', WX_USER_TEMPTIME); $this->session->mark_as_temp('userData', WX_USER_TEMPTIME); }else { // 1. code $this->get_wx_code(); // 2. token and openid $this->get_wx_acctoken(); // 3. 验证token // 4. 如果首次授权请求微信,否则查库获取userData $this->load->model('Mwxuser'); $where = array('openid'=>$this->wx_openid); $userData = $this->Mwxuser->wx_user_list(0, 1, $where); if (!$userData) { $userData = $this->get_wx_user_data(); } // 5. 缓存openid unionid userData $sessionData = array('openid'=>$this->wx_openid, 'unionid'=>$this->wx_unionid, 'userData'=>$userData); $this->session->set_userdata($sessionData); } } }
code
/*----------------------------------------------------------*\ 微信公众号授权登录\*----------------------------------------------------------*/ // get wx code 公众号 scope:snsapi_userinfo同样静默 public function get_wx_code() { $code = $this->input->get('code'); if ($code) { $this->wx_code = $code; }else { $wx_get_code = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.WX_APP_ID.'&redirect_uri='.urlencode(UNION_INDEX).'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'; $this->load->helper('url'); redirect($wx_get_code); } }
acc-token
// get wx access token // FIXME.jtwang: 这里缓存token貌似也没什么用,如果session-openid过期,这里缓存token也是没用的 public function get_wx_acctoken() { // a. 查询有效 token // b. token过期 refresh token 刷新 // c. refresh token 过期 重新获取 $this->load->library('mycurl'); $wx_get_token = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WX_APP_ID.'&secret='.WX_SECRET.'&code='.$this->wx_code.'&grant_type=authorization_code'; $returnContent = $this->mycurl->curl_get($wx_get_token); $returnContent = json_decode($returnContent, true); if (!isset($returnContent['access_token'])) { die('error-page-todo-later-token'.$returnContent['errcode']); } $access_token = $returnContent['access_token']; $refresh_token = $returnContent['refresh_token']; $this->wx_openid = $returnContent['openid']; //openid $this->wx_unionid = $returnContent['unionid']; //unionid $token_tempdate = time()+7200; $refresh_tempdate = time()+30*24*60*60; $this->access_token = $access_token; }
拉取userData
// 请求微信 拉取用户信息 返回 userData public function get_wx_user_data() { $wx_get_userinfo = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$this->access_token.'&openid='.$this->wx_openid.'&lang=zh_CN'; $returnContent = $this->mycurl->curl_get($wx_get_userinfo); $returnContent = json_decode($returnContent, true); if (!isset($returnContent['openid'])) { die('error-page-todo-later-userdata'.$returnContent['errcode']); } $userData = array(); $userData['openid'] = $returnContent['openid']; $userData['unionid'] = $returnContent['unionid']; $userData['nickname'] = $returnContent['nickname']; // $userData['sex'] = $returnContent['sex']; $userData['province'] = $returnContent['province']; $userData['city'] = $returnContent['city']; $userData['country'] = $returnContent['country']; $userData['headimgurl'] = $returnContent['headimgurl']; // $userData['privilege'] = $returnContent['privilege']; // 入库 $this->load->model('Mwxuser'); $res = $this->Mwxuser->wx_user_add($userData); if (!$res) { die('error-page-todo-later add user-data error'); } return $userData; }}
不对的地方还请指教。