bindDevice.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const APP = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. //表单错误信息
  8. openNumError: null,
  9. deviceNameError: null,
  10. deviceNum: '',//设备码,扫码复写表单
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad(options) {
  16. },
  17. /**
  18. * 扫码
  19. */
  20. scanCode() {
  21. const that = this;
  22. wx.scanCode({
  23. success(res) {
  24. that.setData({
  25. deviceNum: res.result
  26. })
  27. },
  28. fail: function () {
  29. console.log("调用摄像头失败")
  30. }
  31. })
  32. },
  33. /**
  34. * 表单提交
  35. */
  36. submit(form) {
  37. //校验表单
  38. if (!this.checkForm(form)) return;
  39. const params = {
  40. openNum: form.detail.value.openNum,
  41. deviceName: form.detail.value.deviceName
  42. };
  43. APP.Post(APP.Url.bind, params).then(res => {
  44. APP.Modal.tips("绑定成功!");
  45. }).catch(res => {
  46. console.log(res)
  47. APP.Modal.tips("绑定失败!")
  48. })
  49. },
  50. /**
  51. * 校验表单输入
  52. */
  53. checkForm(form) {
  54. let result = true;
  55. const openNum = form.detail.value.openNum;
  56. const deviceName = form.detail.value.deviceName;
  57. if (openNum == null || openNum == "") {
  58. result = false;
  59. this.setData({
  60. openNumError: "请输入设备码"
  61. })
  62. } else {
  63. this.setData({
  64. openNumError: ""
  65. })
  66. }
  67. if (deviceName == null || deviceName == "") {
  68. result = false;
  69. this.setData({
  70. deviceNameError: "请输入设备名称"
  71. })
  72. } else {
  73. this.setData({
  74. deviceNameError: ""
  75. })
  76. }
  77. return result;
  78. }
  79. })