Enzo 5 years ago
parent
commit
829f450309
  1. 90
      App.vue
  2. 8
      common/http.js
  3. 23
      common/mixin.js
  4. 43
      common/shared.js
  5. 3
      main.js
  6. 6
      pages.json
  7. 3
      pages/center/index.vue
  8. 165
      pages/login/accountLogin.vue
  9. 78
      pages/login/index.vue

90
App.vue

@ -1,13 +1,99 @@
<script>
export default {
globalData: {
},
onLaunch: function() {
console.log('App Launch')
// this.login(); // TODO
},
onShow: function() {
console.log('App Show')
// #ifdef MP-WEIXIN
this.getUpdateWexin(); //
// #endif
},
onHide: function() {
console.log('App Hide')
},
methods: {
// code
userCode(code){
this.$http('', { code }).then(res => {
console.log("获得用户信息", res);
})
},
//
login(){
this.checkSession().then(code => {
this.userCode(code);
}).catch(err => {
uni.login({
complete: result => {
if(result.errMsg == 'login:ok'){
let code = result.code;
uni.setStorageSync('login_token', code);
this.userCode(code);
}
}
})
});
},
// token
checkSession(){
return new Promise((resolve, reject) => {
uni.checkSession({
complete: result => {
if(result.errMsg == 'checkSession:ok'){ // session
let login_token = uni.getStorageSync();
if(login_token){
resolve(login_token);
}else{
reject(); // token
}
}else{ // session
reject();
}
}
})
});
},
//
getUpdateWexin(){
const updateManager = uni.getUpdateManager(); //
updateManager.onCheckForUpdate(function(res) {
//
if (res.hasUpdate) {
updateManager.onUpdateReady(function(res2) {
uni.showModal({
title: '更新提示',
content: '发现新版本,是否重启应用?',
confirmColor: '#FE9903',
showCancel: false,
success(res2) {
if (res2.confirm) {
// applyUpdate
updateManager.applyUpdate();
}
}
});
});
}
});
updateManager.onUpdateFailed(function(res) {
//
uni.showModal({
title: '提示',
content: '检查到有新版本,但下载失败,请检查网络设置',
confirmColor: '#FE9903',
success(res) {
if (res.confirm) {
// applyUpdate
updateManager.applyUpdate();
}
}
});
});
}
}
}
</script>

8
common/http.js

