| | |
| | | import request from "request"; |
| | | |
| | | export function login(account,passward) { |
| | | import request from '@/utils/request.js' |
| | | export function login(account, password) { |
| | | return request({ |
| | | 'url': '/login', |
| | | 'method' : 'post' |
| | | 'method': 'post', |
| | | 'data': { |
| | | 'username': account, |
| | | 'password': password |
| | | }, |
| | | 'header': { |
| | | 'Content-Type': 'application/json', |
| | | 'dataType': 'json', |
| | | 'Accept': 'application/json' |
| | | } |
| | | }) |
| | | } |
| | |
| | | <u-form-item label="ç¨æ·å" labelWidth=65 prop="userInfo.account" borderBottom> |
| | | <u--input v-model="userInfo.account" border="none" placeholder="请è¾å
¥ç¨æ·å"></u--input> |
| | | </u-form-item> |
| | | <u-form-item label="å¯ç " labelWidth=65 prop="userInfo.passward"> |
| | | <u--input v-model="userInfo.passward" placeholder="请è¾å
¥ç¨æ·å¯ç " border="none" :password="showPassward" |
| | | <u-form-item label="å¯ç " labelWidth=65 prop="userInfo.password"> |
| | | <u--input v-model="userInfo.password" placeholder="请è¾å
¥ç¨æ·å¯ç " border="none" :password="showPassward" |
| | | @click="this.showPassward = !this.showPassward " suffixIcon="eye-fill" |
| | | suffixIconStyle="color: #909399"></u--input> |
| | | </u-form-item> |
| | |
| | | </template> |
| | | |
| | | <script> |
| | | import { |
| | | login |
| | | } from '../../api/login' |
| | | export default { |
| | | data() { |
| | | return { |
| | | showPassward: true, |
| | | userInfo: { |
| | | account: '', |
| | | passward: '' |
| | | password: '' |
| | | } |
| | | } |
| | | }, |
| | |
| | | }) |
| | | return |
| | | } |
| | | |
| | | if (this.userInfo.passward == null || this.userInfo.passward == '') { |
| | | |
| | | if (this.userInfo.password == null || this.userInfo.password == '') { |
| | | this.$refs.uToast.show({ |
| | | message: 'å¯ç 为空', |
| | | type: 'error' |
| | | }) |
| | | return |
| | | } |
| | | |
| | | |
| | | |
| | | console.log(this.userInfo) |
| | | |
| | | uni.navigateTo({ |
| | | url:'/pages/index/index' |
| | | login(this.userInfo.account, this.userInfo.password).then(response => { |
| | | console.log(response) |
| | | if (!response.result) { |
| | | this.$refs.uToast.show({ |
| | | message: response.msg, |
| | | type: 'error' |
| | | }) |
| | | return |
| | | } |
| | | uni.navigateTo({ |
| | | url:'/pages/index/index' |
| | | }) |
| | | }) |
| | | |
| | | |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | const TokenKey = 'App-Token' |
| | | |
| | | export function getToken() { |
| | | return uni.getStorageSync(TokenKey) |
| | | } |
| | | |
| | | export function setToken(token) { |
| | | return uni.setStorageSync(TokenKey, token) |
| | | } |
| | | |
| | | export function removeToken() { |
| | | return uni.removeStorageSync(TokenKey) |
| | | } |
| | |
| | | /** |
| | | * æ¾ç¤ºæ¶æ¯æç¤ºæ¡ |
| | | * @param content æç¤ºçæ é¢ |
| | | */ |
| | | export function toast(content) { |
| | | uni.showToast({ |
| | | icon:'none', |
| | | title:content |
| | | }) |
| | | uni.showToast({ |
| | | icon: 'none', |
| | | title: content |
| | | }) |
| | | } |
| | | |
| | | /** |
| | | * æ¾ç¤ºæ¨¡æå¼¹çª |
| | | * @param content æç¤ºçæ é¢ |
| | | */ |
| | | export function showConfirm(content) { |
| | | return new Promise((resolve, reject) => { |
| | | uni.showModal({ |
| | | title: 'æç¤º', |
| | | content: content, |
| | | cancelText: 'åæ¶', |
| | | confirmText: 'ç¡®å®', |
| | | success: function(res) { |
| | | resolve(res) |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | /** |
| | | * åæ°å¤ç |
| | | * @param params åæ° |
| | | */ |
| | | export function tansParams(params) { |
| | | let result = '' |
| | | for (const propName of Object.keys(params)) { |
| | | const value = params[propName] |
| | | var part = encodeURIComponent(propName) + "=" |
| | | if (value !== null && value !== "" && typeof (value) !== "undefined") { |
| | | if (typeof value === 'object') { |
| | | for (const key of Object.keys(value)) { |
| | | if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') { |
| | | let params = propName + '[' + key + ']' |
| | | var subPart = encodeURIComponent(params) + "=" |
| | | result += subPart + encodeURIComponent(value[key]) + "&" |
| | | } |
| | | } |
| | | } else { |
| | | result += part + encodeURIComponent(value) + "&" |
| | | } |
| | | } |
| | | } |
| | | return result |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | const constant = { |
| | | avatar: 'vuex_avatar', |
| | | name: 'vuex_name', |
| | | roles: 'vuex_roles', |
| | | permissions: 'vuex_permissions' |
| | | } |
| | | |
| | | export default constant |
¶Ô±ÈÐÂÎļþ |
| | |
| | | export default { |
| | | '401': '认è¯å¤±è´¥ï¼æ æ³è®¿é®ç³»ç»èµæº', |
| | | '403': 'å½åæä½æ²¡ææé', |
| | | '404': '访é®èµæºä¸åå¨', |
| | | 'default': 'ç³»ç»æªç¥é误ï¼è¯·åé¦ç»ç®¡çå' |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | import store from '@/store' |
| | | |
| | | /** |
| | | * å符æéæ ¡éª |
| | | * @param {Array} value æ ¡éªå¼ |
| | | * @returns {Boolean} |
| | | */ |
| | | export function checkPermi(value) { |
| | | if (value && value instanceof Array && value.length > 0) { |
| | | const permissions = store.getters && store.getters.permissions |
| | | const permissionDatas = value |
| | | const all_permission = "*:*:*" |
| | | |
| | | const hasPermission = permissions.some(permission => { |
| | | return all_permission === permission || permissionDatas.includes(permission) |
| | | }) |
| | | |
| | | if (!hasPermission) { |
| | | return false |
| | | } |
| | | return true |
| | | } else { |
| | | console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`) |
| | | return false |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è§è²æéæ ¡éª |
| | | * @param {Array} value æ ¡éªå¼ |
| | | * @returns {Boolean} |
| | | */ |
| | | export function checkRole(value) { |
| | | if (value && value instanceof Array && value.length > 0) { |
| | | const roles = store.getters && store.getters.roles |
| | | const permissionRoles = value |
| | | const super_admin = "admin" |
| | | |
| | | const hasRole = roles.some(role => { |
| | | return super_admin === role || permissionRoles.includes(role) |
| | | }) |
| | | |
| | | if (!hasRole) { |
| | | return false |
| | | } |
| | | return true |
| | | } else { |
| | | console.error(`need roles! Like checkRole="['admin','editor']"`) |
| | | return false |
| | | } |
| | | } |
| | |
| | | const { error } = require("jquery") |
| | | const { toast } = require("./common") |
| | | import config from '@/config' |
| | | import { |
| | | getToken |
| | | } from '@/utils/auth' |
| | | import errorCode from '@/utils/errorCode' |
| | | import { |
| | | toast, |
| | | showConfirm, |
| | | tansParams |
| | | } from '@/utils/common' |
| | | |
| | | let timeout = 10000 |
| | | const baseUrl = config.baseUrl |
| | | |
| | | const request = config => { |
| | | return new Promise((resolve,reject) => { |
| | | // æ¯å¦éè¦è®¾ç½® token |
| | | // const isToken = (config.headers || {}).isToken === false |
| | | // config.header = config.header || {} |
| | | // if (getToken() && !isToken) { |
| | | // config.header['Authorization'] = 'Bearer ' + getToken() |
| | | // } |
| | | // getè¯·æ±æ å°paramsåæ° |
| | | if (config.params) { |
| | | let url = config.url + '?' + tansParams(config.params) |
| | | url = url.slice(0, -1) |
| | | config.url = url |
| | | } |
| | | return new Promise((resolve, reject) => { |
| | | uni.request({ |
| | | method:config.method || 'GET', |
| | | timeout:config.timeoue || timeout, |
| | | url:config.baseUrl || baseUrl + config.url, |
| | | data:config.data, |
| | | header: config.header || {'Content-Type': 'application/json'}, |
| | | method: config.method || 'get', |
| | | timeout: config.timeout || timeout, |
| | | url: config.baseUrl || baseUrl + config.url, |
| | | data: config.data, |
| | | header: config.header, |
| | | dataType: 'json' |
| | | }).then(response => { |
| | | let [error,res] = response |
| | | if(error) { |
| | | toast('ç½ç»å¼å¸¸,请ç¨åéè¯') |
| | | return |
| | | if (response.statusCode === 200) { |
| | | resolve(response.data) |
| | | } |
| | | const result = res.data.result || true |
| | | const msg = errorCode[code] || res.data.msg || ErrorCode['defalut'] |
| | | |
| | | if(!res) { |
| | | toast(msg) |
| | | return |
| | | } |
| | | resolve(res.data) |
| | | }).catch(error => { |
| | | let {message} = error |
| | | if(message === 'Network Error') { |
| | | let { |
| | | message |
| | | } = error |
| | | if (message === 'Network Error') { |
| | | message = 'å端æ¥å£è¿æ¥å¼å¸¸' |
| | | } else if(message.includes('timeout')) { |
| | | } else if (message.includes('timeout')) { |
| | | message = 'ç³»ç»æ¥å£è¯·æ±è¶
æ¶' |
| | | } else if (message.includes('Request failed with status code')) { |
| | | message = 'ç³»ç»æ¥å£' + message.substr(message.length - 3) + 'å¼å¸¸' |
| | | } |
| | | toast(message) |
| | | reject(error) |
| | | }) |
| | | }) |
| | | } |
| | | } |
| | | |
| | | export default request |
¶Ô±ÈÐÂÎļþ |
| | |
| | | import constant from './constant' |
| | | |
| | | // åå¨åéå |
| | | let storageKey = 'storage_data' |
| | | |
| | | // åå¨èç¹åéå |
| | | let storageNodeKeys = [constant.avatar, constant.name, constant.roles, constant.permissions] |
| | | |
| | | const storage = { |
| | | set: function(key, value) { |
| | | if (storageNodeKeys.indexOf(key) != -1) { |
| | | let tmp = uni.getStorageSync(storageKey) |
| | | tmp = tmp ? tmp : {} |
| | | tmp[key] = value |
| | | uni.setStorageSync(storageKey, tmp) |
| | | } |
| | | }, |
| | | get: function(key) { |
| | | let storageData = uni.getStorageSync(storageKey) || {} |
| | | return storageData[key] || "" |
| | | }, |
| | | remove: function(key) { |
| | | let storageData = uni.getStorageSync(storageKey) || {} |
| | | delete storageData[key] |
| | | uni.setStorageSync(storageKey, storageData) |
| | | }, |
| | | clean: function() { |
| | | uni.removeStorageSync(storageKey) |
| | | } |
| | | } |
| | | |
| | | export default storage |
¶Ô±ÈÐÂÎļþ |
| | |
| | | import store from '@/store' |
| | | import config from '@/config' |
| | | import { getToken } from '@/utils/auth' |
| | | import errorCode from '@/utils/errorCode' |
| | | import { toast, showConfirm, tansParams } from '@/utils/common' |
| | | |
| | | let timeout = 10000 |
| | | const baseUrl = config.baseUrl |
| | | |
| | | const upload = config => { |
| | | // æ¯å¦éè¦è®¾ç½® token |
| | | const isToken = (config.headers || {}).isToken === false |
| | | config.header = config.header || {} |
| | | if (getToken() && !isToken) { |
| | | config.header['Authorization'] = 'Bearer ' + getToken() |
| | | } |
| | | // getè¯·æ±æ å°paramsåæ° |
| | | if (config.params) { |
| | | let url = config.url + '?' + tansParams(config.params) |
| | | url = url.slice(0, -1) |
| | | config.url = url |
| | | } |
| | | return new Promise((resolve, reject) => { |
| | | uni.uploadFile({ |
| | | timeout: config.timeout || timeout, |
| | | url: baseUrl + config.url, |
| | | filePath: config.filePath, |
| | | name: config.name || 'file', |
| | | header: config.header, |
| | | formData: config.formData, |
| | | success: (res) => { |
| | | let result = JSON.parse(res.data) |
| | | const code = result.code || 200 |
| | | const msg = errorCode[code] || result.msg || errorCode['default'] |
| | | if (code === 200) { |
| | | resolve(result) |
| | | } else if (code == 401) { |
| | | showConfirm("ç»å½ç¶æå·²è¿æï¼æ¨å¯ä»¥ç»§ç»çå¨è¯¥é¡µé¢ï¼æè
éæ°ç»å½?").then(res => { |
| | | if (res.confirm) { |
| | | store.dispatch('LogOut').then(res => { |
| | | uni.reLaunch({ url: '/pages/login/login' }) |
| | | }) |
| | | } |
| | | }) |
| | | reject('æ æçä¼è¯ï¼æè
ä¼è¯å·²è¿æï¼è¯·éæ°ç»å½ã') |
| | | } else if (code === 500) { |
| | | toast(msg) |
| | | reject('500') |
| | | } else if (code !== 200) { |
| | | toast(msg) |
| | | reject(code) |
| | | } |
| | | }, |
| | | fail: (error) => { |
| | | let { message } = error |
| | | if (message == 'Network Error') { |
| | | message = 'å端æ¥å£è¿æ¥å¼å¸¸' |
| | | } else if (message.includes('timeout')) { |
| | | message = 'ç³»ç»æ¥å£è¯·æ±è¶
æ¶' |
| | | } else if (message.includes('Request failed with status code')) { |
| | | message = 'ç³»ç»æ¥å£' + message.substr(message.length - 3) + 'å¼å¸¸' |
| | | } |
| | | toast(message) |
| | | reject(error) |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | export default upload |