feat: 登录联调

This commit is contained in:
zc 2025-11-29 23:11:37 +08:00
parent 9740ed4fc3
commit 17822c9113
7 changed files with 81 additions and 122 deletions

View File

@ -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`,

View File

@ -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
);
}

View File

@ -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.codeuserInfo
// 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.codeuserInfo
// 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;

View File

@ -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 {
// [onLoadview]
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) {
//

View File

@ -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)

View File

@ -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'

View File

@ -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状态码-数据处理