微信登录、绑定手机号、游客登录、封装 request、节流函数
This commit is contained in:
parent
ef316cf9a3
commit
3fad6cecf8
24
api/api.js
Normal file
24
api/api.js
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import fetch from './fetch.js';
|
||||||
|
import config from './config.js';
|
||||||
|
|
||||||
|
class Api {
|
||||||
|
// 微信登录
|
||||||
|
static wechatLogin(data) {
|
||||||
|
let url = `${config.host}/worker/worker/login`;
|
||||||
|
return fetch.request('postFrom', url, data, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绑定手机号
|
||||||
|
static bindPhoneNumber(data) {
|
||||||
|
let url = `${config.host}/worker/worker/bindPhoneNumber`;
|
||||||
|
return fetch.request('postFrom', url, data, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 游客登录
|
||||||
|
static guestLogin(data) {
|
||||||
|
let url = `${config.host}/worker/worker/guestLogin`;
|
||||||
|
return fetch.request('postFrom', url, data, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Api
|
||||||
11
api/config.js
Normal file
11
api/config.js
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
const accountInfo = wx.getAccountInfoSync();
|
||||||
|
const hosts = {
|
||||||
|
"develop": 'https://develop',
|
||||||
|
"trial": 'https://trial',
|
||||||
|
"release": 'https://release',
|
||||||
|
}
|
||||||
|
|
||||||
|
// const host = hosts[accountInfo.miniProgram.envVersion]
|
||||||
|
const host = 'http://wanyu.test';
|
||||||
|
|
||||||
|
export default {host};
|
||||||
38
api/fetch.js
Normal file
38
api/fetch.js
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import helpers from "../utils/helpers";
|
||||||
|
|
||||||
|
class Fetch {
|
||||||
|
static request(methods, url, data, needToken = true, showErrorMsg = true) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (needToken && (data.token === undefined || !data.token)) {
|
||||||
|
data.token = uni.getStorageSync('token');
|
||||||
|
}
|
||||||
|
|
||||||
|
uni.request({
|
||||||
|
url: url,
|
||||||
|
method: methods === 'postFrom' ? 'POST' : methods,
|
||||||
|
data: data,
|
||||||
|
header: {
|
||||||
|
'content-type': methods === 'postFrom' ? 'application/x-www-form-urlencoded' :
|
||||||
|
'application/json'
|
||||||
|
},
|
||||||
|
success(res) {
|
||||||
|
if (res.data.code === 1) {
|
||||||
|
resolve(res.data.data) // 成功,返回数据
|
||||||
|
} else {
|
||||||
|
if (res.data.code === 0 && showErrorMsg) {
|
||||||
|
helpers.showToast(res.data.msg) // 失败且需显示错误提示
|
||||||
|
} else if (res.data.code === 401) {
|
||||||
|
helpers.jumpToPage('login')
|
||||||
|
}
|
||||||
|
reject(res.data) // 统一 reject 错误数据
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail(err) {
|
||||||
|
helpers.showToast(err.errMsg)
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export default Fetch
|
||||||
|
|
@ -39,9 +39,17 @@ import MeIcon from "../../components/me-icon/me-icon.vue";
|
||||||
import MeButton from "../../components/me-button/me-button.vue";
|
import MeButton from "../../components/me-button/me-button.vue";
|
||||||
import { ref, reactive, computed, watch } from 'vue'
|
import { ref, reactive, computed, watch } from 'vue'
|
||||||
import helpers from "../../utils/helpers";
|
import helpers from "../../utils/helpers";
|
||||||
|
import api from "../../api/api";
|
||||||
|
import {throttle} from "../../utils/throttle";
|
||||||
|
|
||||||
const visitor = () => {
|
const visitor = () => {
|
||||||
helpers.jumpToPage('index', '', 'switchTab')
|
api.guestLogin({}).then(res => {
|
||||||
|
storageUserInfo(res)
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
helpers.jumpToPage('index','', 'switchTab')
|
||||||
|
}, 1000)
|
||||||
|
}).catch(() => {})
|
||||||
}
|
}
|
||||||
|
|
||||||
//是否勾选协议
|
//是否勾选协议
|
||||||
|
|
@ -53,17 +61,32 @@ const isShake = ref(false)
|
||||||
//是否已授权登录
|
//是否已授权登录
|
||||||
const isLogin = ref(false)
|
const isLogin = ref(false)
|
||||||
|
|
||||||
|
//绑定手机号的token
|
||||||
|
const vendorToken = ref('')
|
||||||
|
|
||||||
//绑定手机号
|
//绑定手机号
|
||||||
const bindPhoneNumber = (e) => {
|
const bindPhoneNumber = throttle((e) => {
|
||||||
if (e.detail.code === undefined) {
|
if (e.detail.code === undefined) {
|
||||||
return helpers.showToast("请授权手机号")
|
return helpers.showToast("请授权手机号")
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(e.detail.code)
|
uni.showLoading({
|
||||||
}
|
title: '绑定中…'
|
||||||
|
});
|
||||||
|
api.bindPhoneNumber({code: e.detail.code, vendor_token: vendorToken.value}).then(res => {
|
||||||
|
helpers.showToast('绑定成功')
|
||||||
|
storageUserInfo(res)
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
helpers.jumpToPage('index','', 'switchTab')
|
||||||
|
}, 1000)
|
||||||
|
}).catch(() => {}).finally(() => {
|
||||||
|
uni.hideLoading();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
//微信一键登录
|
//微信一键登录
|
||||||
const login = () => {
|
const login = throttle(() => {
|
||||||
if (checked.value === false) {
|
if (checked.value === false) {
|
||||||
helpers.showToast("请阅读并同意用户协议")
|
helpers.showToast("请阅读并同意用户协议")
|
||||||
isShake.value = true
|
isShake.value = true
|
||||||
|
|
@ -77,19 +100,42 @@ const login = () => {
|
||||||
uni.login({
|
uni.login({
|
||||||
provider: 'weixin',
|
provider: 'weixin',
|
||||||
success: function (res) {
|
success: function (res) {
|
||||||
|
uni.showLoading({
|
||||||
|
title: '登录中…'
|
||||||
|
});
|
||||||
|
|
||||||
if (!res.code) {
|
if (!res.code) {
|
||||||
|
uni.hideLoading();
|
||||||
return helpers.showToast('登录失败:' + res.errMsg)
|
return helpers.showToast('登录失败:' + res.errMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(res.code);
|
api.wechatLogin({code: res.code}).then(res => {
|
||||||
|
helpers.showToast('登录成功')
|
||||||
|
storageUserInfo(res)
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
helpers.jumpToPage('index','', 'switchTab')
|
||||||
|
}, 1000)
|
||||||
|
}).catch(err => {
|
||||||
|
vendorToken.value = err.data.vendor_token
|
||||||
|
isLogin.value = true
|
||||||
|
}).finally(() => {
|
||||||
|
uni.hideLoading();
|
||||||
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}, 500)
|
||||||
|
|
||||||
//勾选协议
|
//勾选协议
|
||||||
const select = () => {
|
const select = () => {
|
||||||
checked.value = !checked.value
|
checked.value = !checked.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//存储用户信息
|
||||||
|
const storageUserInfo = (userInfo) => {
|
||||||
|
uni.setStorageSync('token', userInfo.token)
|
||||||
|
uni.setStorageSync('user_info', userInfo)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
<style>
|
<style>
|
||||||
page {
|
page {
|
||||||
|
|
|
||||||
16
utils/throttle.js
Normal file
16
utils/throttle.js
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
//节流函数
|
||||||
|
export function throttle(fn, gapTime = 1500) {
|
||||||
|
let lastTime = 0;
|
||||||
|
|
||||||
|
return function () {
|
||||||
|
const nowTime = Date.now();
|
||||||
|
if (nowTime - lastTime >= gapTime) {
|
||||||
|
lastTime = nowTime;
|
||||||
|
fn.apply(this, arguments);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
throttle
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user