| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- import type { MemberVO } from '@/api/globals'
- import { defineStore } from 'pinia'
- import router from '@/router'
- interface userStroe {
- token: string
- /**
- * 用户登录信息
- */
- userInfo: MemberVO
- redirectName: string
- lat: number
- lng: number
- }
- export const useUserStore = defineStore('user', {
- state: (): userStroe => ({
- token: '',
- userInfo: {
- id: 0,
- channelId: 0,
- channelName: '',
- },
- redirectName: '',
- lat: 0,
- lng: 0,
- }),
- actions: {
- // H5 环境获取位置(使用 uni.getLocation,兼容微信浏览器)
- getLocationH5() {
- return new Promise((resolve, reject) => {
- uni.showLoading({ mask: true, title: '获取位置中...' })
- // if (navigator.geolocation) {
- // // 调用定位接口(enableHighAccuracy:是否高精度定位)
- // navigator.geolocation.getCurrentPosition(
- // // 定位成功回调(返回经纬度)
- // (position) => {
- // this.lat = Number(position.coords.latitude.toFixed(6))
- // this.lng = Number(position.coords.longitude.toFixed(6))
- // resolve(true)
- // uni.hideLoading()
- // },
- // // 定位失败回调(处理用户拒绝、设备无GPS等情况)
- // (error) => {
- // reject(error.code)
- // uni.hideLoading()
- // switch (error.code) {
- // case error.PERMISSION_DENIED:
- // console.log('用户拒绝了定位权限')
- // // 提示用户开启定位:引导打开手机系统定位 + 微信定位权限
- // useGlobalToast().show('请允许定位权限以获取当前位置')
- // break
- // case error.POSITION_UNAVAILABLE:
- // console.log('定位信息不可用')
- // useGlobalToast().show('无法获取定位,请检查设备GPS是否开启')
- // break
- // case error.TIMEOUT:
- // console.log('定位请求超时')
- // useGlobalToast().show('定位超时,请重试')
- // break
- // }
- // },
- // // 可选配置项
- // {
- // enableHighAccuracy: true, // 开启高精度(GPS定位,耗时稍长),关闭则用网络定位(更快但精度低)
- // timeout: 10000, // 超时时间(10秒)
- // maximumAge: 300000, // 缓存时间(5分钟内重复定位可复用之前结果)
- // },
- // )
- // }
- // else {
- // }
- uni.getLocation({
- type: 'wgs84',
- geocode: true,
- success: (res) => {
- uni.hideLoading()
- console.log('H5位置获取成功', res)
- this.lat = Number(res.latitude.toFixed(6))
- this.lng = Number(res.longitude.toFixed(6))
- // this.lat = 26.6643
- // this.lng = 106.625
- resolve(true)
- },
- fail: (err) => {
- uni.hideLoading()
- reject(new Error('获取位置失败'))
- console.log('H5获取位置失败', err)
- const errMsg = err.errMsg || ''
- if (errMsg.includes('auth deny') || errMsg.includes('authorize')) {
- useGlobalToast().show('请授权位置权限')
- }
- else if (errMsg.includes('timeout')) {
- useGlobalToast().show('获取位置超时,请重试')
- }
- else {
- useGlobalToast().show(`获取定位失败:${errMsg}`)
- }
- },
- })
- })
- },
- // 小程序环境获取位置
- getLocationMP() {
- uni.showLoading({ mask: true })
- // 1. 检查当前授权状态
- uni.getSetting({
- success: (res) => {
- const locationAuth = res.authSetting['scope.userLocation']
- // 2. 已授权 - 直接获取位置
- if (locationAuth) {
- this.fetchActualLocation()
- uni.hideLoading()
- }
- // 3. 未授权 - 尝试请求授权
- else {
- uni.authorize({
- scope: 'scope.userLocation',
- success: () => this.fetchActualLocation(),
- fail: () => this.showAuthGuide(), // 用户拒绝,显示引导
- complete: () => uni.hideLoading(),
- })
- }
- },
- })
- },
- // 实际获取位置的方法(小程序)
- fetchActualLocation() {
- uni.getLocation({
- type: 'wgs84',
- isHighAccuracy: true,
- altitude: true,
- success: (res) => {
- console.log('位置获取成功', res)
- this.lat = res.latitude
- this.lng = res.longitude
- },
- fail: (err) => {
- console.log(err, '获取位置失败')
- if (err.errMsg === 'getLocation:fail system permission denied') {
- useGlobalToast().show('手机定位权限未打开,请手动在设置里面打开定位')
- }
- else {
- useGlobalToast().show(`获取定位失败:${err.errMsg}`)
- }
- },
- })
- },
- // 显示授权引导(小程序)
- showAuthGuide() {
- uni.showModal({
- title: '需要位置权限',
- content: '请开启位置授权以使用完整功能',
- confirmText: '去设置',
- success: (res) => {
- if (res.confirm) {
- // 4. 用户点击确认后打开设置页
- uni.openSetting({
- success: (settingRes) => {
- // 5. 检查用户是否在设置页中开启了授权
- if (settingRes.authSetting['scope.userLocation']) {
- this.fetchActualLocation()
- }
- else {
- console.log('用户仍未授权')
- }
- },
- })
- }
- },
- })
- },
- /**
- *
- * @param _item 兑换的券
- */
- async handleExchange(_item: any) {
- uni.showLoading({ mask: true })
- try {
- const res = await Apis.app.get_smqjh_system_app_api_coupon_exchangepoints({ params: { couponId: _item.id } })
- uni.hideLoading()
- // 兑换成功,跳转成功页
- router.push({
- name: 'exchangeSuccess',
- params: {
- couponId: res.data?.id || _item.id,
- batchNo: res.data?.batchId || '',
- expireDays: res.data?.expirationDate || _item.expirationDate,
- discountMoney: res.data?.discountMoney || _item.discountMoney,
- amountMoney: res.data?.amountMoney || _item.amountMoney,
- },
- })
- }
- catch (error: any) {
- console.log(error)
- uni.hideLoading()
- // 兑换失败,跳转失败页
- router.push({
- name: 'exchangeFail',
- params: {
- failReason: error?.message || '兑换失败,请稍后重试',
- },
- })
- }
- },
- /**
- * 取消订单
- * @param orderId 订单ID
- */
- async handleCommonCancelOrder(orderId: string) {
- return new Promise((resolve, reject) => {
- useGlobalMessage().confirm({
- title: '提示',
- msg: '确定要取消该订单吗?',
- zIndex: 9999,
- success: async () => {
- uni.showLoading({ mask: true })
- try {
- await Apis.general.post_smqjh_oms_api_v1_oil_order_cancel({ params: { orderId } })
- useGlobalToast().show('取消成功')
- resolve(true)
- }
- catch {
- useGlobalToast().show('取消失败')
- reject(new Error('取消失败'))
- }
- finally {
- uni.hideLoading()
- }
- },
- })
- })
- },
- /**
- * 拉起小橘支付
- * @param path
- */
- handleCommonPath(path: string) {
- uni.showLoading({ mask: true })
- const url = import.meta.env.VITE_REDIRECT_URL
- window.location.href = `${path}&redirectUrl=${url}`
- uni.hideLoading()
- },
- /**
- * 跳转小程序
- * @param orderNumber
- */
- handleCommonMiniProgram(orderNumber: string) {
- const query = {
- orderNo: orderNumber,
- }
- const queryString = Object.entries(query)
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
- .join('&')
- this.handleCommonWx('subPack-refueling/orderDetaile/index', queryString)
- },
- handleCommonWx(path: string, queryString: string) {
- window.location.href = `weixin://dl/business/?appid=wx43b5b906cc30ed0b&path=${path}&query=${queryString}&env_version=release`
- },
- async handleCommonGoXiaoJuPay(orderNumber: string) {
- try {
- const res = await Apis.general.post_smqjh_oms_api_v1_oil_order_findbypayurl({ params: { orderNumber } })
- this.handleCommonPath(res.data as string)
- }
- catch (error) {
- console.log(error)
- useGlobalToast().show('获取支付信息失败')
- }
- },
- },
- })
|