使用 taro 重写

This commit is contained in:
Sense T 2025-02-12 13:55:57 +08:00
parent cdb933b449
commit 585241e85e
56 changed files with 15353 additions and 344 deletions

12
.editorconfig Normal file
View File

@ -0,0 +1,12 @@
# http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

2
.env.development Normal file
View File

@ -0,0 +1,2 @@
# 配置文档参考 https://taro-docs.jd.com/docs/next/env-mode-config
# TARO_APP_ID="开发环境下的小程序 AppID"

1
.env.production Normal file
View File

@ -0,0 +1 @@
# TARO_APP_ID="生产环境下的小程序 AppID"

1
.env.test Normal file
View File

@ -0,0 +1 @@
# TARO_APP_ID="测试环境下的小程序 AppID"

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake

7
.eslintrc Normal file
View File

@ -0,0 +1,7 @@
{
"extends": ["taro/react"],
"rules": {
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off"
}
}

View File

@ -1,31 +0,0 @@
/*
* Eslint config file
* Documentation: https://eslint.org/docs/user-guide/configuring/
* Install the Eslint extension before using this feature.
*/
module.exports = {
env: {
es6: true,
browser: true,
node: true,
},
ecmaFeatures: {
modules: true,
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
globals: {
wx: true,
App: true,
Page: true,
getCurrentPages: true,
getApp: true,
Component: true,
requirePlugin: true,
requireMiniProgram: true,
},
// extends: 'eslint:recommended',
rules: {},
}

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
dist/
deploy_versions/
.temp/
.rn_temp/
node_modules/
.DS_Store
.swc
*.local
.direnv
create_qr_code.json

2
app.js
View File

@ -1,2 +0,0 @@
// app.js
App({})

View File

@ -1,14 +0,0 @@
{
"pages": [
"pages/index/index",
"pages/connect-wifi/connect-wifi",
"pages/success/success",
"pages/fail/fail"
],
"entryPagePath": "pages/connect-wifi/connect-wifi",
"permission": {
"scope.userLocation": {
"desc": "您的位置信息将用于连接WiFi"
}
}
}

View File

@ -1,10 +0,0 @@
/**app.wxss**/
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}

11
babel.config.js Normal file
View File

@ -0,0 +1,11 @@
// babel-preset-taro 更多选项和默认值:
// https://docs.taro.zone/docs/next/babel-config
module.exports = {
presets: [
['taro', {
framework: 'react',
ts: true,
compiler: 'vite',
}]
]
}

7
config/dev.ts Normal file
View File

@ -0,0 +1,7 @@
import type { UserConfigExport } from "@tarojs/cli"
export default {
mini: {},
h5: {}
} satisfies UserConfigExport<'vite'>

88
config/index.ts Normal file
View File

@ -0,0 +1,88 @@
import { defineConfig, type UserConfigExport } from '@tarojs/cli'
import devConfig from './dev'
import prodConfig from './prod'
// https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
export default defineConfig<'vite'>(async (merge, { command, mode }) => {
const baseConfig: UserConfigExport<'vite'> = {
projectName: 'wifi-assistant',
date: '2025-2-12',
designWidth: 750,
deviceRatio: {
640: 2.34 / 2,
750: 1,
375: 2,
828: 1.81 / 2
},
sourceRoot: 'src',
outputRoot: 'dist',
plugins: [],
defineConstants: {
},
copy: {
patterns: [
],
options: {
}
},
framework: 'react',
compiler: 'vite',
mini: {
postcss: {
pxtransform: {
enable: true,
config: {
}
},
cssModules: {
enable: false, // 默认为 false如需使用 css modules 功能,则设为 true
config: {
namingPattern: 'module', // 转换模式,取值为 global/module
generateScopedName: '[name]__[local]___[hash:base64:5]'
}
}
},
},
h5: {
publicPath: '/',
staticDirectory: 'static',
miniCssExtractPluginOption: {
ignoreOrder: true,
filename: 'css/[name].[hash].css',
chunkFilename: 'css/[name].[chunkhash].css'
},
postcss: {
autoprefixer: {
enable: true,
config: {}
},
cssModules: {
enable: false, // 默认为 false如需使用 css modules 功能,则设为 true
config: {
namingPattern: 'module', // 转换模式,取值为 global/module
generateScopedName: '[name]__[local]___[hash:base64:5]'
}
}
},
},
rn: {
appName: 'taroDemo',
postcss: {
cssModules: {
enable: false, // 默认为 false如需使用 css modules 功能,则设为 true
}
}
}
}
if (process.env.NODE_ENV === 'development') {
// 本地开发构建配置(不混淆压缩)
return merge({}, baseConfig, devConfig)
}
// 生产构建配置(默认开启压缩混淆等)
return merge({}, baseConfig, prodConfig)
})

