WechatController.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. use Com\WechatAuth;
  5. class WechatController extends Controller{
  6. public function __construct(){
  7. parent::__construct();
  8. }
  9. //网页微信授权
  10. public function auth(){
  11. $appid = C('WX_APPID');
  12. $appsecret = C('WX_APPSECRET');
  13. $token = S("token");
  14. if ($token) {
  15. $auth = new WechatAuth($appid, $appsecret, $token);
  16. } else {
  17. $auth = new WechatAuth($appid, $appsecret);
  18. $token = $auth->getAccessToken();
  19. S('token',$token['access_token'],$token['expires_in']);
  20. }
  21. return $auth;
  22. }
  23. public function index(){
  24. $code = I('code');
  25. $imei = I('imei');
  26. $auth = $this->auth();
  27. if (empty($code)){
  28. $data = array('code'=>1,'msg'=>'code不能为空');
  29. $this->returnData($data);exit;
  30. }
  31. //获取微信授权后信息
  32. try{
  33. $accessInfo = $auth->getAccessToken('code',$code);
  34. //if (!isset($accessInfo['openid']) || !isset($accessInfo['unionid']) || !isset($accessInfo['access_token'])){
  35. if (!isset($accessInfo['openid']) || !isset($accessInfo['access_token'])){
  36. $data = array('code'=>1,'msg'=>'参数错误');
  37. echo json_encode($data);exit;
  38. }
  39. //获取微信授权后用户信息
  40. $wechatInfo = $auth->getOauthUserinfo($accessInfo['access_token'], $accessInfo['openid']);
  41. //$data = array('code'=>0,'msg'=>'', 'data'=>$wechatInfo);
  42. $emoji = new \Org\Util\Emoji();
  43. $saveData = array(
  44. 'unionid'=>$wechatInfo['unionid'],
  45. 'user_name'=>$emoji->emoji_unified_to_html($wechatInfo['nickname']),
  46. 'user_sex'=>$wechatInfo['sex'],
  47. 'user_icon'=>$wechatInfo['headimgurl'],
  48. 'operate_dt'=>time(),
  49. 'last_dt'=>time(),
  50. );
  51. $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();
  52. if($info==NULL){
  53. $info = $this->addUser($saveData);
  54. }else{
  55. unset($saveData['operate_dt']);
  56. M('User')->where(array('user_id'=>$info['user_id'],))->save($saveData);
  57. $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();
  58. }
  59. $info = $this->getLevel($info);
  60. if (!empty($imei) && !empty($info['imei']) && $imei!=$info['imei']){
  61. M('User')->where(array('user_id'=>$info['user_id']))->save(array('last_dt'=>time(),'imei'=>$imei,'is_login'=>1));
  62. $title = '已在另一地点登录,如非本人操作,请及时修改密码!';
  63. $data = json_encode(array('type'=>2,'msg'=>$title));
  64. $this->umengPush($info['access_token'], $data);
  65. }
  66. session('user_id', $info['user_id']);
  67. M('User')->where(array('user_id'=>$info['user_id']))->save(array('last_dt'=>time(),'is_login'=>1));
  68. //生成token
  69. $token = think_encrypt($info['user_id'].time());
  70. //获取老token,并使老token缓存的用户信息失效
  71. $oldToken = S('token_'.$info['user_id']);
  72. if($oldToken) {
  73. S('user_'.$oldToken, null);
  74. }
  75. //生成缓存信息
  76. S('token_'.$info['user_id'],$token, 604800);
  77. S('user_'.$token, json_encode($info), 604800);
  78. $info['token'] = $token;
  79. $info['oauth'] = '';
  80. if (!empty($info['unionid'])){
  81. $info['bind_wx'] = 1;
  82. }else{
  83. $info['bind_wx'] = 0;
  84. }
  85. if (!empty($info['fbid'])){
  86. $info['bind_fb'] = 1;
  87. }else{
  88. $info['bind_fb'] = 0;
  89. }
  90. unset($info['unionid']);
  91. unset($info['fbid']);
  92. $data = array('code'=>0,'msg'=>'','data'=>$info);
  93. $this->returnData($data);
  94. }catch (\Think\Exception $e){
  95. $data = array('code'=>1,'msg'=>'请求出错');
  96. $this->returnData($data);exit;
  97. }
  98. }
  99. private function returnData($data){
  100. header('Content-Type:application/json; charset=utf-8');
  101. header("Access-Control-Allow-Origin: *");
  102. exit(json_encode($data));
  103. }
  104. /**
  105. * 发送友盟推送消息
  106. * @param integer $uid 用户id
  107. * @param string $title 推送的标题
  108. * @param integer $type 1:官方小秘书 2:我的评论
  109. * @return boolear 是否成功
  110. */
  111. public function umengPush($device_tokens, $title){
  112. // 导入友盟
  113. Vendor('Umeng.Umeng');
  114. // 自定义字段 根据实际环境分配;如果不用可以忽略
  115. $status=1;
  116. // 消息未读总数统计 根据实际环境获取未读的消息总数 此数量会显示在app图标右上角
  117. $count_number=1;
  118. $data=array(
  119. 'key'=>'status',
  120. 'value'=>"$status",
  121. 'count_number'=>$count_number
  122. );
  123. // 判断device_token 64位表示为苹果 否则为安卓
  124. if(strlen($device_tokens)==64){
  125. $key=C('UMENG_IOS_APP_KEY');
  126. $timestamp=C('UMENG_IOS_SECRET');
  127. $umeng=new \Umeng($key, $timestamp);
  128. $umeng->sendIOSUnicast($data,$title,$device_tokens);
  129. }else{
  130. $key=C('UMENG_ANDROID_APP_KEY');
  131. $timestamp=C('UMENG_ANDROID_SECRET');
  132. $umeng=new \Umeng($key, $timestamp);
  133. $umeng->sendAndroidUnicast($data,$title,$device_tokens);
  134. }
  135. return true;
  136. }
  137. }