Connectsina.php 10 KB

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