# 打卡功能

业务流程:

  • 上传本地图片至服务器,将返回的二维码保存在手机中,供打卡使用
  • 自动点击实现打卡

例子中展示:

  • 如何绑定设备、业务逻辑实现
  • 异步错误捕获 --->(done)的使用
  • 自定义抛出Error
  • post请求实现
import octopus from "jd-octopus"
import * as crypto from "crypto"//在使用的页面引入加密插件

describe("打卡用例", function () { 
 
  var c = octopus("10.222.50.39:8080")
  var {device,info,img,text,point,swipe,sleep} = octopus()
  let uuids = ["ca829b6"]
  var err = ""

  it("获取控制设备",()=>{
    device.bindDevices(uuids) // 绑定第一个设备
    sleep(1000)
  })

 it("打卡接口获取二维码保存在本地", function (done) {
    const fs = require('fs');
    const axios = require("axios");
    var v_timestamp = new Date().getTime();
    let md5 = crypto.createHash("md5");
    var md5Str = "moapplet-manager" + ":" + v_timestamp;
    md5.update(md5Str);
    let token = md5.digest('hex');
    var service = axios.create({
      baseURL: `http://10.13.225.248:80/screen/api/`,
      timeout: 1000000,
      headers: { Accept: "application/json", token: token, timestamp: v_timestamp }
    });
    let bitmap = fs.readFileSync('./test/shantou/img/扫码.png');
    let base64str = Buffer.from(bitmap, 'binary').toString('base64'); // base64编码
    base64str = "data:image/png;base64," + base64str;

    service.post(
      "/checkin/upload",
      {
        mac: "D4-12-43-67-D2-3C",
        file: base64str
      },
      {
        timeout: 2000
      }
    ).then(function (res: any) {
      var base64 = res.data.data.qrCode.replace(/^data:image\/\w+;base64,/, ""); //去掉图片base64码前面部分data:image/png;base64
      if (res.data.code != 200) {
        throw new Error("状态码:" + res.data.code + " message: " + res.data.message);
      } else if (base64.length < 300) {
        throw new Error('token过期,提示更新!');
      }

      var dataBuffer = Buffer.from(base64, "base64");
      fs.writeFile("./test/shantou/img/ewm.png", dataBuffer, function (err: any) {
        if (err) {
          throw new Error('文件写入失败!');
        }
      });
    })
    .catch((e:any)=>{
      done(new Error('服务器错误!')) // 用done捕获异部错误(done在子用例函数中声明)
    })
  });

  it("自动化打卡", function () {
    img("./test/shantou/img/ewm.png").save("/sdcard/DCIM/Screenshots/ewm2.png")
    sleep(3000);
    img("./test/shantou/img/闪投.png").click()
    sleep(15000);
    img("./test/shantou/img/允许.png").click()
    sleep(5000);
    img("./test/shantou/img/三个点.png").click()
    sleep(3000);
    img("./test/shantou/img/开发调试.png").click()
    sleep(3000);
    img("./test/shantou/img/打开调试.png").click()
    sleep(3000);
    img("./test/shantou/img/闪投.png").click()
    sleep(7000);
    img("./test/shantou/img/登录.png").click()
    sleep(3000);
    img("./test/shantou/img/微信用户快速登录.png").click()
    sleep(3000);
    img("./test/shantou/img/允许.png").click()
    sleep(5000);
    img("./test/shantou/img/绑定手机号登录.png").click()
    sleep(3000);
    img("./test/shantou/img/允许.png").click()
    sleep(3000);
    img("./test/shantou/img/扫码.png").click()
    sleep(3000);
    img("./test/shantou/img/相册.png").click()
    sleep(3000);
    point.clickByPercentage(0.35,0.15)
    sleep(3000);
  });

  it("退出小钼闪投",()=>{
    device.runKeyCode(3)
    sleep(2000)
  })

});

Last Updated: 12/8/2020, 3:10:44 PM