answer-form.js 1.8 KB
Newer Older
D
devil_gong 已提交
1 2 3 4 5 6 7 8 9
const app = getApp();
Page({
  data: {
    form_submit_loading: false,
  },

  onLoad() {},

  onShow() {
G
gongfuxiang 已提交
10
    wx.setNavigationBarTitle({title: app.data.common_pages_title.answer_form});
D
devil_gong 已提交
11 12 13 14 15
    this.init();
  },

  // 初始化
  init() {
D
devil_gong 已提交
16 17 18 19 20 21 22
    var user = app.get_user_cache_info(this, "init");
    // 用户未绑定用户则转到登录页面
    if (user == false || ((user.mobile || null) == null)) {
      wx.redirectTo({
        url: "/pages/login/login?event_callback=init"
      });
      return false;
D
devil_gong 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    }
  },

  /**
   * 表单提交
   */
  formSubmit(e)
  {              
    // 数据验证
    var validation = [
      {fields: 'name', msg: '请填写联系人'},
      {fields: 'tel', msg: '请填写联系电话'},
      {fields: 'content', msg: '请填写内容'}
    ];
    if(app.fields_check(e.detail.value, validation))
    {
G
gongfuxiang 已提交
39
      wx.showLoading({title: '提交中...'});
D
devil_gong 已提交
40 41 42 43
      this.setData({form_submit_loading: true});

      // 网络请求
      wx.request({
D
devil_gong 已提交
44
        url: app.get_request_url('add', 'answer'),
D
devil_gong 已提交
45 46 47 48 49 50 51 52 53
        method: 'POST',
        data: e.detail.value,
        dataType: 'json',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: (res) => {
          wx.hideLoading();

          if(res.data.code == 0)
          {
G
gongfuxiang 已提交
54
            app.showToast(res.data.msg, "success");
D
devil_gong 已提交
55 56 57 58 59 60 61 62 63
            setTimeout(function()
            {
              wx.redirectTo({
                url: "/pages/user-answer-list/user-answer-list"
              });
            }, 2000);
          } else {
            this.setData({form_submit_loading: false});
            
G
gongfuxiang 已提交
64
            app.showToast(res.data.msg);
D
devil_gong 已提交
65 66 67 68 69 70
          }
        },
        fail: () => {
          wx.hideLoading();
          this.setData({form_submit_loading: false});

G
gongfuxiang 已提交
71
          app.showToast('服务器请求出错');
D
devil_gong 已提交
72 73 74 75 76 77
        }
      });
    }
  },

});