Browse Source

冲突

master
fanp 6 years ago
parent
commit
809ca2c33f
  1. 59
      app.js
  2. 46
      models/auth.js
  3. 5
      pages/home/index.js
  4. 1
      pages/home/index.json
  5. 24
      pages/user/myActivity/index.js
  6. 64
      pages/user/myIdea/index.js
  7. 52
      utils/http.js

59
app.js

@ -1,32 +1,39 @@
//app.js
import {HTTP} from '/utils/http.js'
const http = new HTTP()
App({
onLaunch: function () {
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
onLaunch: function (options) {
// auth.fetchAuthToken(res => {
// console.log(res)
// })
// // 获取用户信息
// wx.getSetting({
// success: res => {
// console.log(res)
// if (res.authSetting['scope.userInfo']) {
// // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
// // wx.getUserInfo({
// // success: res => {
// // // 可以将 res 发送给后台解码出 unionId
// // this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
// // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// // // 所以此处加入 callback 以防止这种情况
// // if (this.userInfoReadyCallback) {
// // this.userInfoReadyCallback(res)
// // }
// // }
// // })
// } else {
// wx.authorize({
// scope: 'scope.userInfo',
// success() {
// }
// })
// }
// }
// })
},
globalData: {
userInfo: null

46
models/auth.js

@ -1,7 +1,6 @@
import { HTTP, Method, HTTPConst } from '../utils/http.js'
const AuthConst = {
token_url: '/api/user/getToken', // 获取token
check_register_url: '/api/miniuser/isRegister',
mobile_code_url: '/api/miniuser/getSmsCode', // 获取验证码
verify_update_phone_url: '/api/miniuser/updateMiniUser' // mobile 验证
@ -42,10 +41,7 @@ class AuthModel extends HTTP {
url: AuthConst.verify_update_phone_url,
method: Method.POST,
data: {
isRegister: 1,
phone: phone,
realName: name,
verificationCode: vscode,
code: res
},
success: (data) => {
console.log(data)
@ -62,46 +58,6 @@ class AuthModel extends HTTP {
}
this.request(params)
}
// 获取 Token
fetchAuthToken(success) {
// 获取token
this._fetchWXCode().then(res => {
if (!res) return
let params = {
url: AuthConst.token_url,
method: Method.GET,
data: {
code: res
},
success: (data) => {
// 将获取到的token存放起来
let token = 'test token'
wx.setStorageSync(HTTPConst.TokenStoreKey, token)
success(token)
},
}
this.request(params)
})
}
// 获取微信code
_fetchWXCode() {
return new Promise(resolve => {
wx.login({
success(res) {
console.log('获取Code成功 ' + res.code)
if (res.code) {
resolve(res.code)
} else {
wx.showToast({
icon: "none",
title: '登录失败',
})
console.log('登录失败!' + res.errMsg)
}
}
})
})
}
}
export { AuthModel }

5
pages/home/index.js

@ -13,11 +13,12 @@ Page({
list: []
},
onLoad: function () {
console.log(dayjs().toNow())
console.log(dayjs().from(dayjs('1990')))
homeModel.fetchAuthToken(res => {
console.log('Home获取token成功后再请求数据')
this.fetchHomeList()
this.fetchHomeBanner()
this.fetchHomeHot()
})
},
/**
* 页面相关事件处理函数--监听用户下拉动作

1
pages/home/index.json

@ -1,6 +1,5 @@
{
"enablePullDownRefresh": true,
"backgroundColor": "#f7f7f7",
"backgroundTextStyle": "dark",
"usingComponents": {
"e-swiper": "/components/swiper/index",

24
pages/user/myActivity/index.js

@ -9,29 +9,7 @@ Page({
* 页面的初始数据
*/
data: {
list: [
{
articleId: '1',
title: "市北区举行国际航运贸易金融创新中心核心区产业建设",
articleImg: "asdsad",
userName: "用户名",
time: "刚刚",
},
{
articleId: "2",
title: "《大众日报》市北区举行国际航运贸易金融创新中心核心区产业建设",
articleImg: "",
userName: "用户名",
time: "1分钟前"
},
{
articleId: "3",
title: "青岛医疗人工智能科技创新中心落户市北",
articleImg: "asdsadad",
userName: "用户名",
time: "1分钟前"
}
],
list: [],
currentPage:1
},

64
pages/user/myIdea/index.js

@ -12,38 +12,38 @@ Page({
*/
data: {
list: [
{
ideaId: '1',
userIcon: "",
userName: "用户名",
time: "刚刚",
isGoodIdea: 1,
praiseNum: "30",
detail: {
title: "习近平主持政治局第十七次集体... 拷贝",
userIcon: "",
userName: "用户名",
time: "刚刚",
commentNum: "33",
text: "【内容以实际活动为准】市北区位于青岛市主城区地理位置的中心,西部濒临胶州湾,东部与崂山区为邻,北部与李沧区接壤,南部与市南区毗"
}
},
{
ideaId: "2",
userIcon: "",
userName: "用户名",
time: "1分钟前",
isGoodIdea: 0,
praiseNum: "30",
detail: {
title: "习近平主持政治局第十七次集体... 拷贝",
userIcon: "",
userName: "用户名",
time: "刚刚",
commentNum: "33",
text: "市北区位于青岛市主城区地理位置的中心,西部濒临胶州湾,东部与崂山区为邻,北部与李沧区接壤,南部与市南区毗连,东西最大距离11. 拷贝 2"
}
}
// {
// ideaId: '1',
// userIcon: "",
// userName: "用户名",
// time: "刚刚",
// isGoodIdea: 1,
// praiseNum: "30",
// detail: {
// title: "习近平主持政治局第十七次集体... 拷贝",
// userIcon: "",
// userName: "用户名",
// time: "刚刚",
// commentNum: "33",
// text: "【内容以实际活动为准】市北区位于青岛市主城区地理位置的中心,西部濒临胶州湾,东部与崂山区为邻,北部与李沧区接壤,南部与市南区毗"
// }
// },
// {
// ideaId: "2",
// userIcon: "",
// userName: "用户名",
// time: "1分钟前",
// isGoodIdea: 0,
// praiseNum: "30",
// detail: {
// title: "习近平主持政治局第十七次集体... 拷贝",
// userIcon: "",
// userName: "用户名",
// time: "刚刚",
// commentNum: "33",
// text: "市北区位于青岛市主城区地理位置的中心,西部濒临胶州湾,东部与崂山区为邻,北部与李沧区接壤,南部与市南区毗连,东西最大距离11. 拷贝 2"
// }
// }
],
currentPage: 1
},

52
utils/http.js

@ -5,7 +5,9 @@ const Method = {
}
// 定义一些通用常量
const HTTPConst = {
TokenStoreKey: 'ETokenKey'
TokenStoreKey: 'ETokenKey',
UserInfoStoreKey: 'EUserInfo',
TokenURL: '/api/miniuser/getToken'
}
class HTTP {
constructor() {
@ -21,7 +23,8 @@ class HTTP {
}
// token 拼接到 请求体中
let data = params.data
console.log(data)
console.log('请求参数:')
console.log({token: token, ...data})
wx.request({
url: url,
data: data,
@ -32,12 +35,20 @@ class HTTP {
method: params.method,
dataType: 'json',
success: function (res) {
console.log(res)
console.log(res.data)
let code = res.statusCode.toString()
let startCode = code.charAt(0)
if (startCode == '2') {
if (res.data.code == 200 || res.data.code == 0) {
params.success && params.success(res.data)
} else if (res.data.code == 302) {
// token 无效
this.fetchAuthToken(res => {
// 请求Token后,重新请求URL
if(res.code == 200) {
this.request(params)
}
})
} else {
console.log('数据请求失败')
wx.showToast({
@ -63,5 +74,40 @@ class HTTP {
},
})
}
// 获取 Token
fetchAuthToken(success) {
// 获取token
this._fetchWXCode().then(res => {
console.log('啦啦啦啦凉啊')
console.log(res)
if (!res) return
let params = {
url: HTTPConst.TokenURL,
method: Method.POST,
data: {
code: res
},
success: (data) => {
// 将获取到的token存放起来
const { token, userInfo } = data.result
wx.setStorageSync(HTTPConst.UserInfoStoreKey, userInfo)
wx.setStorageSync(HTTPConst.TokenStoreKey, token)
success(data)
},
}
this.request(params)
})
}
// 获取微信code
_fetchWXCode() {
return new Promise(resolve => {
wx.login({
success(res) {
console.log('获取Code成功 ' + res.code)
resolve(res.code)
}
})
})
}
}
export { HTTP, Method, HTTPConst }

Loading…
Cancel
Save