收藏
回答

一个登录按钮同时实现授权和获取formId怎么弄?

由于需要对用户进行消息推送,所以需要获取formId。

现在登录按钮上绑定着submit(form标签上bindsubmit)和getuserinfo(button上的bindgetuserinfo)的两个方法,一个提交一个授权,点击登录按钮两个方法都被触发,能否控制授权完毕才进行提交呢?

在getuserinfo方法里面调用submit无法获取formId及value,反之同理,怎么解决求教。

回答关注问题邀请回答
收藏

2 个回答

  • 毛毛🐟🐠🐡🐬🦈🐳🐋
    毛毛🐟🐠🐡🐬🦈🐳🐋
    2019-07-04

    用 promise 实现



    <form report-submit report-submit-timeout="2000" bindsubmit="formSubmit">
       
        <input class="form-control text-center mb-3" required placeholder="请输入您获得的四位随机码" value="{{ code }}" maxlength="4" bindinput='bindinput' data-name='code' name='code' />
      
        <button wx:else class='btn btn-primary' loading="{{btnLoading}}" form-type="submit" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" disabled="{{code.length !== 4}}">进入排队</button>
         
    </form>
    Page({
      phoneReady: null,
      formReady: null,
      data: {
        btnLoading: false,
      },
      onLoad() {
        this.resetPostPromise();
      },
      formSubmit: function (e) {
     
        console.log('formsubmit', e);
        this.setData({ btnLoading: true, })
        this.formReady.resolve({ form_id: e.detail.formId, code: e.detail.value.code });
      },
      getPhoneNumber(e) {
        if (e.detail.iv) {
          console.log('getPhoneNumber', e)
          postPhoneNumber(e.detail).then(user => {
            console.log('Phone post success.');
            this.phoneReady.resolve({});
          }, this.phoneReady.reject);
        } else { // 用户拒绝授权
     
          this.phoneReady.reject();
          showToast("手机授权失败");
        }
      },
       
      resetPostPromise() {
        const phoneReady = new Promise((resolve, reject) => {
          this.phoneReady = {resolve, reject};
        });
        const formReady = new Promise((resolve, reject) => {
          this.formReady = { resolve, reject };
        });
        Promise.all([phoneReady, formReady]).then(res => {
           // todo something
        }).finally(() => {
          this.setData({ btnLoading: false, });
          console.log('resetPostPromise');
          this.resetPostPromise();
        })
      },
      bindinput(e) {
        const name = e.currentTarget.dataset.name;
        this.setData({
          [name]: e.detail.value
        })
      }
    })


    2019-07-04
    有用
    回复
  • 祺爸💎
    祺爸💎
    2019-06-27

    想得美

    2019-06-27
    有用
    回复
登录 后发表内容