Connectsina.php 10 KB

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