feat: 登录联调
This commit is contained in:
parent
9740ed4fc3
commit
17822c9113
@ -38,6 +38,7 @@ export const skuInfo = (goodsId, goodsSkuId, param) => {
|
||||
return request.get(api.skuInfo, { goodsId, goodsSkuId, ...param })
|
||||
}
|
||||
|
||||
// 获取商品列表
|
||||
export const apiGetCommodityList = (data) => {
|
||||
return httpRequest.post(
|
||||
`${baseUrl}/app/index/page/list`,
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
import request from '@/utils/request'
|
||||
import {httpRequest} from '@/utils/request/axios'
|
||||
const baseUrl = 'https://api.1024api.com/api-interface'
|
||||
|
||||
|
||||
// api地址
|
||||
const api = {
|
||||
login: 'passport/login',
|
||||
loginMpWx: 'passport/loginMpWx',
|
||||
loginMpWxMobile: 'passport/loginMpWxMobile',
|
||||
isPersonalMpweixin: 'passport/isPersonalMpweixin',
|
||||
}
|
||||
|
||||
// 用户登录 (手机号+验证码)
|
||||
@ -18,12 +20,15 @@ export function loginMpWx(data, option) {
|
||||
return request.post(api.loginMpWx, data, option)
|
||||
}
|
||||
|
||||
// 是否需要填写昵称头像 (微信小程序端)
|
||||
export function isPersonalMpweixin(data, option) {
|
||||
return request.post(api.isPersonalMpweixin, data, option)
|
||||
}
|
||||
|
||||
// 微信小程序快捷登录 (授权手机号)
|
||||
export function loginMpWxMobile(data, option) {
|
||||
return request.post(api.loginMpWxMobile, data, option)
|
||||
}
|
||||
|
||||
// 微信快捷登录
|
||||
export const quickLogin = (data) => {
|
||||
return httpRequest.post(
|
||||
`${baseUrl}/app/user/login`,
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<template>
|
||||
<view class="container">
|
||||
<!-- 登录后需要填写昵称头像 -->
|
||||
<view v-if="isPersonal === true" class="personal">
|
||||
<!-- 页面头部 -->
|
||||
<view class="header">
|
||||
@ -35,7 +36,7 @@
|
||||
<view v-if="isPersonal === false" class="authorize">
|
||||
<view class="store-info">
|
||||
<view class="header">
|
||||
<image class="image" :src="storeInfo && storeInfo.image_url ? storeInfo.image_url : '/static/default-logo.png'"></image>
|
||||
<image class="image" src="https://heyuimage.ihzhy.com/prd/202511/697446e044869c29.jpg??access_token=e01c8565446ad0448dfb6d9f69f3d562"></image>
|
||||
</view>
|
||||
</view>
|
||||
<view class="auth-title">申请获取以下权限</view>
|
||||
@ -55,9 +56,7 @@
|
||||
import * as LoginApi from '@/api/login'
|
||||
import * as UploadApi from '@/api/upload'
|
||||
import AvatarImage from '@/components/avatar-image'
|
||||
import { isEmpty } from '@/utils/util'
|
||||
import * as Verify from '@/utils/verify'
|
||||
import StoreModel from '@/common/model/Store'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
@ -65,13 +64,8 @@
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
// 商城基本信息
|
||||
storeInfo: undefined,
|
||||
// 是否需要填写用户昵称和头像
|
||||
isPersonal: undefined,
|
||||
// 微信小程序登录凭证 (code)
|
||||
// 提交到后端,用于换取openid
|
||||
code: '',
|
||||
isPersonal: false,
|
||||
// 按钮禁用
|
||||
disabled: false,
|
||||
// 头像路径 (用于显示)
|
||||
@ -86,31 +80,37 @@
|
||||
}
|
||||
},
|
||||
|
||||
created() {
|
||||
// 获取商城基本信息
|
||||
this.getStoreInfo()
|
||||
// 请求后端是否需要填写昵称头像 (微信小程序端)
|
||||
this.getIsPersonal()
|
||||
},
|
||||
|
||||
methods: {
|
||||
|
||||
// 获取商城基本信息
|
||||
getStoreInfo() {
|
||||
StoreModel.storeInfo().then(storeInfo => this.storeInfo = storeInfo)
|
||||
// 一键登录按钮点击事件
|
||||
handleLogin() {
|
||||
this.onAuthSuccess({})
|
||||
},
|
||||
|
||||
/**
|
||||
* 请求后端是否需要填写昵称头像 (微信小程序端)
|
||||
* - 条件1: 后台开启了填写微信头像和昵称
|
||||
* - 条件2: 用户首次注册或者已注册但未填写过信息
|
||||
*/
|
||||
async getIsPersonal() {
|
||||
// 授权成功事件
|
||||
// 这里分为两个逻辑:
|
||||
// 1.将code和userInfo提交到后端,如果存在该用户 则实现自动登录,无需再填写手机号
|
||||
// 2.如果不存在该用户, 则显示注册页面, 需填写手机号
|
||||
// 3.如果后端报错了, 则显示错误信息
|
||||
async onAuthSuccess(userInfo) {
|
||||
const app = this
|
||||
LoginApi.isPersonalMpweixin({ code: await app.getCode() })
|
||||
.then(result => app.isPersonal = result.data.isPersonalMpweixin)
|
||||
// 提交到后端
|
||||
const code = await app.getCode()
|
||||
store.dispatch('LoginMpWx', { code })
|
||||
.then(result => {
|
||||
// 一键登录成功
|
||||
app.$toast(result.message)
|
||||
// 相应全局事件订阅: 刷新上级页面数据
|
||||
uni.$emit('syncRefresh', true)
|
||||
// 跳转回原页面
|
||||
setTimeout(() => app.onNavigateBack(), 2000)
|
||||
})
|
||||
.catch(err => {
|
||||
const resultData = err.result.data
|
||||
// 判断还需绑定手机号
|
||||
if (resultData.isBindMobile) {
|
||||
app.onEmitSuccess(userInfo)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 获取code
|
||||
// https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
|
||||
getCode() {
|
||||
@ -118,7 +118,6 @@
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success({ code }) {
|
||||
console.log('code', code)
|
||||
resolve(code)
|
||||
},
|
||||
fail: reject
|
||||
@ -203,44 +202,6 @@
|
||||
app.onAuthSuccess({ nickName: app.form.nickName, avatarId: app.form.avatarId })
|
||||
},
|
||||
|
||||
// 一键登录按钮点击事件
|
||||
handleLogin() {
|
||||
// this.onAuthSuccess({ nickName: '微信用户', avatarUrl: '' })
|
||||
this.onAuthSuccess({})
|
||||
},
|
||||
|
||||
// 授权成功事件
|
||||
// 这里分为两个逻辑:
|
||||
// 1.将code和userInfo提交到后端,如果存在该用户 则实现自动登录,无需再填写手机号
|
||||
// 2.如果不存在该用户, 则显示注册页面, 需填写手机号
|
||||
// 3.如果后端报错了, 则显示错误信息
|
||||
async onAuthSuccess(userInfo) {
|
||||
const app = this
|
||||
// 提交到后端
|
||||
store.dispatch('LoginMpWx', {
|
||||
partyData: {
|
||||
code: await app.getCode(),
|
||||
oauth: 'MP-WEIXIN',
|
||||
userInfo
|
||||
}
|
||||
})
|
||||
.then(result => {
|
||||
// 一键登录成功
|
||||
app.$toast(result.message)
|
||||
// 相应全局事件订阅: 刷新上级页面数据
|
||||
uni.$emit('syncRefresh', true)
|
||||
// 跳转回原页面
|
||||
setTimeout(() => app.onNavigateBack(), 2000)
|
||||
})
|
||||
.catch(err => {
|
||||
const resultData = err.result.data
|
||||
// 判断还需绑定手机号
|
||||
if (resultData.isBindMobile) {
|
||||
app.onEmitSuccess(userInfo)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 将oauth提交给父级
|
||||
// 这里要重新获取code, 因为上一次获取的code不能复用(会报错)
|
||||
async onEmitSuccess(userInfo) {
|
||||
@ -352,9 +313,6 @@
|
||||
height: 190rpx;
|
||||
border: 4rpx solid #fff;
|
||||
margin: 0 auto 0;
|
||||
border-radius: 50%;
|
||||
overflow: hidden;
|
||||
box-shadow: 2rpx 0 10rpx rgba(50, 50, 50, 0.3);
|
||||
|
||||
.image {
|
||||
display: block;
|
||||
|
||||
@ -8,8 +8,6 @@
|
||||
<script>
|
||||
import Main from './components/main'
|
||||
import MpWeixin from './components/mp-weixin'
|
||||
import SettingKeyEnum from '@/common/enum/setting/Key'
|
||||
import SettingModel from '@/common/model/Setting'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
@ -21,8 +19,6 @@
|
||||
return {
|
||||
// 数据加载完成 [防止在微信小程序端onLoad和view渲染同步进行]
|
||||
isLoad: false,
|
||||
// 注册设置 (后台设置)
|
||||
setting: {},
|
||||
// 是否显示微信小程序授权登录
|
||||
isMpWeixinAuth: false,
|
||||
// 是否显示微信小程序端 一键授权手机号
|
||||
@ -38,8 +34,6 @@
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
async onLoad(options) {
|
||||
// 获取注册设置
|
||||
await this.getRegisterSetting()
|
||||
// 设置当前是否显示第三方授权登录
|
||||
await this.setShowUserInfo()
|
||||
// 数据加载完成
|
||||
@ -47,12 +41,6 @@
|
||||
},
|
||||
|
||||
methods: {
|
||||
|
||||
// 获取注册设置 [后台-客户端-注册设置]
|
||||
async getRegisterSetting() {
|
||||
this.setting = await SettingModel.item(SettingKeyEnum.REGISTER.value, false)
|
||||
},
|
||||
|
||||
/**
|
||||
* 设置当前是否显示第三方授权登录
|
||||
* - 条件1: 只有对应的客户端显示获取用户信息按钮, 例如微信小程序、微信公众号
|
||||
@ -63,10 +51,9 @@
|
||||
// 判断当前客户端是微信小程序
|
||||
const isMpWeixin = app.platform === 'MP-WEIXIN'
|
||||
// 判断是否显示第三方授权登录
|
||||
app.isMpWeixinAuth = isMpWeixin && Boolean(app.setting.isOauthMpweixin)
|
||||
app.isMpWeixinMobile = isMpWeixin && Boolean(app.setting.isOauthMobileMpweixin)
|
||||
app.isMpWeixinAuth = isMpWeixin
|
||||
app.isMpWeixinMobile = isMpWeixin
|
||||
},
|
||||
|
||||
// 获取到用户信息的回调函数
|
||||
onGetUserInfoSuccess(result) {
|
||||
// 记录第三方用户信息数据
|
||||
|
||||
@ -1,17 +1,19 @@
|
||||
import { ACCESS_TOKEN, USER_ID } from '@/store/mutation-types'
|
||||
import { ACCESS_TOKEN, USER_ID, FLAG } from '@/store/mutation-types'
|
||||
import storage from '@/utils/storage'
|
||||
import * as LoginApi from '@/api/login'
|
||||
|
||||
// 登陆成功后执行
|
||||
const loginSuccess = (commit, { token, userId }) => {
|
||||
const loginSuccess = (commit, { token, buyerId, flag }) => {
|
||||
// 过期时间30天
|
||||
const expiryTime = 30 * 86400
|
||||
// 保存tokne和userId到缓存
|
||||
storage.set(USER_ID, userId, expiryTime)
|
||||
storage.set(USER_ID, buyerId, expiryTime)
|
||||
storage.set(ACCESS_TOKEN, token, expiryTime)
|
||||
storage.set(FLAG, flag, expiryTime)
|
||||
// 记录到store全局变量
|
||||
commit('SET_TOKEN', token)
|
||||
commit('SET_USER_ID', userId)
|
||||
commit('SET_USER_ID', buyerId)
|
||||
commit('SET_FLAG', flag)
|
||||
}
|
||||
|
||||
const user = {
|
||||
@ -19,7 +21,9 @@ const user = {
|
||||
// 用户认证token
|
||||
token: '',
|
||||
// 用户ID
|
||||
userId: null
|
||||
userId: null,
|
||||
// 用户标识
|
||||
flag: null,
|
||||
},
|
||||
|
||||
mutations: {
|
||||
@ -28,6 +32,9 @@ const user = {
|
||||
},
|
||||
SET_USER_ID: (state, value) => {
|
||||
state.userId = value
|
||||
},
|
||||
SET_FLAG: (state, value) => {
|
||||
state.flag = value
|
||||
}
|
||||
},
|
||||
|
||||
@ -49,10 +56,12 @@ const user = {
|
||||
// 微信小程序一键授权登录 (获取用户基本信息)
|
||||
LoginMpWx({ commit }, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
LoginApi.loginMpWx({ form: data }, { isPrompt: true })
|
||||
// LoginApi.loginMpWx({ form: data }, { isPrompt: true })
|
||||
LoginApi.quickLogin(data)
|
||||
.then(response => {
|
||||
const result = response.data
|
||||
loginSuccess(commit, result)
|
||||
uni.setStorageSync('user_info', result.buyer)
|
||||
loginSuccess(commit, result.buyer)
|
||||
resolve(response)
|
||||
})
|
||||
.catch(reject)
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
export const ACCESS_TOKEN = 'AccessToken'
|
||||
export const USER_ID = 'userId'
|
||||
export const FLAG = 'flag'
|
||||
export const PLATFORM = 'platform'
|
||||
export const APP_THEME = 'appTheme'
|
||||
|
||||
@ -38,7 +38,7 @@ class HttpRequest {
|
||||
msg = "服务器开小差了~";
|
||||
}
|
||||
!requestConfig.noShowMsg &&
|
||||
wx.showToast({
|
||||
uni.showToast({
|
||||
title: `${msg},错误码:${statusCode}`,
|
||||
icon: "none",
|
||||
});
|
||||
@ -52,7 +52,7 @@ class HttpRequest {
|
||||
msg = "请求超时";
|
||||
}
|
||||
!requestConfig.noShowMsg &&
|
||||
wx.showToast({
|
||||
uni.showToast({
|
||||
title: msg,
|
||||
icon: "none",
|
||||
});
|
||||
@ -62,14 +62,13 @@ class HttpRequest {
|
||||
// 服务器接口请求
|
||||
request(requestConfig) {
|
||||
const _this = this;
|
||||
const wxSystemInfo = wx.getSystemInfoSync();
|
||||
const user_info = wx.getStorageSync("user_info") || {};
|
||||
const user_info = uni.getStorageSync("user_info") || {};
|
||||
const reqData = Object.assign(requestConfig.data, {
|
||||
buyerId: user_info.buyerId,
|
||||
flag: user_info.flag,
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
uni.request({
|
||||
method: requestConfig.method,
|
||||
url: `${requestConfig.url}`,
|
||||
data: reqData,
|
||||
@ -81,20 +80,20 @@ class HttpRequest {
|
||||
/** 接口请求成功*/
|
||||
if (code == 200) {
|
||||
if (["1003", "1004", "1005", "1006"].includes(data.code)) {
|
||||
wx.showModal({
|
||||
uni.showModal({
|
||||
content: data.msg,
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
wx.removeStorageSync("user_info");
|
||||
wx.navigateTo({ url: "/pages/login/index" });
|
||||
uni.removeStorageSync("user_info");
|
||||
uni.navigateTo({ url: "/pages/login/index" });
|
||||
} else if (res.cancel) {
|
||||
try {
|
||||
wx.removeStorageSync("user_info");
|
||||
uni.removeStorageSync("user_info");
|
||||
} catch (e) {
|
||||
console.log("登录失效catch:", e);
|
||||
}
|
||||
wx.reLaunch({ url: "/pages/index/index" });
|
||||
wx.hideLoading();
|
||||
uni.reLaunch({ url: "/pages/index/index" });
|
||||
uni.hideLoading();
|
||||
}
|
||||
},
|
||||
});
|
||||
@ -104,24 +103,23 @@ class HttpRequest {
|
||||
} else if (code === 401) {
|
||||
// 未授权
|
||||
!requestConfig.noShowMsg &&
|
||||
wx
|
||||
.showModal({
|
||||
title: "登录失效",
|
||||
content: "登录失效,请重新登录",
|
||||
})
|
||||
.then((resModa) => {
|
||||
uni.showModal({
|
||||
title: "登录失效",
|
||||
content: "登录失效,请重新登录",
|
||||
success: (resModa) => {
|
||||
if (resModa.confirm) {
|
||||
wx.removeStorageSync("user_info");
|
||||
wx.navigateTo({ url: "/pages/login/index" });
|
||||
uni.removeStorageSync("user_info");
|
||||
uni.navigateTo({ url: "/pages/login/index" });
|
||||
} else if (resModa.cancel) {
|
||||
try {
|
||||
wx.removeStorageSync("user_info");
|
||||
wx.reLaunch({ url: "/pages/index/index" });
|
||||
uni.removeStorageSync("user_info");
|
||||
uni.reLaunch({ url: "/pages/index/index" });
|
||||
} catch (e) {
|
||||
console.log("登录失效catch:", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
reject({ code, msg: "未登录", data: data });
|
||||
} else {
|
||||
//非200及401状态码-数据处理
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user