Connectsina.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 微博登录控制器
  10. */
  11. class Connectsina extends MobileMall
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize(); // TODO: Change the autogenerated stub
  16. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/connectsina.lang.php');
  17. if (config('ds_config.sina_isuse') != 1) {
  18. ds_json_encode(10001, lang('home_sconnect_unavailable')); //'系统未开启QQ互联功能'
  19. }
  20. if (!session('slast_key')) {
  21. ds_json_encode(10001, lang('param_error'));
  22. }
  23. }
  24. /**
  25. * 首页
  26. */
  27. public function index()
  28. {
  29. /**
  30. * 检查登录状态
  31. */
  32. if (session('key')) {
  33. $this->bindsina();
  34. } else {
  35. $this->autologin();
  36. $this->register();
  37. }
  38. }
  39. /**
  40. * 新浪微博账号绑定新用户
  41. */
  42. public function register()
  43. {
  44. //实例化模型
  45. $member_model = model('member');
  46. if (request()->isPost()) {
  47. $update_info = array();
  48. $update_info['member_password'] = md5(trim(input('post.password')));
  49. $email = input('post.email');
  50. if (!empty($email)) {
  51. $update_info['member_email'] = $email;
  52. session('member_email', $email);
  53. }
  54. $member_model->editMember(array('member_id' => session('member_id')), $update_info, session('member_id'));
  55. $this->success(lang('ds_common_op_succ'), HOME_SITE_URL);
  56. } else {
  57. //检查登录状态
  58. //$member_model->checkloginMember();
  59. //获取新浪微博账号信息
  60. require_once(PLUGINS_PATH . DIRECTORY_SEPARATOR . 'login' . DIRECTORY_SEPARATOR . 'sina_h5' . DIRECTORY_SEPARATOR . 'saetv2.ex.class.php');
  61. $c = new \SaeTClientV2(config('ds_config.sina_wb_akey'), config('ds_config.sina_wb_skey'), session('slast_key.access_token'));
  62. $sinauser_info = $c->show_user_by_id(session('slast_key.uid')); //根据ID获取用户等基本信息
  63. $logic_connect_api = model('connectapi', 'logic');
  64. //注册会员信息 返回会员信息
  65. $reg_info = array(
  66. 'member_sinaopenid' => session('slast_key.uid'),
  67. 'nickname' => isset($sinauser_info['screen_name']) ? $sinauser_info['screen_name'] : '',
  68. 'headimgurl' => isset($sinauser_info['avatar_large']) ? $sinauser_info['avatar_large'] : '',
  69. );
  70. $wx_member = $logic_connect_api->wx_register($reg_info, 'sina');
  71. if ($wx_member) {
  72. $token = $member_model->getBuyerToken($wx_member['member_id'], $wx_member['member_name'], 'wap');
  73. $state_data['key'] = $token;
  74. $state_data['username'] = $wx_member['member_name'];
  75. $state_data['userid'] = $wx_member['member_id'];
  76. wkcache(session('slast_key.uid'), array(
  77. 'user_info' => $this->getMemberUser($wx_member),
  78. 'key' => $state_data['key'],
  79. ));
  80. $url = config('ds_config.h5_site_url') . '/pages/home/memberlogin/Login?id=' . session('slast_key.uid');
  81. @header("location:$url");
  82. } else {
  83. ds_json_encode(10001, lang('home_sconnect_register_fail')); //"会员注册失败"
  84. }
  85. }
  86. }
  87. /**
  88. * 已有用户绑定新浪微博账号
  89. */
  90. public function bindsina()
  91. {
  92. $member_model = model('member');
  93. //验证新浪账号用户是否已经存在
  94. $array = array();
  95. $array['member_sinaopenid'] = session('slast_key.uid');
  96. $member_info = $member_model->getMemberInfo($array);
  97. if (is_array($member_info) && count($member_info) > 0) {
  98. session('slast_key.uid', null);
  99. ds_json_encode(10001, lang('home_sconnect_user_disable'), '', '', false);
  100. }
  101. //处理sina账号信息
  102. require_once(PLUGINS_PATH . DIRECTORY_SEPARATOR . 'login' . DIRECTORY_SEPARATOR . 'sina_h5' . DIRECTORY_SEPARATOR . 'saetv2.ex.class.php');
  103. $c = new \SaeTClientV2(config('ds_config.sina_wb_akey'), config('ds_config.sina_wb_skey'), session('slast_key.access_token'));
  104. $sinauser_info = $c->show_user_by_id(session('slast_key.uid')); //根据ID获取用户等基本信息
  105. $sina_str = serialize($sinauser_info);
  106. $edit_state = $member_model->editMember(array('member_id' => session('member_id')), array('member_sinaopenid' => session('slast_key.uid'), 'member_sinainfo' => $sina_str), session('member_id'));
  107. if ($edit_state) {
  108. ds_json_encode(10000, '', lang('home_sconnect_binding_success'));
  109. } else {
  110. ds_json_encode(10001, lang('home_sconnect_binding_fail'));
  111. }
  112. }
  113. /**
  114. * 绑定新浪微博账号后自动登录
  115. */
  116. public function autologin()
  117. {
  118. //查询是否已经绑定该新浪微博账号,已经绑定则直接跳转
  119. $member_model = model('member');
  120. $array = array();
  121. $array['member_sinaopenid'] = session('slast_key.uid');
  122. $member_info = $member_model->getMemberInfo($array);
  123. if (is_array($member_info) && count($member_info) > 0) {
  124. if (!$member_info['member_state']) { //1为启用 0 为禁用
  125. ds_json_encode(10001, lang('home_sconnect_user_disable'));
  126. }
  127. $token = $member_model->getBuyerToken($member_info['member_id'], $member_info['member_name'], 'wap');
  128. $state_data['key'] = $token;
  129. $state_data['username'] = $member_info['member_name'];
  130. $state_data['userid'] = $member_info['member_id'];
  131. wkcache(session('slast_key.uid'), array(
  132. 'user_info' => $this->getMemberUser($member_info),
  133. 'key' => $state_data['key'],
  134. ));
  135. $url = config('ds_config.h5_site_url') . '/pages/home/memberlogin/Login?id=' . session('slast_key.uid');
  136. @header("location:$url");
  137. }
  138. }
  139. }