人妻夜夜爽天天爽三区丁香花-人妻夜夜爽天天爽三-人妻夜夜爽天天爽欧美色院-人妻夜夜爽天天爽免费视频-人妻夜夜爽天天爽-人妻夜夜爽天天

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C#+UniApp實現微信授權登錄

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

二: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() {			//打開系統,進入微信授權頁面			let wxCode = uni.getStorageSync('wxCode')			if(wxCode==null)			{				this.getWeChatCode()			}		},		methods: {			//請求微信接口,用來獲取code			getWeChatCode() {				let local = encodeURIComponent('http://' + window.location.host +				'/#/pages/wxlogin/wxlogin'); //獲取當前頁面地址作為回調地址				let appid = ''   //公眾號appid				//通過微信官方接口獲取code之后,會重新刷新設置的回調地址【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之后,會重新刷新設置的回調地址【redirect_uri】				}			}		}	}</script>

二:H5用戶同意授權,獲取code

<template>	<view>		微信授權登錄中...	</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);							//綁定到系統用戶表						}					}				},				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			{				//根據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);			}			//根據微信唯一標識openid 去數據庫判斷是否存在			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換取獲取用戶信息(包括非關注用戶的)(此access_token是網頁授權的和普通無關)  /// </summary>  /// <param name="Appid"></param>  /// <param name="Appsecret"></param>  /// <param name="Code">回調頁面帶的code參數</param>  /// <returns>獲取用戶信息(json格式)</returns>  public static Dictionary<string, object> GetUserInfoByCode(string Appid, string Appsecret, string Code){       //通過code換取網頁授權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 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 久久久久国产综合av天堂 | 欧美囗交xx×bbb视频 | 一区二区三区四区免费毛片 | 女性喷液过免费视频 | 任你搞视频这里只有精品 | 国产三级国产av品爱网 | 久久精品视频网站 | 久久精品无码一区二区日韩av老师麻豆综合午夜天天 | 国产a天天免费观看美女 | 色妇色综合久久夜夜 | 麻豆一区产品精品蜜桃的广告语 | 日韩欧美不卡一二三区 | 成人综合色在线一 | 日本三级网址入口 | 久久久久久久影院 | 久久久国产精品亚洲一区久久久成人毛片无码 | 果冻传媒一二三区 | 精品久久aⅴ人妻色欲 | 在线观看av不卡网站永久 | a级a做爰片在线观看视频动漫电视剧在线观看 | 精品国产乱码久久久久app下载 | 熟女丝袜潮喷内裤视频网站 | chinese男男gayvi| 国产一区二区亚洲精品 | 国产欧美成aⅴ人高清 | 韩国精品一区视频在线播放 | 久久影院一区二区三区 | 中文在线免费不卡视频 | 一个人免费观看www在线视频 | 久久国产精品男人的天堂av | 91精品国产乱码在 | 国产欧美日韩精品a在线观看 | 天天干天天爱天天 | 一区二区观看播放 | 中文字幕激情久久 | 亚洲精品久久久久久久久久久 | 精品熟女少妇av免费 | 欧美人与禽交片在线观看 | 日韩精品在线观看av | 老熟妇乱子伦系列视频 | 黄色毛片免费网站 |