提交 2f4c9be8 编写于 作者: DCloud_JSON's avatar DCloud_JSON

新增callfunction的拦截器废除this.request的写法。为callFunction添加:请求失败是否断网判断并提示、恢复网络自动重新执行、自...

新增callfunction的拦截器废除this.request的写法。为callFunction添加:请求失败是否断网判断并提示、恢复网络自动重新执行、自动处理响应体,目前处理了403为token过期自动跳转到登陆页面,今后会添加更多的自动行为、自动延续token过期时间
上级 812b079d
......@@ -37,31 +37,6 @@
console.error('获取服务供应商失败:' + JSON.stringify(err));
})
// #endif
//clientDB的错误提示
const db = uniCloud.database()
function onDBError({
code, // 错误码详见https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=returnvalue
message
}) {
// 处理错误
console.log(code,message);
if([
'TOKEN_INVALID_INVALID_CLIENTID',
'TOKEN_INVALID',
'TOKEN_INVALID_TOKEN_EXPIRED',
'TOKEN_INVALID_WRONG_TOKEN',
'TOKEN_INVALID_ANONYMOUS_USER',
].includes(code)){
uni.navigateTo({
url:'/pages/ucenter/login-page/index/index'
})
}
}
// 绑定clientDB错误事件
db.on('error', onDBError)
// 解绑clientDB错误事件
//db.off('error', onDBError)
},
onShow: function() {
console.log('App Show')
......
......@@ -177,6 +177,22 @@ uni-starter + uniCloud admin,应用开发从未如此简单快捷!
1. 最新的华为应用市场要求,隐私政策提示框上接受按钮的文本,必须为“同意”而不能是其他有歧义的文字。
2. 配置后提交云端打包后生效。理论上绝大部分和`manifest.json`生效相关的配置均需要提交云打包后生效
#### 10.拦截器改造后的uniCloud
1. Debug,调试期间开启Debug。接口一旦file就会弹出真实错误信息。否则将弹出,系统错误请稍后再试!
```
if(Debug){
console.log(e);
uni.showModal({
content: JSON.stringify(e),
showCancel: false
});
}
```
2. 断网自动重试,当callFunction为fail时检测是否因断网引起。如果是会提醒用户并且会在恢复网络之后自动重新发起请求
3. 常规的errCoder自动执行对应程序,如token无效/过期自动跳转到登陆页面。
4. token自动续期。
### 应用启动时序介绍
文件路径: App.vue
```
......
## 1.0.9(2021-05-23)
修复变量被重复定义的问题
## 1.0.8(2021-05-22)
宫格页(/pages/grid/grid),新增根据当前用户是否登陆、是否为管理员的角色来决定是否显示的示范
## 1.0.7(2021-05-22)
......
import uniStarterConfig from '@/uni-starter.config.js';
import store from '@/store'
//应用初始化页
// #ifdef APP-PLUS
import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
import callCheckVersion from '@/uni_modules/uni-upgrade-center-app/utils/call-check-version';
import interceptorChooseImage from '@/uni_modules/json-interceptor-chooseImage/js_sdk/main.js';
// #endif
const db = uniCloud.database()
export default function() {
setTimeout(()=>{
......@@ -22,6 +24,131 @@ export default function() {
// #endif
//clientDB的错误提示
function onDBError({
code, // 错误码详见https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=returnvalue
message
}) {
// 处理错误
console.log(code,message);
if([
'TOKEN_INVALID_INVALID_CLIENTID',
'TOKEN_INVALID',
'TOKEN_INVALID_TOKEN_EXPIRED',
'TOKEN_INVALID_WRONG_TOKEN',
'TOKEN_INVALID_ANONYMOUS_USER',
].includes(code)){
uni.navigateTo({
url:'/pages/ucenter/login-page/index/index'
})
}
}
// 绑定clientDB错误事件
db.on('error', onDBError)
// 解绑clientDB错误事件
//db.off('error', onDBError)
db.on('refreshToken', function({
token,
tokenExpired
}) {
console.log('监听到clientDB的refreshToken',{token,tokenExpired});
store.commit('user/login', {
token,
tokenExpired
})
})
const Debug = true;
//拦截器封装callFunction
let callFunctionOption;
uniCloud.addInterceptor('callFunction',{
invoke(e){
console.log(JSON.stringify(e));
callFunctionOption = e
},
complete(e){
// console.log(JSON.stringify(e));
},
fail(e) { // 失败回调拦截
if(Debug){
console.log(e);
uni.showModal({
content: JSON.stringify(e),
showCancel: false
});
}else{
uni.showModal({
content: "系统错误请稍后再试!",
showCancel: false,
confirmText:"知道了"
});
}
//如果执行错误,检查是否断网
uni.getNetworkType({
complete:res => {
console.log(res);
if (res.networkType == 'none') {
uni.showToast({
title: '手机网络异常',
icon: 'none'
});
console.log('手机网络异常');
let callBack = res=>{
console.log(res);
if (res.isConnected) {
uni.showToast({
title: '恢复联网自动重新执行',
icon: 'none'
});
console.log(res.networkType,"恢复联网自动重新执行");
uni.offNetworkStatusChange(e=>{
console.log("移除监听成功",e);
})
//恢复联网自动重新执行
uniCloud.callFunction(callFunctionOption)
uni.offNetworkStatusChange(callBack);
}
}
uni.onNetworkStatusChange(callBack);
}
}
});
},
success:(e)=>{
console.log(e);
const {token,tokenExpired} = e.result
if (token && tokenExpired) {
store.commit('user/login', {
token,
tokenExpired
})
}
console.log(e.result.code);
switch (e.result.code){
case 403:
uni.showModal({
content: '未登陆,跳登陆',
showCancel: false
});
break;
case 50101:
uni.showToast({
title: e.result.msg,
icon: 'none',
duration:2000
});
break;
default:
console.log('code的值是:'+e.result.code,'可以在这里插入,自动处理响应体');
break;
}
}
})
//自定义路由拦截
const {"router": {needLogin,login} } = uniStarterConfig //需要登录的页面
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
......
......@@ -93,8 +93,8 @@
},
},
created() {
console.log('loginConfig', this.loginConfig);
console.log('this.getRoute(1)', this.getRoute(1));
// console.log('loginConfig', this.loginConfig);
// console.log('this.getRoute(1)', this.getRoute(1));
let servicesList = this.servicesList
//去掉当前页面对应的登录选项
for (var i = 0; i < servicesList.length; i++) {
......@@ -109,7 +109,7 @@
servicesList.splice(i, 1)
}
}
console.log('servicesList', servicesList);
// console.log('servicesList', servicesList);
},
mounted() {
//获取当前环境能用的快捷登录方式
......@@ -265,7 +265,13 @@
params,
type
});
this.request('uni-id-cf/login_by_' + type, params, result => {
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'login_by_'+type,
params
},
success: ({result}) => {
console.log(result);
if (result.code === 0) {
if (type == 'univerify') {
......@@ -276,6 +282,10 @@
delete result.userInfo.token
this.setUserInfo(result.userInfo)
}
},
complete: () => {
uni.hideLoading()
}
})
},
async getUserInfo(e) {
......
......@@ -74,12 +74,16 @@
title: '手机号格式错误',
icon: 'none'
});
this.request('uni-id-cf/sendSmsCode',
{
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'sendSmsCode',
params:{
"mobile": this.phone,
"type": this.codeType
},result=>{
},
},
success: ({result}) => {
console.log(result);
uni.showToast({
title: "短信验证码发送成功",
......@@ -89,7 +93,7 @@
this.getCode();
this.$emit('getCode');
}
)
})
},
getCode() {
if (this.reverseNumber == 0) {
......
/*
1.优雅访问指定路由地址
2.load自动显示与关闭
3.统一路由拦截
3.1 读取云端接口权限配置,先验证本地token再访问
3.2 处理因token过期等问题自动更新本地token,或token无效跳转至登录页面
*/
const debug = true;//开启后,会alert错误信息
export default function request(name,params,callback=false,{showLoading=false,loadText='',fail=()=>{}}={}){
// console.log('request');
showLoading||loadText? uni.showLoading({title:loadText,mask:true}):'';
let routers = name.split('/');
var action = false
if (routers.length>1){
name = routers[0]
action = routers[1]
}
console.log({name,data:{action,params}})
return new Promise((resolve,reject)=>{
uniCloud.callFunction({name,data:{action,params},
success(e){
console.log(e);
const {result:{data,code}} = e
console.log(data,code);
resolve(e)
return callback(e.result,e)
},
fail(err){
reject(err)
console.log(err);
fail(err)
},
complete() {
if(showLoading || loadText) uni.hideLoading()
}
})
})
}
\ No newline at end of file
......@@ -2,10 +2,8 @@ import Vue from 'vue'
import App from './App'
import store from './store/index.js';
import openApp from './common/openApp.js';
import request from './js_sdk/request.js';
Vue.config.productionTip = false
Vue.prototype.request = request
openApp();
App.mpType = 'app'
......
{
"id": "uni-starter",
"displayName": "uni-starter",
"version": "1.0.7",
"version": "1.0.9",
"description": "云端一体应用快速开发模版",
"keywords": [
"uni-starter",
......
......@@ -42,16 +42,31 @@
},
methods: {
submit(){ //完成并提交
this.request('uni-id-cf/loginBySms',
{
// this.-request('uni-id-cf/loginBySms',
// {
// "mobile":this.phone,
// "code":this.code
// },
// e=>{
// console.log(e);
// this.loginSuccess(e)
// },
// {showLoading:true})
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'loginBySms',
params:{
"mobile":this.phone,
"code":this.code
},
e=>{
console.log(e);
this.loginSuccess(e)
},
{showLoading:true})
success: ({result}) => {
this.loginSuccess(result)
}
})
}
}
}
......
......@@ -63,11 +63,17 @@
});
}
// 下边是可以登录
this.request('uni-id-cf/login', {
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'login',
params:{
"username": this.username,
"password": this.password,
"captcha":this.captcha
}, result => {
},
},
success: ({result}) => {
console.log(result);
if (result.code === 0) {
this.loginSuccess(result)
......@@ -87,17 +93,59 @@
});
}
}
}
})
// this.-request('uni-id-cf/login', {
// "username": this.username,
// "password": this.password,
// "captcha":this.captcha
// }, result => {
// console.log(result);
// if (result.code === 0) {
// this.loginSuccess(result)
// } else {
// if (result.needCaptcha) {
// uni.showToast({
// title: result.msg,
// icon: 'none'
// });
// this.createCaptcha()
// }else{
// uni.showModal({
// title: '错误',
// content: result.msg,
// showCancel: false,
// confirmText: '知道了'
// });
// }
// }
// })
},
createCaptcha(){
this.request(
'uni-id-cf/createCaptcha', {
// this.-request(
// 'uni-id-cf/createCaptcha', {
// scene: "login"
// },
// result => {
// if (result.code === 0) {
// this.captchaBase64 = result.captchaBase64
// }
// })
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'createCaptcha',
params:{
scene: "login"
},
result => {
},
success: ({result}) => {
if (result.code === 0) {
this.captchaBase64 = result.captchaBase64
}
}
})
},
/* 前往注册 */
......
......@@ -5,7 +5,7 @@
<uni-forms ref="form" :value="formData" :rules="rules">
<uni-forms-item name="phone">
<!-- focus规则如果上一页携带来“手机号码”数据就focus验证码输入框,否则focus手机号码输入框 -->
<uni-easyinput :focus="formData.phone.length!=11" type="number" class="easyinput" :inputBorder="false"
<uni-easyinput :disabled="lock" :focus="formData.phone.length!=11" type="number" class="easyinput" :inputBorder="false"
v-model="formData.phone" maxlength="11" placeholder="请输入手机号"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="code">
......@@ -36,6 +36,7 @@
mixins: [mixin],
data() {
return {
lock:false,
formData: {
"phone": "",
'pwd': '',
......@@ -121,6 +122,7 @@
onLoad(event) {
if (event && event.phoneNumber) {
this.formData.phone = event.phoneNumber;
this.lock = true
}
},
onReady() {
......@@ -135,11 +137,31 @@
submit() {
this.$refs.form.submit()
.then(res => {
this.request('uni-id-cf/resetPwdBySmsCode', {
// this.-request('uni-id-cf/resetPwdBySmsCode', {
// "mobile": this.formData.phone,
// "code": this.formData.code,
// "password": this.formData.pwd
// }, result => {
// console.log(result);
// uni.showToast({
// title: result.msg,
// icon: 'none'
// });
// if (result.code === 0) {
// uni.navigateBack()
// }
// })
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'resetPwdBySmsCode',
params:{
"mobile": this.formData.phone,
"code": this.formData.code,
"password": this.formData.pwd
}, result => {
},
},
success: ({result}) => {
console.log(result);
uni.showToast({
title: result.msg,
......@@ -148,6 +170,7 @@
if (result.code === 0) {
uni.navigateBack()
}
}
})
})
}
......
......@@ -62,12 +62,25 @@ import mixin from '../common/login-page.mixin.js';
uni.hideLoading()
})
},
submitForm(value) {
this.request('uni-id-cf/register',value,result=>{
submitForm(params) {
// this.-request('uni-id-cf/register',params,result=>{
// console.log(result);
// if(result.code === 0){
// this.loginSuccess(result)
// }
// })
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'register',
params,
},
success: ({result}) => {
console.log(result);
if(result.code === 0){
this.loginSuccess(result)
}
}
})
}
}
......
......@@ -48,10 +48,30 @@
*/
submit() {
console.log(this.formData);
this.request('uni-id-cf/bind_mobile_by_sms', {
// this.-request('uni-id-cf/bind_mobile_by_sms', {
// "mobile": this.formData.phone,
// "code": this.formData.code
// }, result=> {
// console.log(result);
// this.setUserInfo({"mobile":result.mobile})
// uni.showToast({
// title: result.msg,
// icon: 'none'
// });
// if (result.code === 0) {
// uni.navigateBack()
// }
// })
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'bind_mobile_by_sms',
params:{
"mobile": this.formData.phone,
"code": this.formData.code
}, result=> {
},
},
success: ({result}) => {
console.log(result);
this.setUserInfo({"mobile":result.mobile})
uni.showToast({
......@@ -61,6 +81,7 @@
if (result.code === 0) {
uni.navigateBack()
}
}
})
}
}
......
......@@ -7,7 +7,7 @@
<image class="avatarUrl" :src="userInfo.avatar||nullAvatarUrl" mode="widthFix"></image>
</view>
</uni-list-item>
<uni-list-item class="item" @click="setNickname()" title="昵称" :rightText="userInfo.nickname||'未设置'" link></uni-list-item>
<uni-list-item class="item" @click="setNickname('')" title="昵称" :rightText="userInfo.nickname||'未设置'" link></uni-list-item>
<uni-list-item class="item" @click="bindMobile" title="手机号" :rightText="userInfo.mobile||'未绑定'" link></uni-list-item>
</uni-list>
<uni-popup ref="dialog" type="dialog">
......@@ -70,10 +70,32 @@
"univerifyStyle": this.univerifyStyle,
success: async e => {
console.log(e.authResult);
this.request('uni-id-cf/bind_mobile_by_univerify',
e.authResult,
result=>
{
// this.-request('uni-id-cf/bind_mobile_by_univerify',
// e.authResult,
// result=>
// {
// console.log(result);
// if(result.code===0){
// this.setUserInfo({"mobile":result.mobile})
// uni.closeAuthView()
// }else{
// uni.showModal({
// content: JSON.stringify(result.msg),
// showCancel: false,
// complete() {
// uni.closeAuthView()
// }
// });
// }
// }
// )
uniCloud.callFunction({
name:'uni-id-cf',
data:{
action:'bind_mobile_by_univerify',
params:e.authResult,
},
success: ({result}) => {
console.log(result);
if(result.code===0){
this.setUserInfo({"mobile":result.mobile})
......@@ -88,7 +110,7 @@
});
}
}
)
})
},
fail: (err) => {
console.log(err);
......@@ -104,7 +126,7 @@
})
},
setNickname(nickname) {
console.log(nickname);
console.log(9527,nickname);
if (nickname) {
usersTable.where('_id==$env.uid').update({
nickname
......
......@@ -189,7 +189,7 @@ exports.main = async (event, context) => {
res.needCaptcha = needCaptcha;
break;
case 'login_by_weixin':
res = await uniID.loginByWeixin(params);
res = await uniID.loginByWeixin({...params});
await uniID.updateUser({
uid: res.uid,
username: "微信用户"
......
// 本文件中的json内容将在云函数【运行】时作为参数传给云函数。
// 配置教程参考:https://uniapp.dcloud.net.cn/uniCloud/quickstart?id=runparam
{
"action": "login_by_weixin",
"params": {
"code": "093tK5Ga1X1D6B0MSAHa13uRH04tK5Gs"
}
}
// 本文件用于,使用JQL语法操作项目关联的uniCloud空间的数据库,方便开发调试和远程数据库管理
// 编写clientDB的js API(也支持常规js语法,比如var),可以对云数据库进行增删改查操作。不支持uniCloud-db组件写法
// 可以全部运行,也可以选中部分代码运行。点击工具栏上的运行按钮或者按下【F5】键运行代码
// 如果文档中存在多条JQL语句,只有最后一条语句生效
// 如果混写了普通js,最后一条语句需是数据库操作语句
// 此处代码运行不受DB Schema的权限控制,移植代码到实际业务中注意在schema中配好permission
// 不支持clientDB的action
// 数据库查询有最大返回条数限制,详见:https://uniapp.dcloud.net.cn/uniCloud/cf-database?id=limit
// 详细JQL语法,请参考 https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=jsquery
// 下面示例查询uni-id-users表的所有数据
db.collection('uni-id-users').get();
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册