Connectsina.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 微博登录控制器
  15. */
  16. class Connectsina extends MobileMall {
  17. public function initialize() {
  18. parent::initialize(); // TODO: Change the autogenerated stub
  19. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/connectsina.lang.php');
  20. if (config('ds_config.sina_isuse') != 1) {
  21. ds_json_encode(10001, lang('home_sconnect_unavailable')); //'系统未开启QQ互联功能'
  22. }
  23. if (!session('slast_key')) {
  24. ds_json_encode(10001, lang('param_error'));
  25. }
  26. }
  27. /**
  28. * 首页
  29. */
  30. public function index() {
  31. /**
  32. * 检查登录状态
  33. */
  34. if (session('key')) {
  35. $this->bindsina();
  36. } else {
  37. $this->autologin();
  38. $this->register();
  39. }
  40. }
  41. /**
  42. * 新浪微博账号绑定新用户
  43. */
  44. public function register() {
  45. //实例化模型
  46. $member_model = model('member');
  47. if (request()->isPost()) {
  48. $update_info = array();
  49. $update_info['member_password'] = md5(trim(input('post.password')));
  50. $email = input('post.email');
  51. if (!empty($email)) {
  52. $update_info['member_email'] = $email;
  53. session('member_email', $email);
  54. }
  55. $member_model->editMember(array('member_id' => session('member_id')), $update_info, session('member_id'));
  56. $this->success(lang('ds_common_op_succ'), HOME_SITE_URL);
  57. } else {
  58. //检查登录状态
  59. //$member_model->checkloginMember();
  60. //获取新浪微博账号信息
  61. require_once (PLUGINS_PATH . DIRECTORY_SEPARATOR . 'login' . DIRECTORY_SEPARATOR . 'sina_h5' . DIRECTORY_SEPARATOR . 'saetv2.ex.class.php');
  62. $c = new \SaeTClientV2(config('ds_config.sina_wb_akey'), config('ds_config.sina_wb_skey'), session('slast_key.access_token'));
  63. $sinauser_info = $c->show_user_by_id(session('slast_key.uid')); //根据ID获取用户等基本信息
  64. $logic_connect_api = model('connectapi', 'logic');
  65. //注册会员信息 返回会员信息
  66. $reg_info = array(
  67. 'member_sinaopenid' => session('slast_key.uid'),
  68. 'nickname' => isset($sinauser_info['screen_name']) ? $sinauser_info['screen_name'] : '',
  69. 'headimgurl' => isset($sinauser_info['avatar_large']) ? $sinauser_info['avatar_large'] : '',
  70. );
  71. $wx_member = $logic_connect_api->wx_register($reg_info, 'sina');
  72. if ($wx_member) {
  73. $token = $member_model->getBuyerToken($wx_member['member_id'], $wx_member['member_name'], 'wap');
  74. $state_data['key'] = $token;
  75. $state_data['username'] = $wx_member['member_name'];
  76. $state_data['userid'] = $wx_member['member_id'];
  77. wkcache(session('slast_key.uid'), array(
  78. 'user_info' => $this->getMemberUser($wx_member),
  79. 'key' => $state_data['key'],
  80. ));
  81. $url = config('ds_config.h5_site_url') . '/pages/home/memberlogin/Login?id=' . session('slast_key.uid');
  82. @header("location:$url");
  83. } else {
  84. ds_json_encode(10001, lang('home_sconnect_register_fail')); //"会员注册失败"
  85. }
  86. }
  87. }
  88. /**
  89. * 已有用户绑定新浪微博账号
  90. */
  91. public function bindsina() {
  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. $member_model = model('member');
  119. $array = array();
  120. $array['member_sinaopenid'] = session('slast_key.uid');
  121. $member_info = $member_model->getMemberInfo($array);
  122. if (is_array($member_info) && count($member_info) > 0) {
  123. if (!$member_info['member_state']) {//1为启用 0 为禁用
  124. ds_json_encode(10001, lang('home_sconnect_user_disable'));
  125. }
  126. $token = $member_model->getBuyerToken($member_info['member_id'], $member_info['member_name'], 'wap');
  127. $state_data['key'] = $token;
  128. $state_data['username'] = $member_info['member_name'];
  129. $state_data['userid'] = $member_info['member_id'];
  130. wkcache(session('slast_key.uid'), array(
  131. 'user_info' => $this->getMemberUser($member_info),
  132. 'key' => $state_data['key'],
  133. ));
  134. $url = config('ds_config.h5_site_url') . '/pages/home/memberlogin/Login?id=' . session('slast_key.uid');
  135. @header("location:$url");
  136. }
  137. }
  138. }