33
config/prod.ts Normal file
View File

@ -0,0 +1,33 @@
import type { UserConfigExport } from "@tarojs/cli"
export default {
mini: {},
h5: {
/**
* WebpackChain
* @docs https://github.com/neutrinojs/webpack-chain
*/
// webpackChain (chain) {
// /**
// * 如果 h5 端编译后体积过大,可以使用 webpack-bundle-analyzer 插件对打包体积进行分析。
// * @docs https://github.com/webpack-contrib/webpack-bundle-analyzer
// */
// chain.plugin('analyzer')
// .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, [])
// /**
// * 如果 h5 端首屏加载时间过长,可以使用 prerender-spa-plugin 插件预加载首页。
// * @docs https://github.com/chrisvfritz/prerender-spa-plugin
// */
// const path = require('path')
// const Prerender = require('prerender-spa-plugin')
// const staticDir = path.join(__dirname, '..', 'dist')
// chain
// .plugin('prerender')
// .use(new Prerender({
// staticDir,
// routes: [ '/pages/index/index' ],
// postProcess: (context) => ({ ...context, outputPath: path.join(staticDir, 'index.html') })
// }))
// }
}
} satisfies UserConfigExport<'vite'>

View File

@ -0,0 +1,5 @@
{
"access_token": "access_token",
"ssid": "ssid",
"password": "password"
}

32
create_qr_code.ts Normal file
View File

@ -0,0 +1,32 @@
import axios from "axios";
import { writeFileSync } from "fs";
import config from "./create_qr_code.json";
const generateQRCode = async (path: string, scene: string) => {
const url = `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${config.access_token}`;
const params = {
scene: scene, // 参数格式为json字符串
page: path, // 扫码后跳转的小程序页面路径
width: 430, // 二维码的宽度
};
try {
const response = await axios({
method: "POST",
url: url,
data: params,
responseType: "arraybuffer", // 重要需要设置响应类型为arraybuffer
});
// 将response.data一个Buffer写入文件
writeFileSync("qrcode.png", response.data);
console.log("小程序码已生成");
} catch (error) {
console.error("生成小程序码失败", error);
}
};
generateQRCode(
"pages/connect-wifi/connect-wifi",
`{"SSID":"${config.ssid}","password":"${config.password}"}`
);

61
flake.lock generated Normal file
View File

@ -0,0 +1,61 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1739019272,
"narHash": "sha256-7Fu7oazPoYCbDzb9k8D/DdbKrC3aU1zlnc39Y8jy/s8=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "fa35a3c8e17a3de613240fea68f876e5b4896aec",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"utils": "utils"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

26
flake.nix Normal file
View File

@ -0,0 +1,26 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
};
outputs =
{ nixpkgs, utils, ... }:
utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
inherit (pkgs) mkShell;
in
{
devShell = mkShell {
buildInputs = with pkgs; [
nodejs
tokei
];
};
NPM_CONFIG_REGISTRY = "https://registry.npmmirror.com";
}
);
}

View File

