Connectsina.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 微博登录控制器
  14. */
  15. class Connectsina extends MobileMall {
  16. public function initialize() {
  17. parent::initialize(); // TODO: Change the autogenerated stub
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/connectsina.lang.php');
  19. if (config('ds_config.sina_isuse') != 1) {
  20. ds_json_encode(10001, lang('home_sconnect_unavailable')); //'系统未开启QQ互联功能'
  21. }
  22. if (!session('slast_key')) {
  23. ds_json_encode(10001, lang('param_error'));
  24. }
  25. }
  26. /**
  27. * 首页
  28. */
  29. public function index() {
  30. /**
  31. * 检查登录状态
  32. */
  33. if (session('key')) {
  34. $this->bindsina();
  35. } else {
  36. $this->autologin();
  37. $this->register();
  38. }
  39. }
  40. /**
  41. * 新浪微博账号绑定新用户
  42. */
  43. public function register() {
  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. $member_model = model('member');
  92. //验证新浪账号用户是否已经存在
  93. $array = array();
  94. $array['member_sinaopenid'] = session('slast_key.uid');
  95. $member_info = $member_model->getMemberInfo($array);
  96. if (is_array($member_info) && count($member_info) > 0) {
  97. session('slast_key.uid', null);
  98. ds_json_encode(10001, lang('home_sconnect_user_disable'), '', '', false);
  99. }
  100. //处理sina账号信息
  101. require_once (PLUGINS_PATH . DIRECTORY_SEPARATOR . 'login' . DIRECTORY_SEPARATOR . 'sina_h5' . DIRECTORY_SEPARATOR . 'saetv2.ex.class.php');
  102. $c = new \SaeTClientV2(config('ds_config.sina_wb_akey'), config('ds_config.sina_wb_skey'), session('slast_key.access_token'));
  103. $sinauser_info = $c->show_user_by_id(session('slast_key.uid')); //根据ID获取用户等基本信息
  104. $sina_str = serialize($sinauser_info);
  105. $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'));
  106. if ($edit_state) {
  107. ds_json_encode(10000, '', lang('home_sconnect_binding_success'));
  108. } else {
  109. ds_json_encode(10001, lang('home_sconnect_binding_fail'));
  110. }
  111. }
  112. /**
  113. * 绑定新浪微博账号后自动登录
  114. */
  115. public function autologin() {
  116. //查询是否已经绑定该新浪微博账号,已经绑定则直接跳转
  117. $member_model = model('member');
  118. $array = array();
  119. $array['member_sinaopenid'] = session('slast_key.uid');
  120. $member_info = $member_model->getMemberInfo($array);
  121. if (is_array($member_info) && count($member_info) > 0) {
  122. if (!$member_info['member_state']) {//1为启用 0 为禁用
  123. ds_json_encode(10001, lang('home_sconnect_user_disable'));
  124. }
  125. $token = $member_model->getBuyerToken($member_info['member_id'], $member_info['member_name'], 'wap');
  126. $state_data['key'] = $token;
  127. $state_data['username'] = $member_info['member_name'];
  128. $state_data['userid'] = $member_info['member_id'];
  129. wkcache(session('slast_key.uid'), array(
  130. 'user_info' => $this->getMemberUser($member_info),
  131. 'key' => $state_data['key'],
  132. ));
  133. $url = config('ds_config.h5_site_url') . '/pages/home/memberlogin/Login?id=' . session('slast_key.uid');
  134. @header("location:$url");
  135. }
  136. }
  137. }