• 热线电话
  • 17798885678
  • 18021659625
新闻资讯
联系我们

 

南通米锐软件工作室

业务手机:18021659625

企业邮箱:mirocn@163.com

地址:南通市如皋市中山东路210号东景国际

C#后台uniapp微信小程序、APP验证登录

日期:2022/12/14 8:48:17

通过uniapp开发的小程序可以快速打包成微信小程序或者IOS和安卓的APP,最近一个项目就用了这个快速开发工具。后台使用的是C#MVC框架与前端进行交互。程序入口使用微信或QQ账号与本系统账号绑定登录的方案,通过查看微信小程序开发文档以及CSDN,现前后台能够完成跑通。

 

前端代码:

(VIEW模块)

<view class="login-footer">
<view class="footer-tip flex">其他登录方式</view>
<view class="footer-other flex">
<!-- 5+app 用qq/微信/微博 登录 小程序用微信小程序登录 h5不支持 -->
<!-- #ifdef APP-PLUS -->
<view class="other-list">
<image src="../../static/ic-QQ@2x.png" mode="aspectFill" @tap="login_qq()"></image>
</view>
<view class="other-list">
<image src="../../static/ic-weixin@2x.png" mode="aspectFill" @tap="login_weixin()"></image>
</view>
<!-- #endif -->
<!-- #ifdef MP-WEIXIN -->
<view class="other-list">
 
<button class="sys_btn" mode="aspectFill" plain="true" open-type="getUserInfo" lang="zh_CN" @click="wxLoginNew">
</button>
</view>
<!-- #endif -->
</view>
</view>
 
前端交互代码:
 
<script>
import {
mapMutations
} from 'vuex';
import {
DOMParser
} from 'xmldom';
 
