BatteryBaseController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace battery\portal\controller;
  12. use think\Config;
  13. use think\Request;
  14. class BatteryBaseController extends BaseController
  15. {
  16. function __construct(Request $request)
  17. {
  18. $wx_openid=$this->battery_openid_check();
  19. if($wx_openid){
  20. $this->battery_wx_autologin($wx_openid);
  21. }
  22. $filterInfo=$request->module().'|'.$request->controller().'|'.$request->action();
  23. $arrayAction=array(
  24. 'portal|Brand|index',//模块|控制器|方法
  25. 'portal|Brand|goods',//模块|控制器|方法
  26. 'portal|Brand|details',//模块|控制器|方法
  27. 'portal|Brand|searchbrand',//模块|控制器|方法
  28. 'portal|Brand|searchseries',//模块|控制器|方法
  29. 'portal|Brand|searchyear',//模块|控制器|方法
  30. 'portal|Brand|searchmodel',//模块|控制器|方法
  31. 'portal|Brand|getpay',//模块|控制器|方法
  32. 'portal|Brand|payorder',//模块|控制器|方法
  33. );
  34. $isFilter=false;
  35. if(in_array($filterInfo,$arrayAction)){
  36. $isFilter=true;
  37. }
  38. if (battery_is_user_login()||$isFilter) {
  39. //已经登录时直接跳到首页
  40. } else {
  41. $this->redirect('login/index');
  42. }
  43. parent::__construct($request);
  44. }
  45. function battery_wx_autologin($openid)
  46. {
  47. // if ($openid) {
  48. // $hcwBaseModel=new HcwBaseModel();
  49. // $where['wx_md5']=md5($openid);
  50. // $info=$hcwBaseModel->name('user')->where($where)->find();
  51. // if ($info) {
  52. // battery_update_current_user($info);
  53. // }else{
  54. // battery_update_current_user(null);
  55. // }
  56. // }
  57. }
  58. function battery_openid_check()
  59. {
  60. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {//检测是否微信浏览器
  61. $wx_appid=Config::get('WX_APPID');
  62. $wx_appsecret=Config::get('WX_APPSECRET');
  63. $wx_data_auth_key=Config::get('WX_DATA_AUTH_KEY');
  64. if (cookie('openid')) {//已有cookie,验证有效性
  65. $openidsign = md5(cookie('openid'), $wx_data_auth_key);
  66. if (!cookie('openidsign') or $openidsign != cookie('openidsign')) {//未通过openid验证,注销重新检测
  67. cookie('openid', null);
  68. cookie('openidsign', null);
  69. battery_update_current_user(null);
  70. } else {
  71. return cookie('openid');
  72. }
  73. } else {//跳转获取
  74. $request = Request::instance();
  75. $data=$request->param();
  76. if (!isset($data['code'])) {
  77. $redirect_uri = url('index/index','',true,true);
  78. $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $wx_appid . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=login#wechat_redirect';
  79. $this->redirect($url);
  80. } else {
  81. $code = $data['code'];
  82. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' .$wx_appid . '&secret=' . $wx_appsecret . '&code=' . $code . '&grant_type=authorization_code';
  83. // curl获取prepay_id
  84. $result = curl_get_contents($url);
  85. $result = json_decode($result, true);
  86. $openid = $result['openid'];
  87. cookie('openid', $openid, 604800);
  88. $openidsign = md5($openid, $wx_data_auth_key);
  89. cookie('openidsign', $openidsign, 604800);
  90. return $openid;
  91. }
  92. }
  93. }
  94. }
  95. public function setTitleName($title='默认'){
  96. $this->assign('title_name',$title);
  97. }
  98. }