@ -1,35 +0,0 @@
const config = {
access_token = 'access_token',
ssid = 'ssid',
password = 'password'
};
const axios = require('axios');
const generateQrCode = async (path, scene) => {
const url = `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${coonfig.access_token}`;
const params = {
scene: scene, // 参数格式为json字符串
page: path, // 扫码后跳转的小程序页面路径
width: 430 // 二维码的宽度
};
try {
const response = await axios({
method: 'POST',
url: url,
data: params,
responseType: 'arraybuffer' // 重要需要设置响应类型为arraybuffer
});
// 将response.data一个Buffer写入文件
fs.writeFileSync('qrcode.png', response.data);
console.log('小程序码已生成');
} catch (error) {
console.error('生成小程序码失败', error);
}
};
// 使用示例
generateQrCode('pages/connect-wifi/connect-wifi', `{"SSID":"${config.ssid}","password":"${config.password}"}`);

14771
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

69
package.json Normal file
View File

@ -0,0 +1,69 @@
{
"name": "wifi-assistant",
"version": "1.0.0",
"private": true,
"description": "",
"templateInfo": {
"name": "default",
"typescript": true,
"css": "Less",
"framework": "React"
},
"scripts": {
"qrcode": "ts-node create-qr-code.ts",
"newpage": "taro create",
"build:weapp": "taro build --type weapp",
"build:h5": "taro build --type h5",
"build:harmony-hybrid": "taro build --type harmony-hybrid",
"dev:weapp": "npm run build:weapp -- --watch",
"dev:h5": "npm run build:h5 -- --watch",
"dev:harmony-hybrid": "npm run build:harmony-hybrid -- --watch"
},
"browserslist": [
"defaults and fully supports es6-module",
"maintained node versions"
],
"author": "",
"dependencies": {
"@babel/runtime": "^7.24.4",
"@tarojs/components": "4.0.9",
"@tarojs/helper": "4.0.9",
"@tarojs/plugin-framework-react": "4.0.9",
"@tarojs/plugin-platform-alipay": "4.0.9",
"@tarojs/plugin-platform-h5": "4.0.9",
"@tarojs/plugin-platform-harmony-hybrid": "4.0.9",
"@tarojs/plugin-platform-jd": "4.0.9",
"@tarojs/plugin-platform-qq": "4.0.9",
"@tarojs/plugin-platform-swan": "4.0.9",
"@tarojs/plugin-platform-tt": "4.0.9",
"@tarojs/plugin-platform-weapp": "4.0.9",
"@tarojs/react": "4.0.9",
"@tarojs/runtime": "4.0.9",
"@tarojs/shared": "4.0.9",
"@tarojs/taro": "4.0.9",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"ts-node": "^10.9.2",
"@babel/core": "^7.24.4",
"@babel/plugin-proposal-class-properties": "7.14.5",
"@babel/preset-react": "^7.24.1",
"@tarojs/cli": "4.0.9",
"@tarojs/vite-runner": "4.0.9",
"@types/react": "^18.0.0",
"@vitejs/plugin-react": "^4.3.0",
"babel-preset-taro": "4.0.9",
"eslint": "^8.57.0",
"eslint-config-taro": "4.0.9",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.4.0",
"less": "^4.2.0",
"postcss": "^8.4.38",
"react-refresh": "^0.14.0",
"stylelint": "^16.4.0",
"terser": "^5.30.4",
"typescript": "^5.4.5",
"vite": "^4.2.0"
}
}

View File

@ -1,38 +0,0 @@
// pages/connect-wifi/connect-wifi.js
Page({
/**
* 页面的初始数据
*/
data: {
wifiInfo: {
SSID: 'test',
password: 'test'
},
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
try {
const scene = decodeURIComponent(options.scene);
this.data.wifiInfo = JSON.parse(scene)
} catch (e) {
console.log(e)
}
this.connectWifi(this.data.wifiInfo)
},
connectWifi(wifiInfo) {
wx.connectWifi({
...wifiInfo,
manual: true,
success: () => {
wx.navigateTo({
url: '/pages/success/success',
})
},
fail: (err)=> {}
})
},
})

