• 微信公众号打开h5 注册界面点击微信登录,获取微信信息绑定本地用户,一键登录


    注册界面判断是否是微信浏览器打开的,如果是,跳转下面的getcode接口,获取到微信code跟回调信息,通过callback获取到微信用户信息后跳转到

    /bindphone.html页面,带着微信用户信息去实现表单提交注册绑定功能

    <script>
    if(isWeixin()) {
    // alert('是微信浏览器');
    window.location.href = "{:url(\"/bindphone/getcode\")}";
    }

    function isWeixin () {
            var ua = window.navigator.userAgent.toLowerCase();
            if(ua.match(/MicroMessenger/i) == 'micromessenger' || ua.match(/_SQ_/i) == '_sq_'){
                return true;
            } else{
                return false;
            }
        }
    

      

    <?php
    
    namespace app\home\controller;
    
    use think\facade\Cache;
    use think\facade\Db;
    use think\facade\Session;
    
    /*use aliyun\api_demo\SmsDemo;*/
    
    class BindPhone extends Base
    {
        public $nid = "sign";// 模型标识
        public $channeltype;// 模型ID
        private $appid = "wx1ec***********";
        private $AppSecret = '24ca****************';
    
        public function _initialize()
        {
            parent::_initialize();
        }
    
        /*
         * 微信登录绑定手机号
         */
        public function index()
        {
            $param = request()->param();
            $this->assign('openarr', $param);
            return $this->fetch(":bindphone");
        }
    
        public function http_curl($url)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_URL, $url);
            $json = curl_exec($ch);
            curl_close($ch);
            return json_decode($json, 1);
    
        }
    
        public function getcode()
        {
    
            $state = md5(uniqid(rand(), TRUE));
            $hdurl = urlencode("http://www.*****.cn/bindphone/callback");//微信公众号需要绑定该地址
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=" . $hdurl . "&response_type=code&scope=snsapi_userinfo&state=" . $state . "#wechat_redirect";
            $this->redirect($url);
        }
    
        public function callback()
        {
            $param = request()->param();
            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->AppSecret . '&code=' . $param['code'] . '&grant_type=authorization_code';
            $arr = $this->http_curl($url);
            $openids = $arr['openid'];
            Cache::set($openids, $openids, 86400);
            //得到openid  uniid  后查询数据库看看是否有该纪录,如果有--<手机验证通过后>,直接登录,存好uid到session,如果没有,走绑定手机号步骤,进行完善或者注册
            $useinfo = Db::name('nqi_sys_user')->where('openid', $openids)->find();
            if (!empty($useinfo)) {
                //手机号验证登录操作
                $this->redirect("/loginphone.html?oid=".$openids);
                /*$this->sees($useinfo['sys_user_id'],$useinfo['user_name']);
                $this->redirect("/news.html?searchmodels=能源&filter=filter-ny&pageindex=2&pagesize=12");die;*/
            }
            //得到 access_token 与 openid
            $urls = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $arr['access_token'] . '&openid=' .$openids . '&lang=zh_CN';
            $wechatInfos = $this->http_curl($urls);
            //得到 用户资料,将openid  unionid传给页面,跳转去走注册或者绑定流程页面
            $this->redirect("/bindphone.html?openid=".$wechatInfos['openid']."&nickname=".$wechatInfos['nickname']);
        }
    
        /**
         * 更新或者添加用户
         */
        public function addUser()
        {
            $param = request()->param();
            $mobile = $param['mobile_phone'];
            $openid = $param['openid'];
            $jianjie = Db::name('nqi_sys_user')->where('mobile_phone', $param['mobile_phone'])->find();
            $data = [];
            if (!empty($jianjie)) {
                $uid = $jianjie['sys_user_id'];
                if(!empty($jianjie['openid'])){
                    return ['status' => 400, 'msg' => '该手机号已有微信绑定','data' =>'','token'=>''];die;
                }
                Db::name('nqi_sys_user')->where('sys_user_id', $uid)->update(['openid' => $openid]);
                return $this->sees($jianjie['sys_user_id'],$jianjie['user_name']);
    
            } else {
                $time = time();
                $data['sys_user_id'] = uuid();
                $data['password'] = func_encrypt('123456');
                $data['sys_role_id'] = 'CompanyUser';
                $data['user_type'] = 1;
                $data['openid'] = $openid;
                $data['user_status'] = 1;
                $data['user_name'] = "youke".$time;
                $data['real_name'] = $param['real_name'];
                $data['mobile_phone'] = $param['mobile_phone'];
                $res = Db::name('nqi_sys_user')->insert($data);
                return $this->sees($data['sys_user_id'],$data['user_name']);
            }
        }
    
        public function sees($sys_user_id,$user_name){
            $token = md5($sys_user_id . '369369369');
            Session::set('userName', $user_name);
            Session::set('token', $token);
            Session::set('sys_user_id', $sys_user_id);
            return ['status' => 200, 'msg' => '成功','data' => $sys_user_id,'token'=>$token];
        }
    
    
    }
  • 相关阅读:
    lambda表达式
    You can't specify target table 't_mail_marketing' for update in FROM clause
    从对象list中获取对象属性list
    枚举缓存
    Solr语法
    通过Telnet查询注册服务
    日志查询
    Maven 屏蔽静态文件
    抽奖
    随机码生成方法
  • 原文地址:https://www.cnblogs.com/yszr/p/15654321.html
Copyright © 2020-2023  润新知