Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Nov 1, 2023
1 parent 4ed7857 commit b796016
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "miniprogram"
version = "0.8.0"
version = "0.9.0"
edition = "2021"
publish = false

Expand Down
4 changes: 2 additions & 2 deletions miniprogram/api/shorturl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import http from '@utils/http'
import { URL } from '@constant/shortlink'
import { CODE } from '@constant/error'
import { HttpApiError } from '@models/error'
import { error } from '@utils/logger'
import logger from '@utils/logger'

const create = async (link: string) => {
try {
return await http.post<IShortlinkCreateResponse>(URL.CREATE, {link} as IShortlinkCreateRequest)
} catch (e) {
error('创建短链接失败', e.code, e.message)
logger.error('创建短链接失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_SHORTLINK_CREATE))
}
Expand Down
12 changes: 6 additions & 6 deletions miniprogram/api/totp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import http from '@utils/http'
import { URL } from '@constant/totp'
import { CODE } from '@constant/error'
import { HttpApiError } from '@models/error'
import { error } from '@utils/logger'
import logger from '@utils/logger'

const all = async () => {
try {
return await http.post<ITotpItemResponse[]>(URL.ALL)
} catch (e) {
error('查询 TOTP 列表失败', e.code, e.message)
logger.error('查询 TOTP 列表失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_TOTP_ALL))
}
Expand All @@ -18,7 +18,7 @@ const detail = async (id: number) => {
try {
return await http.post<ITotpItemResponse>(URL.DETAIL, {id} as ITotpDetailRequest)
} catch (e) {
error('查询 TOTP 详情失败', e.code, e.message)
logger.error('查询 TOTP 详情失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_TOTP_DETAIL))
}
Expand All @@ -28,7 +28,7 @@ const create = async (uri: string) => {
try {
return await http.post<ITotpResponse>(URL.CREATE, {uri} as ITotpCreateRequest)
} catch (e) {
error('创建 TOTP 失败', e.code, e.message)
logger.error('创建 TOTP 失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_TOTP_CREATE))
}
Expand All @@ -38,7 +38,7 @@ const update = async (data: ITotpUpdateRequest) => {
try {
return await http.post<ITotpResponse>(URL.UPDATE, data)
} catch (e) {
error('更新 TOTP 信息失败', e.code, e.message)
logger.error('更新 TOTP 信息失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_TOTP_UPDATE))
}
Expand All @@ -48,7 +48,7 @@ const deleteTotp = async (id: number) => {
try {
return await http.post<ITotpResponse>(URL.DELETE, {id} as ITotpDeleteRequest)
} catch (e) {
error('删除 TOTP 失败', e.code, e.message)
logger.error('删除 TOTP 失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_TOTP_ALL))
}
Expand Down
8 changes: 4 additions & 4 deletions miniprogram/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import http from '@utils/http'
import { URL } from '@constant/user'
import { CODE } from '@constant/error'
import { error } from '@utils/logger'
import logger from '@utils/logger'
import { HttpApiError } from '@models/error'

const login = async (code: string) => {
try {
return await http.post<IUserLoginResponse>(URL.LOGIN, {code} as IUserLoginRequest, false, false)
} catch (e) {
error('登录接口请求失败', e.code, e.message)
logger.error('登录接口请求失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_USER_LOGIN))
}
Expand All @@ -18,7 +18,7 @@ const detail = async () => {
try {
return await http.post<IUserDetailResponse>(URL.DETAIL)
} catch (e) {
error('查询用户详情失败', e.code, e.message)
logger.error('查询用户详情失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_USER_DETAIL))
}
Expand All @@ -28,7 +28,7 @@ const update = async (updated: IUserUpdateRequest) => {
try {
return await http.post<IUserUpdateResponse>(URL.UPDATE, updated)
} catch (e) {
error('更新用户信息失败', e.code, e.message)
logger.error('更新用户信息失败', e.code, e.message)

return Promise.reject(new HttpApiError(CODE.HTTP_API_USER_UPDATE))
}
Expand Down
11 changes: 8 additions & 3 deletions miniprogram/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import userUtils from '@utils/user'
import { EError, WeixinError } from '@models/error'
import { CODE, MESSAGE } from '@constant/error'
import { DEFAULT } from '@constant/user'
import logger from '@utils/logger'

App<IGlobalData>({
globalData: {
Expand Down Expand Up @@ -42,15 +43,19 @@ App<IGlobalData>({
})
},
onError(e: any) {
console.log(e)
logger.error('小程序异常', e)

wx.showToast({title: '小程序异常', icon: 'error'})
},
onUnhandledRejection(e: any) {
if (e.reason instanceof EError) {
wx.showToast({title: e.reason.message || MESSAGE[e.reason.code], icon: 'error'})
} else {
wx.showToast({title: '未知错误', icon: 'error'})

return;
}

logger.error('未知错误', e)

wx.showToast({title: '未知错误', icon: 'error'})
}
})
1 change: 1 addition & 0 deletions miniprogram/interface/totp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface ITotpDetailRequest extends IRequestData {
}

interface ITotpUpdateRequest extends IRequestData{
id: number,
issuer: string,
username: string,
}
Expand Down
7 changes: 4 additions & 3 deletions miniprogram/pages/totp/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ Page({
username: '',
},
onLoad(query: any) {
this.data.id = query.id || 0
this.data.id = Number(query.id || 0)
},
async onShow() {
const {issuer, username} = await api.detail(this.data.id)
const {id, issuer, username} = await api.detail(this.data.id)

this.setData({
id,
issuer: issuer ?? '',
username: username ?? '',
})
},
async submit(e: any) {
await wx.showToast({title: '更新中', icon: 'loading', mask: true, duration: 3000})

await api.update(e.detail.value as ITotpUpdateRequest)
await api.update({id: this.data.id, ...e.detail.value} as ITotpUpdateRequest)

wx.showToast({
title: '修改成功',
Expand Down
4 changes: 2 additions & 2 deletions miniprogram/pages/totp/edit.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<view class="weui-label">提供商</view>
</view>
<view class="weui-cell__bd">
<input id="issuer" class="weui-input" placeholder="请输入提供商" value="{{issuer}}" />
<input id="issuer" class="weui-input" placeholder="请输入提供商" name="issuer" value="{{issuer}}" />
</view>
</view>
<view for="username" class="weui-cell weui-cell_active">
<view class="weui-cell__hd">
<view class="weui-label">账号</view>
</view>
<view class="weui-cell__bd">
<input id="username" class="weui-input" placeholder="请填写您的账号" value="{{username}}" />
<input id="username" class="weui-input" placeholder="请填写您的账号" name="username" value="{{username}}" />
</view>
</view>
</view>
Expand Down
6 changes: 4 additions & 2 deletions miniprogram/pages/user/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ Page({
async onChooseAvatar(e: any) {
await wx.showLoading({title: '上传中', icon: 'loading', mask: true})

let res = await wx.compressImage({ src: e.detail.avatarUrl, quality: 1})

wx.getFileSystemManager().readFile({
filePath: e.detail.avatarUrl,
filePath: res.tempFilePath,
encoding: 'base64',
success: async (res: any) => {
success: async (res: any) => {
this.setData({ avatar: "data:image/png;base64," + res.data })

await wx.hideLoading()
Expand Down
6 changes: 3 additions & 3 deletions miniprogram/pages/user/edit.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<view class="weui-form__bd">
<view class="weui-form__control-area">
<button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
<image class="avatar" src="{{avatar}}"><input type="text" name="avatar" value="{{ avatar }}" /></image>
<image class="avatar" src="{{avatar}}"><input type="text" name="avatar" value="{{ avatar }}" maxlength="-1"/></image>
</button>
<view class="weui-cells__group weui-cells__group_form">
<view class="weui-cells weui-cells_form">
Expand All @@ -12,15 +12,15 @@
<view class="weui-label">昵称</view>
</view>
<view class="weui-cell__bd">
<input id="nickname" class="weui-input" placeholder="{{nickname}}" value="{{nickname}}" />
<input id="nickname" class="weui-input" placeholder="{{nickname}}" name="nickname" value="{{nickname}}" />
</view>
</view>
<view for="slogan" class="weui-cell weui-cell_active">
<view class="weui-cell__hd">
<view class="weui-label">Slogan</view>
</view>
<view class="weui-cell__bd">
<input id="slogan" class="weui-input" placeholder="请填写 Slogan" value="{{slogan}}" />
<input id="slogan" class="weui-input" placeholder="请填写 Slogan" name="slogan" value="{{slogan}}" />
</view>
</view>
</view>
Expand Down
7 changes: 7 additions & 0 deletions miniprogram/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { URL } from '@constant/app'
import { CODE, MESSAGE } from '@constant/error'
import { HttpError, HttpApiError, LoginError } from '@models/error'
import userUtils from '@utils/user'
import logger from '@utils/logger'

const formatUrl = (request: IRequest): void => {
if (typeof request.query != 'undefined') {
Expand Down Expand Up @@ -45,6 +46,8 @@ const request = <T>(request: IRequest, mustOpenId?: boolean): Promise<T> => {
}

const wxRequest = <T>(request: IRequest) => {
logger.info('请求接口', request)

return new Promise<T>((resolve, reject) => {
wx.request({
url: request.url,
Expand All @@ -53,13 +56,17 @@ const wxRequest = <T>(request: IRequest) => {
timeout: request.timeout || 3000,
method: request.method || 'POST',
success: (res: any) => {
logger.info('接口请求成功', res)

if (res.data.code == 0) {
resolve(res.data.data)
}

reject(new HttpApiError(res.data.code as number, res.data.message as string))
},
fail: (err) => {
logger.warning('接口请求失败', err)

reject(new HttpError(err.errMsg))
},
})
Expand Down
20 changes: 12 additions & 8 deletions miniprogram/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const logger = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
const l = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null

const info = (...args: any[]) => {
logger?.info(args)
const logger = {
info: (...args: any[]) => {
l?.info(args)
},
warning: (...args: any[]) => {
l?.warn(args)
},
error: (...args: any[]) => {
l?.error(args)
}
}

const error = (...args: any[]) => {
logger?.error(args)
}

export { info, error }
export default logger
11 changes: 4 additions & 7 deletions src/repository/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,11 @@ pub async fn update(model: Totp, updated: UpdateTotp) -> Result<()> {
}

pub async fn delete(model: Totp) -> Result<()> {
model
.delete(Pool::get("default"))
.await
.map_err(|e| {
println!("删除 Totp 失败: {:?}", e);
model.delete(Pool::get("default")).await.map_err(|e| {
println!("删除 Totp 失败: {:?}", e);

Error::DatabaseDelete
})?;
Error::DatabaseDelete
})?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/service/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn all(user: User) -> Result<Vec<DetailResponse>> {
pub async fn detail(current_user: CurrentUser, id: i64) -> Result<DetailResponse> {
let t = totp::find(id).await?;

if current_user.id != t.id {
if current_user.id != t.user_id {
return Err(Error::TotpNotFound);
}

Expand Down
5 changes: 1 addition & 4 deletions src/service/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ pub async fn login(code: &str) -> Result<User> {
return user;
}

repository::user::insert(CreateUser {
open_id
})
.await
repository::user::insert(CreateUser { open_id }).await
}

pub async fn detail(open_id: &str) -> Result<User> {
Expand Down

0 comments on commit b796016

Please sign in to comment.