Browse Source

冲突

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

46
models/auth.js

@ -1,7 +1,6 @@
import { HTTP, Method, HTTPConst } from '../utils/http.js' import { HTTP, Method, HTTPConst } from '../utils/http.js'
const AuthConst = { const AuthConst = {
token_url: '/api/user/getToken', // 获取token
check_register_url: '/api/miniuser/isRegister', check_register_url: '/api/miniuser/isRegister',
mobile_code_url: '/api/miniuser/getSmsCode', // 获取验证码 mobile_code_url: '/api/miniuser/getSmsCode', // 获取验证码
verify_update_phone_url: '/api/miniuser/updateMiniUser' // mobile 验证 verify_update_phone_url: '/api/miniuser/updateMiniUser' // mobile 验证
@ -42,10 +41,7 @@ class AuthModel extends HTTP {
url: AuthConst.verify_update_phone_url, url: AuthConst.verify_update_phone_url,
method: Method.POST, method: Method.POST,
data: { data: {
isRegister: 1, code: res
phone: phone,
realName: name,
verificationCode: vscode,
}, },
success: (data) => { success: (data) => {
console.log(data) console.log(data)
@ -62,46 +58,6 @@ class AuthModel extends HTTP {
} }
this.request(params) 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 } export { AuthModel }

11
pages/home/index.js

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

1
pages/home/index.json

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

24
pages/user/myActivity/index.js

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

64
pages/user/myIdea/index.js

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

52
utils/http.js

@ -5,7 +5,9 @@ const Method = {
} }
// 定义一些通用常量 // 定义一些通用常量
const HTTPConst = { const HTTPConst = {
TokenStoreKey: 'ETokenKey' TokenStoreKey: 'ETokenKey',
UserInfoStoreKey: 'EUserInfo',
TokenURL: '/api/miniuser/getToken'
} }
class HTTP { class HTTP {
constructor() { constructor() {
@ -21,7 +23,8 @@ class HTTP {
} }
// token 拼接到 请求体中 // token 拼接到 请求体中
let data = params.data let data = params.data
console.log(data) console.log('请求参数:')
console.log({token: token, ...data})
wx.request({ wx.request({
url: url, url: url,
data: data, data: data,
@ -32,12 +35,20 @@ class HTTP {
method: params.method, method: params.method,
dataType: 'json', dataType: 'json',
success: function (res) { success: function (res) {
console.log(res) console.log(res.data)
let code = res.statusCode.toString() let code = res.statusCode.toString()
let startCode = code.charAt(0) let startCode = code.charAt(0)
if (startCode == '2') { if (startCode == '2') {
if (res.data.code == 200 || res.data.code == 0) { if (res.data.code == 200 || res.data.code == 0) {
params.success && params.success(res.data) 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 { } else {
console.log('数据请求失败') console.log('数据请求失败')
wx.showToast({ 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 } export { HTTP, Method, HTTPConst }

Loading…
Cancel
Save