Connectsina.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Connectsina extends BaseMall
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/connectsina.lang.php');
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/login-register.lang.php');
  19. /**
  20. * 判断新浪微博登录功能是否开启
  21. */
  22. if (config('ds_config.sina_isuse') != 1) {
  23. $this->error(lang('home_sconnect_unavailable'));
  24. }
  25. if (!session('slast_key')) {
  26. $this->error(lang('home_sconnect_error'));
  27. }
  28. View::assign('hidden_nctoolbar', 1);
  29. }
  30. /**
  31. * 首页
  32. */
  33. public function index()
  34. {
  35. /**
  36. * 检查登录状态
  37. */
  38. if (session('is_login') == '1') {
  39. $this->bindsina();
  40. } else {
  41. $this->autologin();
  42. $this->register();
  43. }
  44. }
  45. /**
  46. * 新浪微博账号绑定新用户
  47. */
  48. public function register()
  49. {
  50. //实例化模型
  51. $member_model = model('member');
  52. //检查登录状态
  53. $member_model->checkloginMember();
  54. //获取新浪微博账号信息
  55. require_once(PLUGINS_PATH . '/login/sina/saetv2.ex.class.php');
  56. $c = new \SaeTClientV2(config('ds_config.sina_wb_akey'), config('ds_config.sina_wb_skey'), session('slast_key.access_token'));
  57. $sinauser_info = $c->show_user_by_id(session('slast_key.uid')); //根据ID获取用户等基本信息
  58. if (request()->isPost()) {
  59. $type = input('param.type');
  60. $user = input('param.user');
  61. $password = input('param.password');
  62. $password2 = input('param.password2');
  63. $reg_info = array(
  64. 'member_sinaopenid' => session('slast_key.uid'),
  65. 'nickname' => isset($sinauser_info['screen_name']) ? $sinauser_info['screen_name'] : '',
  66. 'headimgurl' => isset($sinauser_info['avatar_large']) ? $sinauser_info['avatar_large'] : '',
  67. );
  68. $data = array(
  69. 'member_name' => $user,
  70. 'member_password' => $password,
  71. 'member_sinaopenid' => $reg_info['member_sinaopenid'],
  72. 'member_sinainfo' => serialize($reg_info),
  73. 'member_nickname' => $reg_info['nickname'],
  74. );
  75. if ($type == 1) { //注册
  76. $login_validate = ds_validate('member');
  77. if (!$login_validate->scene('register')->check($data)) {
  78. $this->error($login_validate->getError());
  79. }
  80. $member_info = $member_model->register($data);
  81. if (!isset($member_info['error'])) {
  82. $member_model->createSession($member_info, true);
  83. $headimgurl = $reg_info['headimgurl'];
  84. $avatar = @copy($headimgurl, BASE_UPLOAD_PATH . '/' . ATTACH_AVATAR . "/avatar_" . $member_info['member_id'] . ".jpg");
  85. if ($avatar) {
  86. $member_model->editMember(array('member_id' => $member_info['member_id']), array('member_avatar' => "avatar_" . $member_info['member_id'] . ".jpg"), $member_info['member_id']);
  87. }
  88. } else {
  89. $this->error($member_info['error']);
  90. }
  91. } else { //绑定
  92. $login_validate = ds_validate('member');
  93. if (!$login_validate->scene('login')->check($data)) {
  94. ds_json_encode(10001, $login_validate->getError());
  95. }
  96. $map = array(
  97. 'member_name' => $data['member_name'],
  98. 'member_password' => md5($data['member_password']),
  99. );
  100. $member_info = $member_model->getMemberInfo($map);
  101. if ($member_info) {
  102. $member_model->editMember(array('member_id' => $member_info['member_id']), array('member_sinaopenid' => $data['member_sinaopenid'], 'member_sinainfo' => $data['member_sinainfo']), $member_info['member_id']);
  103. } else {
  104. $this->error(lang('login_register_bind_fail'));
  105. }
  106. $member_model->createSession($member_info, true);
  107. }
  108. $this->success(lang('ds_common_save_succ'), HOME_SITE_URL);
  109. } else {
  110. if (config('ds_config.auto_register')) { //如果开启了自动注册
  111. $logic_connect_api = model('connectapi', 'logic');
  112. //注册会员信息 返回会员信息
  113. $reg_info = array(
  114. 'member_sinaopenid' => session('slast_key.uid'),
  115. 'nickname' => isset($sinauser_info['screen_name']) ? $sinauser_info['screen_name'] : '',
  116. 'headimgurl' => isset($sinauser_info['avatar_large']) ? $sinauser_info['avatar_large'] : '',
  117. );
  118. $wx_member = $logic_connect_api->wx_register($reg_info, 'sina');
  119. if ($wx_member) {
  120. if (!$wx_member['member_state']) {
  121. $this->error(lang('login_index_account_stop'), 'Index/index');
  122. }
  123. $member_model->createSession($wx_member, true);
  124. $success_message = lang('login_index_login_success');
  125. $this->success($success_message, HOME_SITE_URL);
  126. } else {
  127. $this->error(lang('login_usersave_regist_fail'), 'login/register'); //"会员注册失败"
  128. }
  129. } else {
  130. View::assign('sinauser_info', $sinauser_info);
  131. View::assign('user_passwd', '');
  132. echo View::fetch($this->template_dir . 'connect_register');
  133. }
  134. }
  135. }
  136. /**
  137. * 绑定新浪微博账号后自动登录
  138. */
  139. public function autologin()
  140. {
  141. //查询是否已经绑定该新浪微博账号,已经绑定则直接跳转
  142. $member_model = model('member');
  143. $array = array();
  144. $array['member_sinaopenid'] = session('slast_key.uid');
  145. $member_info = $member_model->getMemberInfo($array);
  146. if (is_array($member_info) && count($member_info) > 0) {
  147. if (!$member_info['member_state']) { //1为启用 0 为禁用
  148. $this->error(lang('login_index_account_stop'));
  149. }
  150. $member_model->createSession($member_info);
  151. //是否有卖家账户
  152. $seller_model = model('seller');
  153. $seller_info = $seller_model->getSellerInfo(array('member_id' => $member_info['member_id']));
  154. if ($seller_info) {
  155. // 更新卖家登陆时间
  156. $seller_model->editSeller(array('last_logintime' => TIMESTAMP), array('seller_id' => $seller_info['seller_id']));
  157. $sellergroup_model = model('sellergroup');
  158. $seller_group_info = $sellergroup_model->getSellergroupInfo(array('sellergroup_id' => $seller_info['sellergroup_id']));
  159. $store_model = model('store');
  160. $store_info = $store_model->getStoreInfoByID($seller_info['store_id']);
  161. $seller_model->createSellerSession($member_info, $store_info, $seller_info, is_array($seller_group_info) ? $seller_group_info : array());
  162. }
  163. $success_message = lang('login_index_login_success');
  164. $this->success($success_message, 'index/index');
  165. }
  166. }
  167. /**
  168. * 已有用户绑定新浪微博账号
  169. */
  170. public function bindsina()
  171. {
  172. $member_model = model('member');
  173. //验证新浪账号用户是否已经存在
  174. $array = array();
  175. $array['member_sinaopenid'] = session('slast_key.uid');
  176. $member_info = $member_model->getMemberInfo($array);
  177. if (is_array($member_info) && count($member_info) > 0) {
  178. session('slast_key.uid', null);
  179. $this->error(lang('home_sconnect_binding_exist'), 'memberconnect/sinabind');
  180. }
  181. //处理sina账号信息
  182. require_once(PLUGINS_PATH . '/login/sina/saetv2.ex.class.php');
  183. $c = new \SaeTClientV2(config('ds_config.sina_wb_akey'), config('ds_config.sina_wb_skey'), session('slast_key.access_token'));
  184. $sinauser_info = $c->show_user_by_id(session('slast_key.uid')); //根据ID获取用户等基本信息
  185. $sina_arr = array();
  186. $sina_arr['nickname'] = $sinauser_info['name'];
  187. $sina_str = '';
  188. $sina_str = serialize($sina_arr);
  189. $edit_state = $member_model->editMember(array('member_id' => session('member_id')), array(
  190. 'member_sinaopenid' => session('slast_key.uid'), 'member_sinainfo' => $sina_str
  191. ), session('member_id'));
  192. if ($edit_state) {
  193. $this->success(lang('home_sconnect_binding_success'), 'memberconnect/sinabind');
  194. } else {
  195. $this->error(lang('home_sconnect_binding_fail'), 'memberconnect/sinabind');
  196. }
  197. }
  198. /**
  199. * 更换绑定新浪微博账号
  200. */
  201. public function changesina()
  202. {
  203. //如果用户已经登录,进入此链接则显示错误
  204. if (session('is_login') == '1') {
  205. $this->error(lang('home_sconnect_error'));
  206. }
  207. session('slast_key', null);
  208. $this->redirect('api/oa_sina');
  209. exit;
  210. }
  211. }