[Micro] letter applet Log in batches collected through user formId

Reference online articles, it aimed to be a record.

[Micro] Log letter applet formid in batches collected by the user, unlimited templates to send a message

Forms can be submitted once issued a message template, submitting multiple number of independent winding down, not affect each other.
So, I want to send unlimited message template, you need to bulk collection formId.

index.wxml

  <view>
    <view class="inputView" style="margin-top: 10% ">
      <form bind:submit="formSubmit" report-submit="true">
        <button form-type='submit' class='form_button'>
          <view class='vv'>
            <form bind:submit="formSubmit" report-submit="true">
              <button form-type='submit' class='form_button'>
                <view class='vv'>
                  <input class="input" type="string" bindinput='username' placeholder="用户名" placeholder-style="color: #FFFFFF" />
                </view>
              </button>
            </form>
          </view>
        </button>                                                
      </form>
    </view>
    <view class="inputView" style="margin-top: 5% ">
      <form bind:submit="formSubmit" report-submit="true">
        <button form-type='submit' class='form_button'>
          <view class='vv'>
            <form bind:submit="formSubmit" report-submit="true">
              <button form-type='submit' class='form_button'>
                <view class='vv'>
                  <input class="input" password="true" bindinput='pwd' placeholder="密码" placeholder-style="color: #FFFFFF" />
                </view>
              </button>
            </form>
          </view>
        </button>
      </form>
    </view>
    <form bind:submit="login" report-submit="true">
      <button type='default' formType="submit" class='btn'>绑定</button>
    </form>   
  </view>

index.js

  formSubmit:function(e){
    let formId = e.detail.formId; //获取formId
    console.log(formId)
    if (e.detail.formId != 'the formId is a mock one') {
      this.collectFormIds(formId);//保存推送码
    }
  },
  collectFormIds: function (formId) {
    let formIds = app.globalData.globalFormIds; // 获取全局推送码数组
    if (!formIds)
      formIds = [];
    let data = {
      formId: formId,
      expire: new Date().getTime() + 604800000 // 7天后的过期时间戳
    }
    formIds.push(data);// 将data添加到数组的末尾
    app.globalData.globalFormIds = formIds;// 保存推送码并赋值给全局变量
  },
  username:function(e){
    this.data.username = e.detail.value
  },
  pwd:function(e){
    this.data.pwd = e.detail.value 
  },
  login: function (e) {
    let username = this.data.username;
    let pwd =  this.data.pwd;

    wx.request({//通过网络请求发送用户名和密码到服务器
      url: '请求的地址'’,
      data:{  
         username : username ,
         pwd : pwd
      },
      success: function (res) {
        if (res.data.user != null) {
          wx.setStorageSync("userId", res.data.user.id) //保存返回的用户信息
        }
        if (res.data.status == 0) {      
          //登录成功
          wx.redirectTo({
            url: '../second/second',
            success: function (res) {
              console.log('登录跳转成功')
            },
            fail: function () {
              console.log('登录跳转失败')
            }
          })
        } else {
          //跳回首页
          wx.redirectTo({
            url: '../index/index',
            success: function (res) {
              console.log('跳回首页成功')
            },
            fail: function () {
              console.log('跳回首页失败')
            }
          })
        }
      }
    })
  }

second.js

Page({
  onLoad: function (options) {
    this.uploadFormIds(); //上传推送码
  },
  uploadFormIds: function () {
    var openid = wx.getStorageSync("openid")//获取openid
    var userId = wx.getStorageSync("userId")//获取userId
    var formIds = app.globalData.globalFormIds;// 从全局变量中获取formId
    if (formIds) {//gloabalFomIds存在的情况下 将数组转换为JSON字符串
      formIds = JSON.stringify(formIds);
      app.globalData.globalFormIds = '';   //清空formId
    }
    if (userId){
      wx.request({//通过网络请求发送openId和formIds到服务器
        url: '请求地址',
        method: 'POST',
        data: {
          formId: formIds,
          openId: openid,
          userId: userId
        }
        success: function (res) {
        }
      })
    }
  }
})

Will get back to fromId, openid, userid and saved to the database table, to achieve the functionality of batch collection formId.

Note: When testing must be tested on a real machine, micro-channel developer tools to collect less than fromId, Tip: the formId is a mock one
NOTE: The real test, mobile phones and computers connected WIFI is the same.
Note: When testing, call the local interface can be set in the micro-channel developer tools to not examine the legal domain, as shown below

Write pictures described here

Released six original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43055905/article/details/82224044