py-uni
Version:
py-uni
83 lines (74 loc) • 2.83 kB
text/typescript
import {Message} from "../service/message.service";
import {AuthorizeService} from "../service/authorize.service";
const message = new Message();
const authorizeService = new AuthorizeService();
/**
* 扫码
* @param _this this的指向
* @param messageToast 消息指向
*/
function scanCode(_this: any, messageToast: any) {
// #ifdef H5
// #endif
// #ifdef MP-WEIXIN || APP-PLUS
uni.scanCode({
onlyFromCamera: true,
success: (res) => {
const code = substrCode(res);
const isLogin = res.result?.indexOf('login') !== -1;
const isBind = res.result?.indexOf('bind') !== -1;
const isRegister = res.result?.indexOf('type') !== -1;
scanCodeAdaptation(_this, messageToast, res.result, code, isLogin, isBind, isRegister);
},
fail: (err => {
console.log('没有appid');
})
})
// #endif
}
function substrCode(res: any): string {
const index = res.result?.indexOf('?');
const substringString = res.result?.substr(index + 1, res.result?.length);
const substringIndex = substringString?.indexOf('=');
return substringString?.substr(substringIndex + 1, res.result?.length);
}
function scanCodeAdaptation(_this: any, messageToast: any, address: string | undefined, code: string, isLogin: boolean, isBind: boolean, isRegister: boolean) {
// #ifdef H5
// #endif
// #ifdef MP-WEIXIN || APP-PLUS
MPorAPPAdaptation(_this, messageToast, code, isLogin, isBind, isRegister);
// #endif
}
function MPorAPPAdaptation(_this: any, messageToast: any, code: string, isLogin: boolean, isBind: boolean, isRegister: boolean) {
const token = uni.getStorageSync('token');
if (isLogin) {
if (!!token) {
uni.navigateTo({
url: `/pages/operationSure/operationSure?code=${code}`
});
} else {
if (isRegister) {
uni.navigateTo({
url: `/pages/userRegister/userRegister?code=${code}`
});
} else {
message.showToast(messageToast, '请先登录账户', 'error');
}
}
}
if (isBind) {
const userInfo = uni.getStorageSync('userInfo');
ScanBind(_this, messageToast, userInfo.Mob1);
}
}
function ScanBind(_this: any, messageToast: any, mob: string): void {
const token = uni.getStorageSync('token');
authorizeService.Bind(token, '', mob).then((info: any) => {
if (info === 0) {
message.showToast(messageToast, '二维码授权成功', 'success');
} else {
message.showToast(messageToast, '二维码授权失败', 'error');
}
})
}
export default scanCode;