MemberQQService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 17/2/23
  6. * Time: 上午11:20
  7. */
  8. namespace app\frontend\modules\member\services;
  9. use app\frontend\modules\member\services\MemberService;
  10. use app\frontend\modules\member\models\MemberQQModel;
  11. use Illuminate\Session\Store;
  12. class MemberQQService extends MemberService
  13. {
  14. private $_login_type = 6;
  15. public function __construct()
  16. {}
  17. public function login()
  18. {
  19. $uniacid = \YunShop::app()->uniacid;
  20. $appId = \YunShop::app()->account['key'];
  21. $appSecret = \YunShop::app()->account['secret'];
  22. $code = \YunShop::request()->code;
  23. $url = \YunShop::app()->siteroot . 'app/index.php?' . $_SERVER['QUERY_STRING'];
  24. $authurl = $this->_getAuthUrl($appId, $url);
  25. $tokenurl = $this->_getTokenUrl($appId, $appSecret, $code);
  26. if (!empty($code)) {
  27. $resp = \Curl::to($tokenurl)->get();
  28. $token = @json_decode($resp['content'], true);
  29. if (!empty($token) && is_array($token) && $token['errmsg'] == 'invalid code') {
  30. show_json(0, array('msg'=>'请求错误'));
  31. }
  32. $openid_url = $this->_getOpenIdUrl($token['accesstoken'], $token['openid']);
  33. @\Curl::to($openid_url)->get();
  34. $userinfo_url = $this->_getUserInfoUrl($token['accesstoken'], $token['openid']);
  35. $userinfo = @\Curl::to($userinfo_url)->get();
  36. if (is_array($userinfo) && !empty($userinfo['unionid'])) {
  37. $UnionidInfo = MemberUniqueModel::getUnionidInfo($uniacid, $userinfo['unionid']);
  38. $types = explode($UnionidInfo['type'], '|');
  39. if ($UnionidInfo['unionid']) {
  40. if (!in_array($this->_login_type, $types)) {
  41. //更新ims_yz_member_unique表
  42. MemberUniqueModel::updateData(array(
  43. 'unique_id'=>$UnionidInfo['unique_id'],
  44. 'type' => $UnionidInfo['type'] . '|' . $this->_login_type
  45. ));
  46. }
  47. $_SESSION['member_id'] = $UnionidInfo['member_id'];
  48. } else {
  49. $member_id = McMappingFansModel::getUId($uniacid, $token['openid']);
  50. //添加ims_yz_member_unique表
  51. MemberUniqueModel::insertData(array(
  52. 'uniacid' => $uniacid,
  53. 'unionid' => $token['unionid'],
  54. 'member_id' => $member_id,
  55. 'type' => $this->_login_type
  56. ));
  57. session()->put('member_id',$member_id);
  58. }
  59. } else {
  60. show_json(0, array('url'=> $authurl));
  61. }
  62. } else {
  63. show_json(0, array('url'=> $authurl));
  64. }
  65. show_json(1, array('member_id', $_SESSION['member_id']));
  66. }
  67. private function _getAuthUrl($app_id, $url)
  68. {
  69. return " https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={$app_id}&redirect_uri={$url}&state=1234";
  70. }
  71. private function _getTokenUrl($app_id, $app_secret, $code, $url)
  72. {
  73. return "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" . $app_id . "&client_secret=" . $app_secret. "&code=" . $code . "&redirect_uri=" . $url;
  74. }
  75. private function _getOpenIdUrl($access_token)
  76. {
  77. return "https://graph.qq.com/oauth2.0/me?access_token" . $access_token;
  78. }
  79. private function _getUserInfoUrl($accesstoken, $openid)
  80. {
  81. return "https://graph.qq.com/user/get_simple_userinfo? access_token={$accesstoken}&oauth_consumer_key=12345&openid={$openid} ";
  82. }
  83. /**
  84. * 验证登录状态
  85. *
  86. * @return bool
  87. */
  88. public function checkLogged($login = null)
  89. {
  90. return MemberService::isLogged();
  91. }
  92. }