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