export default {
data() {
return {
siteUrl:'',
mobile: '',
password: '',
logining: false,
uInfo: {}
}
},
onLoad() {
this.siteUrl = this.websiteUrl;
},
methods: {
...mapMutations(['login']),
inputChange(e) {
const key = e.currentTarget.dataset.key;
this[key] = e.detail.value;
},
navBack() {
uni.navigateBack();
},
toRegist() {
//this.$api.msg('请至公众号注册!');
uni.redirectTo({
url: '/pages/public/reg'
});
},
toReMemberPass(){
//this.$api.msg('请至公众号找回密码!');
uni.redirectTo({
url: '/pages/public/find'
});
},
async wxLoginNew() {
var me=this;
let sUrl=this.siteUrl;
// #ifdef MP-WEIXIN
uni.showLoading({
mask:true,
title:"登录中..."
});
uni.showNavigationBarLoading();
uni.getUserProfile({
desc:'weixin',
success:infoRes=>{
console.log("-----infoRes------");
console.log(infoRes);
uni.login({
provider: 'weixin',
success: (loginRes) => {
console.log(loginRes);
 
uni.request({
url: sUrl + "/WebService.asmx/GetCode?json_code="+loginRes.code,
data: {},
method: "GET",
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success(res1) {
console.log(res1);
const doc = new DOMParser().parseFromString(res1.data, 'text/xml');
console.log(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
var srvInfo = JSON.parse(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
var srvData=JSON.parse(srvInfo.data);
console.log("openid:"+srvData.openid+",用户名:"+infoRes.userInfo.nickName);
 
uni.request({
url: sUrl + "/WebService.asmx/SetWuserByOpenId?openId=" + srvData.openid + "&nickName=" +
infoRes.userInfo.nickName + "&gender=" + infoRes.userInfo.gender + "&city=" + infoRes.userInfo.city +
"&province=" + infoRes.userInfo.province + "&country=" + infoRes.userInfo.country + "&avatarUrl=" + 
infoRes.userInfo.avatarUrl + "&unionId=" + infoRes.userInfo.unionId,
data: {},
method: 'GET',
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success: (res2) => {
console.log(res2);
const doc2 = new DOMParser().parseFromString(res2.data, 'text/xml');
console.log(doc2.getElementsByTagName("string")[0].childNodes[0].nodeValue);
var custInfo = JSON.parse(doc2.getElementsByTagName("string")[0].childNodes[0].nodeValue);
if(custInfo.msg==="信息同步完成"&&custInfo.mobile===""){
uni.redirectTo({
url: '/pages/public/bind?openId='+srvData.openid
});
}else if(custInfo.msg==="信息同步完成"&&custInfo.mobile!=""){
let tbMobile=custInfo.mobile;
uni.request({
url: sUrl + "/WebService.asmx/LoginByMobile?Mobile=" + tbMobile,
data: {},
method: 'GET',
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success: (res) => {
console.log(res);
const doc = new DOMParser().parseFromString(res.data, 'text/xml');
var uList = JSON.parse(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
me.uInfo = uList;
me.login(uList);
me.initCart();
uni.navigateBack();
}
});
}
}
});
 
 
}
});
},
fail: () => {
uni.showToast({
title: "微信登录授权失败",
icon: "none"
});
}
});
},
fail: () => {
uni.showToast({
title: "微信登录授权失败",
icon: "none"
});
},
complete: () => {
uni.hideNavigationBarLoading();
uni.hideLoading();
}
});
 
//#endif
},
async wxLogin() {
var me=this;
let sUrl=this.siteUrl;
// #ifdef MP-WEIXIN
uni.showLoading({
mask:true,
title:"登录中..."
});
uni.showNavigationBarLoading();
uni.getProvider({
service: 'oauth',
success: function(res) {
if (~res.provider.indexOf('weixin')) {
uni.login({
provider: 'weixin',
success: (loginRes) => {
console.log(loginRes);
uni.getUserInfo({
provider: 'weixin',
success: (infoRes) => { //这里请求接口
console.log(loginRes);
console.log("-----infoRes------");
console.log(infoRes);
console.log("openid:"+infoRes.userInfo.openId+",用户名:"+infoRes.userInfo.nickName);
uni.request({
url: sUrl + "/WebService.asmx/GetCode?json_code="+loginRes.code,
data: {},
method: "GET",
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success(res1) {
console.log(res1);
const doc = new DOMParser().parseFromString(res1.data, 'text/xml');
console.log(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
var srvInfo = JSON.parse(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
var srvData=JSON.parse(srvInfo.data);
console.log("openid:"+srvData.openid+",用户名:"+infoRes.userInfo.nickName);
 
uni.request({
url: sUrl + "/WebService.asmx/SetWuserByOpenId?openId=" + srvData.openid + "&nickName=" +
infoRes.userInfo.nickName + "&gender=" + infoRes.userInfo.gender + "&city=" + infoRes.userInfo.city +
"&province=" + infoRes.userInfo.province + "&country=" + infoRes.userInfo.country + "&avatarUrl=" + 
infoRes.userInfo.avatarUrl + "&unionId=" + infoRes.userInfo.unionId,
data: {},
method: 'GET',
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success: (res2) => {
console.log(res2);
const doc2 = new DOMParser().parseFromString(res2.data, 'text/xml');
console.log(doc2.getElementsByTagName("string")[0].childNodes[0].nodeValue);
var custInfo = JSON.parse(doc2.getElementsByTagName("string")[0].childNodes[0].nodeValue);
if(custInfo.msg==="信息同步完成"&&custInfo.mobile===""){
uni.redirectTo({
url: '/pages/public/bind?openId='+srvData.openid
});
}else if(custInfo.msg==="信息同步完成"&&custInfo.mobile!=""){
let tbMobile=custInfo.mobile;
uni.request({
url: sUrl + "/WebService.asmx/LoginByMobile?Mobile=" + tbMobile,
data: {},
method: 'GET',
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success: (res) => {
console.log(res);
const doc = new DOMParser().parseFromString(res.data, 'text/xml');
var uList = JSON.parse(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
me.uInfo = uList;
me.login(uList);
me.initCart();
uni.navigateBack();
}
});
}
// me.uInfo = custInfo;
// me.login(custInfo);
// me.initCart();
// uni.navigateBack();
 
}
});
// uni.request({
// url: sUrl + "/WebService.asmx/LoginByOpenId?openId=" + srvData.openid + "&nickName=" +
// infoRes.userInfo.nickName + "&gender=" + infoRes.userInfo.gender + "&city=" + infoRes.userInfo.city +
// "&province=" + infoRes.userInfo.province + "&country=" + infoRes.userInfo.country + "&avatarUrl=" + 
// infoRes.userInfo.avatarUrl + "&unionId=" + infoRes.userInfo.unionId,
// data: {},
// method: 'GET',
// header: {
// 'Content-Type': 'text/xml; charset=utf-8',
// },
// success: (res2) => {
// console.log(res2);
// const doc2 = new DOMParser().parseFromString(res2.data, 'text/xml');
// console.log(doc2.getElementsByTagName("string")[0].childNodes[0].nodeValue);
// var custInfo = JSON.parse(doc2.getElementsByTagName("string")[0].childNodes[0].nodeValue);
// me.uInfo = custInfo;
// me.login(custInfo);
// me.initCart();
// uni.navigateBack();
 
// }
// });
 
}
});
 
},
fail: () => {
uni.showToast({
title: "微信登录授权失败",
icon: "none"
});
},
complete: () => {
uni.hideNavigationBarLoading();
uni.hideLoading();
}
});
},
fail: () => {
uni.showToast({
title: "微信登录授权失败",
icon: "none"
});
}
});
 
} else {
uni.showToast({
title: '请先安装微信或升级版本',
icon: "none"
});
}
}
});
//#endif
},
//QQ登录
login_qq() {
let _this = this;
uni.login({
provider: 'qq',
success: function(loginRes) {
// 获取用户信息
uni.getUserInfo({
provider: 'qq',
success: function(infoRes) {
_this.other_login(loginRes, infoRes, 'qq')
}
});
}
});
},
//微信登录
login_weixin() {
let _this = this;
uni.showNavigationBarLoading();
uni.login({
provider: 'weixin',
success: function(loginRes) {
// 获取用户信息
uni.getUserInfo({
provider: 'weixin',
success: function(infoRes) {
_this.other_login(loginRes, infoRes, 'wx');
 
}
});
},
complete: () => {
uni.hideNavigationBarLoading();
}
});
},
//授权登录
other_login(loginRes, infoRes, type) {
let _this = this;
let url;
let pram = {};
// _this.loginRes=JSON.stringify(loginRes).toString();
// _this.infoRes=JSON.stringify(infoRes).toString();
switch (type) {
case 'qq':
url = '/token/sys/login-qq';
pram = {
'openid': loginRes.authResult.openid,
'nickname': infoRes.userInfo.nickname,
'gender': infoRes.userInfo.gender,
'province': infoRes.userInfo.province,
'city': infoRes.userInfo.city,
'figureurl': infoRes.userInfo.figureurl_qq
}
break;
case 'wx':
url = '/token/sys/login-wechat';
pram = {
'openid': loginRes.authResult.openid,
'nickname': infoRes.userInfo.nickName,
'sex': infoRes.userInfo.gender,
'province': infoRes.userInfo.province,
'city': infoRes.userInfo.city,
'country': infoRes.userInfo.country,
'headimgurl': infoRes.userInfo.avatarUrl,
'unionid': loginRes.authResult.unionid
}
break;
default:
}
 
//console.log(infoRes.userInfo);
uni.request({
url: this.websiteUrl + "/WebService.asmx/LoginByOpenId?openId=" + infoRes.userInfo.openId + "&nickName=" +
infoRes.userInfo.nickName + "&gender=" + infoRes.userInfo.gender + "&city=" + infoRes.userInfo.city +
"&province=" + infoRes.userInfo.province + "&country=" + infoRes.userInfo.country + "&avatarUrl=" + infoRes.userInfo
.avatarUrl + "&avatarUrl=" + infoRes.userInfo.avatarUrl + "&unionId=" + infoRes.userInfo.unionId,
data: {},
method: 'GET',
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success: (res) => {
const doc = new DOMParser().parseFromString(res.data, 'text/xml');
console.log(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
var userInfo = JSON.parse(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
this.uInfo = userInfo;
this.login(userInfo);
this.initCart();
uni.navigateBack();
if (res.data.code == 200) {
//_this.testData=JSON.stringify(res.data.data).toString();;
//_this.login(true, res.data.data, function() {
// _this.getRongyToken();
//});
}
}
});
 
},
toLogin() {
this.logining = true;
const {
mobile,
password
} = this;
if (mobile != "" && password != "") {
uni.request({
url: this.websiteUrl + "/WebService.asmx/Login?UserName=" + mobile + "&Password=" + password,
data: {},
method: 'GET',
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success: (res) => {
//console.log(res.data);
const doc = new DOMParser().parseFromString(res.data, 'text/xml');
var userInfo = JSON.parse(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
this.uInfo = userInfo;
//console.log(userInfo.msg);
if (userInfo.msg == "该用户被锁定,请稍候再试!") {
uni.showToast({
title: '该用户被锁定,请稍候再试!',
duration: 2000,
icon: "none"
});
this.logining = false;
} else if (userInfo.msg == "用户名或密码不正确,请重新输入!") {
console.log(userInfo);
uni.showToast({
title: '用户名或密码不正确,请重新输入!',
duration: 2000,
icon: "none"
});
this.logining = false;
} else {
 
this.login(userInfo);
this.initCart();
// uni.redirectTo({
// url: '/pages/public/bind'
// });
uni.navigateBack();
}
}
});
} else {
uni.showToast({
title: '用户名或密码不能为空!',
duration: 2000,
icon: "none"
});
this.logining = false;
}
},
initCart() {
console.log(this.uInfo.UserId);
uni.request({
url: this.websiteUrl + "/WebService.asmx/GetCartProducts?customerId=" + this.uInfo.UserId,
data: {},
method: 'GET',
header: {
'Content-Type': 'text/xml; charset=utf-8',
},
success: (res) => {
console.log(res);
const doc = new DOMParser().parseFromString(res.data, 'text/xml');
var proList = JSON.parse(doc.getElementsByTagName("string")[0].childNodes[0].nodeValue);
let list = proList || [];
let cartList = list.map(item => {
item.checked = true;
return item;
});
this.cartList = cartList;
uni.setStorageSync("cartGlobalList", this.cartList);
}
});
}
},
 
}
</script>