123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace Api\Controller;
- use Think\Controller;
- use Com\WechatAuth;
- class WechatController extends Controller{
- public function __construct(){
- parent::__construct();
- }
- //网页微信授权
- public function auth(){
- $appid = C('WX_APPID');
- $appsecret = C('WX_APPSECRET');
- $token = S("token");
- if ($token) {
- $auth = new WechatAuth($appid, $appsecret, $token);
- } else {
- $auth = new WechatAuth($appid, $appsecret);
- $token = $auth->getAccessToken();
- S('token',$token['access_token'],$token['expires_in']);
- }
- return $auth;
- }
-
- public function index(){
- $code = I('code');
- $imei = I('imei');
- $auth = $this->auth();
- if (empty($code)){
- $data = array('code'=>1,'msg'=>'code不能为空');
- $this->returnData($data);exit;
- }
- //获取微信授权后信息
- try{
- $accessInfo = $auth->getAccessToken('code',$code);
- //if (!isset($accessInfo['openid']) || !isset($accessInfo['unionid']) || !isset($accessInfo['access_token'])){
- if (!isset($accessInfo['openid']) || !isset($accessInfo['access_token'])){
- $data = array('code'=>1,'msg'=>'参数错误');
- echo json_encode($data);exit;
- }
- //获取微信授权后用户信息
- $wechatInfo = $auth->getOauthUserinfo($accessInfo['access_token'], $accessInfo['openid']);
- //$data = array('code'=>0,'msg'=>'', 'data'=>$wechatInfo);
- $emoji = new \Org\Util\Emoji();
- $saveData = array(
- 'unionid'=>$wechatInfo['unionid'],
- 'user_name'=>$emoji->emoji_unified_to_html($wechatInfo['nickname']),
- 'user_sex'=>$wechatInfo['sex'],
- 'user_icon'=>$wechatInfo['headimgurl'],
- 'operate_dt'=>time(),
- 'last_dt'=>time(),
- );
- $info = M('User')->where(array('unionid'=>$wechatInfo['unionid'],))->field('user_id,user_phone,user_name,user_level,user_sex,user_icon,user_city,user_province,user_birthdate,user_score,zan_notice,sys_notice,friend_notice,operate_dt,unionid,fbid')->find();
- if($info==NULL){
- $info = $this->addUser($saveData);
- }else{
- unset($saveData['operate_dt']);
- M('User')->where(array('user_id'=>$info['user_id'],))->save($saveData);
- $info = M('User')->where(array('user_id'=>$info['user_id']))->field('user_id,user_phone,user_name,user_level,user_sex,user_icon,user_city,user_province,user_birthdate,user_score,zan_notice,sys_notice,friend_notice,operate_dt,unionid,fbid,imei')->find();
- }
- $info = $this->getLevel($info);
- if (!empty($imei) && !empty($info['imei']) && $imei!=$info['imei']){
- M('User')->where(array('user_id'=>$info['user_id']))->save(array('last_dt'=>time(),'imei'=>$imei,'is_login'=>1));
- $title = '已在另一地点登录,如非本人操作,请及时修改密码!';
- $data = json_encode(array('type'=>2,'msg'=>$title));
- $this->umengPush($info['access_token'], $data);
- }
- session('user_id', $info['user_id']);
- M('User')->where(array('user_id'=>$info['user_id']))->save(array('last_dt'=>time(),'is_login'=>1));
-
- //生成token
- $token = think_encrypt($info['user_id'].time());
- //获取老token,并使老token缓存的用户信息失效
- $oldToken = S('token_'.$info['user_id']);
- if($oldToken) {
- S('user_'.$oldToken, null);
- }
- //生成缓存信息
- S('token_'.$info['user_id'],$token, 604800);
- S('user_'.$token, json_encode($info), 604800);
- $info['token'] = $token;
-
- $info['oauth'] = '';
- if (!empty($info['unionid'])){
- $info['bind_wx'] = 1;
- }else{
- $info['bind_wx'] = 0;
- }
- if (!empty($info['fbid'])){
- $info['bind_fb'] = 1;
- }else{
- $info['bind_fb'] = 0;
- }
- unset($info['unionid']);
- unset($info['fbid']);
- $data = array('code'=>0,'msg'=>'','data'=>$info);
- $this->returnData($data);
- }catch (\Think\Exception $e){
- $data = array('code'=>1,'msg'=>'请求出错');
- $this->returnData($data);exit;
- }
- }
-
- private function returnData($data){
- header('Content-Type:application/json; charset=utf-8');
- header("Access-Control-Allow-Origin: *");
- exit(json_encode($data));
- }
-
-
- /**
- * 发送友盟推送消息
- * @param integer $uid 用户id
- * @param string $title 推送的标题
- * @param integer $type 1:官方小秘书 2:我的评论
- * @return boolear 是否成功
- */
- public function umengPush($device_tokens, $title){
- // 导入友盟
- Vendor('Umeng.Umeng');
- // 自定义字段 根据实际环境分配;如果不用可以忽略
- $status=1;
- // 消息未读总数统计 根据实际环境获取未读的消息总数 此数量会显示在app图标右上角
- $count_number=1;
- $data=array(
- 'key'=>'status',
- 'value'=>"$status",
- 'count_number'=>$count_number
- );
- // 判断device_token 64位表示为苹果 否则为安卓
- if(strlen($device_tokens)==64){
- $key=C('UMENG_IOS_APP_KEY');
- $timestamp=C('UMENG_IOS_SECRET');
- $umeng=new \Umeng($key, $timestamp);
- $umeng->sendIOSUnicast($data,$title,$device_tokens);
- }else{
- $key=C('UMENG_ANDROID_APP_KEY');
- $timestamp=C('UMENG_ANDROID_SECRET');
- $umeng=new \Umeng($key, $timestamp);
- $umeng->sendAndroidUnicast($data,$title,$device_tokens);
- }
- return true;
- }
- }
|