@ -24,7 +24,7 @@ function getsign(params) {
return params;
}
function $http(url, data = {}){
function $http(url, data = {}, options = {}){
return new Promise((resolve, reject) => {
// 绑定this
let that = this;
@ -74,6 +74,12 @@ function $http(url, data = {}){
// 生成sign
getsign(data);
console.log(url, data);
// 动态赋值是否显示loading加载框 TODO验证一下是否有问题
if(that.$shared.isValueType(options.showLoading) != 'undefined'){
that.$u.http.setConfig({showLoading: options.showLoading});
}
// 发起请求
that.$u.post(url, data).then(res => {
resolve(res);

23
common/mixin.js

@ -70,18 +70,19 @@ export default{
});
},
$url(url, options = {}){
// TODO 判断登录逻辑;防抖
if(options.type && options.type !== ''){
if(options.type === 'redirect'){ // 关闭当前,跳转
uni.redirectTo({ url })
}else if(options.type === 'switch'){ // 跳转
uni.switchTab({ url })
}else if(options.type === 'launch'){ // 关闭所有,跳转
uni.reLaunch({ url })
this.$u.throttle(() => {
if(options.type && options.type !== ''){
if(options.type === 'redirect'){ // 关闭当前,跳转
uni.redirectTo({ url })
}else if(options.type === 'switch'){ // 跳转
uni.switchTab({ url })
}else if(options.type === 'launch'){ // 关闭所有,跳转
uni.reLaunch({ url })
}
}else{
uni.navigateTo({ url }) // 跳转
}
}else{
uni.navigateTo({ url }) // 跳转
}
}, 100);
},
$toBack(){
let pages = getCurrentPages(); // 当前页

43
common/shared.js

@ -0,0 +1,43 @@
/*
全局共享实用方法 shared.js
*/
// 设置角标, 必须传入下标值,设置值可传可不传,传参时显示,不传时移除角标
function setBadge(index = 0, value){
if(isRight(value)){
uni.setTabBarBadge({
index: Number(index),
text: value > 99 ? '99+' : String(value)
})
}else{
uni.removeTabBarBadge({index: Number(index)});
}
}
// 判断对错/是否显示,万能校验
function isRight(obj) {
if (isValueType(obj) === 'string') {
obj = obj.trim();
if (obj === 'null' || obj === 'undefined') {
return false;
}
} else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) {
return true;
}
for (var key in obj) {
return true;
}
return false;
}
// 判断一个值所属的类型,返回一个字符串
function isValueType(value) {
let str = Object.prototype.toString.call(value);
return str.match(/\[object (.*?)\]/)[1].toLowerCase();
}
module.exports = {
setBadge,
isRight,
isValueType
}

3
main.js

@ -2,6 +2,7 @@ import Vue from 'vue'
import App from './App'
import mixin from '@/common/mixin.js';
import * as API from '@/common/api.js';
import $shared from '@/common/shared.js';
Vue.config.productionTip = false
@ -10,6 +11,8 @@ Vue.mixin(mixin);
// 将API注入全局
Vue.prototype.API = API;
// 全局共享方法
Vue.prototype.$shared = $shared;
// 引入全局uView
import uView from 'uview-ui'

6
pages.json

@ -76,6 +76,12 @@
"style": {
"navigationBarTitleText": "登录"
}
},
{
"path": "pages/login/accountLogin",
"style": {
"navigationBarTitleText": "绑定"
}
}
],
"globalStyle": {

3
pages/center/index.vue

@ -149,7 +149,8 @@
position: absolute;
width: 100%;
height: 100%;
z-index: -1;
z-index: 1;
background: transparent;
}
&:last-child{
border-bottom: none;

165
pages/login/accountLogin.vue

@ -0,0 +1,165 @@
<template>
<view>
<view class="lf-flex-column lf-row-center logo">
<image ></image>
</view>
<view class="lf-row-between inpu-box">
<u-icon name="phone" size="50"></u-icon>
<input maxlength="11" v-model="phoneNum" placeholder="请输入手机号" />
</view>
<view class="lf-row-between verif-code" v-if="isCodeLogin">
<view class="lf-row-between code-input">
<u-icon name="lock" size="50" color="#999999"></u-icon>
<input maxlength="5" v-model="codeNum" placeholder="验证码" />
</view>
<button class="code-btn" :style="{'background-color': isGetCode ? '#FE9903' : '#999999'}" @click="getCode">
{{ isGetCode ? '获取验证码' : codeTimeNum +'s后重新获取' }}
</button>
</view>
<view class="lf-row-between inpu-box lf-m-t-30" v-else>
<u-icon name="lock" size="50"></u-icon>
<input maxlength="11" v-model="password" :password="true" placeholder="请输入密码" />
</view>
<view @click="switchLoginType" class="login-type">{{ isCodeLogin ? '使用密码登录' : '使用验证码登录' }}</view>
<button class="login-btn" @click="login">登录</button>
</view>
</template>
<script>
export default {
data(){
return {
phoneNum: '',
codeNum: '',
password: '',
isGetCode: true,
isCodeLogin: true,
codeTimeNum: 10,
codeTime: null
}
},
onLoad(){
},
onUnload(){
if(this.codeTime){
clearInterval(this.codeTime);
this.codeTime = null;
}
},
methods: {
//
switchLoginType(){
this.codeNum = '';
this.password = '';
this.isCodeLogin = !this.isCodeLogin;
},
//
getCode(){
if(this.isGetCode){
console.log("此处请求")
}
if(this.codeTimeNum == 10){
this.isGetCode = false;
if(this.codeTime){
clearInterval(this.codeTime);
this.codeTime = null;
this.codeTimeNum = 10;
}
this.codeTime = setInterval(() => {
this.codeTimeNum = this.codeTimeNum - 1;
if(this.codeTimeNum < 0){
clearInterval(this.codeTime);
this.codeTime = null;
this.codeTimeNum = 10;
this.isGetCode = true;
}
}, 1000);
}
},
//
login(){
let pages = getCurrentPages();
let prevPage = pages[pages.length - 2];
// prevPage.$vm.isLogin = true; //
// this.$toBack(); //
}
}
}
</script>
<style lang="scss" scoped="scoped">
.logo{
width: 100%;
height: 220rpx;
image{
width: 160rpx;
height: 160rpx;
border-radius: 50%;
background-color: #EEEEEE;
}
}
.inpu-box{
width: 686rpx;
height: 88rpx;
border: 2rpx solid #FE9903;
border-radius: 10rpx;
color: #FE9903;
box-sizing: border-box;
padding: 0 20rpx;
margin: 0 auto;
margin-top: 50rpx;
input{
width: 570rpx;
height: 66rpx;
font-size: 32rpx;
display: flex;
align-items: center;
padding: 0 20rpx;
}
}
.verif-code{
width: 686rpx;
height: 88rpx;
margin: 0 auto;
margin-top: 30rpx;
.code-input{
width: 438rpx;
height: 88rpx;
border: 2rpx solid #999999;
border-radius: 10rpx;
box-sizing: border-box;
padding: 0 20rpx;
input{
width: 344rpx;
height: 66rpx;
padding: 0 20rpx;
font-size: 32rpx;
}
}
.code-btn{
margin: 0;
width: 228rpx;
height: 88rpx;
border-radius: 10rpx;
color: #FFFFFF;
font-size: 28rpx;
line-height: 88rpx;
}
}
.login-type{
color: #FE9903;
text-align: center;
margin-top: 40rpx;
}
.login-btn{
width: 686rpx;
height: 88rpx;
background-color: #FE9903;
color: #FFFFFF;
line-height: 88rpx;
border-radius: 42rpx;
margin-top: 100rpx;
}
</style>

78
pages/login/index.vue

@ -2,16 +2,28 @@
<view>
<view class="lf-row-center lf-flex-column box">
<image class="img"></image>
<view class="lf-m-t-10 lf-font-32">游客jdsfbuskdnko</view>
<button class="btn">
<u-icon name="weixin-fill" color="#ffffff" size="60" class="lf-text-vertical"></u-icon>
<text class="lf-m-l-20">绑定微信</text>
</button>
<view class="lf-m-t-40 lf-font-28">暂不绑定继续操作</view>
<view class="lf-m-t-10 lf-font-32" v-if="isLogin">游客jdsfbuskdnko</view>
<block v-if="isLogin">
<button class="btn" @click="getUserProfile">
<u-icon name="weixin-fill" color="#ffffff" size="60" class="lf-text-vertical"></u-icon>
<text class="lf-m-l-20">绑定微信</text>
</button>
<view class="lf-m-t-40 lf-font-28" @click="$toBack()">暂不绑定继续操作</view>
</block>
<block v-else>
<button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
<u-icon name="weixin-fill" color="#ffffff" size="60" class="lf-text-vertical"></u-icon>
<text class="lf-m-l-20">微信快捷登录</text>
</button>
<view class="lf-m-t-40 lf-font-28" @click="$url('/pages/login/accountLogin')">使用手机号登录</view>
<view class="mask" v-if="!checked" @click="$msg('您未同意授权')"></view>
</block>
</view>
<!-- 服务条款 -->
<view class="fixed-bottom">
<checkbox class="lf-text-vertical" :checked="checked"></checkbox>
<view class="fixed-bottom" v-if="!isLogin">
<checkbox-group @change="checkboxChange" style="display: inline-block;">
<checkbox class="lf-text-vertical" :checked="checked"></checkbox>
</checkbox-group>
<text class="lf-m-l-10 lf-font-24 lf-color-gray">
<text>请认真阅读并同意</text>
<text class="highlight" @click="enterAgree">时空网协议</text>
@ -27,14 +39,51 @@
export default {
data(){
return {
checked: true
checked: false, //
isLogin: false, //
userInfo: {}
}
},
onLoad(){
},
methods: {
//
enterAgree(){
console.log("查看协议");
},
//
checkboxChange(event){
console.log(event)
this.checked = event.detail.value.length > 0;
},
//
getPhoneNumber(event){
console.log(event);
if(event.detail.errMsg == 'getPhoneNumber:ok'){
let encryptedData = event.detail.encryptedData;
let iv = event.detail.iv;
this.isLogin = true; // TODO
}
},
//
getUserProfile(){
uni.getUserProfile({
desc: '您的信息将用于时空网显示',
lang: 'zh_CN',
complete: result => {
console.log(result)
if(result.errMsg == 'getUserProfile:ok'){
let encryptedData = result.encryptedData;
let iv = result.iv;
let signature = result.signature;
let userInfo = result.userInfo;
//
this.$toBack();
}
}
});
}
}
}
</script>
@ -50,6 +99,7 @@
width: 750rpx;
height: auto;
box-sizing: border-box;
position: relative;
.img{
width: 180rpx;
height: 180rpx;
@ -76,4 +126,12 @@
color: #1e90ff;
}
}
.mask{
position: absolute;
bottom: 46rpx;
left: 0;
width: 100%;
height: 190rpx;
}
</style>
Loading…
Cancel
Save