Browse Source

调用接口

master
unknown 1 year ago
parent
commit
2dac4e3e62
  1. 13
      config/api.js
  2. 20
      config/request.js
  3. 8
      config/site.config.js
  4. 2
      manifest.json
  5. 19
      pages/tabbar/index/index.vue
  6. 2
      pages/userCenter/login/login.vue
  7. 19
      pages/userCenter/login/loginByPhone.vue
  8. 9
      site.config.js
  9. 22
      store/modules/user.js

13
config/api.js

@ -1,10 +1,17 @@
const http = uni.$u.http
var http = uni.$u.http
// 隐私政策
export const getAgreement = (params, config = {}) => http.post('/util/manage/getAgreement.do', params, config)
// 验证码登录
export const loginSMS = (data) => http.post('/account/manage/login.do', data)
export const loginSMS = (data) => http.post('member/auth/sms-login', data)
// 登录发验证码
export const getLoginCode = (data) => http.post('/util/manage/getLoginRegistCode.do', data)
export const getLoginCode = (data) => http.post('member/auth/send-sms-code', data)
// 获取个人信息
export const getUserInfo = (data) => http.post('member/user/get', {header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}})
// 刷新token
export const refreshToken = (data) => http.post('member/auth/refresh-token', data)

20
config/request.js

