帮忙看下这个代码写的有问题吗?在开发者工具上面正常,发布后在手机上就出错不会请求接口

发布于 7 年前作者 lei9812237 次浏览最后编辑 7 年前来自 ask
// pages/login/login.js
const app = getApp()
const conf = require('../../lib/js/config.js');
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    mechanism_id: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (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,
          hasUserInfo: true
        })
      } 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,
            hasUserInfo: true
          })
        }
      } else {
        
        console.log(3333333)
        // 在没有 open-type=getUserInfo 版本的兼容处理
        wx.getUserInfo({
          success: res => {
            app.globalData.userInfo = res.userInfo
            this.setData({
              userInfo: res.userInfo,
              hasUserInfo: true
            })
          }
        })
      }
    }
    if(options.id){
      this.setData({
        mechanism_id: options.id
      })
    }
  },
  getUserInfo: function(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,
                  duration: 1000,
                })
              }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,
                        hasUserInfo: true,
                        userInfo: e.detail.userInfo
                      })
                      wx.navigateBack({
                        delta: 0,
                      })
                    }
                  },
                  function(res){}
                )
              }
              
            },
            function (res) {}
          )
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
1 回复
yhu
yhu1 楼5 年前

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