123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- import Base64 from "./base64.js"
- import store from "../store/index.js"
- export default class Parse {
- // private property
- static getInstance() {
- if (!this.instance) {
- this.instance = new Parse();
- }
- return this.instance
- }
- info = {}
- // public method for decoding
- _processor(res) {
- const base64 = Base64.getInstance();
- console.log(res)
- let data = res.replace("https://goto.huijy.net/", "")
- let stationId
- let employeeId
- let labelId
- let info = {
- stationId: undefined,
- labelId: undefined,
- employeeId: undefined
- }
-
- const idArr = data.match(/^\d{1,}/g)
- if (idArr === null) {
- throw new Error("二维码是个啥玩意?!")
- }
- // 处理后的data
- data = data.replace(/^\d{1,}\//g, '')
- // 解压后的data
- data = base64.decode(data)
- // 员工id
- if (data.includes('e')) {
- const employeeIdStrArr = data.match(/e\d{1,}/g)
- if (employeeIdStrArr === null) {
- return
- }
- const employeeIdArr = employeeIdStrArr[0].match(/\d{1,}/g)
- if (employeeIdArr === null) {
- return
- }
- employeeId = employeeIdArr[0]
- this.info.employeeId = employeeId
- store.commit('updateEmployeeId', employeeId);
- }
- // 标签id
- if (data.includes('l')) {
- const labelIdStrArr = data.match(/l\d{1,}/g)
- if (labelIdStrArr === null) {
- return
- }
- const labelIdArr = labelIdStrArr[0].match(/\d{1,}/g)
- if (labelIdArr === null) {
- return
- }
- labelId = labelIdArr[0]
- this.info.labelId = labelId
- store.commit("updateLabelId", labelId)
- }
- stationId = idArr[0]
- this.info.stationId = stationId
- store.commit("updateStationId", stationId)
- return;
- }
- async parseCode(res) {
- try {
- this._processor(res)
- return Promise.resolve(this.info)
- } catch (e) {
- return Promise.reject("亲,请扫油站提供的码呦~")
- }
- }
- async _scanCode() {
- try {
- const scanCodePro = await uni.scanCode({
- onlyFromCamera: true
- })
- if (scanCodePro[0] != null && scanCodePro[0].errMsg == "scanCode:fail") {
- throw new Error(1)
- }
- if (scanCodePro[0] != null) {
- throw new Error(2)
- }
- return this.parseCode(scanCodePro[1].result)
- // return Promise.resolve(scanCodePro[1].result)
- } catch (e) {
- if(e.message == 1){
- return Promise.reject("亲,请扫油站提供的码呦~")
- }else{
- return Promise.reject("亲,请扫二维码呀~")
- }
- // const showModalPro = await uni.showModal({
- // title: '( •̀ ω •́ )✧',
- // showCancel: false,
- // content: '请扫描二维码呦',
- // confirmText: '重新扫码',
- // })
- // return this.scanCode();
- }
- }
- async scanCode(showModalFlag = true, moduleConfig = {}) { //msg 消息
- let showModalPro = [
- null,
- {
- cancel: false,
- confirm: true
- }
- ]
- if (showModalFlag) {
- showModalPro = await uni.showModal({
- title: moduleConfig.title || '(~ ̄▽ ̄)~',
- showCancel: moduleConfig.showCancel,
- content: moduleConfig.content || '亲,请扫码使用~',
- cancelText: moduleConfig.cancelText || '返回',
- confirmText: moduleConfig.confirmText || '立刻扫码',
- })
- }
- if (showModalPro[1].cancel) {
- uni.navigateBack({
- delta: 1
- })
- return Promise.reject("返回")
- } else if (showModalPro[1].confirm) {
- return this._scanCode().catch((errMsg) => {
- console.log(errMsg)
- return this.scanCode(true, {
- content: errMsg
- })
- });
- }
- }
- }
|