@ -1,11 +1,17 @@
import { H5_API, WX_API } from './site.config.js'
// 此vm参数为页面的实例,可以通过它引用vuex中的变量
module.exports = (vm) => {
// 初始化请求配置
uni.$u.http.setConfig((config) => {
let prefix = config.prefix?config.prefix:'app-api/'
/* config 为默认全局配置*/
config.baseURL = 'http://121.41.97.244:8090'; /* 根域名 */
config.header['content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
config.baseURL = H5_API+ WX_API + prefix; /* 根域名 */
console.log(config.baseURL)
// config.header['content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
config.header['tenant-id'] = '1'
return config
})
@ -14,10 +20,8 @@ module.exports = (vm) => {
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
config.data = config.data || {}
// 根据custom参数中配置的是否需要token,添加对应的请求头
if(config?.custom?.auth) {
// 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
config.header.token = vm.$store.state.userInfo.token
}
// config.header.Authorization = 'Bearer ' + vm.$store.state.user.vuex_loginInfo.accessToken
return config
}, config => { // 可使用async await 做异步操作
return Promise.reject(config)
@ -26,14 +30,14 @@ module.exports = (vm) => {
// 响应拦截
uni.$u.http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
const data = response.data
console.log('response')
console.log('请求结果')
console.log(data)
// 自定义参数
const custom = response.config?.custom
if (data.code !== 0) {
// 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
if (custom.toast !== false) {
uni.$u.toast(data.message)
uni.$u.toast(data.msg)
}
// 如果需要catch返回,则进行reject

8
config/site.config.js

@ -0,0 +1,8 @@
const VUE_APP_PLATFORM = process.env.VUE_APP_PLATFORM;
// localIp = false
module.exports = {
H5_API: VUE_APP_PLATFORM === 'h5' ? '/api' : '',//h5代理
WX_API: VUE_APP_PLATFORM === 'h5' ? '' : 'http://192.168.1.26:48080/',//非代理地址
TEMP_HOST: VUE_APP_PLATFORM === 'h5' ? '' : 'http://192.168.1.26:48080/',
};

2
manifest.json

@ -93,7 +93,7 @@
"disableHostCheck" : true,
"proxy" : {
"/api" : {
"target" : "http://121.41.97.244:8090",
"target" : "http://192.168.1.26:48080/",
"changeOrigin" : true,
"secure" : true,
"pathRewrite" : {

19
pages/tabbar/index/index.vue

@ -206,6 +206,18 @@
icon: require('../../../static/images/index/img_6@2x.png'),
url: '/pages/indexEntry/consult/consult'
},
{
text: '满分学习考试',
tps: '网络教育 数据联网',
icon: require('../../../static/images/index/img_7@2x.png'),
url: ''
},
{
text: '送考卷下乡',
tps: '便民利民 就近考试',
icon: require('../../../static/images/index/img_8@2x.png'),
url: ''
},
],
swiperDotIndex: 1,
current: 1,
@ -530,7 +542,12 @@
&.li2Bg5 {
background: linear-gradient(180deg, #FCF9FB 0%, #FFF5F8 100%);
}
&.li2Bg6 {
background: linear-gradient(180deg, #FDFFF3 0%, #F5FFE1 100%);
}
&.li2Bg7 {
background: linear-gradient(180deg, #EDF7FF 0%, #F3F1FF 100%);
}
.icon {
width: 80rpx;
height: 80rpx;

2
pages/userCenter/login/login.vue

@ -4,7 +4,7 @@
<image src="../../../static/images/userCenter/loginTitle.png" mode=""></image>
</view>
<view class="btnCon">
<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号</button>
<!-- <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号</button> -->
<view class="btn" @click="loginFn">手机号快捷登录</view>
<!-- <button type="success" size="mini" style="margin-top: 15rpx;" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" text="获取手机号">获取手机号</button> -->
<view class="btn border" @click="$goPage('/pages/userCenter/login/loginByPhone');">手机号登录/注册</view>

19
pages/userCenter/login/loginByPhone.vue

@ -35,7 +35,7 @@
</template>
<script>
import { getLoginCode } from '@/config/api.js'
import { getLoginCode, loginSMS } from '@/config/api.js'
export default {
data() {
return {
@ -47,7 +47,7 @@
}
},
onLoad() {
this.$store.dispatch('getUserInfo')
},
computed: {
isPhone() {
@ -74,8 +74,8 @@
if (!this.isPhone) return this.$u.toast('手机号格式有误');
if (this.codeOn) return
const data = await getLoginCode({
codeType: 1,
phone: FormData.phone,
scene: 1,
mobile: FormData.phone,
})
console.log(data)
//
@ -91,7 +91,16 @@
}
}, 1000);
},
submitFn() {
async submitFn() {
if(!this.btnHighlight) return
if(!this.isCheck) return this.$u.toast('请勾选产品协议与隐私政策');
let obj = {
mobile: this.FormData.phone,
code: this.FormData.code
}
const res = await loginSMS(obj)
this.$store.commit('update_vuex_loginInfo',res.data)
this.$store.dispatch('getUserInfo')
uni.switchTab({
url: '/pages/tabbar/index/index'
})

9
site.config.js

@ -1,7 +1,8 @@
const VUE_APP_PLATFORM = process.env.VUE_APP_PLATFORM;
// localIp = false
module.exports = {
APP_API: VUE_APP_PLATFORM === 'h5' ? '/api' : 'http://121.41.97.244:8090',
APP_HOST: VUE_APP_PLATFORM === 'h5' ? '' : 'https://www.jaxc.cn/api',
TEMP_HOST: VUE_APP_PLATFORM === 'h5' ? '' : 'http://121.41.97.244:8090 https://www.jaxc.cn/api',
//是否启用人脸识别
H5_API: VUE_APP_PLATFORM === 'h5' ? '/api' : '',//h5代理
WX_HOST: VUE_APP_PLATFORM === 'h5' ? '' : 'http://192.168.1.26:48080/',//非代理地址
TEMP_HOST: VUE_APP_PLATFORM === 'h5' ? '' : 'http://192.168.1.26:48080/',
};

22
store/modules/user.js

@ -5,11 +5,20 @@ const user = {
state: {
vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: '杭州市'},
vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {},
vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {},
},
mutations: {
update_vuex_cityInfo(state, payload) {
state.vuex_cityInfo = payload
uni.setStorageSync('vuex_cityInfo', payload);
},
update_vuex_loginInfo(state, payload) {
state.vuex_loginInfo = payload
uni.setStorageSync('vuex_loginInfo', payload);
},
update_vuex_userInfo(state, payload) {
state.vuex_userInfo = payload
uni.setStorageSync('vuex_userInfo', payload);
}
},
actions: {
@ -45,6 +54,19 @@ const user = {
// #endif
})
},
// 获取用户信息
async getUserInfo({commit}) {
const http = uni.$u.http
let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
const res = await http.get('member/user/get',config)
commit('update_vuex_userInfo',res.data)
},
// 刷新token
async refreshToken({state}) {
const http = uni.$u.http
let refreshToken = state.vuex_loginInfo.refreshToken
const res = await http.post('member/auth/refresh-token',{refreshToken}, config)
}
}
}

Loading…
Cancel
Save