LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#+UniApp實現(xiàn)微信授權(quán)登錄

admin
2025年1月15日 8:32 本文熱度 371
一、效果展示

二:H5登錄頁面

<template>	<view class="content">		<image class="img" src="../../static/images/logo.png" mode=""></image>		<form>			<view class="list" style="margin-top: 80rpx;">				<input class="uni-input maininput" v-model="name" placeholder="用戶名" />				<text class="bl_icon_yonghu"></text>			</view>			<view class="list texttop">				<input class="uni-input maininput" type="password" v-model="password" placeholder="密碼" />				<text class="bl_icon_mima"></text>			</view>			<view>				<button type="primary" class="buttoncolor martop" @click="formSubmit">登陸</button>			</view>			<view class="bottom-side-otherLogin" @click="getWeChatCode">				<text>微信登錄</text>				<image src="../../static/images/wx.png"></image>			</view>		</form>	</view></template><script>	export default {		data() {			return {			}		},		onLoad() {			//打開系統(tǒng),進入微信授權(quán)頁面			let wxCode = uni.getStorageSync('wxCode')			if(wxCode==null)			{				this.getWeChatCode()			}		},		methods: {			//請求微信接口,用來獲取code			getWeChatCode() {				let local = encodeURIComponent('http://' + window.location.host +				'/#/pages/wxlogin/wxlogin'); //獲取當前頁面地址作為回調(diào)地址				let appid = ''   //公眾號appid				//通過微信官方接口獲取code之后,會重新刷新設(shè)置的回調(diào)地址【redirect_uri】				let code = this.getUrlCode('code')				//如果沒有code 去獲取code				if (code == null) {					uni.setStorageSync('isLogin', 1)					window.location.href =						"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +						appid +						"&redirect_uri=" +						local +						"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";				} else {					//this.checkWeChatCode(code) //通過微信官方接口獲取code之后,會重新刷新設(shè)置的回調(diào)地址【redirect_uri】				}			}		}	}</script>

二:H5用戶同意授權(quán),獲取code

<template>	<view>		微信授權(quán)登錄中...	</view></template><script>export default {	data() {		return {			openid'',			isBind'',		}	},	onLoad() {		let isLogin = uni.getStorageSync('isLogin')		if (isLogin == 1) {			let code = this.getUrlCode('code')			if (code != null) {				this.checkWeChatCode(code)				uni.setStorageSync('wxCode', code)			}		}	},	methods: {		//方法:用來提取code		getUrlCode(name) {			return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||				[, ''				])[1]				.replace(/\+/g, '%20')) || null		},		//檢查瀏覽器地址欄中微信接口返回的		checkWeChatCode(code) {			if (code) {				this.getOpenidAndUserinfo(code)			}		},		//把code傳遞給后臺接口,靜默登錄		getOpenidAndUserinfo(code) {			let that = this;			uni.request({				url: that.websiteUrl + '/api/Wx/WechatLogin',				method: 'GET',				data: {					code: code				},				header: {					'content-type': 'application/x-www-form-urlencoded' //自定義請求頭信息				},				success: function(res) {					if (res.statusCode == 200) {						if (res.data.errCode == 0) {							that.openid = res.data.result.Openid;							that.isBind = res.data.result.IsBind;							uni.setStorageSync('openid', that.openid);							uni.setStorageSync('headimgurl', res.data.result.Headimgurl);							//綁定到系統(tǒng)用戶表						}					}				},				fail: function(data) {				}			})		}	}}</script>

三:后端獲取微信用戶信息

?

/// <summary>  /// 獲取微信用戶信息/// </summary>  /// <returns></returns>[HttpGet]public ApiResponse WechatLogin(string code){	try {		if (!string.IsNullOrEmpty(code))		{			string openid = string.Empty;			string headimgurl = string.Empty;			if (CacheHelper.Get(code)!=null)			{				openid = CacheHelper.Get(code).ToString();			}			else			{				//根據(jù)appid,secret,code取到用戶的全部信息  				Dictionary<string, object> dic = GetUserInfoByCode(AppId, AppSecret, code.Trim());				if (dic.ContainsKey("errcode"))				{					return BaseApiResponse.ApiError(dic["errmsg"].ToString());				}				openid = dic["openid"].ToString();				headimgurl = dic["headimgurl"].ToString();				CacheHelper.Insert(code, openid);			}			//根據(jù)微信唯一標識openid 去數(shù)據(jù)庫判斷是否存在			UserDao userDao = new UserDao();			UserEntity userEntity = userDao.GetByOpenid(openid);			Dictionary<string, string> userDic = new Dictionary<string, string>();			if (userEntity != null)			{				userDic.Add("UserName", userEntity.Code);				userDic.Add("PassWord", EncryptCommon.DecryptStr(userEntity.Password));				userDic.Add("Openid", openid);				userDic.Add("IsBind""1");				userDic.Add("Headimgurl", headimgurl);			}			else			{				userDic.Add("UserName""");				userDic.Add("PassWord""");				userDic.Add("Openid", openid);				userDic.Add("IsBind""0");				userDic.Add("Headimgurl", headimgurl);			}			return BaseApiResponse.ApiSuccess(userDic);		}		return BaseApiResponse.ApiError("code不能為空!");	}	catch(Exception ex){		return BaseApiResponse.ApiError(ex.ToString());	}}
/// <summary>  ///用code換取獲取用戶信息(包括非關(guān)注用戶的)(此access_token是網(wǎng)頁授權(quán)的和普通無關(guān))  /// </summary>  /// <param name="Appid"></param>  /// <param name="Appsecret"></param>  /// <param name="Code">回調(diào)頁面帶的code參數(shù)</param>  /// <returns>獲取用戶信息(json格式)</returns>  public static Dictionary<string, object> GetUserInfoByCode(string Appid, string Appsecret, string Code){       //通過code換取網(wǎng)頁授權(quán)access_token	JavaScriptSerializer Jss = new JavaScriptSerializer();	string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", Appid, Appsecret, Code);	string ReText = Tools.WebRequestPostOrGet(url, "");//post/get方法獲取信息  	Dictionary<string, object> DicText = (Dictionary<string, object>)Jss.DeserializeObject(ReText);	if (!DicText.ContainsKey("openid"))	{		return DicText;	}	else	{	        //拉取用戶信息(需scope為 snsapi_userinfo)		Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(Tools.WebRequestPostOrGet("https://api.weixin.qq.com/sns/userinfo?access_token=" + DicText["access_token"] + "&openid=" + DicText["openid"] + "&lang=zh_CN", ""));		return respDic;	}}


閱讀原文:原文鏈接


該文章在 2025/1/15 10:17:20 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲国产精品久久久男人的天堂 | 免费人成国产网页在线观看 | 制服中文字幕在线一区 | 日韩欧美国产传媒第一区二区 | 婷婷色中文字幕综合在线 | 日本人成在线视频免费播放 |