12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- //app.js
- App({
- onLaunch: function () {
- // 展示本地存储能力
- var logs = wx.getStorageSync('logs') || []
- logs.unshift(Date.now())
- wx.setStorageSync('logs', logs)
- var that = this;
- // 登录
- wx.showLoading({
- title: '加载中...',
- mask: true,
- })
- wx.login({
- success: res => {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- if (res.code) {
- that.globalData.code = res.code;
- //获取openId
- // 需要放到服务端去请求
- try {
- wx.request({
- url: that.globalData.contentpath + 'api/train/appAuth?js_code=' + res.code,
- method: 'GET',
- header: { 'content-type': 'application/json' },
- success: function (result) {
- console.log(result);
- if (result.statusCode === 200) {
- const res = result.data;
- that.globalData.openid = res.openid;
- that.globalData.userInfo = res.user;
- } else {
- wx.showToast({ title: '未请求到用户', icon: 'none', duration: 0, mask: true })
- }
- },
- fail: function (error) {
- wx.showToast({ title: '未请求到用户', icon: 'none', duration: 0, mask: true })
- console.info("获取用户openId失败");
- console.info(error);
- },
- complete() {
- wx.hideLoading()
- },
- })
- } catch (error) {
- wx.showToast({
- title: '用户信息无效',
- icon: "none"
- })
- }
- }
- }
- })
- },
- globalData: {
- userInfo: null,
- openid: null,
- unionid: null,
- contentpath: 'https://jytz.jilinjobs.cn/', //http://127.0.0.1:2001/
- cookie: null,
- code: null,
- }
- })
|