index.obj.js 1003 B

1234567891011121314151617181920212223242526272829303132333435
  1. // 开发文档: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
  2. //导入验证码公共模块
  3. const uniCaptcha = require('uni-captcha')
  4. //获取数据库对象
  5. const db = uniCloud.database();
  6. //获取数据表opendb-verify-codes对象
  7. const verifyCodes = db.collection('opendb-verify-codes')
  8. module.exports = {
  9. async getImageCaptcha({
  10. scene,isUniAppX
  11. }) {
  12. //获取设备id
  13. let {
  14. deviceId,
  15. platform
  16. } = this.getClientInfo();
  17. //根据:设备id、场景值、状态,查找记录是否存在
  18. let res = await verifyCodes.where({
  19. scene,
  20. deviceId,
  21. state: 0
  22. }).limit(1).get()
  23. //如果已存在则调用刷新接口,反之调用插件接口
  24. let action = res.data.length ? 'refresh' : 'create'
  25. //执行并返回结果
  26. let option = {
  27. scene, //来源客户端传递,表示:使用场景值,用于防止不同功能的验证码混用
  28. uniPlatform: platform
  29. }
  30. if(isUniAppX){
  31. option.mode = "bmp"
  32. }
  33. return await uniCaptcha[action](option)
  34. }
  35. }