小程序开发环境登录

本地运行时,往往需要登录。

但是由于不同项目,可能测试环境不一致版本不一致,使用统一登录组件

安装 pai-mp-ui

  • 默认框架有,则更新至最新版(1.0.71 以上)

    "@flyriselink/pai-mp-ui": "1.0.71",

引入[登录组件] pai-mp-login

pages/login/index

<template>
    <view>
        <pai-mp-login :routerSuccess="routerSuccess" :routerFailed="routerFailed"></pai-mp-login>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                routerSuccess: "/pages/user/detail",
                routerFailed: ""
            }
        },
    }
</script>

增加配置文件 [const.js]

  • 没有则新建文件
  • 一般不需要修改该文件
  • 登录的域名使用 "manifest.json" 里面的

src/const.js

import Vue from 'vue'
import manifest from 'manifest'

const STATIC_BASE_URL = ''
let API_BASE_URL = ''
let isEncrypt = false

// #ifdef MP-WEIXIN || H5
let proxy = manifest?.h5?.devServer?.proxy || {}
let proxyItem = proxy['/api']
let target = proxyItem?.target
// #ifdef H5
if (process.env.NODE_ENV !== 'development') {
    let origin = location.origin
    API_BASE_URL = origin

    if (origin.indexOf('39.108.55.230') > -1) {
        isEncrypt = true
    }
} else {
    if (target.indexOf('39.108.55.230') > -1) {
        isEncrypt = true
    }
}
// #endif
// #ifdef MP-WEIXIN
if (target) {
    API_BASE_URL = target
}
// #endif
Vue.prototype.$BASE_URL = target
// #endif

const DEFAULT_CONSTS = {
    NAME: '模板应用', // 应用名称
    WX_APPID: '', // 微信APPID
    SYSTEM: 'MP', // 系统
    VERSION: '1.0.0', // 版本号
    API_BASE_URL: API_BASE_URL, // 接口baseURL -- 正式版
    CONSOLE: '/console-api',
    AUTH: '/auth-api',
    ENCRYPT_LOGIN: isEncrypt, // 是否加密安全本地存储信息
    ENCRYPT_KEY: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQ20qdDrEYwy6W9GIvP6LplF7bfkU0i0Yet6/Z2rUmPyqoGt7jp+Fa+p2nMItq1d3W9BLvhlVjoA/jMXG74TAKeLWNRHHSY/lBPe5kQNM2hgeAh46XUb20wdxw9Qip3kIq3YhhGko4FPmBgzGA/jm7D++bRrn3vcnnj3JJDkeRHwIDAQAB' // 加密key
}
export default DEFAULT_CONSTS

暂时只支持手机号加密登录

配置代理 [manifest.json]

    "h5" : {
        "router" : {
            "base" : "/业务的路由/"
        },
        "devServer" : {
            "https" : false,
            "proxy" : {
                "/api" : {
                    "target" : "http://39.108.55.230:31080",
                    "changeOrigin" : true,
                    "secure" : false,
                    "pathRewrite" : {
                        "^/api" : "/"
                    }
                }
            }
        }
    }
文档更新时间: 2023-06-21 18:02   作者:戴均豪