uniapp实现公众号微信登录

前端代码

javascript 复制代码
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: 'https://api.ecom20200909.com/Api/getOpenid',
			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;
	},
}

后端php代码

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;
}
相关推荐
@Ma5 小时前
企业微信外部群机器人接入 AI:一套能落地的工程方案
微信·机器人
于先生吖8 小时前
前后端分离二手商城开发,质检登记、回收回款整套业务源码部署教程
java·开发语言·uni-app
2601_961194028 小时前
27考研资料|免费全套|电子版
考研·百度·微信·pdf·微信公众平台·facebook·新浪微博
Geek_Vison15 小时前
政务一网通APP如何引入AI能力,通过一个AI助手就能够调用所有的功能,实现对话即办事
人工智能·ai·小程序·uni-app·小程序容器
凌奕1 天前
微信小程序接入微信 AI:让用户"说一句话"就能下单
微信·微信小程序·agent
狗凯之家源码网2 天前
UniApp 数藏系统源码部署与定制开发全指南
uni-app
弓乙图2 天前
弓乙歌/岐黄真源赋
经验分享·微信
2601_961194022 天前
初中英语教资笔试资源|科三教案模板和知识点资料
百度·微信·微信公众平台·facebook·twitter·新浪微博
RuoyiOffice3 天前
2026 企业定制开发选型:从零开发、低代码、SaaS 与 RuoYi Office 怎么选?
spring boot·uni-app·开源·saas·oa·定制化·ruoyioffice
三天不学习3 天前
【超详细】Vue3+UniApp+.NET8集成腾讯云IM即时通信全攻略
uni-app·.net·腾讯云·im·即时通信