Prechádzať zdrojové kódy

完成登录逻辑,封装api

eason 1 rok pred
rodič
commit
d9766d036c

+ 51 - 2
fore-end/API/appraise.js

@@ -1,7 +1,14 @@
+const { useParamsEaseFuncs } = require('XrFrame/xrFrameSystem');
 const { http } = require('../utils/util')
 
 const API = {
-    createCommentURL: '/appraise/create',
+	createCommentURL: '/appraise/create',
+	loginURL: '/user/login',
+	getUserInfoURL: '/user/info',
+	registerURL: '/user/register',
+	modifyURL: '/user/modify',
+
+	
     //放接口URL
 }
 
@@ -35,9 +42,51 @@ function getCommentList(params) {
 // function deleteComment() {
 
 // }
+function login(params) {
+	return http(
+		API.loginURL,
+		{
+			method:'POST',
+			params: params,
+		}
+	);
+}
+
+function getUserInfo(params) {
+	return http(
+		API.getUserInfoURL,
+		{
+			method: 'GET',
+			params: params,
+		}
+	)
+}
 
+function register(params) {
+	return  http(
+		API.registerURL,
+		{
+			method: 'POST',
+			params:params,
+		}
+	)
+}
+
+function modify(params) {
+	return http(
+		API.modifyURL,
+		{
+			method: 'POST',
+			params: params,
+		}
+	)
+}
 
 module.exports = {
     createCommont,
-    getCommentList
+	getCommentList,
+	login,
+	getUserInfo,
+	register,
+	modify,
 }

+ 74 - 65
fore-end/app.js

@@ -1,4 +1,7 @@
 // app.js
+const {http} = require('./utils/util')
+const {login,getUserInfo} = require('./API/appraise')
+
 App({
   onLaunch() {
     // 展示本地存储能力
@@ -13,18 +16,24 @@ App({
 	}
 const ui_w = 360;
 	//把vid存到缓存
-	wx.setStorage({
-		key: 'vid',
-		data: '',
-		success: function() {
-			console.log('写入vid缓存成功')
-		},
-		fail: function() {
-			console.log('写入vid发生错误')
-		}
-	})
+	wx.setStorageSync('vid', '')
 // 获取屏幕的宽度
 var clientWidth = wx.getSystemInfoSync().windowWidth;
+var loginOption = {
+	
+	header :{
+		"content-type": 'application/x-www-form-urlencoded',
+	},
+	data:{
+		vid: wx.getStorageSync('vid')
+	},
+}
+
+var infoOption = {
+	header:{
+		"Authorization": wx.getStorageSync('token')
+	},
+}
 	//检测缓存是否有token
 	// console.log(wx.getStorageSync('token'));
 	if(wx.getStorageSync('token') == ''){
@@ -38,68 +47,68 @@ var clientWidth = wx.getSystemInfoSync().windowWidth;
 				//和微信接口服务校验
 			  console.log("code: "+res.code);
 			  var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxInfo.appid + '&secret=' + wxInfo.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code'
-			   wx.request({
-				url: url,
-				success(res){
-				  wx.setStorageSync('vid', res.data.openid);
-				  console.log(wx.getStorageSync('vid'));
-				  wx.getUserProfile({
-					  desc: '登录',
-					  
-					  success: (res) => {
-						  wx.setStorageSync('nickName', res.userInfo.nickuName);
-						  wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl);
-					  }
-					});
-				  //登录
-				  wx.request({
-					  url: 'http://192.168.31.29:8084/user/login',
-					  method:'POST',
-					  header: {
-						  'content-type': 'application/x-www-form-urlencoded'
-					  },
-					  data:{
-						  vid: wx.getStorageSync('vid')
-					  },
-					  
-					  success(res){
-						  console.log(res);
-						  
-						  // console.log(vid);
-						  console.log('vid: '+ wx.getStorageSync('vid'));
-						  if(res.data.code == 403){
-							  wx.navigateTo({
-								url: '/pages/register/register',
-							  })
-					  	}else if(res.data.code == 200){
-							wx.setStorageSync('token', res.data.data.token);
-							wx.setStorageSync('uid', res.data.data.Uid);
-						  }
-							  
-					  }
-				  })
+			  wx.request({
+				url:url,
+				success: (res) => {
+					console.log(res);
+					console.log(res.data.openid);
+					wx.setStorageSync('vid', res.data.openid);
+					console.log(wx.getStorageSync('vid'));
+					
+					login(loginOption).then(res => {
+						console.log(res);
+						wx.setStorageSync('token', res.token);
+						wx.setStorageSync('uid', res.uid);
+						wx.setStorageSync('avatarUrl', res.user_info.avatar);
+						wx.setStorageSync('nickName', res.user_info.name);
+					}).catch(err => {
+						wx.navigateTo({
+							url: '/pages/register/register',
+						})
+					})
 				}
 			  })
-			  
-			  
 			}
 		  })
 	}else{
-		wx.request({
-		  url: 'http://192.168.31.29:8084/user/info',
-		  header:{
-			  "Authorization": wx.getStorageSync('token')
-		  },
-		  success(res){
-			  console.log(res);
-			  if(res.data.code == 200){
-				  console.log('token有效');
-				  wx.setStorageSync('uid', res.data.data.Uid);
-			  }
-		  }
+		getUserInfo(infoOption).then(res => {
+			console.log(res);
+			console.log('token有效');
+			wx.setStorageSync('uid', res.uid);
+		}).catch(err => {
+			console.log(err);
+			console.log('token已失效,请重新登录');
+			wx.login({
+				success: (res) => {
+					//和微信接口服务校验
+				  console.log("code: "+res.code);
+				  var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxInfo.appid + '&secret=' + wxInfo.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code'
+				  wx.request({
+					url:url,
+					success: (res) => {
+						console.log(res);
+						console.log(res.data.openid);
+						wx.setStorageSync('vid', res.data.openid);
+						console.log(wx.getStorageSync('vid'));
+						login(loginOption).then(res => {
+							console.log(res);
+							wx.setStorageSync('token', res.token);
+							wx.setStorageSync('uid', res.user_info.uid);
+							wx.setStorageSync('avatarUrl', res.user_info.avatar);
+							wx.setStorageSync('nickName', res.user_info.name);
+						}).catch(err => {
+							wx.navigateTo({
+								url: '/pages/register/register',
+							})
+						})
+					}
+				  })
+				}
+			  })
+			
 		})
-	}
 
+	}
     
   },
   globalData: {

+ 83 - 61
fore-end/pages/register/register.js

@@ -3,7 +3,27 @@ const app = getApp()
 
 const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
 const {upLoad} = require('../../utils/util');
+const {chooseImage} = require('../../utils/util');
+const {register, login} = require('../../API/appraise');
 var clientWidth = wx.getSystemInfoSync().windowWidth;
+var registerOption = {
+	data: {
+		avatar: avatar,
+		name: name,
+		vid: wx.getStorageSync('vid'),
+		phone: '',
+		sign: '',
+	},
+}
+var loginOption = {
+	
+	header :{
+		"content-type": 'application/x-www-form-urlencoded',
+	},
+	data:{
+		vid: wx.getStorageSync('vid')
+	},
+}
 Page({
 
     /**
@@ -82,6 +102,7 @@ Page({
 		  })
 		})
 	  },
+	//微信提供的获取头像和昵称
 	//   onChooseAvatar(e) {
 	// 	const { avatarUrl } = e.detail 
 	// 	this.setData({
@@ -89,14 +110,17 @@ Page({
 	// 	})
 	//   },
 	onChooseAvatar1(){
-		wx.chooseImage({
-			count: 1,//可选择图片数量
-			sizeType: ['compressed'],//压缩图片
-			sourceType: ['album','camera'],//来源
-			success: (res) => {
-				this.uploadImage(res.tempFilePaths[0]);
-			}
-		  })
+		// wx.chooseImage({
+		// 	count: 1,//可选择图片数量
+		// 	sizeType: ['compressed'],//压缩图片
+		// 	sourceType: ['album','camera'],//来源
+		// 	success: (res) => {
+		// 		this.uploadImage(res.tempFilePaths[0]);
+		// 	}
+		//   })
+		chooseImage().then((res)=>{
+			this.uploadImage(res.tempFilePaths[0]);
+		})
 	},
 	uploadImage(imagePath){
 		upLoad( imagePath ).then((res) => {
@@ -104,69 +128,67 @@ Page({
 			this.setData({
 				avatarUrl: res.Url
 			});
-			wx.setStorage({
-				key: 'avatarUrl',
-				data: res.Url,
-				success: function() {
-					console.log('写入avatarUrl缓存成功')
-				},
-				fail:function() {
-					console.log('写入avatarUrl发生错误')
-				}
-			});
+			wx.setStorageSync('avatarUrl', res.Url);
 		})
 	},
 	onInputUsername:function(e){
 		console.log(e);
-		wx.setStorage({
-			key: 'nickName',
-			data: e.detail.value,
-			success: function() {
-				console.log('写入nickName缓存成功')
-			},
-			fail:function() {
-				console.log('写入nickName发生错误')
-			}
-		})
+		wx.setStorageSync('nickName', e.detail.value);
 	},
 	  register(){
 		var avatar = wx.getStorageSync('avatarUrl');
 		var name = wx.getStorageSync('nickName');
 		console.log(avatar);
 		console.log(name);
-		wx.request({
-			url: 'http://192.168.31.29:8084/user/register',
-			method: 'POST',
-			data: {
-				avatar: avatar,
-				name: name,
-				vid: wx.getStorageSync('vid'),
-				phone: '',
-				sign: '',
-			},
-			success(res){
-			  console.log(res);
-
-			  wx.request({
-				url: 'http://192.168.31.29:8084/user/login',
-				method: 'POST',
-				header: {
-					'content-type': 'application/x-www-form-urlencoded'
-				},
-				data:{
-					vid: wx.getStorageSync('vid')
-				},
+		// wx.request({
+		// 	url: 'http://192.168.31.28:8084/user/register',
+		// 	method: 'POST',
+		// 	data: {
+		// 		avatar: avatar,
+		// 		name: name,
+		// 		vid: wx.getStorageSync('vid'),
+		// 		phone: '',
+		// 		sign: '',
+		// 	},
+		// 	success(res){
+		// 	  console.log(res);
+
+		// 	  wx.request({
+		// 		url: 'http://192.168.31.28:8084/user/login',
+		// 		method: 'POST',
+		// 		header: {
+		// 			'content-type': 'application/x-www-form-urlencoded'
+		// 		},
+		// 		data:{
+		// 			vid: wx.getStorageSync('vid')
+		// 		},
 				
-				success(res){
-					console.log(res);
-					wx.setStorageSync('token', res.data.data.token);
-					wx.setStorageSync('uid', res.data.data.Uid);
-					wx.switchTab({
-					  url: '/pages/home/home',
-					})
-				}
-			  })
-			}
-		  })
+		// 		success(res){
+		// 			console.log(res);
+		// 			console.log(res.data.data.user_info.uid);
+		// 			wx.setStorageSync('token', res.data.data.token);
+		// 			wx.setStorageSync('uid', res.data.data.user_info.uid);
+		// 			wx.setStorageSync('avatarUrl', res.data.data.user_info.avatar);
+		// 			wx.setStorageSync('nickName', res.data.data.user_info.name);
+		// 			wx.switchTab({
+		// 			  url: '/pages/home/home',
+		// 			})
+		// 		}
+		// 	  })
+		// 	}
+		//   })
+		register(registerOption).then((res)=>{
+			console.log(res);
+			login(loginOption).then((res)=>{
+				console.log(res);
+				wx.setStorageSync('token', res.data.data.token);
+				wx.setStorageSync('uid', res.data.data.user_info.uid);
+				wx.setStorageSync('avatarUrl', res.data.data.user_info.avatar);
+				wx.setStorageSync('nickName', res.data.data.user_info.name);
+				wx.switchTab({
+				  url: '/pages/home/home',
+				})
+			})
+		})
 	  }
 	})

+ 19 - 2
fore-end/pages/user/user.js

@@ -1,5 +1,10 @@
 // pages/user/user.js
-
+const {http} = require("../../utils/util")
+var infoOption = {
+	header:{
+		"Authorization": wx.getStorageSync('token')
+	},
+}
 Page({
 
     /**
@@ -16,7 +21,19 @@ Page({
      * 生命周期函数--监听页面加载
      */
     onLoad(options) {
-
+		this.setData({
+			avatarUrl: wx.getStorageSync('avatarUrl'),
+			nickName: wx.getStorageSync('nickName'),
+		})
+		// http('/user/info',infoOption).then(res => {
+		// 	console.log(res);
+		// 	this.setData({
+		// 		avatarUrl: 	res.avatar,
+		// 		nickName: res.name,
+		// 		personalSignatrue: res.sign,
+		// 	})
+		// })
+			
     },
 
     /**

+ 2 - 2
fore-end/pages/user/user.wxml

@@ -2,11 +2,11 @@
 <van-cell-group inset>
 	<view class="pages-user">
 		<view class="pages-user-profile" bindtap="getUserProfile">
-			<image class="pages-user-profile-1" src="{{avatarUrl}}" style="width: 244rpx; height: 238rpx; display: inline-block; box-sizing: border-box; left: 10rpx; top: 13rpx; position: relative"></image>
+			<image class="pages-user-profile-1" src="{{avatarUrl}}" style="width: 236rpx; height: 232rpx; display: inline-block; box-sizing: border-box; left: 13rpx; top: 15rpx; position: relative"></image>
 		</view>
 		<view class="pages-user-username">
 			<view class="pages-user-username-1" style="font-size: {{fontSize}};">{{nickName}}</view>
-			<view class="pages-user-username-2">wxID:{{userWxID}}</view>
+			<!-- <view class="pages-user-username-2">wxID:{{userWxID}}</view> -->
 			<view class="pages-user-username-4">
 				<!-- <van-icon name="/assets/tabBar_img/修改.png"></van-icon> -->
 				<view class="pages-user-username-4-title">个性签名:</view>

+ 65 - 101
fore-end/pages/userchange/userchange.js

@@ -1,39 +1,38 @@
 // pages/userchange/userchange.js
-const {upLoad} = require('../../utils/util');
+const {upLoad,chooseImage} = require('../../utils/util');
+const {modify} = require('../../API/appraise');
+var avatar = wx.getStorageSync('changeAvatarUrl');
+var nickName = wx.getStorageSync('changeNickName');
+var phoneNumber = wx.getStorageSync('phoneNumber');
+var personalSignatrue = wx.getStorageSync('personalSignatrue');
+var uid = wx.getStorageSync('uid');
+var modifyOption = {
+	data: {
+		"avatar": avatar,
+		"name": nickName,
+		"phone": phoneNumber,
+		"sign": personalSignatrue,
+		"uid": uid,
+	},
+}
 Page({
 
     /**
      * 页面的初始数据
      */
     data: {
-		avatar:'',
-		nickName:'',
+		changeAvatarUrl:'',
+		changeNickName:'',
 		phoneNumber:'',
 		password:'',
-		personalSignatrue:'编辑个性签名,展示我的独特态度'
+		personalSignatrue:''
     },
 
     /**
      * 生命周期函数--监听页面加载
      */
     onLoad(options) {
-		var that = this;
-		wx.getStorage({
-			key: 'nickName',
-			success (res) {
-			  that.setData({
-				nickName:res.data
-			  })
-			}
-		  })
-		  wx.getStorage({
-			key: 'avatarUrl',
-			success (res) {
-			  that.setData({
-				avatarUrl:res.data
-			  })
-			}
-		  })
+		
     },
 
     /**
@@ -86,108 +85,73 @@ Page({
 	},
 	
 	chooseImage() {
-		wx.chooseImage({
-			count: 1,//可选择图片数量
-			sizeType: ['compressed'],//压缩图片
-			sourceType: ['album','camera'],//来源
-			success: (res) => {
-				this.uploadImage(res.tempFilePaths[0]);
-			}
-		  })
+		// wx.chooseImage({
+		// 	count: 1,//可选择图片数量
+		// 	sizeType: ['compressed'],//压缩图片
+		// 	sourceType: ['album','camera'],//来源
+		// 	success: (res) => {
+		// 		this.uploadImage(res.tempFilePaths[0]);
+		// 	}
+		//   })
+		chooseImage().then((res)=>{
+			this.uploadImage(res.tempFilePaths[0]);
+		})
 	},
 	uploadImage(imagePath){
 		upLoad( imagePath ).then((res) => {
 			console.log(res);
 			this.setData({
-				avatar: res.Url
-			});
-			wx.setStorage({
-				key: 'avatar',
-				data: res.Url,
-				success: function() {
-					console.log('写入avatar缓存成功')
-				},
-				fail:function() {
-					console.log('写入avatar发生错误')
-				}
+				changeAvatarUrl: res.Url
 			});
+			wx.setStorageSync('changeAvatarUrl', res.Url);
 		})
 	},
 
 	onChangeUsername:function(e){
-		wx.setStorage({
-			key: 'nickName',
-			data: e.detail,
-			success: function() {
-				console.log('写入nickName缓存成功')
-			},
-			fail:function() {
-				console.log('写入nickName发生错误')
-			}
-		})
+		wx.setStorageSync('changeNickName', e.detail);
 	},
 
 	onChangePhonenumber:function(e){
-		wx.setStorage({
-			key: 'phoneNumber',
-			data: e.detail,
-			success: function() {
-				console.log('写入phoneNumber缓存成功')
-			},
-			fail:function() {
-				console.log('写入phoneNumber发生错误')
-			}
-		})
+		wx.setStorageSync('phoneNumber', e.detail);
 	},
 
 	onChangePassword: function(e) {
-		wx.setStorage({
-			key: 'password',
-			data: e.detail,
-			success: function() {
-				console.log('写入password缓存成功')
-			},
-			fail:function() {
-				console.log('写入password发生错误')
-			}
-		})
+		wx.setStorageSync('password', e.detail);
 	},
 
 	onChangePersonalSignatrue: function(e) {
-		wx.setStorage({
-			key: 'personalSignatrue',
-			data: e.detail,
-			success: function() {
-				console.log('写入personalSignatrue缓存成功')
-			},
-			fail:function() {
-				console.log('写入personalSignatrue发生错误')
-			}
-		})
+		wx.setStorageSync('personalSignatrue', e.detail);
 	},
 	
 	changeInfo:function(){
-
-		var avatar = wx.getStorageSync('avatar');
-		var nickName = wx.getStorageSync('nickName');
-		var phoneNumber = wx.getStorageSync('phoneNumber');
-		var personalSignatrue = wx.getStorageSync('personalSignatrue');
-		var uid = wx.getStorageSync('uid');
 		console.log(uid);
-		
-		wx.request({
-		  url: 'http://192.168.31.29:8084/user/modify',
-		  method: 'POST',
-		  data: {
-			  "avatar": avatar,
-			  "name": nickName,
-			  "phone": phoneNumber,
-			  "uid": uid,
-		  },
-		  success (res) {
-			  console.log(res)
-		  }
-		}),
-		wx.setStorageSync('avatar', '');
+		// wx.request({
+		//   url: 'http://192.168.31.28:8084',
+		//   method: 'POST',
+		//   data: {
+		// 	  "avatar": avatar,
+		// 	  "name": nickName,
+		// 	  "phone": phoneNumber,
+		// 	  "sign": personalSignatrue,
+		// 	  "uid": uid,
+		//   },
+		//   success (res) {
+		// 	  console.log(res);
+
+		// 	  wx.switchTab({
+		// 		url: '/pages/user/user',
+		// 	  })
+		//   }
+		// }),
+		modify(modifyOption).then((res)=>{
+			console.log(res);
+			wx.switchTab({
+			  url: '/pages/user/user',
+			})
+		})
+		wx.setStorageSync('changeAvatarUrl', '');
+		wx.setStorageSync('changeNickName', '');
+		wx.setStorageSync('phoneNumber', '');
+		wx.setStorageSync('personalSignatrue', '');
 	}
 })

+ 2 - 2
fore-end/pages/userchange/userchange.wxml

@@ -1,8 +1,8 @@
 <!--pages/userchange/index.wxml-->
 <view class="pages">
-	<van-image round fit="cover" src="{{avatar}}" class="pages-userchange-profile" bindtap="chooseImage" bindinput="onChangePhoto"></van-image>
+	<van-image round fit="cover" src="{{changeAvatarUrl}}" class="pages-userchange-profile" bindtap="chooseImage" bindinput="onChangePhoto"></van-image>
 	<van-cell-group>
-		<van-field label="昵称"  class="pages-userchange-username" custom-style="background: rgba(56, 56, 56, 0.17);border-radius: 15px;width:320px;margin-bottom:33px;" title-width="60px" value="{{nickName}}" placeholder="请输入昵称" adjust-position bindinput="onChangeUsername"></van-field>
+		<van-field label="昵称"  class="pages-userchange-username" custom-style="background: rgba(56, 56, 56, 0.17);border-radius: 15px;width:320px;margin-bottom:33px;" title-width="60px" value="{{changeNickName}}" placeholder="请输入昵称" adjust-position bindinput="onChangeUsername"></van-field>
 		<van-field label="电话" type="number" class="pages-userchange-username" custom-style="background: rgba(56, 56, 56, 0.17);border-radius: 15px;width:320px;margin-bottom:33px;" title-width="60px" value="{{phoneNumber}}" placeholder="请输入电话号码" adjust-position bindinput="onChangePhonenumber"></van-field>
 		<!-- <van-field label="密码" class="pages-userchange-username" custom-style="background: rgba(56, 56, 56, 0.17);border-radius: 15px;width:320px;margin-bottom:33px;" title-width="60px" value="{{password}}" placeholder="请输入密码" adjust-position password="{{true}}" bindinput="onChangePassword"></van-field> -->
 		<van-field label="个性签名" class="pages-userchange-username" custom-style="background: rgba(56, 56, 56, 0.17);border-radius: 15px;width:320px;margin-bottom:33px;" title-width="60px" value="{{personalSignatrue}}" placeholder="请输入个性签名" adjust-position bindinput="onChangePersonalSignatrue"></van-field>

+ 18 - 3
fore-end/utils/util.js

@@ -1,5 +1,5 @@
 import Toast from '@vant/weapp/toast/toast';
-const baseURL = "http://192.168.31.29:8084";//后台服务提供的地址
+const baseURL = "http://192.168.31.28:8084";//后台服务提供的地址
 const uploadUrl = "/pic/upload";
 const formatTime = date => {
   const year = date.getFullYear()
@@ -26,9 +26,11 @@ function http(url, option) {
 		  ...option,
 		  
           success(res) {
-			  const {data,code,message} = JSON.parse(res.data);
+			//   const {data,code,message} = JSON.parse(res.data);
+				const {data,code,message} = res.data;
 			  if(code == 200){
-				resolve(data);
+				// resolve(data);
+				resolve(res);
 			  }else{
 				Toast.fail(message);
 				reject();
@@ -71,10 +73,23 @@ function upLoad(filePath) {
 	)
 }
 
+function chooseImage(){
+	return new Promise((resolve,reject) => {
+		wx.chooseImage({
+			count: 1,//可选择图片数量
+			sizeType: ['compressed'],//压缩图片
+			sourceType: ['album','camera'],//来源
+			success: (res)=>{
+				resolve(res);
+			}
+		})
+	})
+}
 
 
 module.exports = {
     formatTime,
 	http,
 	upLoad,
+	chooseImage,
   }