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 }) return request.get(api.skuInfo, { goodsId, goodsSkuId, ...param })
} }
// 获取商品列表
export const apiGetCommodityList = (data) => { export const apiGetCommodityList = (data) => {
return httpRequest.post( return httpRequest.post(
`${baseUrl}/app/index/page/list`, `${baseUrl}/app/index/page/list`,

View File

@ -1,11 +1,13 @@
import request from '@/utils/request' import request from '@/utils/request'
import {httpRequest} from '@/utils/request/axios'
const baseUrl = 'https://api.1024api.com/api-interface'
// api地址 // api地址
const api = { const api = {
login: 'passport/login', login: 'passport/login',
loginMpWx: 'passport/loginMpWx', loginMpWx: 'passport/loginMpWx',
loginMpWxMobile: 'passport/loginMpWxMobile', loginMpWxMobile: 'passport/loginMpWxMobile',
isPersonalMpweixin: 'passport/isPersonalMpweixin',
} }
// 用户登录 (手机号+验证码) // 用户登录 (手机号+验证码)
@ -18,12 +20,15 @@ export function loginMpWx(data, option) {
return request.post(api.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) { export function loginMpWxMobile(data, option) {
return request.post(api.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> <template>
<view class="container"> <view class="container">
<!-- 登录后需要填写昵称头像 -->
<view v-if="isPersonal === true" class="personal"> <view v-if="isPersonal === true" class="personal">
<!-- 页面头部 --> <!-- 页面头部 -->
<view class="header"> <view class="header">
@ -35,7 +36,7 @@
<view v-if="isPersonal === false" class="authorize"> <view v-if="isPersonal === false" class="authorize">
<view class="store-info"> <view class="store-info">
<view class="header"> <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> </view>
<view class="auth-title">申请获取以下权限</view> <view class="auth-title">申请获取以下权限</view>
@ -55,9 +56,7 @@
import * as LoginApi from '@/api/login' import * as LoginApi from '@/api/login'
import * as UploadApi from '@/api/upload' import * as UploadApi from '@/api/upload'
import AvatarImage from '@/components/avatar-image' import AvatarImage from '@/components/avatar-image'
import { isEmpty } from '@/utils/util'
import * as Verify from '@/utils/verify' import * as Verify from '@/utils/verify'
import StoreModel from '@/common/model/Store'
export default { export default {
components: { components: {
@ -65,13 +64,8 @@
}, },
data() { data() {
return { return {
//
storeInfo: undefined,
// //
isPersonal: undefined, isPersonal: false,
// (code)
// openid
code: '',
// //
disabled: false, disabled: false,
// () // ()
@ -86,31 +80,37 @@
} }
}, },
created() {
//
this.getStoreInfo()
// ()
this.getIsPersonal()
},
methods: { methods: {
//
// handleLogin() {
getStoreInfo() { this.onAuthSuccess({})
StoreModel.storeInfo().then(storeInfo => this.storeInfo = storeInfo)
}, },
//
/** // :
* 请求后端是否需要填写昵称头像 (微信小程序端) // 1.codeuserInfo
* - 条件1: 后台开启了填写微信头像和昵称 // 2., ,
* - 条件2: 用户首次注册或者已注册但未填写过信息 // 3.,
*/ async onAuthSuccess(userInfo) {
async getIsPersonal() {
const app = this 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 // code
// https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
getCode() { getCode() {
@ -118,7 +118,6 @@
uni.login({ uni.login({
provider: 'weixin', provider: 'weixin',
success({ code }) { success({ code }) {
console.log('code', code)
resolve(code) resolve(code)
}, },
fail: reject fail: reject
@ -203,44 +202,6 @@
app.onAuthSuccess({ nickName: app.form.nickName, avatarId: app.form.avatarId }) 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 // oauth
// code, code() // code, code()
async onEmitSuccess(userInfo) { async onEmitSuccess(userInfo) {
@ -352,9 +313,6 @@
height: 190rpx; height: 190rpx;
border: 4rpx solid #fff; border: 4rpx solid #fff;
margin: 0 auto 0; margin: 0 auto 0;
border-radius: 50%;
overflow: hidden;
box-shadow: 2rpx 0 10rpx rgba(50, 50, 50, 0.3);
.image { .image {
display: block; display: block;

View File

@ -8,8 +8,6 @@
<script> <script>
import Main from './components/main' import Main from './components/main'
import MpWeixin from './components/mp-weixin' import MpWeixin from './components/mp-weixin'
import SettingKeyEnum from '@/common/enum/setting/Key'
import SettingModel from '@/common/model/Setting'
export default { export default {
components: { components: {
@ -21,8 +19,6 @@
return { return {
// [onLoadview] // [onLoadview]
isLoad: false, isLoad: false,
// ()
setting: {},
// //
isMpWeixinAuth: false, isMpWeixinAuth: false,
// //
@ -38,8 +34,6 @@
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
async onLoad(options) { async onLoad(options) {
//
await this.getRegisterSetting()
// //
await this.setShowUserInfo() await this.setShowUserInfo()
// //
@ -47,12 +41,6 @@
}, },
methods: { methods: {
// [--]
async getRegisterSetting() {
this.setting = await SettingModel.item(SettingKeyEnum.REGISTER.value, false)
},
/** /**
* 设置当前是否显示第三方授权登录 * 设置当前是否显示第三方授权登录
* - 条件1: 只有对应的客户端显示获取用户信息按钮, 例如微信小程序微信公众号 * - 条件1: 只有对应的客户端显示获取用户信息按钮, 例如微信小程序微信公众号
@ -63,10 +51,9 @@
// //
const isMpWeixin = app.platform === 'MP-WEIXIN' const isMpWeixin = app.platform === 'MP-WEIXIN'
// //
app.isMpWeixinAuth = isMpWeixin && Boolean(app.setting.isOauthMpweixin) app.isMpWeixinAuth = isMpWeixin
app.isMpWeixinMobile = isMpWeixin && Boolean(app.setting.isOauthMobileMpweixin) app.isMpWeixinMobile = isMpWeixin
}, },
// //
onGetUserInfoSuccess(result) { 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 storage from '@/utils/storage'
import * as LoginApi from '@/api/login' import * as LoginApi from '@/api/login'
// 登陆成功后执行 // 登陆成功后执行
const loginSuccess = (commit, { token, userId }) => { const loginSuccess = (commit, { token, buyerId, flag }) => {
// 过期时间30天 // 过期时间30天
const expiryTime = 30 * 86400 const expiryTime = 30 * 86400
// 保存tokne和userId到缓存 // 保存tokne和userId到缓存
storage.set(USER_ID, userId, expiryTime) storage.set(USER_ID, buyerId, expiryTime)
storage.set(ACCESS_TOKEN, token, expiryTime) storage.set(ACCESS_TOKEN, token, expiryTime)
storage.set(FLAG, flag, expiryTime)
// 记录到store全局变量 // 记录到store全局变量
commit('SET_TOKEN', token) commit('SET_TOKEN', token)
commit('SET_USER_ID', userId) commit('SET_USER_ID', buyerId)
commit('SET_FLAG', flag)
} }
const user = { const user = {
@ -19,7 +21,9 @@ const user = {
// 用户认证token // 用户认证token
token: '', token: '',
// 用户ID // 用户ID
userId: null userId: null,
// 用户标识
flag: null,
}, },
mutations: { mutations: {
@ -28,6 +32,9 @@ const user = {
}, },
SET_USER_ID: (state, value) => { SET_USER_ID: (state, value) => {
state.userId = value state.userId = value
},
SET_FLAG: (state, value) => {
state.flag = value
} }
}, },
@ -49,10 +56,12 @@ const user = {
// 微信小程序一键授权登录 (获取用户基本信息) // 微信小程序一键授权登录 (获取用户基本信息)
LoginMpWx({ commit }, data) { LoginMpWx({ commit }, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
LoginApi.loginMpWx({ form: data }, { isPrompt: true }) // LoginApi.loginMpWx({ form: data }, { isPrompt: true })
LoginApi.quickLogin(data)
.then(response => { .then(response => {
const result = response.data const result = response.data
loginSuccess(commit, result) uni.setStorageSync('user_info', result.buyer)
loginSuccess(commit, result.buyer)
resolve(response) resolve(response)
}) })
.catch(reject) .catch(reject)

View File

@ -1,4 +1,5 @@
export const ACCESS_TOKEN = 'AccessToken' export const ACCESS_TOKEN = 'AccessToken'
export const USER_ID = 'userId' export const USER_ID = 'userId'
export const FLAG = 'flag'
export const PLATFORM = 'platform' export const PLATFORM = 'platform'
export const APP_THEME = 'appTheme' export const APP_THEME = 'appTheme'

View File

@ -38,7 +38,7 @@ class HttpRequest {
msg = "服务器开小差了~"; msg = "服务器开小差了~";
} }
!requestConfig.noShowMsg && !requestConfig.noShowMsg &&
wx.showToast({ uni.showToast({
title: `${msg},错误码:${statusCode}`, title: `${msg},错误码:${statusCode}`,
icon: "none", icon: "none",
}); });
@ -52,7 +52,7 @@ class HttpRequest {
msg = "请求超时"; msg = "请求超时";
} }
!requestConfig.noShowMsg && !requestConfig.noShowMsg &&
wx.showToast({ uni.showToast({
title: msg, title: msg,
icon: "none", icon: "none",
}); });
@ -62,14 +62,13 @@ class HttpRequest {
// 服务器接口请求 // 服务器接口请求
request(requestConfig) { request(requestConfig) {
const _this = this; const _this = this;
const wxSystemInfo = wx.getSystemInfoSync(); const user_info = uni.getStorageSync("user_info") || {};
const user_info = wx.getStorageSync("user_info") || {};
const reqData = Object.assign(requestConfig.data, { const reqData = Object.assign(requestConfig.data, {
buyerId: user_info.buyerId, buyerId: user_info.buyerId,
flag: user_info.flag, flag: user_info.flag,
}); });
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
wx.request({ uni.request({
method: requestConfig.method, method: requestConfig.method,
url: `${requestConfig.url}`, url: `${requestConfig.url}`,
data: reqData, data: reqData,
@ -81,20 +80,20 @@ class HttpRequest {
/** 接口请求成功*/ /** 接口请求成功*/
if (code == 200) { if (code == 200) {
if (["1003", "1004", "1005", "1006"].includes(data.code)) { if (["1003", "1004", "1005", "1006"].includes(data.code)) {
wx.showModal({ uni.showModal({
content: data.msg, content: data.msg,
success(res) { success(res) {
if (res.confirm) { if (res.confirm) {
wx.removeStorageSync("user_info"); uni.removeStorageSync("user_info");
wx.navigateTo({ url: "/pages/login/index" }); uni.navigateTo({ url: "/pages/login/index" });
} else if (res.cancel) { } else if (res.cancel) {
try { try {
wx.removeStorageSync("user_info"); uni.removeStorageSync("user_info");
} catch (e) { } catch (e) {
console.log("登录失效catch", e); console.log("登录失效catch", e);
} }
wx.reLaunch({ url: "/pages/index/index" }); uni.reLaunch({ url: "/pages/index/index" });
wx.hideLoading(); uni.hideLoading();
} }
}, },
}); });
@ -104,24 +103,23 @@ class HttpRequest {
} else if (code === 401) { } else if (code === 401) {
// 未授权 // 未授权
!requestConfig.noShowMsg && !requestConfig.noShowMsg &&
wx uni.showModal({
.showModal({ title: "登录失效",
title: "登录失效", content: "登录失效,请重新登录",
content: "登录失效,请重新登录", success: (resModa) => {
})
.then((resModa) => {
if (resModa.confirm) { if (resModa.confirm) {
wx.removeStorageSync("user_info"); uni.removeStorageSync("user_info");
wx.navigateTo({ url: "/pages/login/index" }); uni.navigateTo({ url: "/pages/login/index" });
} else if (resModa.cancel) { } else if (resModa.cancel) {
try { try {
wx.removeStorageSync("user_info"); uni.removeStorageSync("user_info");
wx.reLaunch({ url: "/pages/index/index" }); uni.reLaunch({ url: "/pages/index/index" });
} catch (e) { } catch (e) {
console.log("登录失效catch", e); console.log("登录失效catch", e);
} }
} }
}); },
});
reject({ code, msg: "未登录", data: data }); reject({ code, msg: "未登录", data: data });
} else { } else {
//非200及401状态码-数据处理 //非200及401状态码-数据处理