import { WxCode , WxLogin } from '@/api/user.js'
WxCode({
callback_url: 'http://m.test.com/#/pages/index/wxlogin' //回调地址
}).then(data => {
location.href=data;
// console.log(data);
})
import { WxCode , WxLogin } from '@/api/user.js'
export default {
data() {
return {
code:''
}
},
methods: {
getUrlParam(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
var r = window.location.search.substr(1).match(reg)
if (r != null) return unescape(r[2])
return null
},
getRequestParams(name) {
let url = location.href;
let requestParams = {};
if (url.indexOf('?') !== -1) {
let str = url.substr(url.indexOf('?') + 1); //截取?后面的内容作为字符串
// console.log(str, '?后面的内容');
let strs = str.split('&'); //将字符串内容以&分隔为一个数组
// console.log(strs, '以&切割的数组');
for (let i = 0; i < strs.length; i++) {
requestParams[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
// 将数组元素中'='左边的内容作为对象的属性名,'='右边的内容作为对象对应属性的属性值
}
}
// console.log(requestParams, '处理后的对象');
if(name){
return requestParams[name];
}else{
return requestParams;
}
},
wxlogin(){
WxLogin({
code: this.code
}).then(data => {
console.log(data);
uni.setStorageSync('token', data.userinfo.token)
uni.redirectTo({
url:'/pages/evaluate/index'
});
})
}
},
created(){
// this.code=this.getUrlParam('cdoe');
// console.log(this.code);
},
onLoad(options) {
this.code=this.getUrlParam('code');
console.log(this.code);
this.wxlogin();
}
}
返回code 链接
getUrlParam:m.test.com/?code=hZVwoVJCy8cIqBFTNEPmUvpG_csDDL7X9gE799M07PY&state=1#/pages/index/wxlogin
getRequestParams:m.test.com/#/?state=1 getRequestParams('state');