View File

@ -1,3 +0,0 @@
{
"usingComponents": {}
}

View File

@ -1,4 +0,0 @@
<!--pages/connect-wifi/connect-wifi.wxml-->
<view class="container">
<text>你将连接到 {{wifiInfo.SSID}}</text>
</view>

View File

@ -1 +0,0 @@
/* pages/connect-wifi/connect-wifi.wxss */

View File

@ -1,66 +0,0 @@
// pages/fail/fail.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})

View File

@ -1,3 +0,0 @@
{
"usingComponents": {}
}

View File

@ -1,4 +0,0 @@
<!--pages/fail/fail.wxml-->
<view class="container">
<text>连接失败,请退出小程序重新扫码</text>
</view>

View File

@ -1 +0,0 @@
/* pages/fail/fail.wxss */

View File

@ -1,2 +0,0 @@
// index.js
Page({})

View File

@ -1 +0,0 @@
{}

View File

@ -1,6 +0,0 @@
<!--index.wxml-->
<scroll-view class="scrollarea" scroll-y type="list">
<view class="container">
Weixin
</view>
</scroll-view>

View File

@ -1,10 +0,0 @@
/**index.wxss**/
page {
height: 100vh;
display: flex;
flex-direction: column;
}
.scrollarea {
flex: 1;
overflow-y: hidden;
}

View File

@ -1,66 +0,0 @@
// pages/success/success.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})

View File

@ -1,3 +0,0 @@
{
"usingComponents": {}
}

View File

@ -1,4 +0,0 @@
<!--pages/success/success.wxml-->
<view class="container">
<text>连接成功了</text>
</view>

View File

@ -1 +0,0 @@
/* pages/success/success.wxss */

View File

@ -1,28 +1,15 @@
{
"appid": "wx127af54b7533d89e",
"compileType": "miniprogram",
"libVersion": "3.7.3",
"packOptions": {
"ignore": [],
"include": []
},
"miniprogramRoot": "./dist",
"projectname": "wifi-assistant",
"description": "",
"appid": "touristappid",
"setting": {
"coverView": true,
"es6": true,
"postcss": true,
"minified": true,
"enhance": true,
"showShadowRootInWxmlPanel": true,
"packNpmRelationList": [],
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
}
"urlCheck": true,
"es6": false,
"enhance": false,
"compileHotReLoad": false,
"postcss": false,
"minified": false
},
"condition": {},
"editorSetting": {
"tabIndent": "insertSpaces",
"tabSize": 2
}
}
"compileType": "miniprogram"
}

View File

@ -1,7 +0,0 @@
{
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
"projectname": "wifi-assistant",
"setting": {
"compileHotReLoad": true
}
}

View File

@ -1,7 +0,0 @@
{
"desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html",
"rules": [{
"action": "allow",
"page": "*"
}]
}

13
src/app.config.ts Normal file
View File

@ -0,0 +1,13 @@
export default defineAppConfig({
pages: [
'pages/index/index',
'pages/connect-wifi/index',
'pages/success/index'
],
window: {
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
navigationBarTitleText: 'WeChat',
navigationBarTextStyle: 'black'
}
})

0
src/app.less Normal file
View File

17
src/app.ts Normal file
View File

@ -0,0 +1,17 @@
import { PropsWithChildren } from 'react'
import { useLaunch } from '@tarojs/taro'
import './app.less'
function App({ children }: PropsWithChildren<any>) {
useLaunch(() => {
console.log('App launched.')
})
// children 是将要会渲染的页面
return children
}
export default App

17
src/index.html Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta content="width=device-width,initial-scale=1,user-scalable=no" name="viewport">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="format-detection" content="telephone=no,address=no">
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
<title>wifi-assistant</title>
<script><%= htmlWebpackPlugin.options.script %></script>
</head>
<body>
<div id="app"></div>
</body>
</html>

