• uniapp实现公众号微信登录


    前端代码

    data() {
    	return {
    		openid: "",
    		server: '',
    		code:''
    	};
    },
    mounted() {
    	this.getCode()
    },
    methods:{
    	getCode() {
    		// 非静默授权,第一次有弹框
    		this.code = '';
    		var callback_url = '回调地址'; // 获取页面url
    		var appid = 'APPID';
    		this.code = this.getUrlCode().code; // 截取code
    		if (this.code == null || this.code === '') {
    			// 如果没有code,则去请求
    			window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
    	            callback_url
    	        )}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
    		} else {
    			// 当code不等于空时,调用后端接口获取用户信息
    			this.getUserInfo();
    			// 你自己的业务逻辑
    		}
    	},
    	getUserInfo() {
    		let token = uni.getStorageSync("token");
    		const header = {
    			"Content-Type": "application/json",
    			"Authorization": token
    		};
    		let data = {}
    		data.code = this.code
    		data.appid= '你的appid'
    		data.secret= '你的secret'
    		uni.request({
    			url: '接口地址',
    			data: data,
    			header: header,
    			timeout: 20000,
    			method: 'POST',
    			dataType: 'json',
    			success: (res) => {
    				this.setOpenid(res.data.openid);
    				uni.setStorageSync('openid', res.data.openid)
    			}
    		})
    	},
    	getUrlCode() {
    		// 截取url中的code方法
    		var url = location.search;
    		var theRequest = new Object();
    		if (url.indexOf('?') != -1) {
    			var str = url.substr(1);
    			var strs = str.split('&');
    			for (var i = 0; i < strs.length; i++) {
    				theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
    			}
    		}
    		return theRequest;
    	},
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    后端php代码

    public function getOpenid(){
        $code = input('post.code');
        $param["appid"]=input('post.appid');
        $param["secret"]=input('post.secret');
        $param["code"]=$code;
        $param["grant_type"]="authorization_code";
        $json = $this->curlPost("https://api.weixin.qq.com/sns/oauth2/access_token",$param);
        $rv=json_decode($json,true);
        return json($rv);
    }
    
    public function curlPost($url = '', $postData = '', $options = array()) {
      if (is_array($postData)) {
          $postData = http_build_query($postData);
      }
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
      curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
      if (!empty($options)) {
          curl_setopt_array($ch, $options);
      }
      //https请求 不验证证书和host
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
      $data = curl_exec($ch);
      curl_close($ch);
    
      return $data;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    Shiro入门以及Shiro与web整合
    SpringCloud Alibaba(三) - GateWay网关
    初识JavaScript
    MySQL 计算年龄
    浅谈用KUSTO查询语言(KQL)在Azure Synapse Analytics(Azure SQL DW)审计某DB账号的操作记录
    CLIP论文解读
    Linux下JSON解析工具
    leetcode day12 二叉树的最大深度
    目标检测YOLO实战应用案例100讲-基于弱监督学习的目标检测
    STP生成树(端口状态+端口角色+收敛机制 )|||| STP优化技术( uplinkfast技术+Portfast技术+backbonefast技术 )详解
  • 原文地址:https://blog.csdn.net/weixin_61769998/article/details/134079308