Browse Source

微信登录修改

master
邓平艺 5 years ago
parent
commit
52894b711a
  1. 194
      App - 副本.vue
  2. 56
      App.vue
  3. 1
      common/api.js
  4. 22
      common/shared.js
  5. 2
      main.js
  6. 25
      pages/login/index.vue

194
App - 副本.vue

@ -0,0 +1,194 @@
<script>
export default {
globalData: {
wxlogin: () => {}
},
onLaunch: function() {
this.getUserInfo();
},
onShow: function() {
// #ifdef MP-WEIXIN
this.getUpdateWexin(); //
// #endif
},
onHide: function() {
console.log('App Hide')
},
methods: {
wxlogin(){
uni.login({
complete: result => {
if(result.errMsg == 'login:ok'){
let code = result.code;
this.$http(this.API.API_WXLOGIN, { code }).then(res => {
console.log("获得用户信息", res);
uni.setStorageSync('userinfo', res.data);
})
}
}
})
},
//
getUserInfo(){
this.globalData.wxlogin = this.wxlogin;
let userinfo = uni.getStorageSync('userinfo');
if(userinfo && userinfo.id){
return;
}
// console.log(this.globalData)
this.wxlogin();
// uni.login({
// complete: result => {
// if(result.errMsg == 'login:ok'){
// let code = result.code;
// this.userCode(code);
// }
// }
// })
},
// code
userCode(code){
console.log("code", code);
this.$http(this.API.API_WXLOGIN, { code }).then(res => {
console.log("获得用户信息", res);
uni.setStorageSync('userinfo', res.data);
})
},
//
login(){
this.checkSession().then(code => {
this.userCode(code);
}).catch(err => {
console.log("进入。。。。。。")
uni.login({
complete: result => {
console.log(result)
if(result.errMsg == 'login:ok'){
let code = result.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('login_token');
let login_token = undefined;
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>
<style lang="scss">
@import "uview-ui/index.scss";
@import './colorui/main.css';
@import './colorui/icon.css';
@import "@/colorui/animation.css";
@import '@/common/styles/common.css';
/*每个页面公共css */
//
.btn-bottom {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
z-index: 1;
}
// loading
.loading-more {
align-items: center;
justify-content: center;
padding-top: 10px;
padding-bottom: 10px;
text-align: center;
font-size: 28rpx;
color: #999;
}
.loading-more-text::before {
content: '';
width: 20px;
height: 20px;
display: inline-block;
vertical-align: middle;
-webkit-animation: weuiLoading 1s steps(12, end) infinite;
animation: weuiLoading 1s steps(12, end) infinite;
background-repeat: no-repeat;
background-image: url("data:image/svg+xml;charset=utf8, %3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120' viewBox='0 0 100 100'%3E%3Cpath fill='none' d='M0 0h100v100H0z'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23E9E9E9' rx='5' ry='5' transform='translate(0 -30)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23989697' rx='5' ry='5' transform='rotate(30 105.98 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%239B999A' rx='5' ry='5' transform='rotate(60 75.98 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23A3A1A2' rx='5' ry='5' transform='rotate(90 65 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23ABA9AA' rx='5' ry='5' transform='rotate(120 58.66 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23B2B2B2' rx='5' ry='5' transform='rotate(150 54.02 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23BAB8B9' rx='5' ry='5' transform='rotate(180 50 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23C2C0C1' rx='5' ry='5' transform='rotate(-150 45.98 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23CBCBCB' rx='5' ry='5' transform='rotate(-120 41.34 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23D2D2D2' rx='5' ry='5' transform='rotate(-90 35 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23DADADA' rx='5' ry='5' transform='rotate(-60 24.02 65)'/%3E%3Crect width='7' height='20' x='46.5' y='40' fill='%23E2E2E2' rx='5' ry='5' transform='rotate(-30 -5.98 65)'/%3E%3C/svg%3E");
background-size: 100%;
}
@-webkit-keyframes weuiLoading {
0% {
transform: rotate3d(0, 0, 1, 0deg);
}
100% {
transform: rotate3d(0, 0, 1, 360deg);
}
}
@keyframes weuiLoading {
0% {
transform: rotate3d(0, 0, 1, 0deg);
}
100% {
transform: rotate3d(0, 0, 1, 360deg);
}
}
</style>

56
App.vue

@ -1,7 +1,7 @@
<script>
export default {
globalData: {
wxlogin: () => {}
},
onLaunch: function() {
this.getUserInfo();
@ -17,64 +17,26 @@
methods: {
//
getUserInfo(){
this.globalData.wxlogin = this.wxlogin;
let userinfo = uni.getStorageSync('userinfo');
if(userinfo && userinfo.id){
return;
}
this.globalData.wxlogin();
},
wxlogin(){
uni.login({
complete: result => {
if(result.errMsg == 'login:ok'){
let code = result.code;
this.userCode(code);
this.$http(this.API.API_WXLOGIN, { code }).then(res => {
console.log("获得用户信息", res);
uni.setStorageSync('userinfo', res.data);
})
}
}
})
},
// code
userCode(code){
console.log("code", code);
this.$http(this.API.API_WXLOGIN, { code }).then(res => {
console.log("获得用户信息", res);
uni.setStorageSync('userinfo', res.data);
})
},
//
login(){
this.checkSession().then(code => {
this.userCode(code);
}).catch(err => {
console.log("进入。。。。。。")
uni.login({
complete: result => {
console.log(result)
if(result.errMsg == 'login:ok'){
let code = result.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('login_token');
let login_token = undefined;
if(login_token){
resolve(login_token);
}else{
reject(); // token
}
}else{ // session
reject();
}
}
})
});
},
//
getUpdateWexin(){
const updateManager = uni.getUpdateManager(); //

1
common/api.js

@ -26,5 +26,6 @@ export const API_COLLECT_LIST = '/api/collect/list'; // 我的收藏列表
/* 用户相关 */
export const API_WXLOGIN = '/api/wxlogin'; // 用户登录
export const API_WECHAT_SETPHONE = '/api/wechat/setPhone'; // 用户授权手机号提交
export const TEST = '/api/order/deal';

22
common/shared.js

@ -2,20 +2,8 @@
全局共享实用方法 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) {
export function isRight(obj) {
if (isValueType(obj) === 'string') {
obj = obj.trim();
if (obj === 'null' || obj === 'undefined') {
@ -31,13 +19,7 @@ function isRight(obj) {
}
// 判断一个值所属的类型,返回一个字符串
function isValueType(value) {
export function isValueType(value) {
let str = Object.prototype.toString.call(value);
return str.match(/\[object (.*?)\]/)[1].toLowerCase();
}
module.exports = {
setBadge,
isRight,
isValueType
}

2
main.js

@ -2,7 +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';
import * as $shared from '@/common/shared.js';
Vue.config.productionTip = false

25
pages/login/index.vue

@ -2,15 +2,15 @@
<view>
<view class="lf-row-center lf-flex-column box">
<image class="img"></image>
<view class="lf-m-t-10 lf-font-32" v-if="isLogin">游客jdsfbuskdnko</view>
<block v-if="isLogin">
<view class="lf-m-t-10 lf-font-32" v-if="type == 'phone'">游客jdsfbuskdnko</view>
<block v-if="type == 'userinfo'">
<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>
<block v-else-if="type == 'phone'">
<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>
@ -20,7 +20,7 @@
</block>
</view>
<!-- 服务条款 -->
<view class="fixed-bottom" v-if="!isLogin">
<view class="fixed-bottom" v-if="type == 'phone'">
<checkbox-group @change="checkboxChange" style="display: inline-block;">
<checkbox class="lf-text-vertical" :checked="checked"></checkbox>
</checkbox-group>
@ -42,11 +42,12 @@
checked: false, //
isLogin: false, //
userInfo: {},
type: 'phone', // phone | userinfo
type: 'userinfo', // phone | userinfo
}
},
onLoad(){
onLoad(options){
this.type = options.type || this.type;
getApp().globalData.wxlogin();
},
methods: {
//
@ -55,7 +56,6 @@
},
//
checkboxChange(event){
console.log(event)
this.checked = event.detail.value.length > 0;
},
//
@ -64,7 +64,14 @@
if(event.detail.errMsg == 'getPhoneNumber:ok'){
let encryptedData = event.detail.encryptedData;
let iv = event.detail.iv;
this.isLogin = true; // TODO
let userInfo = uni.getStorageSync('userinfo') || {};
this.$http(this.API.API_WECHAT_SETPHONE, {
encryptedData,
iv,
token: userInfo.token
}).then(res => {
console.log("更新手机号", res);
})
}
},
//

Loading…
Cancel
Save