帮忙看下这个代码写的有问题吗?在开发者工具上面正常,发布后在手机上就出错不会请求接口
发布于 6 年前 作者 lei98 12160 次浏览 来自 问答
// pages/login/login.js
const app = getApp()
const conf = require('../../lib/js/config.js');
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfofalse,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    mechanism_id''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoadfunction (options{
    if(wx.getStorageSync('openId') != ''){
      if (app.globalData.userInfo) {
        wx.switchTab({
          url'../index/index',
        })
        console.log(app.globalData.userInfo,1111111)
        this.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfotrue
        })
      } else if (this.data.canIUse){
        
        console.log(this.data.canIUse, 22222222)
        // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        app.userInfoReadyCallback = res => {
          wx.switchTab({
            url'../index/index',
          })
          this.setData({
            userInfo: res.userInfo,
            hasUserInfotrue
          })
        }
      } else {
        
        console.log(3333333)
        // 在没有 open-type=getUserInfo 版本的兼容处理
        wx.getUserInfo({
          successres => {
            app.globalData.userInfo = res.userInfo
            this.setData({
              userInfo: res.userInfo,
              hasUserInfotrue
            })
          }
        })
      }
    }
    if(options.id){
      this.setData({
        mechanism_id: options.id
      })
    }
  },
  getUserInfofunction(e{
    app.globalData.userInfo = e.detail.userInfo;
    var that = this;
    wx.login({ 
      success:resp=>{
        const code = resp.code;
        console.log(code);
        console.log(1111111111111111111111);
          //DoSomthing 携带code请求服务器
          util.request(
            conf.itUrl.getOpenid,
            {
              code: code,
              mechanism_id: that.data.mechanism_id,//扫码进来之后保存
            },
            'post',
            function (res{
              if(res.code == 0){
                console.log(res.msg)
                wx.showToast({
                  icon"error",
                  title: res.msg,
                  duration1000,
                })
              }else{
                console.log(2222222222222222222222)
                let openId = res.data;
                wx.setStorageSync('openId', res.data);
                util.request(
                  conf.itUrl.saveUserInfo,
                  {
                    nickname: e.detail.userInfo.nickName,
                    avatarurl: e.detail.userInfo.avatarUrl,
                    gender: e.detail.userInfo.gender,
                    city: e.detail.userInfo.city,
                    province: e.detail.userInfo.province,
                    country: e.detail.userInfo.country,
                    openid: openId,
                  },
                  'post',
                  function(result){
                    if(result.code == 0){
                      that.setData({
                        openid: result.data,
                        hasUserInfotrue,
                        userInfo: e.detail.userInfo
                      })
                      wx.navigateBack({
                        delta0,
                      })
                    }
                  },
                  function(res){}
                )
              }
              
            },
            function (res{}
          )
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReadyfunction () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShowfunction () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHidefunction () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnloadfunction () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefreshfunction () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottomfunction () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessagefunction () {

  }
})
1 回复

你有没有配置服务器的请求合法域名呢?

回到顶部