Connectqq.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * QQ登录控制器
  15. */
  16. class Connectqq 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') . '/connectqq.lang.php');
  20. /**
  21. * 判断qq互联功能是否开启
  22. */
  23. if (config('ds_config.qq_isuse') != 1) {
  24. ds_json_encode(10001, lang('home_qqconnect_unavailable')); //'系统未开启QQ互联功能'
  25. }
  26. if (!session('openid')) {
  27. ds_json_encode(10001, lang('param_error')); //'系统错误'
  28. }
  29. }
  30. /**
  31. * 首页
  32. */
  33. public function index() {
  34. /**
  35. * 检查登录状态
  36. */
  37. if(session('key')){
  38. //qq绑定
  39. $this->bindqq();
  40. }else {
  41. $this->autologin();
  42. $this->register();
  43. }
  44. }
  45. /**
  46. * qq绑定新用户
  47. */
  48. public function register() {
  49. //实例化模型
  50. $member_model = model('member');
  51. if (request()->isPost()) {
  52. $update_info = array();
  53. $update_info['member_password'] = md5(trim(input('post.password')));
  54. $email = input('post.email');
  55. if (!empty($email)) {
  56. $update_info['member_email'] = $email;
  57. session('member_email', $email);
  58. }
  59. $member_model->editMember(array('member_id' => session('member_id')), $update_info, session('member_id'));
  60. } else {
  61. //获取qq账号信息
  62. require_once (PLUGINS_PATH . '/login/qq_h5/user/get_user_info.php');
  63. $qquser_info = get_user_info(session("appid"), session("appkey"), session("token"), session("secret"), session("openid"));
  64. $logic_connect_api = model('connectapi', 'logic');
  65. //注册会员信息 返回会员信息
  66. $reg_info = array(
  67. 'member_qqopenid' => session('openid'),
  68. 'nickname' => isset($qquser_info['nickname']) ? $qquser_info['nickname'] : '',
  69. 'headimgurl' => isset($qquser_info['figureurl_qq_2']) ? $qquser_info['figureurl_qq_2'] : '',
  70. );
  71. $wx_member = $logic_connect_api->wx_register($reg_info, 'qq');
  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('openid'), 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('openid');
  82. @header("location:$url");
  83. } else {
  84. ds_json_encode(10001, lang('home_qqconnect_register_fail')); //"会员注册失败"
  85. }
  86. }
  87. }
  88. /**
  89. * 已有用户绑定QQ
  90. */
  91. public function bindqq() {
  92. $member_model = model('member');
  93. //验证QQ账号用户是否已经存在
  94. $array = array();
  95. $array['member_qqopenid'] = session('openid');
  96. $member_info = $member_model->getMemberInfo($array);
  97. if (is_array($member_info) && count($member_info) > 0) {
  98. session('openid', null);
  99. ds_json_encode(10001, lang('home_qqconnect_binding_exist'), '', '', false); //'该QQ账号已经绑定其他商城账号,请使用其他QQ账号与本账号绑定'
  100. }
  101. //获取qq账号信息
  102. require_once (PLUGINS_PATH . '/login/qq_h5/user/get_user_info.php');
  103. $qquser_info = get_user_info(session('appid'), session('appkey'), session('token'), session('secret'), session('openid'));
  104. $edit_state = $member_model->editMember(array('member_id' => session('member_id')), array('member_qqopenid' => session('openid'), 'member_qqinfo' => serialize($qquser_info)), session('member_id'));
  105. if ($edit_state) {
  106. ds_json_encode(10000, '', lang('home_qqconnect_binding_success'));
  107. } else {
  108. ds_json_encode(10001, lang('home_qqconnect_binding_fail')); //'绑定QQ失败'
  109. }
  110. }
  111. /**
  112. * 绑定qq后自动登录
  113. */
  114. public function autologin() {
  115. //查询是否已经绑定该qq,已经绑定则直接跳转
  116. $member_model = model('member');
  117. $array = array();
  118. $array['member_qqopenid'] = session('openid');
  119. $member_info = $member_model->getMemberInfo($array);
  120. if (is_array($member_info) && count($member_info) > 0) {
  121. if (!$member_info['member_state']) {//1为启用 0 为禁用
  122. ds_json_encode(10001, lang('home_qqconnect_user_disable'));
  123. }
  124. $token = $member_model->getBuyerToken($member_info['member_id'], $member_info['member_name'], 'wap');
  125. $state_data['key'] = $token;
  126. $state_data['username'] = $member_info['member_name'];
  127. $state_data['userid'] = $member_info['member_id'];
  128. wkcache(session('openid'), array(
  129. 'user_info' => $this->getMemberUser($member_info),
  130. 'key' => $state_data['key'],
  131. ));
  132. $url = config('ds_config.h5_site_url') . '/pages/home/memberlogin/Login?id='.session('openid');
  133. @header("location:$url");
  134. }
  135. }
  136. }