* [xcxLogin.js](https://www.kancloud.cn/wangking/uniapp/1899323#xcxLoginjs_1)
* [main.js 中注册该方法](https://www.kancloud.cn/wangking/uniapp/1899323#mainjs__157)
* [客户端调用](https://www.kancloud.cn/wangking/uniapp/1899323#_177)
## xcxLogin.js
> 存放路径 /common/xcxLogin.js
> 参考 《[一个5年的web前端开源的uni-app快速开发模板](https://ext.dcloud.net.cn/plugin?id=2009)》
~~~
import store from '@/config/store';
import $http from '@/config/requestConfig'
import base from '@/config/baseUrl';
// #ifdef H5
import { h5Login } from '@/config/html5Utils';
// #endif
let code = "";
let loginStart = true;
let userInfo = {
token: ""
};
let lastPageUrl = "";
// 微信小程序登录
function onLogin(type = "judge",callback) {
//判断登录状态
if (loginStart) {
lastPageUrl = "";
loginStart = false;
const _this = this;
let platform;
// #ifdef MP-WEIXIN
platform = 'weixin';
// #endif
// #ifdef MP-ALIPAY
platform = 'alipay';
// #endif
// #ifdef MP-BAIDU
platform = 'baidu';
// #endif
uni.login({
provider: platform,
success: function(loginRes) {
if (loginRes.errMsg == 'login:ok') {
code = loginRes.code;
// 获取用户信息
uni.getUserInfo({
provider: platform,
success: function(infoRes) {
getUserInfo(infoRes, "", callback);
},
fail() {
if(type != "try"){
store.commit('setLoginPopupShow', true);
Object.defineProperty(userInfo, "token", {
get: function(val) {
return {};
},
set: function(newVal) {
callback && callback();
}
});
setTimeout(() => {
loginStart = true;
}, 2000);
}else{
loginStart = true;
}
}
});
}
}
});
}
}
//微信小程序获取用户信息
function getUserInfo(info, type, callback) {
let httpData = {
wxSmallCode: code, //小程序code
iv: info.iv, //小程序加密算法的初始向量
encryptedData: info.encryptedData //包括敏感数据在内的完整用户信息的加密数据
};
// store.state.chatScenesInfo里面是小程序二维码附带的信息
if(store.state.chatScenesInfo.recommendCode){
// 推荐码
httpData.recommendUid = store.state.chatScenesInfo.recommendCode;
}
$http.post('api/open/v1/login', httpData).then(res => {
loginStart = true;
store.commit('setUserInfo', res);
if (type == "authorized") {
userInfo.token = res.token;
store.commit('setLoginPopupShow', false);
}else{
callback && callback();
}
uni.showToast({
title: "登录成功"
});
}, err => {
loginStart = true;
});
}
//判断是否登录(所有端)
function judgeLogin(callback, type = "judge"){
let storeUserInfo = store.state.userInfo;
if(!storeUserInfo.token){ // nvue页面读取不到vuex里面数据,将取缓存
storeUserInfo = uni.getStorageSync("userInfo");
}
if (type != "force" && storeUserInfo.token) {
callback();
} else if (storeUserInfo.token && !storeUserInfo.phone) {
if (type == "force") {
uni.navigateTo({
url: '/pages/user/bindPhone'
});
} else {
uni.showModal({
title: "提示",
content: "您还未绑定手机号,请先绑定~",
confirmText: "去绑定",
cancelText: "再逛会",
success: (res) => {
if (res.confirm) {
uni.navigateTo({
url: '/pages/user/bindPhone'
});
}
}
});
}
} else {
// #ifdef MP
onLogin(type, callback);
// #endif
// #ifdef APP-PLUS
uni.showModal({
title: "登录提示",
content: "此时此刻需要您登录喔~",
confirmText: "去登录",
cancelText: "再逛会",
success: (res) => {
if (res.confirm) {
uni.navigateTo({
url: "/pages/user/login"
});
}
}
});
// #endif
// #ifdef H5
h5Login(type, () => {
callback();
});
// #endif
}
}
export {
onLogin,
getUserInfo,
judgeLogin
}
~~~
## main.js 中注册该方法
> 路径 /main.js
~~~
import Vue from 'vue'
import App from './App'
import xcxLogin from './common/xcxLogin.js'
Vue.config.productionTip = false
Vue.prototype.$xcxLogin = xcxLogin
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
~~~
## 客户端调用
> 参考 《[一个5年的web前端开源的uni-app快速开发模板](https://ext.dcloud.net.cn/plugin?id=2009)》
~~~
// 暂无
~~~
- 基础知识
- UNI核心介绍
- flex布局
- 生命周期
- 全局方法
- 组件定义
- 自定义组件
- 全局组件
- 组件之间的数据传输
- 条件编译
- 自定义头部
- 节点信息 (SelectorQuery)
- vuejs基础语法
- 页面跳转以及参数传递
- 事件的监听注册以及触发
- css3动画
- block的妙用
- mixin (混入)
- uniapp快捷键
- vuex状态管理
- 实用功能
- 获取服务提供商
- 启动页 / 启动界面
- 引导页
- tabbar配置
- 头部导航栏基础设置
- 上拉下拉(刷新/加载)
- 第三方登录
- 第三方分享
- 推送通知 之 unipush
- scroll-view双联动
- 配置iOS通用链接(Universal Links)
- 本地缓存操作
- 升级/更新方案
- 热更新
- 图片上传
- 搜索页实现
- canvas绘图助手
- 地图定位
- 第三方支付————todo
- 分类轮播
- 清除应用缓存
- uniapp与webview的实时通讯
- 视频-----todo
- 聊天----todo
- 长列表swiper左右切换
- 第三方插件
- uview
- mescroll
- uCharts (图表)
- 无名 (更新插件)
- 第三方模版
- 自定义基座
- 打包发行
- 要封装的方法
- 缓存 cache.js
- 请求接口 request.js
- 工具类 util.js
- 小程序登录 xcxLogin.js
- 版本更新 update.js
- 优质插件
- 更新插件----todo
- 语音
- 语音识别 (含上传)
- 百度语音合成播报接口
- 官方常用组建
- input 输入框
- image 图片
- audio 音频
- picker 选择器
- video 视频
- scroll-view 滚动视图
- uni-app 地图全解析+事件监听