收藏
回答

重写app.js,开发工具上可以正常的调用接口,真机的预览就不行也不报什么错?

我参考别的写法把app.js自己重写了下,开发工具上可以正常的调用接口,然后放在真机的预览就不行也不报什么错!请大家帮忙解释下代码如下:

/***
 * 加载的模块请在这里配置
 * @type {*[]}
 */
let modules = [{
  name: 'const',
  file: './core/const.js',
}, {
    name: 'api',
    file: './core/api.js',
}, {
    name: 'request',
    file: './core/request.js',
  }
];
 
/***
 * App对象配置(args参数配置)
 * @type {{onLaunch: args.onLaunch, onShow: args.onShow}}
 */
let args = {
  // 第一次进入在触发
  onLaunch:function() {
    this.getOpenid();
  },
  // 每次进来都会触发
  onShow(e) {
     
  },
};
 
for (let i in modules) {
  args[modules[i].name] = require('' + modules[i].file);
}
 
// 示例获取用户openid
args.getOpenid = function() {
  console.log("openid")
  wx.login({
    success: res => {
      this.request(this.api.user.getOpenid, {
        code: res.code,
      }, "GET", res => {
        if (res.status == 1) {
          this.const.OPEN_ID = res.result.open_id;
          this.const.UNION_ID = res.result.union_id;
          this.getUserid();
        }
      });
    }
  });
};
// 获取uid
args.getUserid = function (){
  wx.getSetting({
    success: res => {
      if (res.authSetting['scope.userInfo']) {
        wx.getUserInfo({
          success: res => {
            this.const.USER_INFO = res.userInfo;
            this.request(this.api.user.getUserid, {
              open_id: this.const.OPEN_ID,
              union_id: this.const.UNION_ID,
              avatarUrl: this.const.USER_INFO.avatarUrl,
              nickName: this.const.USER_INFO.nickName
            }, "GET" ,res => {
              if (res.status == 1) {
                this.const.USER_ID = res.user_id;
                console.log("用户ID:" + this.const.USER_ID);
              }
            });
          }
        })
      }
    }
  })
}
 
let app = App(args);


最后一次编辑于  08-14  (未经腾讯允许,不得转载)
回答关注问题邀请回答
收藏
问题标签