View File

@ -0,0 +1,3 @@
export default definePageConfig({
navigationBarTitleText: '连接 WIFI'
})

View File

@ -0,0 +1,13 @@
.connect-wifi {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
flex-wrap: nowrap;
margin-top: 15em;
}
.connect-wifi > .small-text {
color: gray;
font-size: 14pt;
}

View File

@ -0,0 +1,38 @@
import { useState } from 'react'
import { View, Text, Icon } from '@tarojs/components'
import Taro, { useLoad, navigateTo } from '@tarojs/taro'
import './index.less'
type wifiInfo = Pick<Taro.connectWifi.Option, 'SSID'| 'password'>
export default function ConnectWifi () {
const [wifiInfo, setWifiInfo] = useState({} as wifiInfo)
useLoad((options) => {
try{
const scene = JSON.parse(decodeURIComponent(options.scene)) as wifiInfo
setWifiInfo(scene)
} catch (e) {
console.log(e)
}
Taro.connectWifi({
...wifiInfo, maunal: true
}).then(()=>{
navigateTo({
url:'pages/success/index'
})
}).catch((e) => {
console.log(e)
})
})
return (
<View className='connect-wifi'>
<Icon size={60} type='waiting' />
<Text> {wifiInfo.SSID || 'WIFI'}</Text>
<Text className='small-text'> WIFI</Text>
</View>
)
}

View File

@ -0,0 +1,3 @@
export default definePageConfig({
navigationBarTitleText: '首页'
})

View File

12
src/pages/index/index.tsx Normal file
View File

@ -0,0 +1,12 @@
import { View, Text } from '@tarojs/components'
import './index.less'
export default function Index () {
// TODO: 广告
return (
<View className='index'>
<Text>Hello world!</Text>
</View>
)
}

View File

@ -0,0 +1,3 @@
export default definePageConfig({
navigationBarTitleText: '连接成功'
})

View File

@ -0,0 +1,13 @@
.success {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
flex-wrap: nowrap;
margin-top: 15em;
}
.success > .small-text {
color: gray;
font-size: 14pt;
}

View File

@ -0,0 +1,13 @@
import { View, Text, Icon } from '@tarojs/components'
import './index.less'
export default function Success () {
return (
<View className='success'>
<Icon size={60} type='success' />
<Text></Text>
<Text className='small-text'>退</Text>
</View>
)
}

30
tsconfig.json Normal file
View File

@ -0,0 +1,30 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"removeComments": false,
"preserveConstEnums": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"outDir": "lib",
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictNullChecks": true,
"sourceMap": true,
"rootDir": ".",
"jsx": "react-jsx",
"allowJs": true,
"resolveJsonModule": true,
"typeRoots": [
"node_modules/@types"
],
"paths": {
// TS5090 leading './'
"@/*": ["./src/*"]
}
},
"include": ["./src", "./types", "./config"],
"compileOnSave": false
}

29
types/global.d.ts vendored Normal file
View File

@ -0,0 +1,29 @@
/// <reference types="@tarojs/taro" />
declare module '*.png';
declare module '*.gif';
declare module '*.jpg';
declare module '*.jpeg';
declare module '*.svg';
declare module '*.css';
declare module '*.less';
declare module '*.scss';
declare module '*.sass';
declare module '*.styl';
declare namespace NodeJS {
interface ProcessEnv {
/** NODE 内置环境变量, 会影响到最终构建生成产物 */
NODE_ENV: 'development' | 'production',
/** 当前构建的平台 */
TARO_ENV: 'weapp' | 'swan' | 'alipay' | 'h5' | 'rn' | 'tt' | 'qq' | 'jd' | 'harmony' | 'jdrn'
/**
* appid
* @description env `TARO_APP_ID`便 appid dist/project.config.json
* @see https://taro-docs.jd.com/docs/next/env-mode-config#特殊环境变量-taro_app_id
*/
TARO_APP_ID: string
}
}