PublicController.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Controller;
  15. class PublicController extends CommonController {
  16. public $weixin_config;
  17. protected function _initialize()
  18. {
  19. parent::_initialize();
  20. $appid_info = M('config')->where( array('name' => 'APPID') )->find();
  21. $appsecret_info = M('config')->where( array('name' => 'APPSECRET') )->find();
  22. $mchid_info = M('config')->where( array('name' => 'MCHID') )->find();
  23. $weixin_config = array();
  24. $weixin_config['appid'] = $appid_info['value'];
  25. $weixin_config['appscert'] = $appsecret_info['value'];
  26. $weixin_config['mchid'] = $mchid_info['value'];
  27. $this->weixin_config = $weixin_config;
  28. }
  29. public function test(){
  30. $not = D('Home/Weixinnotify');
  31. $not->sendBuyMsg();
  32. }
  33. /**
  34. * 微信授权登陆
  35. */
  36. public function wxlogin()
  37. {
  38. $jssdk = new \Lib\Weixin\Jssdk( $this->weixin_config['appid'], $this->weixin_config['appscert']);
  39. $jssdk->getsnsapi_userinfo();
  40. }
  41. /**
  42. * 微信授权登陆回调
  43. */
  44. public function wxauthcallback()
  45. {
  46. $jssdk = new \Lib\Weixin\Jssdk( $this->weixin_config['appid'], $this->weixin_config['appscert']);
  47. $code = $_GET['code'];
  48. $state = $_GET['state'];
  49. if(is_login() && false)
  50. {
  51. $redirct_url = cookie('redirct_url');
  52. if( empty($redirct_url) )
  53. {
  54. $redirct_url = U('Index/index');
  55. }
  56. if( empty($redirct_url) )
  57. {
  58. $redirct_url = U('Index/index');
  59. }
  60. $head_http = 'http://';
  61. $url = C('SITE_URL');
  62. if( strpos($url,'https:') !== false )
  63. {
  64. $head_http = 'https://';
  65. }
  66. $url = str_replace('http://','',$url);
  67. $url = str_replace('https://','',$url);
  68. $url_arr = explode('/',$url);
  69. $domain_site = $head_http.$url_arr[0];
  70. header('Location: '.$domain_site.urldecode($redirct_url));
  71. }
  72. $auth_accsss_info = $jssdk->getAutoAccessToken($code);
  73. $user_info = $jssdk->getSnsapiUserinfo($auth_accsss_info);
  74. $member_info = M('member')->where( array('openid' =>$user_info['openid']) )->find();
  75. if( empty($member_info) && !empty($user_info['unionid']) )
  76. {
  77. $member_info = M('member')->where( array('unionid' =>$user_info['unionid']) )->find();
  78. }
  79. if(!empty($member_info) )
  80. {
  81. $data = array();
  82. $data['member_id'] = $member_info['member_id'];
  83. $data['last_login_time'] = time();
  84. $data['login_count'] = array('exp','login_count+1');
  85. $data['last_login_ip'] = get_client_ip();
  86. $data['openid'] = trim($user_info['openid']);
  87. M('Member')->save($data);
  88. $auth = array(
  89. 'uid' => $member_info['member_id'],
  90. 'username' => $member_info['uname'],
  91. );
  92. session('user_auth', $auth);
  93. session('user_auth_sign', data_auth_sign($auth));
  94. cookie('auth_rp_string',think_ucenter_encrypt($user_info['openid'],C('PWD_KEY')),86400*7);
  95. cookie('rmid','',-86400*7);
  96. } else {
  97. $data = array();
  98. $data['email']= time().mt_rand(1,9999).'@lf.com';
  99. $data['uname']=trim($user_info['nickname']);
  100. $data['name']=trim($user_info['nickname']);
  101. $data['avatar']=trim($user_info['headimgurl']);
  102. $data['openid'] = trim($user_info['openid']);
  103. $data['unionid'] = trim($user_info['unionid']);
  104. $data['pwd'] = think_ucenter_encrypt($user_info['nickname'],C('PWD_KEY'));
  105. $data['status']=1;
  106. $data['create_time'] = time();
  107. $data['last_login_ip'] = get_client_ip();
  108. $re= M('Member')->add($data);
  109. if($re){
  110. $auth = array(
  111. 'uid' => $re,
  112. 'username' => $data['uname'],
  113. );
  114. $rmid = cookie('rmid');
  115. if( !empty($rmid) )
  116. {
  117. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  118. $re_member_id = $hashids->decode($rmid);
  119. $re_member_id = $re_member_id[0];
  120. $fenxiao_model = D('Home/Fenxiao');
  121. $fenxiao_model->relation_fenxiao($re_member_id,$re);
  122. cookie('rmid','',-86400*7);
  123. }
  124. session('user_auth', $auth);
  125. session('user_auth_sign', data_auth_sign($auth));
  126. cookie('auth_rp_string',think_ucenter_encrypt($user_info['openid'],C('PWD_KEY')),86400*7);
  127. }
  128. }
  129. $redirct_url = cookie('redirct_url');
  130. //
  131. /**
  132. if($user_info['openid'] == 'o0n_HwcGIfwf5b8PN8-gmNfsJBLA')
  133. {
  134. var_dump($redirct_url);die();
  135. }
  136. **/
  137. if( empty($redirct_url) )
  138. {
  139. $redirct_url = U('Index/index');
  140. }
  141. $head_http = 'http://';
  142. $url = C('SITE_URL');
  143. if( strpos($url,'https:') !== false )
  144. {
  145. $head_http = 'https://';
  146. }
  147. $url = str_replace('http://','',$url);
  148. $url = str_replace('https://','',$url);
  149. $url_arr = explode('/',$url);
  150. $domain_site = $head_http.$url_arr[0];
  151. //var_dump($domain_site.urldecode($redirct_url));
  152. //die();
  153. header('Location: '.$domain_site.urldecode($redirct_url));
  154. /**
  155. $url = C('SITE_URL');
  156. $url = str_replace('http://','',$url);
  157. $url = str_replace('https://','',$url);
  158. $url_arr = explode('/',$url);
  159. $domain_site = 'http://'.$url_arr[0].'/';
  160. header('Location: '.$domain_site.urldecode($redirct_url));
  161. **/
  162. }
  163. /* 登录页面 */
  164. public function login(){
  165. if(IS_POST){
  166. if(!check_verify(I('code'))){
  167. $this->error='验证码输入错误!';
  168. $this->display();
  169. die();
  170. }
  171. if(empty($_POST['uname'])){
  172. $this->error="用户名 / email不能为空!!";
  173. $this->display();die();
  174. }elseif(empty($_POST['pwd'])){
  175. $this->error="密码不能为空!!";
  176. $this->display();die();
  177. }
  178. $user=M('Member')->getByUname($_POST['uname']);
  179. if(!$user){
  180. $user=M('Member')->getByEmail($_POST['uname']);
  181. }
  182. //用户存在且可用
  183. if($user&&$user['status']==1){
  184. //验证密码
  185. if(think_ucenter_encrypt($_POST['pwd'],C('PWD_KEY'))==$user['pwd']){
  186. $auth = array(
  187. 'uid' => $user['member_id'],
  188. 'username' => $user['uname'],
  189. 'status' => $user['status']
  190. );
  191. session('user_auth', $auth);
  192. session('user_auth_sign', data_auth_sign($auth));
  193. if($user['address_id']!=0){
  194. session('shipping_address_id',$user['address_id']);
  195. }
  196. storage_user_action($user['member_id'],$user['uname'],C('FRONTEND_USER'),'登录了网站');
  197. $data = array();
  198. $data['member_id'] = $user['member_id'];
  199. $data['last_login_time'] = time();
  200. $data['login_count'] = array('exp','login_count+1');
  201. $data['last_login_ip'] = get_client_ip();
  202. $tip=new \Lib\Taobaoip();
  203. $ip_region=$tip->getLocation($data['last_login_ip']);
  204. $data['last_ip_region']=$ip_region['region'].'-'.$ip_region['city'];
  205. M('Member')->save($data);
  206. $this->redirect('/order');
  207. }else{
  208. $this->error='密码错误!!';
  209. $this->display();die();
  210. }
  211. }else{
  212. $this->error="用户不存在或被禁用!!";
  213. $this->display();die();
  214. }
  215. } else {
  216. $this->title='用户登录-';
  217. $this->meta_keywords=C('SITE_KEYWORDS');
  218. $this->meta_description=C('SITE_DESCRIPTION');
  219. if(is_login()){
  220. $this->redirect('/order');
  221. }else{
  222. $this->display();
  223. }
  224. }
  225. }
  226. /* 退出登录 */
  227. public function logout(){
  228. session('[destroy]');
  229. session('user_auth', '');
  230. session('user_auth_sign', '');
  231. cookie('auth_rp_string','',-86400*7);
  232. die('ok');
  233. //$this->redirect('/login');
  234. }
  235. public function verify(){
  236. $verify = new \Think\Verify();
  237. $verify->codeSet = '2345689';
  238. $verify->fontSize = 30;
  239. $verify->length = 4;
  240. $verify->useCurve = false;
  241. $verify->useNoise = true;
  242. $verify->entry(1);
  243. }
  244. }