99 Commits

Author SHA1 Message Date
0f76b61152 fix(addon/aikefu): 使用ThinkPHP Model来处理数据 2025-12-06 15:13:24 +08:00
6cfff15c62 fix(addon/aikefu): 使用curl来发送请求 2025-12-06 14:55:10 +08:00
6d3887ec06 fix(addon/aikefu): 使用uniacid方便以后迁移,唯一值 2025-12-06 14:47:26 +08:00
5af0b07775 fix(addon/aikefu): 支持外部传入site_id 等参数 2025-12-06 14:34:17 +08:00
8ae10dd2c3 fix(addon/aikefu): 获得当前配置有问题 2025-12-06 14:16:13 +08:00
17c1ce2cc6 chore(addon/aikefu): 更新控制器及更新事件 2025-12-06 14:10:27 +08:00
a209dc8080 chore(addon/aikefu): 更新控制器及更新事件 2025-12-06 14:06:12 +08:00
1d4fff13a1 chore(addon/aikefu): 增加统一的事件配置 2025-12-06 13:52:45 +08:00
a811e36635 chore(addon/aikefu): 调整事件名称 2025-12-06 13:41:37 +08:00
d8a0dd5d31 chore(addon/aikefu): 调整config的配置内容 2025-12-06 13:25:15 +08:00
0ff979917c chore(addon/aikefu): 调整config的配置内容 2025-12-06 12:52:30 +08:00
fc5615a9c7 chore(addon/aikefu): 更新获取配置及保存逻辑 2025-12-06 11:59:53 +08:00
cdcd9eeffa chore(addon/aikefu): update html 2025-12-06 11:51:16 +08:00
c0da89735c chore(db): 升级数据包含新增的智能客服插件sql脚本 2025-12-06 11:43:34 +08:00
fc34d83692 chore(addon/aikefu): 更新info 2025-12-06 10:15:40 +08:00
8ceb252d79 feat(addon/aikefu): 新增AI智能客服插件 2025-12-06 10:09:08 +08:00
8da4563435 chore(docker): update supervisord.conf 2025-12-06 09:30:42 +08:00
045e6ab3df chore(addon): 支付相关判断是否平台开通配置 2025-12-05 17:37:12 +08:00
5591e17446 chore(docker): fix supervisord.conf 2025-12-05 17:27:48 +08:00
8f783fd765 fix(docker): 针对已经存在的容器或后期新建的容器权限设置更新 2025-12-05 17:01:50 +08:00
6b41e46f30 chore(addon): weapp 插件与niushopV5部分代码同步比较 2025-12-05 16:04:46 +08:00
ff89fdf5e9 chore(addon): 新增wechat插件 2025-12-05 15:52:21 +08:00
402c425575 chore(src): 更新代码注释 2025-12-05 15:05:54 +08:00
ee2785f972 chore(addon/huawiepay): 增强huaweipay 的预下单及单元测试 2025-12-05 15:05:20 +08:00
bbb0271a5e chore(scripts): 更新生成私钥和公钥的脚本处理 2025-12-05 15:04:10 +08:00
75ff4bb0a4 chore(docker): 更新supervisord.conf 针对Docker容器重启后,设置权限 2025-12-05 15:03:38 +08:00
776f0ed029 fix(addon): alipay 及 wechatpay 没有判断支付类型 2025-12-04 11:19:14 +08:00
eef56291eb fix(addon/wechatpay): 修复微信支付回调错误 2025-12-04 11:05:08 +08:00
8e159edf1d chore(addon/huaweipay): mch_id -> merc_no 2025-12-04 11:00:24 +08:00
1793e4b2aa fix(pay): 修复PayNotify 没有正确的传回参数 2025-12-04 10:32:42 +08:00
a793ed541b chore(addon/huaweipay): 更新huaweipay 2025-12-04 10:32:00 +08:00
09ed1bd427 chore(vendor): 依赖 Vendor 升级测试 2025-12-04 08:39:14 +08:00
ff666975da chore(docker): 更新升级sql,把华为支付及线下支付添加到数据库中 2025-12-03 15:50:20 +08:00
b4403cedd9 chore(addon): 增强针对插件列表的处理 2025-12-03 15:45:08 +08:00
98d2eb8a2a chore(addon/huaweipay): 变更华为支付的测试方法内容 2025-12-03 15:34:19 +08:00
ae5f56c16f chore(event): 更新event 默认的初始化路由等操作 2025-12-03 15:33:33 +08:00
d374034694 chore(docker): 增加升级数据库脚本 2025-12-03 15:07:23 +08:00
b0f399c814 chore(docker): 暂时存储upgrade.sql 2025-12-03 14:51:21 +08:00
8489ef35cb chore(docker): 更新初始化脚本 2025-12-03 14:40:51 +08:00
7d8c2d4e37 chore(event): 使用 log_write 记录日志 2025-12-03 11:47:42 +08:00
ec60eee8fe chore(event): 更新InitAddon, 跟踪初始化事件监听 2025-12-03 11:31:55 +08:00
dac15250a2 chore(event): 更新InitAddon,要求手动添加了addon,也会更新缓存,添加到监听事件中 2025-12-03 11:12:13 +08:00
fec0198537 chore(docker): 更新数据库初始化脚本 2025-12-03 10:48:53 +08:00
f5ac4d10c0 chore(addon/huaweipay): 支持证书文本填写模式 2025-12-03 10:37:39 +08:00
ca07a6cea5 chore(doc): 添加华为支付插件说明文档 2025-12-03 09:42:54 +08:00
b5d89aef72 fix(docker): 只处理 runtime 及 upload目录 2025-12-03 09:02:24 +08:00
d151d45e99 fix(sms): 显示出验证码 2025-12-03 08:56:35 +08:00
3525af81bf chore(docker): update dockerfile 2025-12-03 08:52:06 +08:00
01c86ce0a3 chore(docker): 更新初始化脚本 2025-12-03 08:47:20 +08:00
e5e619a241 chore(docker): 更新PHP Dockerfile 2025-12-03 08:45:35 +08:00
d9b10c1621 chore(docker): update .gitignore 2025-12-03 08:34:11 +08:00
c6e72e5b79 chore(sms): 更新sms 2025-12-02 18:05:39 +08:00
980effc420 fix(docker): 去除不用的目录 2025-12-02 18:01:11 +08:00
e4040a27e7 chore(src): 还原src\app\model\upload\Upload.php 2025-12-02 17:31:37 +08:00
23170dcc3f fix(docker): 修复PHP进程使用的是Web服务用户,而没有相应权限创建目录的问题 2025-12-02 17:29:26 +08:00
64c92857a6 chore(src): 更新上传的判断路径的逻辑 2025-12-02 17:19:41 +08:00
4346bfebb7 chore(src): 使用绝对路径来判断权限 2025-12-02 17:18:00 +08:00
c24271c075 chore(src): 检查路径的权限 2025-12-02 17:15:02 +08:00
526982c431 chore(src): 更新上传错误,检查更全面 2025-12-02 17:12:05 +08:00
c161bc55e5 chore(src): 针对上传权限,提供更多有价值的信息 2025-12-02 17:07:35 +08:00
eb79ad260c chore(src): 所有代码上传 2025-12-02 15:36:42 +08:00
ce8e59902c chore(docker): 更新本地docker-compose.local.yml配置 2025-12-02 15:17:52 +08:00
b8ed400d12 chore(docker): 更新.gitignore 2025-12-02 15:08:54 +08:00
5f48980d31 chore(docker): 根据不同环境区分docker容器及网络,完全隔离 2025-12-02 15:00:06 +08:00
b81e7b8b1b chore(docker): 更新.gitignore文件 2025-12-02 14:01:16 +08:00
a468c0919d chore(docker): 更新.gitignore文件 2025-12-02 14:00:17 +08:00
1ae5b46523 chore(docker): 更新.env.development 2025-12-02 11:33:38 +08:00
ecac45a74a chore(docker): 更新数据库初始化脚本 2025-12-02 11:32:22 +08:00
51c4cb7767 chore(src): 将database.php 纳入代码管理中 2025-12-02 11:24:40 +08:00
ae7cfebb44 chore(docker): 更新docker配置 2025-12-02 11:10:00 +08:00
e4ccbbcbd1 chore(docker): 更新环境变量 2025-12-02 10:46:21 +08:00
bdfcd1cedb chore(docker): 更新docker的网络设置 2025-12-02 10:21:43 +08:00
6a7b465944 chore(docker): 更新supervisord.conf 2025-12-02 10:19:06 +08:00
41ac96630c chore(env): 更新.env.development文件配置 2025-12-02 10:04:24 +08:00
3a8fbc3e1b chore(docker): 更新nignx volumes 设置 2025-12-02 09:55:49 +08:00
e3e57ee154 chore(docker): 更新挂载点 2025-12-02 09:53:42 +08:00
0883c1318b chore(docker): 使用挂载点,确保目录能够正常被读写 2025-12-02 09:47:34 +08:00
fe79d04343 chore(docker): 更新docker-compose.yml 的volumes 使用主机目录持久化 2025-12-02 09:37:14 +08:00
2857283558 chore(docker): 为dev准备部署条件 2025-12-02 09:28:19 +08:00
981779126c chore(addon-huaweipay): 支持华为支付参数配置 2025-12-01 15:28:38 +08:00
39ce5882cb chore(scripts): 新增生成私有公有证书的nodejs脚本 2025-12-01 15:27:30 +08:00
e51f6c6544 chore(addon): 添加线下支付及华为支付基本配置 2025-12-01 11:36:51 +08:00
3a7f510e19 chore(db): 比较于niushop的差异 2025-11-29 17:51:17 +08:00
1c9e72e28d chore: 新增replace_comments.py 脚本,使用python replace_comments.py --path ./src来去掉版权注释及空注释 2025-11-29 16:45:45 +08:00
9d79b2585e chore(docs): 添加GIT分支管理及发布流程 2025-11-29 10:45:24 +08:00
1fc9a39ffe chore(docker): 指定容器内目录是可读可写的 2025-11-29 10:39:30 +08:00
e2d6a02860 chore: .well-known 不作为版本控制的文件 2025-11-28 14:36:14 +08:00
1f8504f002 fix(license): 发现源码属于上海牛之云网络科技有限公司的NiuShop开源商城。 2025-11-25 18:05:00 +08:00
9ff2492962 fix(src): 解决GROUP BY问题:只查询主键ID,避免ONLY_FULL_GROUP_BY错误 2025-11-25 11:32:31 +08:00
88355a3f48 fix(src): memberrecharge js脚本报错,找不到Order对象的定义 2025-11-24 16:57:37 +08:00
3effc03a19 chore: 注释掉file_put_contents 2025-11-24 16:04:44 +08:00
be65996398 chore(db): 数据库获得完整的初始化脚本,用来部署新的服务器 2025-11-24 16:04:01 +08:00
c58bf929b1 chore(src): 引入vue2系列最终版本,为后面稳定升级做准备 2025-11-22 16:29:40 +08:00
ec1b93e473 chore: 注释掉 file_put_contents(__DIR__ . '/debug.txt', 这样的调试方式 2025-11-22 16:22:39 +08:00
8a78cea36d chore(addon): 同步华为云第1台服务器上的addon代码 2025-11-22 16:14:32 +08:00
96f0d9b602 chore(docs): 增加关于CLodop的安装及使用说明 2025-11-22 16:08:25 +08:00
ee884a3737 chore: 同步华为云第1台服务器上代码,public下所有静态资源 2025-11-22 16:01:53 +08:00
326dbb1c3c chore: 同步华为云第1台服务器上的关于Menu针对特殊用户限制的代码 2025-11-22 15:56:34 +08:00
f03dbbfc60 chore: src/app 目录与华为云第一台服务器上的代码做第一次比较合并 2025-11-22 15:00:41 +08:00
4572 changed files with 624749 additions and 126361 deletions

27
.env.development Normal file
View File

@@ -0,0 +1,27 @@
# 项目配置, 请根据实际情况修改
PROJECT_NAME=newshop
# ThinkPHP 6.x 配置, 请根据实际情况修改
APP_ENV=development
# PHP/PHP-FPM 配置
PHP_VERSION=7.4
PHP_FPM_VERSION=7.4-fpm
PHP_FPM_PORT=9105
XDEBUG_POST=9108
# 数据库配置
MYSQL_ROOT_HOST=%
MYSQL_DATABASE=shop_mallnew
MYSQL_USER=shop_mallnew
MYSQL_PASSWORD=shop_mallnew
MYSQL_PORT=3326
# Redis 绑定端口及密码
REDIS_PASSWORD=luckyshop123!@#
REDIS_PORT=6499
# Nginx 暴漏端口
NGINX_PORT=8050
NGINX_SSL_PORT=8052

View File

@@ -1,24 +1,26 @@
# 项目配置, 请根据实际情况修改
PROJECT_NAME=newshop
# ThinkPHP 6.x 配置, 请根据实际情况修改
APP_ENV=development
# PHP/PHP-FPM 配置
PHP_VERSION=7.4
PHP_FPM_VERSION=7.4-fpm
PHP_FPM_PORT=9000
XDEBUG_POST=9003
PHP_FPM_PORT=9100
XDEBUG_POST=9103
# 数据库配置
MYSQL_ROOT_HOST=%
MYSQL_DATABASE=shop_mallnew
MYSQL_USER=shop_mallnew
MYSQL_PASSWORD=shop_mallnew
MYSQL_PORT=3306
MYSQL_PORT=3316
# Redis 配置
REDIS_PASSWORD=luckyshop123!@#
REDIS_PORT=6379
REDIS_PORT=6399
# Nginx 配置
NGINX_PORT=80
NGINX_SSL_PORT=443
NGINX_PORT=8010
NGINX_SSL_PORT=8012

27
.env.local Normal file
View File

@@ -0,0 +1,27 @@
# 项目配置, 请根据实际情况修改
PROJECT_NAME=newshop
# ThinkPHP 6.x 配置, 请根据实际情况修改
APP_ENV=local
# PHP/PHP-FPM 配置
PHP_VERSION=7.4
PHP_FPM_VERSION=7.4-fpm
PHP_FPM_PORT=9100
XDEBUG_POST=9103
# 数据库配置
MYSQL_ROOT_HOST=%
MYSQL_DATABASE=shop_dev
MYSQL_USER=shop_mallnew
MYSQL_PASSWORD=shop_mallnew
MYSQL_PORT=3316
# Redis 配置
REDIS_PASSWORD=luckyshop123!@#
REDIS_PORT=6399
# Nginx 配置
NGINX_PORT=8010
NGINX_SSL_PORT=8012

3
.gitignore vendored
View File

@@ -18,6 +18,9 @@ __pycache__
.idea
.vscode
# 环境变量
.env
# 源码结构
debug.txt
.travis.yml

43
README.md Normal file
View File

@@ -0,0 +1,43 @@
# 在线商城PHP项目
## Docker 部署
```bash
cp .env.example .env.development
```
**注意**
- 在同一目录下面,执行 `docker-compose` 命令时,需要指定项目名称。用来区分不同的环境。如 `shop_local``shop_dev` 等。
- 本地部署时,需要将 `APP_ENV` 设置为 `local`
- 开发环境部署时,需要将 `APP_ENV` 设置为 `development`
## 环境变量
- `APP_ENV`: 应用环境,默认值为 `development`
## 开发环境-local 部署
```bash
# 本地部署时,需要将 APP_ENV 设置为 local, 并指定 docker-compose.local.yml 文件
docker-compose --env-file .env.local -f docker-compose.local.yml up -d
# docker-compose --project-name shop_local --env-file .env.local -f docker-compose.local.yml up -d
# docker-compose down 命令,用来停止并删除容器
docker-compose -f docker-compose.local.yml down -v
# docker-compose --project-name shop_local down -v
```
## 开发环境-development 部署
```bash
# 默认使用 docker-compose.yml 文件
docker-compose --project-name shop_development --env-file .env.development up -d
# docker-compose down 命令,用来停止并删除容器
docker-compose --project-name shop_development down -v
```

126
docker-compose.local.yml Normal file
View File

@@ -0,0 +1,126 @@
# 特别说明本地local环境方便操作所以未使用的统一的docker-compose.yml文件只保留了local环境的配置
x-shared-env: &shared-api-env
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-rootpassword}
MYSQL_ROOT_HOST: ${MYSQL_ROOT_HOST:-'%'} # 允许root从任何主机连接
MYSQL_DATABASE: ${MYSQL_DATABASE:-shop_mallnew}
MYSQL_USER: ${MYSQL_USER:-shop_mallnew}
MYSQL_PASSWORD: ${MYSQL_PASSWORD:-shop_mallnew}
REDIS_PASSWORD: ${REDIS_PASSWORD:-luckyshop123!@#}
REDIS_PORT: ${REDIS_PORT:-6379}
# 将服务归类到目录 A 中
services:
php-fpm:
build:
context: ./docker/php
dockerfile: Dockerfile
container_name: ${PROJECT_NAME}_php
restart: always
extra_hosts:
- "host.docker.internal:host-gateway" # 支持主机名解析
environment:
PHP_ENV: ${PHP_ENV:-development}
# 环境变量, APP_ENV 应用于 ThinkPHP 6.x 框架, .env.local 要想启用,需要在项目根目录下创建 .env.local 文件,并将 APP_ENV 设置为 local
# 同理,如果要启用开发环境,则将 APP_ENV 设置为 development如果要启用生产环境则将 APP_ENV 设置为 production
# 不然ThinkPHP 6.x 系列,会只加载 .env 文件,而不会加载 .env.local 文件,导致 .env.local 文件中的配置不会生效
APP_ENV: ${APP_ENV:-local}
APP_DEBUG: ${APP_DEBUG:-true}
XDEBUG_CONFIG: ${XDEBUG_CONFIG:-client_host=host.docker.internal client_port=9003}
PHP_IDE_CONFIG: serverName=docker-php
ports:
- "${PHP_FPM_PORT:-9000}:9000" # PHP-FPM
- "${XDEBUG_POST:-9003}:9003" # Xdebug
volumes:
- ./:/var/www/all_source
- ./src:/var/www/html
# 更新下载源列表以加速apt-get
- ./docker/debian/sources.list:/etc/apt/sources.list:ro
- ./docker/php/php.ini:/usr/local/etc/php/php.ini:ro
- ./docker/php/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
- xdebug_logs:/tmp # Xdebug 日志目录
depends_on:
- db
healthcheck:
test: ["CMD", "bash", "-c", "curl -f http://localhost:9000/status && ps aux | grep '[p]hp think cron:schedule'"]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
networks:
- sass-platform-net
labels:
- "com.docker.compose.project.working_dir=${PROJECT_NAME}"
nginx:
build:
context: ./docker/nginx
dockerfile: Dockerfile
container_name: ${PROJECT_NAME}_nginx
restart: always
ports:
- "${NGINX_PORT:-80}:80"
- "${NGINX_SSL_PORT:-443}:443"
volumes:
# 挂载项目代码到 Nginx 容器中
- ./src:/var/www/html:rw
# 更新下载源列表以加速apt-get
- ./docker/debian/sources.list:/etc/apt/sources.list:ro
# 创建临时目录
- /var/www/server/nginx/proxy_temp_dir
- /var/www/server/nginx/proxy_cache_dir
depends_on:
- php-fpm
networks:
- sass-platform-net
labels:
- "com.docker.compose.project.working_dir=${PROJECT_NAME}"
db:
image: mysql:5.7.44
container_name: ${PROJECT_NAME}_mysql
environment:
<<: *shared-api-env
volumes:
- mysql_db_data:/var/lib/mysql
- ./docker/mysql/init:/docker-entrypoint-initdb.d
- ./docker/mysql/my.cnf:/etc/mysql/conf.d/custom.cnf
ports:
- ${MYSQL_PORT:-3306}:3306
networks:
- sass-platform-net
restart: unless-stopped
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --innodb_buffer_pool_size=256M
labels:
- "com.docker.compose.project.working_dir=${PROJECT_NAME}"
# Redis 服务(可选)
redis:
image: redis:8.2
container_name: ${PROJECT_NAME}_redis
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD:-luckyshop123!@#}
REDISCLI_AUTH: ${REDIS_PASSWORD:-luckyshop123!@#}
ports:
- "${REDIS_PORT:-6379}:6379"
volumes:
- redis_data:/data
- ./docker/redis/redis.conf:/etc/redis/redis.conf
networks:
- sass-platform-net
restart: unless-stopped
labels:
- "com.docker.compose.project.working_dir=${PROJECT_NAME}"
volumes:
mysql_db_data:
redis_data:
xdebug_logs:
networks:
sass-platform-net:
driver: bridge

View File

@@ -14,7 +14,7 @@ services:
build:
context: ./docker/php
dockerfile: Dockerfile
container_name: ${PROJECT_NAME}_php
container_name: ${PROJECT_NAME}_${APP_ENV}_php
restart: always
extra_hosts:
- "host.docker.internal:host-gateway" # 支持主机名解析
@@ -49,19 +49,20 @@ services:
networks:
- sass-platform-net
labels:
- "com.docker.compose.project.working_dir=${PROJECT_NAME}"
- "com.docker.compose.project.working_dir=${PROJECT_NAME}_${APP_ENV}"
nginx:
build:
context: ./docker/nginx
dockerfile: Dockerfile
container_name: ${PROJECT_NAME}_nginx
container_name: ${PROJECT_NAME}_${APP_ENV}_nginx
restart: always
ports:
- "${NGINX_PORT:-80}:80"
- "${NGINX_SSL_PORT:-443}:443"
volumes:
- ./src:/var/www/html:ro
# 挂载项目代码到 Nginx 容器中
- ./src:/var/www/html:rw
# 更新下载源列表以加速apt-get
- ./docker/debian/sources.list:/etc/apt/sources.list:ro
# 创建临时目录
@@ -72,11 +73,11 @@ services:
networks:
- sass-platform-net
labels:
- "com.docker.compose.project.working_dir=${PROJECT_NAME}"
- "com.docker.compose.project.working_dir=${PROJECT_NAME}_${APP_ENV}"
db:
image: mysql:5.7.44
container_name: ${PROJECT_NAME}_mysql
container_name: ${PROJECT_NAME}_${APP_ENV}_mysql
environment:
<<: *shared-api-env
volumes:
@@ -93,12 +94,12 @@ services:
- --collation-server=utf8mb4_unicode_ci
- --innodb_buffer_pool_size=256M
labels:
- "com.docker.compose.project.working_dir=${PROJECT_NAME}"
- "com.docker.compose.project.working_dir=${PROJECT_NAME}_${APP_ENV}"
# Redis 服务(可选)
redis:
image: redis:8.2
container_name: ${PROJECT_NAME}_redis
container_name: ${PROJECT_NAME}_${APP_ENV}_redis
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD:-luckyshop123!@#}
REDISCLI_AUTH: ${REDIS_PASSWORD:-luckyshop123!@#}
@@ -111,13 +112,32 @@ services:
- sass-platform-net
restart: unless-stopped
labels:
- "com.docker.compose.project.working_dir=${PROJECT_NAME}"
- "com.docker.compose.project.working_dir=${PROJECT_NAME}_${APP_ENV}"
volumes:
mysql_db_data:
name: ${PROJECT_NAME}_${APP_ENV}_mysql_db_data
driver: local
driver_opts:
type: none
o: bind
device: ./docker/mysql_db_data/${APP_ENV}
redis_data:
name: ${PROJECT_NAME}_${APP_ENV}_redis_data
driver: local
driver_opts:
type: none
o: bind
device: ./docker/redis_data/${APP_ENV}
xdebug_logs:
name: ${PROJECT_NAME}_${APP_ENV}_xdebug_logs
driver: local
driver_opts:
type: none
o: bind
device: ./docker/xdebug_logs/${APP_ENV}
networks:
sass-platform-net:
name: ${PROJECT_NAME}_${APP_ENV}_net
driver: bridge

File diff suppressed because one or more lines are too long

10
docker/mysql_db_data/.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
# 忽略目录下所有文件和子目录
*
# 忽略所有子目录
*/
# 但不忽略 .gitkeep 文件
!.gitkeep
# 不忽略 .gitignore 文件自身
!.gitignore
# 不忽略 development/.gitkeep 文件
!development/.gitkeep

View File

View File

@@ -27,6 +27,7 @@ RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
iputils-ping \
&& rm -rf /var/lib/apt/lists/*
# 安装 PHP 扩展
@@ -66,21 +67,9 @@ RUN echo "zend_extension=xdebug.so" > /usr/local/etc/php/conf.d/xdebug.ini
# RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# RUN composer install --no-dev --optimize-autoloader --working-dir=/var/www/html
# # 创建非 root 用户
# RUN useradd -m -u 1000 phpuser && chown -R phpuser:phpuser /var/www/html
# 设置权限, 防止以下目录无法写入的问题
RUN chmod -R a+rw /var/www/html/runtime
RUN chmod -R a+rw /var/www/html/uploads
RUN chmod -R a+rw /var/www/html/tmp
RUN chmod -R a+rw /var/www/html/temp
# USER phpuser
# 暴露端口
EXPOSE 9000 9003
############ 查看 cron 进程
## 查看 cron 进程
# ps aux | grep "think cron:schedule"
@@ -95,4 +84,10 @@ EXPOSE 9000 9003
#######################################
# 启动Supervisor
# 添加在Dockerfile末尾CMD命令之前
COPY ./entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
# 修改CMD命令
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

59
docker/php/entrypoint.sh Normal file
View File

@@ -0,0 +1,59 @@
#!/bin/bash
set -e
# 设置全局umask
umask 0002
echo "=== ThinkPHP Docker权限初始化 ==="
# 修复目录所有权和权限
fix_directory_permissions() {
local dir=$1
echo "修复目录权限: $dir"
# 确保目录存在
mkdir -p "$dir"
# 设置所有权
chown -R www-data:www-data "$dir"
# 设置权限
chmod -R 775 "$dir"
# 设置setgid权限
chmod g+s "$dir"
# 尝试设置ACL如果支持
if command -v setfacl >/dev/null 2>&1; then
setfacl -d -m u:www-data:rwx -m u:root:rwx "$dir" 2>/dev/null || true
setfacl -Rm u:www-data:rwx "$dir" 2>/dev/null || true
fi
echo "$dir 权限设置完成"
}
# 处理所有需要权限的目录
directories=("runtime" "upload")
for dir in "${directories[@]}"; do
fix_directory_permissions "/var/www/html/$dir"
done
# 验证权限
echo "=== 权限验证 ==="
echo "当前用户: $(whoami)"
echo "当前UID: $(id -u), GID: $(id -g)"
echo "当前umask: $(umask)"
# 测试写入权限
sudo -u www-data mkdir -p /var/www/html/runtime/test_dir 2>/dev/null && \
echo "✅ runtime目录新建子目录测试通过" || \
echo "❌ runtime目录新建子目录失败"
sudo -u www-data mkdir -p /var/www/html/upload/test_dir 2>/dev/null && \
echo "✅ upload目录新建子目录测试通过" || \
echo "❌ upload目录新建子目录失败"
echo "=== 启动应用 ==="
# 执行原有的启动命令
exec "$@"

View File

@@ -6,25 +6,40 @@ logfile_backups=10
loglevel=info
pidfile=/var/run/supervisord.pid
[program:chmod]
command=/bin/bash -c "while true; do chmod -R 775 /var/www/html/runtime/ /var/www/html/upload/ 2>/dev/null || true; sleep 30; done"
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
[program:php-fpm]
command=php-fpm
autostart=true
autorestart=true
startretries=3
startsecs=1
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
stopasgroup=true
killasgroup=true
stdout_logfile=/var/log/supervisor/php-fpm.log
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=10
stderr_logfile=/var/log/supervisor/php-fpm-error.log
stderr_logfile_maxbytes=10MB
stderr_logfile_backups=10
[program:think-cron]
command=php /var/www/html/think cron:schedule
environment=APP_ENV=local
process_name=%(program_name)s_%(process_num)02d
numprocs=1
autostart=true
autorestart=true
startretries=5
startsecs=2
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
startretries=3
stdout_logfile=/var/log/supervisor/think-cron.log
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=10
stderr_logfile=/var/log/supervisor/think-cron-error.log
stderr_logfile_maxbytes=10MB
stderr_logfile_backups=10
startsecs=3
stopwaitsecs=10

10
docker/redis_data/.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
# 忽略目录下所有文件和子目录
*
# 忽略所有子目录
*/
# 但不忽略 .gitkeep 文件
!.gitkeep
# 不忽略 .gitignore 文件自身
!.gitignore
# 不忽略 development/.gitkeep 文件
!development/.gitkeep

View File

10
docker/xdebug_logs/.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
# 忽略目录下所有文件和子目录
*
# 忽略所有子目录
*/
# 但不忽略 .gitkeep 文件
!.gitkeep
# 不忽略 .gitignore 文件自身
!.gitignore
# 不忽略 development/.gitkeep 文件
!development/.gitkeep

View File

128
docs/GIT_REALEASE.md Normal file
View File

@@ -0,0 +1,128 @@
适用于中大型团队的方案主要有两种:
---
## ✅ 推荐方案一:**GitFlow适合版本发布节奏明确的项目**
### 📌 核心分支说明
| 分支 | 作用 | 生命周期 | 是否长期存在 |
|------|------|--------|------------|
| `main`(或 `master` | **生产环境代码**,每个 commit 对应一个可发布版本 | 永久 | ✅ |
| `develop` | **集成开发分支**,最新开发成果,用于测试环境部署 | 永久 | ✅ |
| `feature/*` | 功能开发分支(如 `feature/user-auth` | 临时 | ❌ |
| `release/*` | 发布准备分支(如 `release/v1.2.0` | 临时 | ❌ |
| `hotfix/*` | 紧急线上修复分支(如 `hotfix/login-bug` | 临时 | ❌ |
### 🔁 典型流程
1. **日常开发**
-`develop` 拉出 `feature/xxx`
- 开发完成后,**PR/MR 合并回 `develop`**
2. **准备发布**
-`develop` 拉出 `release/vX.Y.Z`
- 在此分支修复 bug、更新版本号、生成 changelog
- 测试通过后:
- 合并到 `main`(打 tag`v1.2.0`
- 合并回 `develop`(同步修复)
3. **紧急修复**
-`main`(或对应 tag拉出 `hotfix/xxx`
- 修复后:
- 合并到 `main`(打新 patch tag`v1.2.1`
- 合并到 `develop`
### ✅ 优点
- 版本清晰,适合有明确发布周期的系统(如每月发版)
- 支持并行开发与紧急修复
- `main` 始终代表线上状态
### ⚠️ 缺点
- 分支较多,对小型团队略显复杂
- 不适合持续部署CI/CD 频繁上线)场景
---
## ✅ 推荐方案二:**Trunk-Based Development + Release Branches适合 DevOps / 持续交付)**
> 越来越多互联网公司(如 Google、Facebook、Netflix采用此模式尤其适合**高频发布、自动化 CI/CD** 的后台服务。
### 📌 核心分支说明
| 分支 | 作用 |
|------|------|
| `main`(或 `trunk` | **唯一主干分支**,所有开发直接或间接流向这里,保持可随时发布状态 |
| `release/*`(可选) | 仅在需要维护多个线上版本时使用(如 `release/v1.3` |
| `feature/*`(短生命周期) | 功能分支,**必须短(<1天~2天**,通过 PR 快速合并到 `main` |
> 💡 实践中常配合 **Feature Toggle功能开关**,即使未完成的功能也可合入 `main`,但默认关闭。
### 🔁 典型流程
1. 开发者从 `main` 拉出短命 `feature/xxx`
2. 提交 PR → 自动化测试(单元、集成、安全扫描)→ Code Review
3. 合并到 `main`
4. **CI/CD 自动部署到测试/预发环境**
5. 人工验证后,**一键发布到生产**(或自动金丝雀发布)
6. 若需回滚,直接回退 `main` 的 commit 或触发回滚流程
### ✅ 优点
- 极简分支模型,减少合并冲突
- 支持每天多次发布
- 与现代 CI/CD 工具链Jenkins, GitLab CI, ArgoCD 等)天然契合
### ⚠️ 要求
- 强大的自动化测试覆盖(>80%
- 快速 Code Review 文化
- 监控与快速回滚能力
---
## 🏆 企业后台服务推荐选择
| 场景 | 推荐策略 |
|------|--------|
| **传统企业、月度/季度发版、强合规要求** | ✅ **GitFlow** |
| **互联网公司、SaaS 服务、每日/每小时发布** | ✅ **Trunk-Based + Short-lived Feature Branches** |
| **混合模式(主干开发 + 定期大版本)** | 主干开发为主,大版本前切 `release` 分支 |
> 🔔 **当前趋势**:越来越多企业后台服务(尤其是微服务架构)倾向于 **Trunk-Based**,因为:
> - 后台服务通常无“客户端版本”约束
> - 可独立部署、灰度发布
> - 自动化程度高
---
## 🔐 补充建议(无论哪种策略)
1. **保护关键分支**
- 在 GitHub/GitLab 中设置:
- `main` / `develop`**protected branch**
- 要求 **PR/MR + 至少1人审批 + CI 通过**
- (可选)要求 **GPG 签名提交**
2. **标准化 Commit & PR 模板**
- 使用 Conventional Commits`feat:`, `fix:`, `chore:`
- 自动生成 changelog 和版本号(配合 semantic-release
3. **Tag 语义化版本**
- 所有生产发布必须打 tag`v1.2.3`
- 格式遵循 [SemVer](https://semver.org/)
4. **禁止直接 push 到主干**
- 所有代码必须通过 PR/MR 合并
---
## 📌 总结:最优实践(推荐)
> **对于大多数现代企业后台服务,采用:**
> **✅ Trunk-Based Development主干开发 + 短生命周期 feature 分支 + 自动化 CI/CD**
> 是最高效、可扩展、符合 DevOps 理念的方式。
只有在**强版本管控、多客户定制、无法频繁上线**等特殊场景下,才考虑 GitFlow。
---
如果你能提供更多信息如团队规模、发布频率、是否微服务、CI/CD 成熟度),我可以给出更定制化的建议!

View File

@@ -0,0 +1,26 @@
# 华为支付插件
## Demo
```
安卓快应用ID115644647
安卓快应用包名com.jieganfsj.fivegshop
安卓快应用名称:秸秆粉碎机
商户名称:徐州明文机械有限公司
商户号102751500028
开发者ID10086000901972225
支付ID10086000901972225
公钥:
MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEA9g1+QcqvC4f1pUiwJ1um1iBUlNn6hRDJrNdv5zB77l5DNo6S6hE4w7VyhkMnkIk89i8kTej1m1ByjRpo7B5OPqafNqI9JBQyQ26A1Zp71zSfe/UicAFiMtF4lWNnAHBYH06sUTvybwYllDVybpi6lL2i8VAGIN8YgoK36lPaYsxWZ911lPCegy7B3kDj1xhBe41cNHgu8wYmjqLU7njleY5Pseherx+Kb58aQvB5xQr8w7KgAyMrsfRH30Btpg/ZWRn8qOXd/DW6eEla3djah4ug8jKdi0qUkA24FLDdOZST4vb5qhgQDVXpqJhYmBIU14YOHsCX9Olu6b7DDjQo/dvOaY3vzWROfV+sV60fUVIps8Vy1EpS/UXeHUxg6r37U8WAxUbSV8d6e4VylLuiIgbX5JpSC1s7jq/cwUwXfSJmKzaCj+C+LJ958IM17FYxIz5xWJtZEzWsPAH7WVCP3b1m4MHU/UwGuMu/Gfdzusnr+Qtan6Wqn9AqUyJP/JfrAgMBAAE=
支付私钥:
MIIG/QIBADANBgkqhkiG9w0BAQEFAASCBucwggbjAgEAAoIBgQD2DX5Byq8Lh/WlSLAnW6bWIFSU2fqFEMms12/nMHvuXkM2jpLqETjDtXKGQyeQiTz2LyRN6PWbUHKNGmjsHk4+pp82oj0kFDJDboDVmnvXNJ979SJwAWIy0XiVY2cAcFgfTqxRO/JvBiWUNXJumLqUvaLxUAYg3xiCgrfqU9pizFZn3XWU8J6DLsHeQOPXGEF7jVw0eC7zBiaOotTueOV5jk+x6F6vH4pvnxpC8HnFCvzDsqADIyux9EffQG2mD9lZGfyo5d38Nbp4SVrd2NqHi6DyMp2LSpSQDbgUsN05lJPi9vmqGBANVemomFiYEhTXhg4ewJf06W7pvsMONCj9285pje/NZE59X6xXrR9RUimzxXLUSlL9Rd4dTGDqvftTxYDFRtJXx3p7hXKUu6IiBtfkmlILWzuOr9zBTBd9ImYrNoKP4L4sn3nwgzXsVjEjPnFYm1kTNaw8AftZUI/dvWbgwdT9TAa4y78Z93O6yev5C1qfpaqf0CpTIk/8l+sCAwEAAQKCAYAVlJFiS9iWdlJBMOLiUNONLEC+3W9vhE1r72lNKZ91BKd4fYC9Ls1/vMZSqEksEB1cqj3Q54HDIYcqgQp6yx2puQt1yzz5kRvndiWulmIOOftS7+kZUcW/F0gwMguyqifQdyH97fgRbMSW/ykOMi8LJKbJ627eKzMHH1fqIXih+bIKYg4SBhihANTYHXDeSK5Vm8xefbwAbKWtFPMAB3J4+tZakDrduTJ3H8k53cWQVqpcr6oBHHCUpww2tHvpeLI3a/FXyHYBqrx8ErnXCjkVBHBtwQf+43H+buyDjrYUwJUi3RSJgeefcyyJoO0I9GwGb6nY8kK5kZ0aeIIkfiVOexMYS9w11FVl96LjC7HjJQrNY4jOLI/X76xyEwBFy9LNogRTafjZVZmVRj/9Kembx6+/eDxvyEv5FnelsHqfFbv1KPkR6e7FvwpDbYgJBpfKTE6SICqo52bHxewpSL7KHRlTpp0IfMM/IOOs8CCwI37ixKQun6W4en457j/tP4ECgcEA+0pa3omCKJfiVR4PpyXY+t9FEqdcZkdYEDtCSX9vqu6yE4sdt7rNnZ6cIiD6ViC2xvQ5VLry1y57JvP57svt5OJYhmILuj3jJ7pdcfKRAVaV4W7C8vGMit6ssckEOnOSj/bvEazfHorjXKU9cn9uCymczRQC9azaRkN+1LLk2mBbczghxd7rSJyXtbt9NVNUi9Gr11AzJsrWN/Bqqna5BizBLIaGYthJ/04LYTl1AjWPEJXjXI72/WvMdQ/w6itxAoHBAPqqAi5mbvjRDitmLQchc/R3Yl329OaB3GSz3YtFqfk9lLJIyl91+MzxBg1ChHnRFC8MB1s1Z05ZkFmcy8AVqvJ6MSZhUCLNjq8HyVgp24CqQTr8ciomF03ncXnKKSsH7tidInkQ8R3e7qRRGQMnDvV+Hs3FjlOk8vJyZCcGkUCVA0kkwkk/95qIfrZsthtewAxJR/rY63FnG+MUayEBB1lwyJZ7xDi/GyX6SAnPBl6bLRA1BdBeTV8K0MEwwIqzGwKBwBqo+9UKT7XQz2FqbAy2tjt/fouJGAN95DjsoI69p3JCGsB6DPAWMIRddIEmcIi8tceL151GrEbqFoS+c7DDD/0timjPdCEROc1YN1vEeV/j+MjPAH3X5KpDD51ZD0rIQi9l6l08svtBjvegTFGedWVXx9v2GI5KBWpY9NbKF/+XI3yo4uRkTyAIBQxx1MnYimq/FvUj/BlMgceziQ2GxQCDtQbtSsqn2cntVMW+28wdNI106YdDX67pRerRgyTE8QKBwHAwLxG9XuWWC5V5AaYzXsaHuEr+ANY6QP4BUqLG5zBaU3cIBSt8jYKMTX0ZzFkJLtNvussjt7zlcSnqd3bdO8mSzvSykT9CaR4FiiQfd9K6YL+ZxS8AJWYEtFEiHhLYVho1Gfy9jG0mHgEFGwDCNnvBmt/WD8F4DhRdBl5BHjmdd/8AqMRIEPXlKXFUbp0JZ0MYeVLYS2hSEbUsqlX3M+bgB6bydfw/7FKvFhbtxZgKM70RPizoSBDFsnEE9OgfCQKBwQD4M4f2f678YScvrwQnV2J8SSnJhQbZqht1Rlb34zU0JIcfpwOKpoNvRyZz1BxvzPSm5S3TLDytK16uvAsMFCHVByyCajPozlWnLzeEqvE4DZXZcWeY+/+MZ4nsajLTTeJWos6UrazZcd8/2c301x4OUKfgZWW1tS65MA48X0+Y4uPyOuH365wQ0obBfg4aB85sZl71v7Rveq+COHBstp/BdDVAsdufWCr7Nkbeu/3qh886ZumpYe+89So1oDIuoys=
```
支付密钥用于交易过程中的签名认证,请妥善保管,谨防泄露,签名认证规范参考开发指南服务端开发。

266
docs/api_kefu.md Normal file
View File

@@ -0,0 +1,266 @@
# 智能客服API接口文档
## 一、接口说明
本接口用于连接微信小程序与Dify聊天机器人实现智能客服功能。
## 二、配置说明
### 1. 安装插件
在ThinkPHP后台的插件管理页面中找到智能客服插件aikefu并点击安装按钮。
### 2. 配置插件
1. 进入智能客服配置页面
2. 输入从Dify平台获取的API密钥
3. 配置API基础地址默认https://api.dify.ai/v1
4. 配置聊天接口端点(默认:/chat-messages
5. 启用智能客服功能
### 3. 获取Dify API密钥
1. 登录Dify平台
2. 进入工作台
3. 选择您的聊天机器人项目
4. 点击"发布"按钮
5. 在API访问页面获取API密钥
## 三、接口列表
### 1. 智能客服聊天接口
**接口地址**`/api/kefu/chat`
**请求方式**POST
**请求参数**
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| message | string | 是 | 用户输入的消息内容 |
| user_id | string | 否 | 用户ID默认使用当前登录会员ID |
| conversation_id | string | 否 | 会话ID第一次聊天可不传系统会自动创建 |
| stream | bool | 否 | 是否使用流式响应默认false |
**响应示例**
```json
{
"code": 0,
"message": "success",
"data": {
"conversation_id": "conv_123456789",
"reply": "您好,我是智能客服,有什么可以帮助您的?",
"message_id": "msg_123456789",
"finish_reason": "stop",
"usage": {
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
},
"timestamp": 1640995200
}
```
### 2. 获取会话历史
**接口地址**`/api/kefu/getHistory`
**请求方式**POST
**请求参数**
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| conversation_id | string | 是 | 会话ID |
| user_id | string | 否 | 用户ID默认使用当前登录会员ID |
| limit | int | 否 | 每页条数默认20 |
| offset | int | 否 | 偏移量默认0 |
**响应示例**
```json
{
"code": 0,
"message": "success",
"data": {
"messages": [
{
"id": "msg_123456789",
"role": "user",
"content": "你好",
"created_at": "2023-01-01T00:00:00Z"
},
{
"id": "msg_987654321",
"role": "assistant",
"content": "您好,我是智能客服,有什么可以帮助您的?",
"created_at": "2023-01-01T00:00:01Z"
}
],
"total": 2,
"limit": 20,
"offset": 0
},
"timestamp": 1640995200
}
```
### 3. 创建新会话
**接口地址**`/api/kefu/createConversation`
**请求方式**POST
**请求参数**
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| user_id | string | 否 | 用户ID默认使用当前登录会员ID |
**响应示例**
```json
{
"code": 0,
"message": "success",
"data": {
"conversation_id": "conv_123456789",
"name": "智能客服会话",
"created_at": "2023-01-01T00:00:00Z"
},
"timestamp": 1640995200
}
```
## 四、前端调用示例
### Uniapp调用示例
```javascript
// 引入请求封装(根据项目实际情况调整)
import { request } from '@/utils/request';
// 智能客服聊天
async function chatWithAI(message, conversationId = '') {
try {
const res = await request({
url: '/api/kefu/chat',
method: 'POST',
data: {
message: message,
conversation_id: conversationId,
// user_id: 'your-user-id', // 可选
// stream: false // 可选
}
});
if (res.code === 0) {
return res.data;
} else {
console.error('聊天失败:', res.message);
return null;
}
} catch (error) {
console.error('聊天请求失败:', error);
return null;
}
}
// 获取会话历史
async function getChatHistory(conversationId, limit = 20, offset = 0) {
try {
const res = await request({
url: '/api/kefu/getHistory',
method: 'POST',
data: {
conversation_id: conversationId,
limit: limit,
offset: offset
// user_id: 'your-user-id', // 可选
}
});
if (res.code === 0) {
return res.data;
} else {
console.error('获取历史记录失败:', res.message);
return null;
}
} catch (error) {
console.error('获取历史记录请求失败:', error);
return null;
}
}
// 创建新会话
async function createNewConversation() {
try {
const res = await request({
url: '/api/kefu/createConversation',
method: 'POST',
data: {
// user_id: 'your-user-id', // 可选
}
});
if (res.code === 0) {
return res.data.conversation_id;
} else {
console.error('创建会话失败:', res.message);
return null;
}
} catch (error) {
console.error('创建会话请求失败:', error);
return null;
}
}
```
## 五、使用流程
1. **初始化会话**:小程序端进入客服页面时,调用`createConversation`接口创建新会话或使用本地存储的会话ID
2. **发送消息**:用户输入消息后,调用`chat`接口发送消息,获取机器人回复
3. **显示消息**:将用户消息和机器人回复显示在聊天界面
4. **加载历史记录**:需要时调用`getHistory`接口加载历史消息
5. **维护会话**保持会话ID用于后续消息交流
## 六、注意事项
1. 请确保Dify API密钥的安全性不要泄露给前端
2. 建议对用户ID进行加密处理避免直接使用敏感信息
3. 对于大量用户的场景,建议实现会话管理机制,定期清理过期会话
4. 建议添加请求频率限制,防止恶意请求
5. 在生产环境中建议关闭DEBUG模式
## 七、测试建议
1. 首先在Dify平台测试聊天机器人功能是否正常
2. 在后端配置正确的API密钥
3. 使用Postman或类似工具测试后端API接口
4. 在小程序端集成并测试完整流程
5. 模拟不同场景下的用户输入,测试机器人回复效果
## 八、常见问题
### 1. 接口返回401错误
**原因**Dify API密钥无效或过期
**解决方法**重新获取有效的API密钥并更新插件配置
### 2. 接口返回500错误
**原因**后端服务器错误或Dify API服务异常
**解决方法**查看服务器日志检查Dify API服务状态
### 3. 机器人回复为空
**原因**Dify聊天机器人配置问题或请求参数错误
**解决方法**检查Dify机器人配置验证请求参数是否正确
### 4. 会话ID无效
**原因**:会话已过期或不存在
**解决方法**创建新会话获取新的会话ID

670
docs/common/CLodop.md Normal file
View File

@@ -0,0 +1,670 @@
好的!我来详细介绍一下**Web打印服务CLodop**,这是一个非常强大的浏览器打印解决方案。
## 🖨️ **CLodop - 专业Web打印服务详解**
### **1. CLodop 是什么?**
**CLodop**Cloud Lodop是**C-Lodop 云打印服务**的简称,是 Lodop 技术的一个轻量级版本,专门为 Web 应用提供强大的打印功能。
#### **核心特点**
-**跨浏览器兼容** - 支持所有主流浏览器
-**无需安装插件** - 基于云服务架构
-**精确打印控制** - 像素级打印精度
-**批量打印支持** - 高效处理大量打印任务
-**多种打印方式** - 直接打印、预览打印、设计打印
### **2. CLodop 与 Lodop 的区别**
| 特性 | CLodop云打印 | Lodop传统打印 |
|------|-----------------|------------------|
| **架构** | 云服务模式,无需安装 | 需要安装本地插件 |
| **部署** | 服务端部署,客户端零配置 | 每台客户端都需要安装 |
| **更新** | 服务端统一更新 | 每台客户端单独更新 |
| **兼容性** | 支持所有现代浏览器 | 对高版本浏览器支持有限 |
| **适用场景** | Web应用、移动端 | 企业内部系统、局域网 |
### **3. CLodop 核心架构**
#### **系统架构图**
```
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Web应用 │ -> │ CLodop服务 │ -> │ 打印机 │
│ (浏览器) │ │ (服务端) │ │ (本地/网络) │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
JavaScript 打印服务 打印输出
API 任务队列
```
### **4. 完整安装和配置**
#### **4.1 服务端安装Windows**
```bash
# 下载CLodop服务端
# 官方下载地址http://www.c-lodop.com/download.html
# 安装步骤:
1. 下载 CLodop_Setup.exe
2. 以管理员身份运行安装
3. 默认安装目录C:\Program Files (x86)\CLodop
4. 服务自动启动监听端口8000、18000
```
#### **4.2 服务端配置文件**
```json
// CLodop 服务配置 (config.json)
{
"server": {
"port": 8000,
"sslPort": 8443,
"host": "0.0.0.0"
},
"print": {
"defaultPaper": "A4",
"dpi": 300,
"timeout": 30000
},
"security": {
"allowedOrigins": ["http://localhost:3000", "https://yourdomain.com"],
"authRequired": false
}
}
```
#### **4.3 验证安装**
```bash
# 检查服务状态
netstat -ano | findstr :8000
# 访问测试页面
http://localhost:8000
```
### **5. 前端集成完整代码**
#### **5.1 基础集成方案**
```html
<!DOCTYPE html>
<html>
<head>
<title>CLodop 打印示例</title>
<!-- 引入CLodop JS文件 -->
<script src="http://localhost:8000/CLodopfuncs.js"></script>
<script>
// 检查CLodop服务状态
function checkCLodop() {
try {
if (getCLodop()) {
console.log('CLodop服务已就绪');
return true;
} else {
console.warn('CLodop服务未启动');
return false;
}
} catch (e) {
console.error('CLodop检查失败:', e);
return false;
}
}
// 获取CLodop对象
function getCLodop() {
if (window.getCLodop) {
return window.getLodop();
}
return null;
}
</script>
</head>
<body onload="checkCLodop()">
<!-- 打印内容 -->
<div id="printContent">
<h1>可打印内容</h1>
<table border="1" style="width:100%">
<tr><th>姓名</th><th>年龄</th><th>职位</th></tr>
<tr><td>张三</td><td>30</td><td>工程师</td></tr>
<tr><td>李四</td><td>25</td><td>设计师</td></tr>
</table>
</div>
<button onclick="printDirect()">直接打印</button>
<button onclick="printPreview()">打印预览</button>
<button onclick="printDesign()">打印设计</button>
</body>
</html>
```
#### **5.2 高级打印控制器**
```javascript
// clodop-manager.js
class CLodopManager {
constructor() {
this.lodop = null;
this.isReady = false;
this.init();
}
// 初始化CLodop
init() {
if (typeof window.getCLodop === 'undefined') {
console.error('CLodop未加载请检查脚本引入');
return;
}
this.lodop = window.getLodop();
if (this.lodop) {
this.isReady = true;
console.log('CLodop初始化成功');
} else {
console.error('CLodop初始化失败');
}
}
// 检查服务状态
checkStatus() {
if (!this.lodop) return false;
try {
const status = this.lodop.PRINT_STATUS;
return status === 'READY';
} catch (e) {
return false;
}
}
// 创建打印任务
createPrintJob(title = '打印文档') {
if (!this.isReady) {
throw new Error('CLodop未就绪');
}
this.lodop.PRINT_INIT(title);
return this;
}
// 设置打印内容
setContent(htmlContent, options = {}) {
const config = {
top: options.top || '10mm',
left: options.left || '10mm',
width: options.width || '190mm',
height: options.height || '277mm',
...options
};
this.lodop.ADD_PRINT_HTML(
config.top, config.left, config.width, config.height,
htmlContent
);
return this;
}
// 设置打印机
setPrinter(printerName = '') {
if (printerName) {
this.lodop.SET_PRINTER_INDEX(printerName);
}
return this;
}
// 设置纸张大小
setPaperSize(paperName = 'A4') {
this.lodop.SET_PRINT_PAGESIZE(1, 0, 0, paperName);
return this;
}
// 设置打印份数
setCopies(copies = 1) {
this.lodop.SET_PRINT_COPIES(copies);
return this;
}
// 直接打印
printDirect() {
if (!this.isReady) return false;
try {
this.lodop.PRINT();
return true;
} catch (e) {
console.error('打印失败:', e);
return false;
}
}
// 打印预览
printPreview() {
if (!this.isReady) return false;
try {
this.lodop.PREVIEW();
return true;
} catch (e) {
console.error('预览失败:', e);
return false;
}
}
// 打印设计
printDesign() {
if (!this.isReady) return false;
try {
this.lodop.PRINT_DESIGN();
return true;
} catch (e) {
console.error('设计失败:', e);
return false;
}
}
// 批量打印
batchPrint(documents = []) {
if (!this.isReady) return false;
try {
documents.forEach((doc, index) => {
this.lodop.PRINT_INIT(`文档${index + 1}`);
this.lodop.ADD_PRINT_HTML("10mm", "10mm", "190mm", "277mm", doc);
if (index < documents.length - 1) {
this.lodop.NEWPAGE(); // 分页
}
});
this.lodop.PREVIEW();
return true;
} catch (e) {
console.error('批量打印失败:', e);
return false;
}
}
}
// 全局实例
window.clodopManager = new CLodopManager();
```
### **6. 实际应用示例**
#### **6.1 票据打印**
```javascript
// receipt-print.js
function printReceipt(orderData) {
const manager = window.clodopManager;
if (!manager.isReady) {
alert('打印服务未就绪');
return;
}
const receiptHTML = `
<div style="width:80mm;font-family:'宋体';font-size:12px;">
<h3 style="text-align:center;">销售小票</h3>
<hr>
<p><strong>订单号:</strong>${orderData.orderNo}</p>
<p><strong>时间:</strong>${new Date().toLocaleString()}</p>
<hr>
<table style="width:100%;">
${orderData.items.map(item => `
<tr>
<td>${item.name}</td>
<td>×${item.quantity}</td>
<td>¥${item.price}</td>
</tr>
`).join('')}
</table>
<hr>
<p style="text-align:right;"><strong>总计:¥${orderData.total}</strong></p>
</div>
`;
manager.createPrintJob('销售小票')
.setPaperSize(1) // 1=80mm 小票
.setContent(receiptHTML, {
width: '80mm',
height: 'auto'
})
.printDirect();
}
```
#### **6.2 报表打印**
```javascript
// report-print.js
function printReport(reportData) {
const manager = window.clodopManager;
const reportHTML = `
<div style="font-family:'微软雅黑';padding:20px;">
<h1 style="text-align:center;">${reportData.title}</h1>
<table border="1" style="width:100%;border-collapse:collapse;">
<thead>
<tr style="background:#f5f5f5;">
${reportData.headers.map(header =>
`<th style="padding:8px;">${header}</th>`
).join('')}
</tr>
</thead>
<tbody>
${reportData.rows.map(row => `
<tr>
${row.map(cell =>
`<td style="padding:6px;">${cell}</td>`
).join('')}
</tr>
`).join('')}
</tbody>
</table>
<div style="margin-top:20px;">
<p>打印时间:${new Date().toLocaleString()}</p>
<p>打印人:${reportData.printUser}</p>
</div>
</div>
`;
manager.createPrintJob(reportData.title)
.setPaperSize('A4')
.setContent(reportHTML)
.setPrinter(reportData.printer || '')
.printPreview();
}
```
#### **6.3 标签打印**
```javascript
// label-print.js
function printLabels(labelsData) {
const manager = window.clodopManager;
manager.createPrintJob('产品标签');
labelsData.forEach((label, index) => {
const labelHTML = `
<div style="width:50mm;height:30mm;border:1px dotted #ccc;padding:5mm;font-size:10px;">
<div style="text-align:center;font-weight:bold;">${label.productName}</div>
<div>规格:${label.spec}</div>
<div>批次:${label.batchNo}</div>
<div>有效期:${label.expiryDate}</div>
<div style="text-align:center;margin-top:2mm;">
<img src="${label.barcodeUrl}" style="height:15mm;">
</div>
</div>
`;
manager.setContent(labelHTML, {
top: `${(index % 5) * 32}mm`,
left: `${Math.floor(index / 5) * 52}mm`,
width: '50mm',
height: '30mm'
});
if (index < labelsData.length - 1) {
manager.lodop.NEWPAGE();
}
});
manager.printPreview();
}
```
### **7. 高级功能实现**
#### **7.1 打印状态监控**
```javascript
// print-monitor.js
class PrintMonitor {
constructor() {
this.printJobs = new Map();
this.initEvents();
}
initEvents() {
// 监听打印开始
window.addEventListener('beforeprint', (e) => {
console.log('打印开始');
this.onPrintStart(e);
});
// 监听打印结束
window.addEventListener('afterprint', (e) => {
console.log('打印结束');
this.onPrintEnd(e);
});
}
onPrintStart(jobId) {
this.printJobs.set(jobId, {
startTime: new Date(),
status: 'printing'
});
}
onPrintEnd(jobId, success = true) {
const job = this.printJobs.get(jobId);
if (job) {
job.endTime = new Date();
job.status = success ? 'completed' : 'failed';
job.duration = job.endTime - job.startTime;
// 发送打印统计
this.sendPrintStatistics(job);
}
}
sendPrintStatistics(job) {
// 发送到服务器记录
fetch('/api/print/log', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(job)
});
}
// 获取打印机列表
getPrinterList() {
const lodop = getCLodop();
if (!lodop) return [];
const printers = [];
const count = lodop.GET_PRINTER_COUNT();
for (let i = 0; i < count; i++) {
printers.push({
name: lodop.GET_PRINTER_NAME(i),
status: lodop.GET_PRINTER_STATUS(i)
});
}
return printers;
}
}
```
#### **7.2 错误处理和重试机制**
```javascript
// error-handler.js
class PrintErrorHandler {
static async printWithRetry(printFunction, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const result = await printFunction();
return result;
} catch (error) {
console.error(`打印尝试 ${attempt} 失败:`, error);
if (attempt === maxRetries) {
throw new Error(`打印失败,已重试 ${maxRetries}`);
}
// 等待后重试
await this.delay(1000 * attempt);
}
}
}
static delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
static handleCommonErrors(error) {
const errorMap = {
'PRINTER_NOT_FOUND': '打印机未找到,请检查打印机连接',
'OUT_OF_PAPER': '打印机缺纸,请添加纸张',
'SERVICE_UNAVAILABLE': '打印服务不可用请检查CLodop服务',
'TIMEOUT': '打印超时,请重试'
};
const message = errorMap[error.code] || `打印错误: ${error.message}`;
this.showErrorDialog(message);
}
static showErrorDialog(message) {
// 可以替换为更友好的UI提示
alert(`打印错误: ${message}`);
}
}
```
### **8. 部署和配置最佳实践**
#### **8.1 Docker 部署**
```dockerfile
# Dockerfile
FROM windows/servercore:ltsc2019
# 安装CLodop服务
COPY CLodop_Setup.exe /setup/
RUN /setup/CLodop_Setup.exe /S
# 暴露端口
EXPOSE 8000 18000
# 启动服务
CMD ["C:\\Program Files (x86)\\CLodop\\CLodop.exe", "-start"]
```
#### **8.2 Nginx 反向代理配置**
```nginx
# nginx.conf
server {
listen 80;
server_name print.yourdomain.com;
location /CLodop/ {
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# WebSocket 支持
location /CLodop/ws {
proxy_pass http://localhost:8000/ws;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
```
### **9. 安全配置**
#### **9.1 访问控制**
```javascript
// security-middleware.js
const express = require('express');
const app = express();
// IP白名单
const allowedIPs = ['192.168.1.0/24', '10.0.0.0/8'];
app.use('/print', (req, res, next) => {
const clientIP = req.ip || req.connection.remoteAddress;
if (!isIPAllowed(clientIP, allowedIPs)) {
return res.status(403).json({ error: '访问被拒绝' });
}
next();
});
function isIPAllowed(ip, allowedRanges) {
// IP地址验证逻辑
return allowedRanges.some(range => {
// 简单的IP范围检查实现
return ip.startsWith(range.split('.')[0]);
});
}
```
### **10. 性能优化**
#### **10.1 打印任务队列**
```javascript
// print-queue.js
class PrintQueue {
constructor() {
this.queue = [];
this.isProcessing = false;
this.maxConcurrent = 1; // 串行打印
}
async addJob(printJob) {
return new Promise((resolve, reject) => {
this.queue.push({ printJob, resolve, reject });
this.processQueue();
});
}
async processQueue() {
if (this.isProcessing || this.queue.length === 0) {
return;
}
this.isProcessing = true;
while (this.queue.length > 0) {
const { printJob, resolve, reject } = this.queue.shift();
try {
const result = await printJob();
resolve(result);
} catch (error) {
reject(error);
}
// 延迟一下,避免打印机过载
await this.delay(500);
}
this.isProcessing = false;
}
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
```
## 💡 **总结**
### **CLodop 核心优势**
1. **零客户端安装** - 基于云服务架构
2. **完美浏览器兼容** - 支持所有现代浏览器
3. **企业级功能** - 批量打印、精确控制、多种纸张支持
4. **易于集成** - 简单的 JavaScript API
### **适用场景**
-**企业ERP系统** - 报表、单据打印
-**电商平台** - 订单、发货单打印
-**医疗系统** - 处方、报告打印
-**政府办公** - 公文、证件打印
-**教育系统** - 成绩单、证书打印
### **部署建议**
1. **开发环境**本地安装CLodop服务
2. **测试环境**:内网服务器部署
3. **生产环境**:高可用集群部署,配合负载均衡
CLodop 是解决Web打印难题的优秀方案特别适合需要精确控制打印格式的企业级应用

View File

@@ -0,0 +1,605 @@
# 开发中使用的存储过程
## 统计一下数据库中哪些表中有site_id字段或者store_id字段而且表中是有数据的
```sql
-- 检查符合条件的表
DELIMITER $$
CREATE PROCEDURE CheckTablesWithHasData()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tbl_name VARCHAR(255);
DECLARE cur CURSOR FOR
SELECT DISTINCT TABLE_NAME
FROM information_schema.COLUMNS
WHERE COLUMN_NAME IN ('site_id', 'store_id')
AND TABLE_SCHEMA = DATABASE(); -- 使用当前数据库
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
CREATE TEMPORARY TABLE tmp_results (table_name VARCHAR(255));
OPEN cur;
read_loop: LOOP
FETCH cur INTO tbl_name;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql = CONCAT('INSERT INTO tmp_results SELECT ''', tbl_name, ''' FROM ', tbl_name, ' HAVING COUNT(*) > 0');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
SELECT table_name FROM tmp_results;
DROP TEMPORARY TABLE tmp_results;
END$$
DELIMITER ;
-- 执行
CALL CheckTablesWithHasData();
--- 结果
+-----------------+
| table_name |
+-----------------+
| t_order_info |
| t_order_item |
| t_order_payment |
| t_order_refund |
+-----------------+
```
## 统计一下,数据库中哪些表中是有数据的
```sql
-- 检查符合条件的表
DELIMITER $$
CREATE PROCEDURE CheckTablesIsNotEmpty()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tbl_name VARCHAR(255);
DECLARE cur CURSOR FOR
SELECT DISTINCT TABLE_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE(); -- 使用当前数据库
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
CREATE TEMPORARY TABLE tmp_results (table_name VARCHAR(255));
OPEN cur;
read_loop: LOOP
FETCH cur INTO tbl_name;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql = CONCAT('INSERT INTO tmp_results SELECT ''', tbl_name, ''' FROM ', tbl_name, ' HAVING COUNT(*) > 0');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
SELECT table_name FROM tmp_results;
DROP TEMPORARY TABLE tmp_results;
END$$
DELIMITER ;
-- 执行
CALL CheckTablesIsNotEmpty();
```
## 对于空表或者无数据表,重置 AUTO_INCREMENT
```sql
-- 定义存储过程
DELIMITER $$
CREATE PROCEDURE ResetAutoIncrementForEmptyTables()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tbl_name VARCHAR(255);
DECLARE cnt BIGINT;
-- 声明游标:获取所有表名
DECLARE cur CURSOR FOR
SELECT DISTINCT c.TABLE_NAME
FROM information_schema.COLUMNS c
INNER JOIN information_schema.TABLES t
ON c.TABLE_SCHEMA = t.TABLE_SCHEMA
AND c.TABLE_NAME = t.TABLE_NAME
WHERE
c.TABLE_SCHEMA = DATABASE()
AND t.TABLE_TYPE = 'BASE TABLE'; -- 排除视图
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO tbl_name;
IF done THEN
LEAVE read_loop;
END IF;
-- 动态获取行数
SET @sql = CONCAT('SELECT COUNT(*) INTO @cnt FROM `', tbl_name, '`');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- 如果为空,重置 AUTO_INCREMENT
IF @cnt = 0 THEN
SET @reset_sql = CONCAT('ALTER TABLE `', tbl_name, '` AUTO_INCREMENT = 1');
PREPARE reset_stmt FROM @reset_sql;
EXECUTE reset_stmt;
DEALLOCATE PREPARE reset_stmt;
SELECT CONCAT('Reset AUTO_INCREMENT for table: ', tbl_name) AS message;
END IF;
END LOOP;
CLOSE cur;
END$$
DELIMITER ;
-- 执行
CALL ResetAutoIncrementForEmptyTables();
```
## 数据重置到初始化状态
### 1. 删除数据保留表结构当表中有site_id字段时只删除site_id不为0的数据
```sql
-- 定义存储过程
-- 增强版数据库重置脚本
-- 支持 MySQL/PostgreSQL/SQL Server
DELIMITER $$
DROP PROCEDURE IF EXISTS reset_tables_has_site_id_where;
CREATE PROCEDURE reset_tables_has_site_id_where(
IN p_preserve_site_zero BOOLEAN, -- 是否保留 site_id = 0 的数据
IN p_dry_run BOOLEAN, -- 是否试运行(不实际执行)
IN p_exclude_tables TEXT -- 排除的表列表,逗号分隔
)
BEGIN
DECLARE v_done INT DEFAULT FALSE;
DECLARE v_table_name VARCHAR(255);
DECLARE v_has_site_id INT DEFAULT 0;
DECLARE v_table_count INT DEFAULT 0;
DECLARE v_processed_count INT DEFAULT 0;
DECLARE v_skipped_count INT DEFAULT 0;
DECLARE v_error_count INT DEFAULT 0;
DECLARE v_no_site_id_tables TEXT DEFAULT '';
DECLARE v_excluded_tables TEXT DEFAULT '';
-- 游标声明
DECLARE table_cursor CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME NOT IN (
'sys_operation_log', 'sys_login_log', 'system_parameters' -- 系统表默认排除
)
ORDER BY TABLE_NAME;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
-- 创建详细的日志表
DROP TEMPORARY TABLE IF EXISTS reset_operation_log;
CREATE TEMPORARY TABLE reset_operation_log (
id INT AUTO_INCREMENT PRIMARY KEY,
log_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
table_name VARCHAR(255) NOT NULL,
action_type ENUM('CLEARED', 'SKIPPED', 'ERROR', 'EXCLUDED') NOT NULL,
records_affected INT DEFAULT 0,
sql_statement TEXT,
error_message TEXT,
execution_time_ms INT DEFAULT 0
);
-- 开始事务(确保原子性)
START TRANSACTION;
OPEN table_cursor;
process_tables: LOOP
FETCH table_cursor INTO v_table_name;
IF v_done THEN
LEAVE process_tables;
END IF;
SET v_table_count = v_table_count + 1;
-- 检查是否在排除列表中
IF FIND_IN_SET(v_table_name, p_exclude_tables) > 0 OR
FIND_IN_SET(v_table_name, v_excluded_tables) > 0 THEN
INSERT INTO reset_operation_log (table_name, action_type, records_affected, sql_statement)
VALUES (v_table_name, 'EXCLUDED', 0, '表在排除列表中,跳过处理');
SET v_skipped_count = v_skipped_count + 1;
ITERATE process_tables;
END IF;
-- 检查表是否有 site_id 字段
SELECT COUNT(*) INTO v_has_site_id
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = v_table_name
AND COLUMN_NAME = 'site_id';
SET @start_time = UNIX_TIMESTAMP(NOW(3)) * 1000;
IF v_has_site_id > 0 THEN
-- 构建动态SQL
IF p_preserve_site_zero THEN
SET @sql = CONCAT('DELETE FROM `', v_table_name, '` WHERE site_id != 0');
ELSE
SET @sql = CONCAT('TRUNCATE TABLE `', v_table_name, '`');
END IF;
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, @errmsg = MESSAGE_TEXT;
INSERT INTO reset_operation_log
(table_name, action_type, records_affected, sql_statement, error_message)
VALUES (v_table_name, 'ERROR', 0, @sql, CONCAT(@sqlstate, ' - ', @errmsg));
SET v_error_count = v_error_count + 1;
END;
IF p_dry_run THEN
-- 试运行模式,只记录不执行
INSERT INTO reset_operation_log
(table_name, action_type, records_affected, sql_statement)
VALUES (v_table_name, 'SKIPPED', 0, CONCAT('试运行: ', @sql));
SET v_skipped_count = v_skipped_count + 1;
ELSE
-- 实际执行
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @row_count = ROW_COUNT();
SET @end_time = UNIX_TIMESTAMP(NOW(3)) * 1000;
INSERT INTO reset_operation_log
(table_name, action_type, records_affected, sql_statement, execution_time_ms)
VALUES (v_table_name, 'CLEARED', @row_count, @sql, (@end_time - @start_time));
SET v_processed_count = v_processed_count + 1;
END IF;
END;
ELSE
-- 表没有 site_id 字段
SET v_no_site_id_tables = CONCAT_WS(', ', v_no_site_id_tables, v_table_name);
INSERT INTO reset_operation_log
(table_name, action_type, records_affected, sql_statement)
VALUES (v_table_name, 'SKIPPED', 0, '表没有 site_id 字段,跳过处理');
SET v_skipped_count = v_skipped_count + 1;
END IF;
SET v_has_site_id = 0;
END LOOP;
CLOSE table_cursor;
IF p_dry_run THEN
ROLLBACK; -- 试运行模式回滚
SELECT '=== 试运行模式 - 未实际执行任何操作 ===' as notice;
ELSE
COMMIT; -- 提交事务
SELECT '=== 数据库重置完成 ===' as summary;
END IF;
-- 生成总结报告
SELECT
CONCAT('数据库: ', DATABASE()) as database_info,
CONCAT('开始时间: ', DATE_FORMAT(MIN(log_time), '%Y-%m-%d %H:%i:%s')) as start_time,
CONCAT('结束时间: ', DATE_FORMAT(MAX(log_time), '%Y-%m-%d %H:%i:%s')) as end_time,
CONCAT('总表数: ', v_table_count) as total_tables,
CONCAT('已处理表: ', v_processed_count) as processed_tables,
CONCAT('跳过表: ', v_skipped_count) as skipped_tables,
CONCAT('错误数: ', v_error_count) as error_count,
CONCAT('总耗时: ', SUM(execution_time_ms), 'ms') as total_duration
FROM reset_operation_log;
-- 显示没有 site_id 字段的表
IF v_no_site_id_tables != '' THEN
SELECT
'以下表没有 site_id 字段,已被跳过:' as warning_title,
v_no_site_id_tables as skipped_tables;
END IF;
-- 显示错误详情
IF v_error_count > 0 THEN
SELECT '=== 错误详情 ===' as error_title;
SELECT table_name, error_message, sql_statement
FROM reset_operation_log
WHERE action_type = 'ERROR'
ORDER BY log_time;
END IF;
-- 显示处理详情
SELECT '=== 处理详情 ===' as details_title;
SELECT
table_name as '表名',
CASE action_type
WHEN 'CLEARED' THEN '已清空'
WHEN 'SKIPPED' THEN '已跳过'
WHEN 'ERROR' THEN '错误'
WHEN 'EXCLUDED' THEN '已排除'
END as '操作状态',
records_affected as '影响行数',
execution_time_ms as '耗时(ms)',
CASE
WHEN error_message IS NOT NULL THEN error_message
ELSE LEFT(sql_statement, 100)
END as '详情'
FROM reset_operation_log
ORDER BY action_type, table_name;
-- 性能统计
SELECT '=== 性能统计 ===' as performance_title;
SELECT
action_type as '操作类型',
COUNT(*) as '表数量',
SUM(records_affected) as '总影响行数',
AVG(execution_time_ms) as '平均耗时(ms)',
SUM(execution_time_ms) as '总耗时(ms)'
FROM reset_operation_log
GROUP BY action_type;
-- 清理
DROP TEMPORARY TABLE IF EXISTS reset_operation_log;
END
$$
DELIMITER ;
-- 使用示例
-- 试运行(不实际执行)
CALL reset_tables_has_site_id_where(TRUE, TRUE, 'user,config');
-- 实际执行保留site_id=0的数据排除user和config表
CALL reset_tables_has_site_id_where(TRUE, FALSE, '');
-- 完全清空所有表(不保留任何数据)
CALL reset_tables_has_site_id_where(FALSE, FALSE, '');
```
## 删除数据保留表结构当表中有store_id字段时只删除store_id不为0的数据
```sql
DELIMITER $$
DROP PROCEDURE IF EXISTS reset_tables_has_store_id_where;
CREATE PROCEDURE reset_tables_has_store_id_where(
IN p_preserve_site_zero BOOLEAN, -- 是否保留 store_id = 0 的数据
IN p_dry_run BOOLEAN, -- 是否试运行(不实际执行)
IN p_exclude_tables TEXT -- 排除的表列表,逗号分隔
)
BEGIN
DECLARE v_done INT DEFAULT FALSE;
DECLARE v_table_name VARCHAR(255);
DECLARE v_has_store_id INT DEFAULT 0;
DECLARE v_table_count INT DEFAULT 0;
DECLARE v_processed_count INT DEFAULT 0;
DECLARE v_skipped_count INT DEFAULT 0;
DECLARE v_error_count INT DEFAULT 0;
DECLARE v_no_store_id_tables TEXT DEFAULT '';
DECLARE v_excluded_tables TEXT DEFAULT '';
-- 游标声明
DECLARE table_cursor CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME NOT IN (
'sys_operation_log', 'sys_login_log', 'system_parameters' -- 系统表默认排除
)
ORDER BY TABLE_NAME;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
-- 创建详细的日志表
DROP TEMPORARY TABLE IF EXISTS reset_operation_log;
CREATE TEMPORARY TABLE reset_operation_log (
id INT AUTO_INCREMENT PRIMARY KEY,
log_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
table_name VARCHAR(255) NOT NULL,
action_type ENUM('CLEARED', 'SKIPPED', 'ERROR', 'EXCLUDED') NOT NULL,
records_affected INT DEFAULT 0,
sql_statement TEXT,
error_message TEXT,
execution_time_ms INT DEFAULT 0
);
-- 开始事务(确保原子性)
START TRANSACTION;
OPEN table_cursor;
process_tables: LOOP
FETCH table_cursor INTO v_table_name;
IF v_done THEN
LEAVE process_tables;
END IF;
SET v_table_count = v_table_count + 1;
-- 检查是否在排除列表中
IF FIND_IN_SET(v_table_name, p_exclude_tables) > 0 OR
FIND_IN_SET(v_table_name, v_excluded_tables) > 0 THEN
INSERT INTO reset_operation_log (table_name, action_type, records_affected, sql_statement)
VALUES (v_table_name, 'EXCLUDED', 0, '表在排除列表中,跳过处理');
SET v_skipped_count = v_skipped_count + 1;
ITERATE process_tables;
END IF;
-- 检查表是否有 store_id 字段
SELECT COUNT(*) INTO v_has_store_id
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = v_table_name
AND COLUMN_NAME = 'store_id';
SET @start_time = UNIX_TIMESTAMP(NOW(3)) * 1000;
IF v_has_store_id > 0 THEN
-- 构建动态SQL
IF p_preserve_site_zero THEN
SET @sql = CONCAT('DELETE FROM `', v_table_name, '` WHERE store_id != 0');
ELSE
SET @sql = CONCAT('TRUNCATE TABLE `', v_table_name, '`');
END IF;
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, @errmsg = MESSAGE_TEXT;
INSERT INTO reset_operation_log
(table_name, action_type, records_affected, sql_statement, error_message)
VALUES (v_table_name, 'ERROR', 0, @sql, CONCAT(@sqlstate, ' - ', @errmsg));
SET v_error_count = v_error_count + 1;
END;
IF p_dry_run THEN
-- 试运行模式,只记录不执行
INSERT INTO reset_operation_log
(table_name, action_type, records_affected, sql_statement)
VALUES (v_table_name, 'SKIPPED', 0, CONCAT('试运行: ', @sql));
SET v_skipped_count = v_skipped_count + 1;
ELSE
-- 实际执行
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @row_count = ROW_COUNT();
SET @end_time = UNIX_TIMESTAMP(NOW(3)) * 1000;
INSERT INTO reset_operation_log
(table_name, action_type, records_affected, sql_statement, execution_time_ms)
VALUES (v_table_name, 'CLEARED', @row_count, @sql, (@end_time - @start_time));
SET v_processed_count = v_processed_count + 1;
END IF;
END;
ELSE
-- 表没有 store_id 字段
SET v_no_store_id_tables = CONCAT_WS(', ', v_no_store_id_tables, v_table_name);
INSERT INTO reset_operation_log
(table_name, action_type, records_affected, sql_statement)
VALUES (v_table_name, 'SKIPPED', 0, '表没有 store_id 字段,跳过处理');
SET v_skipped_count = v_skipped_count + 1;
END IF;
SET v_has_store_id = 0;
END LOOP;
CLOSE table_cursor;
IF p_dry_run THEN
ROLLBACK; -- 试运行模式回滚
SELECT '=== 试运行模式 - 未实际执行任何操作 ===' as notice;
ELSE
COMMIT; -- 提交事务
SELECT '=== 数据库重置完成 ===' as summary;
END IF;
-- 生成总结报告
SELECT
CONCAT('数据库: ', DATABASE()) as database_info,
CONCAT('开始时间: ', DATE_FORMAT(MIN(log_time), '%Y-%m-%d %H:%i:%s')) as start_time,
CONCAT('结束时间: ', DATE_FORMAT(MAX(log_time), '%Y-%m-%d %H:%i:%s')) as end_time,
CONCAT('总表数: ', v_table_count) as total_tables,
CONCAT('已处理表: ', v_processed_count) as processed_tables,
CONCAT('跳过表: ', v_skipped_count) as skipped_tables,
CONCAT('错误数: ', v_error_count) as error_count,
CONCAT('总耗时: ', SUM(execution_time_ms), 'ms') as total_duration
FROM reset_operation_log;
-- 显示没有 store_id 字段的表
IF v_no_store_id_tables != '' THEN
SELECT
'以下表没有 store_id 字段,已被跳过:' as warning_title,
v_no_store_id_tables as skipped_tables;
END IF;
-- 显示错误详情
IF v_error_count > 0 THEN
SELECT '=== 错误详情 ===' as error_title;
SELECT table_name, error_message, sql_statement
FROM reset_operation_log
WHERE action_type = 'ERROR'
ORDER BY log_time;
END IF;
-- 显示处理详情
SELECT '=== 处理详情 ===' as details_title;
SELECT
table_name as '表名',
CASE action_type
WHEN 'CLEARED' THEN '已清空'
WHEN 'SKIPPED' THEN '已跳过'
WHEN 'ERROR' THEN '错误'
WHEN 'EXCLUDED' THEN '已排除'
END as '操作状态',
records_affected as '影响行数',
execution_time_ms as '耗时(ms)',
CASE
WHEN error_message IS NOT NULL THEN error_message
ELSE LEFT(sql_statement, 100)
END as '详情'
FROM reset_operation_log
ORDER BY action_type, table_name;
-- 性能统计
SELECT '=== 性能统计 ===' as performance_title;
SELECT
action_type as '操作类型',
COUNT(*) as '表数量',
SUM(records_affected) as '总影响行数',
AVG(execution_time_ms) as '平均耗时(ms)',
SUM(execution_time_ms) as '总耗时(ms)'
FROM reset_operation_log
GROUP BY action_type;
-- 清理
DROP TEMPORARY TABLE IF EXISTS reset_operation_log;
END
$$
DELIMITER ;
-- 实际执行保留site_id=0的数据排除user和config表
CALL reset_tables_has_store_id_where(TRUE, FALSE, '');
```

View File

@@ -0,0 +1,375 @@
import re
import os
# 解析SQL文件提取表结构
def parse_sql_file(file_path, ignore_prefix=None):
tables = {}
# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 匹配CREATE TABLE语句
table_pattern = re.compile(r'CREATE TABLE\s+`?([^`\s]+)`?\s*\(([^;]+)\)\s*(?:[^;]+);', re.DOTALL | re.IGNORECASE)
matches = table_pattern.findall(content)
for full_table_name, table_def in matches:
# 处理表名,忽略前缀
table_name = full_table_name
if ignore_prefix and table_name.startswith(ignore_prefix):
table_name = table_name[len(ignore_prefix):]
# 提取列定义
columns = []
# 匹配列定义,包括列名、类型、约束等
column_pattern = re.compile(r'\s*`?([^`\s,]+)`?\s+([^\s,]+)\s*([^,]+)(?:,|$)', re.DOTALL)
column_matches = column_pattern.findall(table_def)
for col_name, col_type, col_constraints in column_matches:
# 清理约束中的换行符和多余空格
col_constraints = re.sub(r'\s+', ' ', col_constraints.strip())
columns.append((col_name, col_type, col_constraints))
# 提取主键
primary_key = None
pk_pattern = re.compile(r'PRIMARY\s+KEY\s*\(`?([^`\s,]+)`?\)', re.IGNORECASE)
pk_match = pk_pattern.search(table_def)
if pk_match:
primary_key = pk_match.group(1)
# 提取索引
indexes = []
index_pattern = re.compile(r'INDEX\s+`?([^`\s]+)`?\s*\(([^)]+)\)', re.IGNORECASE)
index_matches = index_pattern.findall(table_def)
for index_name, index_cols in index_matches:
indexes.append((index_name, index_cols.strip()))
# 提取唯一约束
unique_constraints = []
unique_pattern = re.compile(r'UNIQUE\s+KEY\s*`?([^`\s]+)`?\s*\(([^)]+)\)', re.IGNORECASE)
unique_matches = unique_pattern.findall(table_def)
for unique_name, unique_cols in unique_matches:
unique_constraints.append((unique_name, unique_cols.strip()))
tables[table_name] = {
'full_name': full_table_name,
'columns': columns,
'primary_key': primary_key,
'indexes': indexes,
'unique_constraints': unique_constraints
}
return tables
# 比较两个数据库表结构
def compare_databases(db1, db2, db1_name, db2_name):
diffs = {
'only_in_db1': [],
'only_in_db2': [],
'column_diffs': {},
'primary_key_diffs': {},
'index_diffs': {},
'unique_constraint_diffs': {}
}
# 找出只在db1中存在的表
for table_name in db1:
if table_name not in db2:
diffs['only_in_db1'].append(table_name)
# 找出只在db2中存在的表
for table_name in db2:
if table_name not in db1:
diffs['only_in_db2'].append(table_name)
# 比较共同存在的表
common_tables = set(db1.keys()) & set(db2.keys())
for table_name in common_tables:
table1 = db1[table_name]
table2 = db2[table_name]
# 比较列
col_diffs = {
'only_in_db1': [],
'only_in_db2': [],
'type_diffs': [],
'constraint_diffs': []
}
# 列名集合
cols1 = {col[0]: col for col in table1['columns']}
cols2 = {col[0]: col for col in table2['columns']}
# 只在db1中的列
for col_name in cols1:
if col_name not in cols2:
col_diffs['only_in_db1'].append(col_name)
# 只在db2中的列
for col_name in cols2:
if col_name not in cols1:
col_diffs['only_in_db2'].append(col_name)
# 比较列类型和约束
common_cols = set(cols1.keys()) & set(cols2.keys())
for col_name in common_cols:
col1 = cols1[col_name]
col2 = cols2[col_name]
# 类型差异
if col1[1] != col2[1]:
col_diffs['type_diffs'].append((col_name, col1[1], col2[1]))
# 约束差异
if col1[2] != col2[2]:
col_diffs['constraint_diffs'].append((col_name, col1[2], col2[2]))
if any(col_diffs.values()):
diffs['column_diffs'][table_name] = col_diffs
# 比较主键
if table1['primary_key'] != table2['primary_key']:
diffs['primary_key_diffs'][table_name] = (table1['primary_key'], table2['primary_key'])
# 比较索引
index_diffs = {
'only_in_db1': [],
'only_in_db2': [],
'definition_diffs': []
}
indexes1 = {idx[0]: idx[1] for idx in table1['indexes']}
indexes2 = {idx[0]: idx[1] for idx in table2['indexes']}
# 只在db1中的索引
for idx_name in indexes1:
if idx_name not in indexes2:
index_diffs['only_in_db1'].append((idx_name, indexes1[idx_name]))
# 只在db2中的索引
for idx_name in indexes2:
if idx_name not in indexes1:
index_diffs['only_in_db2'].append((idx_name, indexes2[idx_name]))
# 比较索引定义
common_indexes = set(indexes1.keys()) & set(indexes2.keys())
for idx_name in common_indexes:
if indexes1[idx_name] != indexes2[idx_name]:
index_diffs['definition_diffs'].append((idx_name, indexes1[idx_name], indexes2[idx_name]))
if any(index_diffs.values()):
diffs['index_diffs'][table_name] = index_diffs
# 比较唯一约束
unique_diffs = {
'only_in_db1': [],
'only_in_db2': [],
'definition_diffs': []
}
unique1 = {uc[0]: uc[1] for uc in table1['unique_constraints']}
unique2 = {uc[0]: uc[1] for uc in table2['unique_constraints']}
# 只在db1中的唯一约束
for uc_name in unique1:
if uc_name not in unique2:
unique_diffs['only_in_db1'].append((uc_name, unique1[uc_name]))
# 只在db2中的唯一约束
for uc_name in unique2:
if uc_name not in unique1:
unique_diffs['only_in_db2'].append((uc_name, unique2[uc_name]))
# 比较唯一约束定义
common_unique = set(unique1.keys()) & set(unique2.keys())
for uc_name in common_unique:
if unique1[uc_name] != unique2[uc_name]:
unique_diffs['definition_diffs'].append((uc_name, unique1[uc_name], unique2[uc_name]))
if any(unique_diffs.values()):
diffs['unique_constraint_diffs'][table_name] = unique_diffs
return diffs
# 打印差异报告
# 生成Markdown格式的差异报告
def generate_markdown_report(diffs, db1_name, db2_name, db1_table_count, db2_table_count):
report = []
# 报告标题
report.append(f"# 数据库差异报告: {db1_name} vs {db2_name}")
report.append("\n## 1. 表数量统计")
report.append("| 数据库文件 | 表数量 |")
report.append("|------------|--------|")
report.append(f"| {db1_name} | {db1_table_count} |")
report.append(f"| {db2_name} | {db2_table_count} |")
# 表存在性差异
report.append("\n## 2. 表存在性差异")
# 仅在db1中的表
if diffs['only_in_db1']:
report.append(f"\n### 2.1 仅在 {db1_name} 中存在的表 ({len(diffs['only_in_db1'])} 个)")
report.append("| 表名 |")
report.append("|------|")
for table in sorted(diffs['only_in_db1']):
report.append(f"| {table} |")
# 仅在db2中的表
if diffs['only_in_db2']:
report.append(f"\n### 2.2 仅在 {db2_name} 中存在的表 ({len(diffs['only_in_db2'])} 个)")
report.append("| 表名 |")
report.append("|------|")
for table in sorted(diffs['only_in_db2']):
report.append(f"| {table} |")
# 列结构差异
if diffs['column_diffs']:
report.append(f"\n## 3. 列结构差异的表 ({len(diffs['column_diffs'])} 个)")
for table, col_diffs in diffs['column_diffs'].items():
report.append(f"\n### 3.1 表: {table}")
# 仅在db1中的列
if col_diffs['only_in_db1']:
report.append(f"\n#### 3.1.1 仅在 {db1_name} 中存在的列")
report.append("| 列名 |")
report.append("|------|")
for col in col_diffs['only_in_db1']:
report.append(f"| {col} |")
# 仅在db2中的列
if col_diffs['only_in_db2']:
report.append(f"\n#### 3.1.2 仅在 {db2_name} 中存在的列")
report.append("| 列名 |")
report.append("|------|")
for col in col_diffs['only_in_db2']:
report.append(f"| {col} |")
# 列类型差异
if col_diffs['type_diffs']:
report.append(f"\n#### 3.1.3 列类型差异")
report.append(f"| 列名 | {db1_name} | {db2_name} |")
report.append("|------|------------|------------|")
for col_name, type1, type2 in col_diffs['type_diffs']:
report.append(f"| {col_name} | {type1} | {type2} |")
# 列约束差异
if col_diffs['constraint_diffs']:
report.append(f"\n#### 3.1.4 列约束差异")
report.append(f"| 列名 | {db1_name} | {db2_name} |")
report.append("|------|------------|------------|")
for col_name, constraint1, constraint2 in col_diffs['constraint_diffs']:
report.append(f"| {col_name} | {constraint1} | {constraint2} |")
# 主键差异
if diffs['primary_key_diffs']:
report.append(f"\n## 4. 主键差异的表 ({len(diffs['primary_key_diffs'])} 个)")
report.append(f"| 表名 | {db1_name} | {db2_name} |")
report.append("|------|------------|------------|")
for table, (pk1, pk2) in diffs['primary_key_diffs'].items():
report.append(f"| {table} | {pk1} | {pk2} |")
# 索引差异
if diffs['index_diffs']:
report.append(f"\n## 5. 索引差异的表 ({len(diffs['index_diffs'])} 个)")
for table, idx_diffs in diffs['index_diffs'].items():
report.append(f"\n### 5.1 表: {table}")
# 仅在db1中的索引
if idx_diffs['only_in_db1']:
report.append(f"\n#### 5.1.1 仅在 {db1_name} 中存在的索引")
report.append("| 索引名 | 索引列 |")
report.append("|--------|--------|")
for idx_name, idx_cols in idx_diffs['only_in_db1']:
report.append(f"| {idx_name} | {idx_cols} |")
# 仅在db2中的索引
if idx_diffs['only_in_db2']:
report.append(f"\n#### 5.1.2 仅在 {db2_name} 中存在的索引")
report.append("| 索引名 | 索引列 |")
report.append("|--------|--------|")
for idx_name, idx_cols in idx_diffs['only_in_db2']:
report.append(f"| {idx_name} | {idx_cols} |")
# 索引定义差异
if idx_diffs['definition_diffs']:
report.append(f"\n#### 5.1.3 索引定义差异")
report.append(f"| 索引名 | {db1_name} | {db2_name} |")
report.append("|--------|------------|------------|")
for idx_name, idx1, idx2 in idx_diffs['definition_diffs']:
report.append(f"| {idx_name} | {idx1} | {idx2} |")
# 唯一约束差异
if diffs['unique_constraint_diffs']:
report.append(f"\n## 6. 唯一约束差异的表 ({len(diffs['unique_constraint_diffs'])} 个)")
for table, uc_diffs in diffs['unique_constraint_diffs'].items():
report.append(f"\n### 6.1 表: {table}")
# 仅在db1中的唯一约束
if uc_diffs['only_in_db1']:
report.append(f"\n#### 6.1.1 仅在 {db1_name} 中存在的唯一约束")
report.append("| 约束名 | 约束列 |")
report.append("|--------|--------|")
for uc_name, uc_cols in uc_diffs['only_in_db1']:
report.append(f"| {uc_name} | {uc_cols} |")
# 仅在db2中的唯一约束
if uc_diffs['only_in_db2']:
report.append(f"\n#### 6.1.2 仅在 {db2_name} 中存在的唯一约束")
report.append("| 约束名 | 约束列 |")
report.append("|--------|--------|")
for uc_name, uc_cols in uc_diffs['only_in_db2']:
report.append(f"| {uc_name} | {uc_cols} |")
# 唯一约束定义差异
if uc_diffs['definition_diffs']:
report.append(f"\n#### 6.1.3 唯一约束定义差异")
report.append(f"| 约束名 | {db1_name} | {db2_name} |")
report.append("|--------|------------|------------|")
for uc_name, uc1, uc2 in uc_diffs['definition_diffs']:
report.append(f"| {uc_name} | {uc1} | {uc2} |")
report.append("\n## 7. 总结")
report.append("差异比较完成!")
return '\n'.join(report)
# 主函数
def main():
# 文件路径
db1_path = r'D:\projects\shop-projects\backend\docs\db\niushop_database.sql'
db2_path = r'D:\projects\shop-projects\backend\docs\db\init_v2.0_with_data.sql'
report_path = r'D:\projects\shop-projects\backend\docs\db\database_diff_report.md'
# 解析数据库结构
print(f"正在解析 {db1_path}...")
db1 = parse_sql_file(db1_path)
db1_table_count = len(db1)
print(f"解析完成,共 {db1_table_count} 个表")
print(f"\n正在解析 {db2_path}...")
db2 = parse_sql_file(db2_path, ignore_prefix='lucky_')
db2_table_count = len(db2)
print(f"解析完成,共 {db2_table_count} 个表")
# 比较差异
print("\n正在比较数据库差异...")
diffs = compare_databases(db1, db2, 'niushop_database.sql', 'init_v2.0_with_data.sql')
# 生成Markdown差异报告
print("\n正在生成Markdown差异报告...")
report = generate_markdown_report(diffs, 'niushop_database.sql', 'init_v2.0_with_data.sql', db1_table_count, db2_table_count)
# 保存报告到文件
with open(report_path, 'w', encoding='utf-8') as f:
f.write(report)
print(f"\n差异报告已生成: {report_path}")
print("差异比较完成!")
if __name__ == "__main__":
main()

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

11578
docs/db/niushop_database.sql Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,117 @@
import re
import os
# 读取 SQL 文件内容
def read_sql_file(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
# 解析 database.sql提取表结构和注释
def parse_database_sql(sql_content):
tables = {}
# 匹配 CREATE TABLE 语句,更灵活的格式
# 匹配所有 CREATE TABLE 语句,不依赖于 ENGINE、CHARACTER SET 等子句的顺序
table_pattern = re.compile(r"CREATE TABLE\s+`?([^`\s]+)`?\s*\(([^;]+)\)\s*(?:[^;]+COMMENT\s*=\s*'([^']+)'[^;]*)?\s*;", re.DOTALL | re.IGNORECASE)
matches = table_pattern.findall(sql_content)
for table_name, table_def, table_comment in matches:
if not table_comment:
# 如果没有匹配到表注释,尝试从其他位置获取
comment_match = re.search(r"COMMENT\s*=\s*'([^']+)'", table_def, re.IGNORECASE)
if comment_match:
table_comment = comment_match.group(1)
else:
table_comment = ''
# 解析列定义和注释
columns = {}
# 匹配列定义,包括 COMMENT
column_lines = table_def.split('\n')
for line in column_lines:
# 匹配列名、类型和注释
column_match = re.search(r"\s*([^\s,]+)\s+([^\s,]+)\s*(?:[^,]+COMMENT\s*=\s*'([^']+)'[^,]*|[^,]*)", line)
if column_match:
column_name = column_match.group(1)
column_comment = column_match.group(3) or ''
if column_comment:
columns[column_name] = column_comment
tables[table_name] = {
'comment': table_comment,
'columns': columns
}
return tables
# 更新 init_v2.0.sql 文件中的注释
def update_init_sql(init_sql_path, database_tables):
# 读取 init_v2.0.sql 内容
init_content = read_sql_file(init_sql_path)
# 匹配 CREATE TABLE 语句,适应 init_v2.0.sql 的格式
table_pattern = re.compile(r"(create table if not exists lucky_([^\s]+)\s*\(([^;]+)\)\s*comment\s*=\s*'[^']*'\s*(.*?);)", re.DOTALL | re.IGNORECASE)
def replace_table(match):
full_match = match.group(0)
table_name = match.group(2)
table_def = match.group(3)
table_suffix = match.group(4)
if table_name in database_tables:
# 获取数据库表的注释和列注释
db_table = database_tables[table_name]
table_comment = db_table['comment']
columns = db_table['columns']
# 更新列注释
new_table_def = table_def
for column_name, column_comment in columns.items():
# 匹配列定义,替换注释
# 格式:列名 类型 default 默认值 not null comment '注释'
column_pattern = re.compile(r"(\s*" + column_name + r"\s+[^\s,]+\s*(?:default\s+[^\s,]+\s*)?(?:not null\s*)?comment\s*=\s*')([^']*)'([^,]*)", re.IGNORECASE)
new_table_def = column_pattern.sub(r"\1" + column_comment + r"'\3", new_table_def)
# 重新构建 CREATE TABLE 语句
new_full_match = f"create table if not exists lucky_{table_name} ({new_table_def}) comment = '{table_comment}' {table_suffix};"
return new_full_match
return full_match
# 替换所有表
updated_content = table_pattern.sub(replace_table, init_content)
# 写回文件
with open(init_sql_path, 'w', encoding='utf-8') as f:
f.write(updated_content)
print(f"Updated {init_sql_path}")
# 主函数
def main():
# 文件路径
database_sql_path = r'./niushop_database.sql'
init_v20_sql_path = r'./init_v2.0.sql'
init_v20_with_data_sql_path = r'./init_v2.0_with_data.sql'
# 解析 database.sql
print("Parsing database.sql...")
database_content = read_sql_file(database_sql_path)
database_tables = parse_database_sql(database_content)
print(f"Found {len(database_tables)} tables in database.sql")
# 更新 init_v2.0.sql
if os.path.exists(init_v20_sql_path):
print("Updating init_v2.0.sql...")
update_init_sql(init_v20_sql_path, database_tables)
# 更新 init_v2.0_with_data.sql
if os.path.exists(init_v20_with_data_sql_path):
print("Updating init_v2.0_with_data.sql...")
update_init_sql(init_v20_with_data_sql_path, database_tables)
print("All files updated successfully!")
if __name__ == "__main__":
main()

27
docs/diy/RADEME.md Normal file
View File

@@ -0,0 +1,27 @@
# 自定义页面
## 对应的组件存放到数据表中
表: lucky_diy_view_util
```sql
create table if not exists lucky_diy_view_util
(
id int auto_increment
primary key,
name varchar(50) default '' not null comment '标识',
title varchar(50) default '' not null comment '组件名称',
type varchar(50) default 'SYSTEM' not null comment '组件类型',
value text null comment '配置:json格式',
addon_name varchar(50) default '' not null comment '插件标识',
sort int default 0 not null comment '排序号',
support_diy_view varchar(500) default '' not null comment '支持的自定义页面(为空表示公共组件都支持)',
max_count int default 0 not null comment '限制添加次数',
is_delete int default 0 not null comment '是否可以删除0 允许1 禁用',
icon varchar(255) default '' not null comment '组件图标',
icon_type int default 0 not null comment '0图片1图标',
constraint name
unique (name)
)
```

170
replace_comments.py Normal file
View File

@@ -0,0 +1,170 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import argparse
# 定义要查找和替换的注释内容
# 使用原始字符串数组支持多个旧内容避免Unicode转义问题
OLD_COMMENTS = [
r"""
<?php
/**
*/
""".strip(),
r"""
<?php
/**
*/
""".strip(),
# 格式1标准注释格式
r"""
<?php
/**
*/
""".strip(),
r"""
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
""".strip(),
# 格式1标准注释格式
r"""
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/""".strip(),
# 格式2带有额外空行的注释格式
r"""
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/""".strip(),
]
NEW_COMMENT = r"""
<?php
""".strip()
# 定义要处理的文件类型
FILE_TYPES = [".php", ".js", ".css", ".html", ".vue", ".ts", ".tsx", ".jsx", ".scss", ".less"]
def replace_comments(file_path):
"""
替换文件中的注释
"""
try:
# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 检查文件是否包含任何旧注释
has_old_comment = False
for old_comment in OLD_COMMENTS:
if old_comment in content:
has_old_comment = True
break
if has_old_comment:
# 替换所有旧注释
new_content = content
for old_comment in OLD_COMMENTS:
if old_comment in new_content:
new_content = new_content.replace(old_comment, NEW_COMMENT)
# 写回文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"✓ 已处理: {file_path}")
return True
return False
except UnicodeDecodeError:
# 跳过二进制文件
print(f"✗ 跳过二进制文件: {file_path}")
return False
except PermissionError:
# 跳过没有权限的文件
print(f"✗ 权限不足: {file_path}")
return False
except Exception as e:
# 处理其他异常
print(f"✗ 处理失败 {file_path}: {str(e)}")
return False
def main():
"""
主函数
"""
parser = argparse.ArgumentParser(description="替换Niushop商城系统的注释")
parser.add_argument("--path", type=str, default=".", help="要遍历的目录路径")
args = parser.parse_args()
root_path = args.path
total_files = 0
processed_files = 0
print(f"开始遍历目录: {root_path}")
print(f"将处理的文件类型: {', '.join(FILE_TYPES)}")
print("=" * 60)
# 遍历所有文件
for root, dirs, files in os.walk(root_path):
# 跳过某些目录(如.git、vendor、node_modules等
dirs[:] = [d for d in dirs if d not in ['.git', 'vendor', 'node_modules', 'runtime', 'upload', 'public', 'static']]
for file in files:
# 检查文件类型
if any(file.endswith(ext) for ext in FILE_TYPES):
total_files += 1
file_path = os.path.join(root, file)
if replace_comments(file_path):
processed_files += 1
print("=" * 60)
print(f"处理完成!")
print(f"总文件数: {total_files}")
print(f"已处理文件数: {processed_files}")
print(f"替换率: {processed_files / total_files * 100:.2f}%" if total_files > 0 else "未找到匹配的文件")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,22 @@
const crypto = require('crypto');
// 生成密钥对
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 3072, // 密钥长度不少于3072
publicKeyEncoding: {
type: 'spki', // 公钥编码格式
format: 'pem' // 公钥输出格式
},
privateKeyEncoding: {
type: 'pkcs8', // 私钥编码格式
format: 'pem' // 私钥输出格式
}
});
console.info('生成的公钥:');
console.info(publicKey);
console.info('生成的私钥:');
console.info(privateKey);
// 保存密钥对到文件
const fs = require('fs');
fs.writeFileSync('merchant_public_key.pem', publicKey);
fs.writeFileSync('merchant_private_key.pem', privateKey);

View File

@@ -1,25 +1,22 @@
APP_DEBUG = true
APP_TRACE = true
[APP]
DEFAULT_TIMEZONE = Asia/Shanghai
ENV_MODE = development
[LANG]
default_lang = zh-cn
[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = shop_mallnew_dev
USERNAME = root
PASSWORD = root
HOSTNAME = db
DATABASE = shop_mallnew
USERNAME = shop_mallnew
PASSWORD = shop_mallnew
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true
[RRDATABASE]
HOSTNAME = host.docker.internal
[redis]
HOST = 127.0.0.1
HOST = redis
PORT = 6379
PASSWORD = ''
PASSWORD = 'luckyshop123!@#'
EXPIRY = 604800

View File

@@ -6,7 +6,7 @@ DEFAULT_TIMEZONE = Asia/Shanghai
default_lang = zh-cn
[DATABASE]
TYPE = mysql
HOSTNAME = newshop_mysql
HOSTNAME = db
DATABASE = shop_dev
USERNAME = shop_mallnew
PASSWORD = shop_mallnew
@@ -14,9 +14,9 @@ HOSTPORT = 3306
CHARSET = utf8
DEBUG = true
[RRDATABASE]
HOSTNAME = 192.168.2.64
HOSTNAME = redis
[redis]
HOST = newshop_redis
HOST = redis
PORT = 6379
PASSWORD = 'luckyshop123!@#'
EXPIRY = 604800

View File

@@ -1 +0,0 @@
HUIqMBxOaiPqppGTd9WxmjJwFwX3x-hpbnI1xujpUW4.m7oE98dVL-9XQE2gAJ9VpnsbTfOJ2csnVDIzFqe9osw

View File

@@ -1 +0,0 @@
39aLknx0mLrzjinZRvjc5pYAXUkRq0eo

201
src/LICENSE Normal file
View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2020 上海牛之云网络科技有限公司
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

144
src/README.md Normal file
View File

@@ -0,0 +1,144 @@
![输入图片说明](https://images.gitee.com/uploads/images/2020/1130/170320_60fa7937_6569472.png "gitee.png")
### 产品介绍
### Niushop开源商城单商户V4
**快速搭建专属店铺,迅速展开线上业务** <br/>
NIUSHOP开源商城B2C单商户V4,功能强大安全便捷框架成熟稳定便于扩展源码100%开源,支持二次开发定制,让企业用更低的成本、更少的人力,更快的速度构建自己的商城,开启网上商城销售业务。
### 公司介绍
上海牛之云网络科技有限公司成立于2016年是一家从事移动互联网电商软件为主导的技术研发型企业。公司总部位于上海研发中心设立于锦绣龙城太原市目前有团队成员50多人产品研发实力雄厚。拥有NIUSHOP开源商城NIUCLOUD开源小程序应用市场牛客云商企业级SAAS电商服务平台等产品。业务遍及全国26个省市在北京、广州、上海、深圳等地区拥有多家合作企业。我们始终保持专业、专注、专一的原则旨在为用户提供最好用的全功能型电商软件产品是您转型新零售新媒体掘金千亿电商市场的首选。
### 操作指南
[Niushop开源商城单商户V4使用手册](https://www.kancloud.cn/niucloud/niushop_b2c_v4/1842076)
| [api文档地址](https://www.kancloud.cn/niucloud/niushop_b2c_v4_api/1830441)
| [二开手册](https://www.kancloud.cn/niucloud/niushop_b2c_v4_develop/1830902)
| [论坛地址](https://bbs.niushop.com.cn/forum.php)
| [官网地址](https://www.niushop.com)
- - -
### 推荐阿里云服务器配置
阿里云2000元代金劵<a href="https://www.aliyun.com/minisite/goods?userCode=hvxtm3ee">马上领取</a>
![输入图片说明](https://images.gitee.com/uploads/images/2020/0731/095424_ac477fe3_6569472.png "fuwuqi.png")
### 商城特色:
强大的营销功能模块,丰富的行业模板和装修组件,快速搭建最适合自己的电商平台,轻松获客、裂变。开启电商运营之路。
1. &nbsp;<img src="https://images.gitee.com/uploads/images/2020/0724/121556_a96bd648_6569472.png"/>&nbsp;&nbsp;ThinkPhp6 + LayUi + ElementUi,学习维护成本低<br/>
2. &nbsp;<img src="https://images.gitee.com/uploads/images/2020/0724/121615_f801f981_6569472.png"/>&nbsp;&nbsp;前端由UNI-APP框架编写支持多端易于维护<br/>
3. &nbsp;<img src="https://images.gitee.com/uploads/images/2020/0724/121635_e51987c4_6569472.png"/>&nbsp;&nbsp;钩子 + 插件,组件化开发,可复用,开发便捷<br/>
4. &nbsp;<img src="https://images.gitee.com/uploads/images/2020/0724/121645_df103f55_6569472.png"/>&nbsp;&nbsp;标准API接口前后端分离二次开发更方便<br/>
5. &nbsp;<img src="https://images.gitee.com/uploads/images/2020/0724/121708_74c55984_6569472.png"/>&nbsp;&nbsp;代码全部开源,方便企业扩展自身业务需求
### Niushop官方群
Niushop商城qq开发群1:<a href="https://jq.qq.com/?_wv=1027&k=VrVzi1FI" target="_blank"></a>| qq开发群2:<a href="https://jq.qq.com/?_wv=1027&k=MCtjz6B9" target="_blank"> </a>| qq开发群3:<a href="https://jq.qq.com/?_wv=1027&k=H9FLIfTP" target="_blank"></a>
### 体验演示二维码
![输入图片说明](https://images.gitee.com/uploads/images/2020/1204/122100_d7469b38_6569472.png "微信图片_20201204121838.png")
#### :fire: 体验演示站后台:[<a href='https://uniapp.v4.niuteam.cn/' target="_blank"> 查看 </a>]
<a href='https://uniapp.v4.niuteam.cn/' target="_blank">https://uniapp.v4.niuteam.cn/</a> 账号test 密码123456
### 开源版使用须知
1.仅允许用于个人学习研究使用;
2.开源版不建议商用,如果商用必须保留版权信息,望自觉遵守;
3.禁止将本开源的代码和资源进行任何形式任何名义的出售,否则产生的一切任何后果责任由侵权者自负。
4.推荐3名会员关注公众号将免费获得单商户V4基础版授权。
5.绑定码云评论并点赞支持可获得单商户V4基础版授权
6.本基础版本后台源码全部开源小程序为uniapp编译版如需小程序源码请点击下方图片马上获取。
<a href='https://www.niushop.com/web/index/promotion' target="_blank"><img src="https://images.gitee.com/uploads/images/2020/0805/103536_c7d1b001_6569472.png"/></a>
### 技术亮点
1.框架采用全新thinkphp6+事件开发设计+layui+uniapp进行设计代码完全重构采用支持百万级
2.前端以layui + uniapp模块化开发
3.数据导出采用phpExcel,使数据更加直观,更方便于管理统计;
4.插件钩子机制,功能模块独立,更有助于二次开发;
5.后台采用ECharts,直观体现关系数据可视化的图,支持图与图之间的混搭。实现完善的数据统计和分析;
6.EasyWeChat部署微信开发,微信接入更加快捷,简单;
7.内置强大灵活的权限管理体系,有利于专人专项运营;
8.内置组合数据统计,系统配置,管理碎片化数据统计;
9.客户端完善的交互效果和动画,提升用户端视觉体验;
10.可以完美对接公众号和小程序,并且数据同步,实现真正意义上的一端开发,多端使用;
11.内置客服系统,可以对接微信客服,客服在线实时聊天;
12.高频数据缓存,数据库读写分离,很大程度减轻服务器压力,提升访问速度;
13.后台设置菜单中可以一键数据备份和恢复,完全傻瓜式操作就可以轻松升级备份;
14.在线一键升级,轻松跨越到最新版本;
15.标准Api接口、前后端分离二次开发更方便快捷;
16.支持数据库结构、数据、模板在线缓存清除,提升用户体验;
17.可视化DIY店铺装修方便、快捷、直观可以随心所欲装扮自己的店铺
18.无缝事件机制行为扩展更方便,方便二次开发;
19.支持队列降低流量高峰,解除代码耦合性,高可用性;
20.在线一键安装部署,自动检测系统环境一键安装,省时省力快捷部署;
### 系统功能
![输入图片说明](https://images.gitee.com/uploads/images/2020/0728/121652_92526e64_6569472.png "新增模板 拷贝.png")
### 页面展示
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/111329_22be9395_6569472.png "页面.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/115734_9722c3b5_6569472.png "QQ图片20200725114407.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/115756_68fd9140_6569472.png "QQ图片20200725114501.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/115806_3e7d7f5e_6569472.png "QQ图片20200725114536.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/115857_dd2d4a9f_6569472.png "QQ图片20200725114608.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/115918_fb9521c8_6569472.png "QQ图片20200725114633.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/115931_8271c952_6569472.png "QQ图片20200725114915.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/115945_bc1cf15f_6569472.png "QQ图片20200725114915.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120006_8e2f928c_6569472.png "QQ图片20200725115009.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120032_91c3a07e_6569472.png "QQ图片20200725115036.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120047_5038561c_6569472.png "QQ图片20200725115145.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120057_33e0f799_6569472.png "QQ图片20200725115230.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120113_560de733_6569472.png "QQ图片20200725115258.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120124_76d33659_6569472.png "QQ图片20200725115325.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120133_aa4fd7d6_6569472.png "QQ图片20200725114536.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120147_36252bb9_6569472.png "QQ图片20200725115348.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120201_6ae4c750_6569472.png "QQ图片20200725115409.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120211_d84435a6_6569472.png "QQ图片20200725115430.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120225_82060219_6569472.png "QQ图片20200725115452.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120240_8a9bb074_6569472.png "QQ图片20200725115545.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120256_d3e3cba4_6569472.png "QQ图片20200725115616.png")
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120311_81c2f1d9_6569472.png "QQ图片20200725115659.png")
### 合作伙伴
![输入图片说明](https://images.gitee.com/uploads/images/2020/0725/120430_ab7fff0d_6569472.png "画板 1 拷贝 3(4).png")
### 版权信息
版权所有Copyright © 2015-2020 NiuShop开源商城&nbsp;版权所有
All rights reserved。
上海牛之云网络科技有限公司&nbsp;提供技术支持

View File

@@ -0,0 +1,313 @@
<?php
namespace addon\aikefu\api\controller;
use addon\aikefu\model\Config as KefuConfigModel;
use addon\aikefu\model\Conversation as KefuConversationModel;
use addon\aikefu\model\Message as KefuMessageModel;
use app\api\controller\BaseApi;
class Kefu extends BaseApi
{
/**
* 封装curl请求方法
* @param string $url 请求URL
* @param string $method 请求方法
* @param array $data 请求数据
* @param array $headers 请求头
* @return string 响应内容
*/
private function curlRequest($url, $method = 'GET', $data = [], $headers = [])
{
$ch = curl_init();
// 设置URL
curl_setopt($ch, CURLOPT_URL, $url);
// 设置请求方法
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
// 设置POST数据
if ($method === 'POST' && !empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? json_encode($data) : $data);
}
// 设置请求头
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
// 设置返回值
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// 执行请求
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭连接
curl_close($ch);
if ($response === false) {
throw new \Exception('Curl请求失败');
}
if ($httpCode >= 400) {
throw new \Exception('HTTP请求失败状态码' . $httpCode);
}
return $response;
}
/**
* 为事件调用初始化属性
* @param array $data 事件数据
*/
public function initializeForEvent($data)
{
if (!empty($data['site_id'])) {
$this->site_id = $data['site_id'] ?? 0;
}
if (!empty($data['member_id'])) {
$this->member_id = $data['member_id'] ?? 0;
}
if (!empty($data['token'])) {
$this->token = $data['token'] ?? '';
}
$this->params = [
'message' => $data['message'] ?? '',
'user_id' => $data['user_id'] ?? '',
'conversation_id' => $data['conversation_id'] ?? '',
'stream' => $data['stream'] ?? false,
];
}
/**
* 智能客服聊天接口
* @return \think\response\Json
*/
public function chat()
{
// 获取请求参数
$message = $this->params['message'] ?? '';
$user_id = $this->params['user_id'] ?? $this->member_id;
$conversation_id = $this->params['conversation_id'] ?? '';
$stream = $this->params['stream'] ?? false;
// 验证参数
if (empty($message)) {
return $this->response($this->error('请输入消息内容'));
}
try {
// 获取智能客服配置
$kefu_config_model = new KefuConfigModel();
$config_info = $kefu_config_model->getConfig($this->site_id)['data']['value'] ?? [];
if (empty($config_info) || $config_info['status'] != 1) {
return $this->response($this->error('智能客服暂未启用'));
}
$config = $config_info;
$apiKey = $config['api_key'];
$baseUrl = $config['base_url'];
$chatEndpoint = $config['chat_endpoint'];
// 构建请求数据
$requestData = [
'inputs' => [],
'query' => $message,
'response_mode' => $stream ? 'streaming' : 'blocking',
'user' => $user_id,
];
// 如果有会话ID添加到请求中
if (!empty($conversation_id)) {
$requestData['conversation_id'] = $conversation_id;
}
// 构建请求头
$headers = [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
];
// 发送请求到Dify API
$url = $baseUrl . $chatEndpoint;
$response = $this->curlRequest($url, 'POST', $requestData, $headers);
// 解析响应
$result = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return $this->response($this->error('解析响应失败'));
}
// 保存消息记录
$kefu_message_model = new KefuMessageModel();
$kefu_conversation_model = new KefuConversationModel();
// 保存用户消息
$kefu_message_model->addMessage([
'site_id' => $this->site_id,
'user_id' => $user_id,
'conversation_id' => $result['conversation_id'] ?? $conversation_id,
'message_id' => $result['message_id'] ?? '',
'role' => 'user',
'content' => $message,
]);
// 保存机器人回复
$kefu_message_model->addMessage([
'site_id' => $this->site_id,
'user_id' => $user_id,
'conversation_id' => $result['conversation_id'] ?? $conversation_id,
'message_id' => $result['id'] ?? '',
'role' => 'assistant',
'content' => $result['answer'] ?? '',
]);
// 更新会话状态或创建新会话
$conversation_info = $kefu_conversation_model->getConversationInfo([
['site_id', '=', $this->site_id],
['conversation_id', '=', $result['conversation_id'] ?? $conversation_id],
]);
if (empty($conversation_info['data'])) {
// 创建新会话
$kefu_conversation_model->addConversation([
'site_id' => $this->site_id,
'user_id' => $user_id,
'conversation_id' => $result['conversation_id'] ?? '',
'name' => '智能客服会话',
]);
} else {
// 更新会话状态
$kefu_conversation_model->updateConversation([
'status' => 1,
], [
['id', '=', $conversation_info['data']['id']],
]);
}
// 返回成功响应
return $this->response($this->success([
'conversation_id' => $result['conversation_id'] ?? '',
'reply' => $result['answer'] ?? '',
'message_id' => $result['message_id'] ?? '',
'finish_reason' => $result['finish_reason'] ?? '',
'usage' => $result['usage'] ?? [],
]));
} catch (\Exception $e) {
return $this->response($this->error('请求失败:' . $e->getMessage()));
}
}
/**
* 获取会话历史
* @return \think\response\Json
*/
public function getHistory()
{
// 获取请求参数
$conversation_id = $this->params['conversation_id'] ?? '';
$user_id = $this->params['user_id'] ?? $this->member_id;
$limit = $this->params['limit'] ?? 20;
$offset = $this->params['offset'] ?? 0;
// 验证参数
if (empty($conversation_id)) {
return $this->response($this->error('会话ID不能为空'));
}
try {
// 获取会话历史记录
$kefu_message_model = new KefuMessageModel();
$message_list = $kefu_message_model->getMessageList([
['site_id', '=', $this->site_id],
['user_id', '=', $user_id],
['conversation_id', '=', $conversation_id],
], 'id, role, content, create_time', 'create_time asc', $limit, $offset);
// 返回成功响应
return $this->response($this->success([
'messages' => $message_list['data'] ?? [],
'total' => $message_list['total'] ?? 0,
'limit' => $limit,
'offset' => $offset,
]));
} catch (\Exception $e) {
return $this->response($this->error('请求失败:' . $e->getMessage()));
}
}
/**
* 创建新会话
* @return \think\response\Json
*/
public function createConversation()
{
// 获取请求参数
$user_id = $this->params['user_id'] ?? $this->member_id;
try {
// 获取智能客服配置
$kefu_config_model = new KefuConfigModel();
$config_info = $kefu_config_model->getConfig($this->site_id)['data']['value'] ?? [];
if (empty($config_info) || $config_info['status'] != 1) {
return $this->response($this->error('智能客服暂未启用'));
}
$config = $config_info;
$apiKey = $config['api_key'];
$baseUrl = $config['base_url'];
// 构建请求数据
$requestData = [
'name' => '智能客服会话',
'user' => $user_id,
];
// 构建请求头
$headers = [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
];
// 发送请求到Dify API
$url = $baseUrl . '/conversations';
$response = $this->curlRequest($url, 'POST', $requestData, $headers);
// 解析响应
$result = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return $this->response($this->error('解析响应失败'));
}
// 保存会话记录
$kefu_conversation_model = new KefuConversationModel();
$kefu_conversation_model->addConversation([
'site_id' => $this->site_id,
'user_id' => $user_id,
'conversation_id' => $result['id'] ?? '',
'name' => $result['name'] ?? '智能客服会话',
]);
// 返回成功响应
return $this->response($this->success([
'conversation_id' => $result['id'] ?? '',
'name' => $result['name'] ?? '',
'created_at' => $result['created_at'] ?? '',
]));
} catch (\Exception $e) {
return $this->response($this->error('请求失败:' . $e->getMessage()));
}
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* 智能客服扩展事件配置
*/
return [
'bind' => [
],
'listen' => [
'KefuChat' => [
'addon\aikefu\event\KefuChat'
],
'KefuCreateConversation' => [
'addon\aikefu\event\KefuCreateConversation'
],
'KefuGetHistory' => [
'addon\aikefu\event\KefuGetHistory'
],
],
'subscribe' => [
],
];

View File

@@ -0,0 +1,13 @@
<?php
return [
'name' => 'aikefu',
'title' => '智能客服',
'description' => '基于Dify的智能客服系统',
'type' => 'system', //插件类型 system :系统插件(自动安装), business:业务插件 promotion:扩展营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '1.0.0',
'version_no' => '525231212001',
'content' => '',
];

View File

@@ -0,0 +1,36 @@
-- 智能客服插件安装脚本
-- 1. 智能客服插件使用系统配置表存储配置信息,无需创建独立数据表
-- 2. 会话和消息数据存储在独立数据表中
-- 创建智能客服会话表
CREATE TABLE IF NOT EXISTS `lucky_aikefu_conversation` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`site_id` int(11) NOT NULL COMMENT '站点ID',
`user_id` varchar(50) NOT NULL COMMENT '用户ID',
`conversation_id` varchar(100) NOT NULL COMMENT 'Dify会话ID',
`name` varchar(255) NOT NULL COMMENT '会话名称',
`status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态1活跃0结束',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `site_id` (`site_id`),
KEY `user_id` (`user_id`),
KEY `conversation_id` (`conversation_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='智能客服会话表';
-- 创建智能客服消息表
CREATE TABLE IF NOT EXISTS `lucky_aikefu_message` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`site_id` int(11) NOT NULL COMMENT '站点ID',
`user_id` varchar(50) NOT NULL COMMENT '用户ID',
`conversation_id` varchar(100) NOT NULL COMMENT '会话ID',
`message_id` varchar(100) NOT NULL COMMENT '消息ID',
`role` varchar(20) NOT NULL COMMENT '角色user用户assistant助手',
`content` text NOT NULL COMMENT '消息内容',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `site_id` (`site_id`),
KEY `user_id` (`user_id`),
KEY `conversation_id` (`conversation_id`),
KEY `message_id` (`message_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='智能客服消息表';

View File

@@ -0,0 +1,4 @@
-- 智能客服插件卸载脚本
-- 删除智能客服相关表(配置信息存储在系统配置表中,无需单独删除)
DROP TABLE IF EXISTS `lucky_aikefu_message`;
DROP TABLE IF EXISTS `lucky_aikefu_conversation`;

View File

@@ -0,0 +1,50 @@
<?php
namespace addon\aikefu\event;
use app\model\system\Addon as AddonModel;
/**
* 智能客服插件安装
*/
class Install
{
public function handle()
{
$addon_model = new AddonModel();
$info = $addon_model->getAddonInfo(['name' => 'aikefu']);
if (empty($info['data'])) {
// 插件未安装,执行安装逻辑
$addon_model->addAddon([
'name' => 'aikefu',
'title' => '智能客服',
'description' => '基于Dify的智能客服系统',
'author' => 'admin',
'version' => '1.0.0',
'scene' => 'web',
'state' => 0,
'category' => 'business',
'need_install' => 1,
'need_cache' => 1,
'create_time' => time(),
'update_time' => time()
]);
} else {
// 插件已存在,更新插件信息
$addon_model->updateAddon([
'title' => '智能客服',
'description' => '基于Dify的智能客服系统',
'author' => 'admin',
'version' => '1.0.0',
'scene' => 'web',
'category' => 'business',
'need_install' => 1,
'need_cache' => 1,
'update_time' => time()
], ['name' => 'aikefu']);
}
return success(1);
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace addon\aikefu\event;
use addon\aikefu\api\controller\Kefu as KefuApi;
/**
* 客服聊天
*/
class KefuChat
{
/**
* 处理智能客服聊天事件
* @param array $data 事件数据
* @return array
*/
public function handle($data)
{
try {
// 创建addon的KefuApi实例
$kefu_api = new KefuApi();
// 调用初始化方法设置属性
$kefu_api->initializeForEvent($data);
// 调用addon的chat方法
$response = $kefu_api->chat();
// 返回响应数据
return json_decode($response->getContent(), true);
} catch (\Exception $e) {
return [
'code' => -1,
'message' => '聊天失败:' . $e->getMessage(),
'data' => []
];
}
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace addon\aikefu\event;
use addon\aikefu\api\controller\Kefu as KefuApi;
/**
* 处理智能客服创建会话事件
*/
class KefuCreateConversation
{
/**
* 处理智能客服创建会话事件
* @param array $data 事件数据
* @return array
*/
public function handle($data)
{
try {
// 创建addon的KefuApi实例
$kefu_api = new KefuApi();
// 调用初始化方法设置属性
$kefu_api->initializeForEvent($data);
// 调用addon的createConversation方法
$response = $kefu_api->createConversation();
// 返回响应数据
return json_decode($response->getContent(), true);
} catch (\Exception $e) {
return [
'code' => -1,
'message' => '创建会话失败:' . $e->getMessage(),
'data' => []
];
}
}
/**
* 处理智能客服获取历史消息事件
* @param array $data 事件数据
* @return array
*/
public function handleKefuGetHistory($data)
{
try {
// 创建addon的KefuApi实例
$kefu_api = new KefuApi();
// 调用初始化方法设置属性
$kefu_api->initializeForEvent($data);
// 调用addon的getHistory方法
$response = $kefu_api->getHistory();
// 返回响应数据
return json_decode($response->getContent(), true);
} catch (\Exception $e) {
return [
'code' => -1,
'message' => '获取历史消息失败:' . $e->getMessage(),
'data' => []
];
}
}
/**
* 事件监听映射
* @return array
*/
public function subscribe()
{
return [
'KefuChat' => 'handleKefuChat',
'KefuCreateConversation' => 'handleKefuCreateConversation',
'KefuGetHistory' => 'handleKefuGetHistory',
];
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace addon\aikefu\event;
use addon\aikefu\api\controller\Kefu as KefuApi;
/**
* 处理智能客服获取历史消息事件
*/
class KefuGetHistory
{
/**
* 处理智能客服获取历史消息事件
* @param array $data 事件数据
* @return array
*/
public function handle($data)
{
try {
// 创建addon的KefuApi实例
$kefu_api = new KefuApi();
// 调用初始化方法设置属性
$kefu_api->initializeForEvent($data);
// 调用addon的getHistory方法
$response = $kefu_api->getHistory();
// 返回响应数据
return json_decode($response->getContent(), true);
} catch (\Exception $e) {
return [
'code' => -1,
'message' => '获取历史消息失败:' . $e->getMessage(),
'data' => []
];
}
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace addon\aikefu\event;
use app\model\system\Addon as AddonModel;
/**
* 智能客服插件卸载
*/
class UnInstall
{
public function handle()
{
$addon_model = new AddonModel();
// 删除插件信息
$addon_model->deleteAddon(['name' => 'aikefu']);
return success(1);
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,67 @@
<?php
/**
* 智能客服配置模型
* 用于存储和管理智能客服的配置信息
* 版本1.0.0
*/
namespace addon\aikefu\model;
use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
/**
* 智能客服配置
*/
class Config extends BaseModel
{
/**
* 设置智能客服配置
* @param array $data
* @param int $site_id
* @param string $app_module
* @return array
*/
public function setConfig($data, $site_id = 0, $app_module = 'shop')
{
$config = new ConfigModel();
// 获取原始配置
$original_config = $this->getConfig($site_id, $app_module)['data']['value'] ?? [];
// 如果 API Key 为空或保持不变,则使用原始值
if (isset($data['api_key']) && empty($data['api_key'])) {
$data['api_key'] = $original_config['api_key'] ?? '';
}
$res = $config->setConfig($data, '智能客服配置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'AIKEFU_CONFIG']]);
return $res;
}
/**
* 获取智能客服配置
* @param int $site_id
* @param string $app_module
* @return array
*/
public function getConfig($site_id = 0, $app_module = 'shop')
{
$config = new ConfigModel();
$res = $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'AIKEFU_CONFIG']]);
return $res;
}
/**
* 获取智能客服配置信息
* @param array $condition
* @param string $field
* @return array
*/
public function getConfigInfo($condition = [], $field = '*')
{
// 兼容旧的调用方式
$site_id = $condition[0][1] ?? 0;
$res = $this->getConfig($site_id);
return $res;
}
}

View File

@@ -0,0 +1,192 @@
<?php
namespace addon\aikefu\model;
use think\Model;
class Conversation extends Model
{
/**
* 操作成功返回值函数
* @param string $data
* @param string $code_var
* @return array
*/
public function success($data = '', $code_var = 'SUCCESS')
{
$lang_array = $this->getLang();
$lang_var = $lang_array[$code_var] ?? $code_var;
if ($code_var == 'SUCCESS') {
$code_var = 0;
} else {
$code_array = array_keys($lang_array);
$code_index = array_search($code_var, $code_array);
if ($code_index != false) {
$code_var = 10000 + $code_index;
}
}
return success($code_var, $lang_var, $data);
}
/**
* 操作失败返回值函数
* @param string $data
* @param string $code_var
* @return array
*/
public function error($data = '', $code_var = 'FAIL')
{
$lang_array = $this->getLang();
if (isset($lang_array[$code_var])) {
$lang_var = $lang_array[$code_var];
} else {
$lang_var = $code_var;
$code_var = 'FAIL';
}
$code_array = array_keys($lang_array);
$code_index = array_search($code_var, $code_array);
if ($code_index != false) {
$code_var = -10000 - $code_index;
}
return error($code_var, $lang_var, $data);
}
/**
* 获取语言包数组
* @return Ambigous <multitype:, unknown>
*/
public function getLang()
{
$default_lang = config("lang.default_lang");
$cache_common = \think\facade\Cache::get("lang_app/lang/" . $default_lang . '/model.php');
if (empty($cache_common)) {
$cache_common = include 'app/lang/' . $default_lang . '/model.php';
\think\facade\Cache::tag("lang")->set("lang_app/lang/" . $default_lang, $cache_common);
}
$lang_path = $this->lang ?? '';
if (!empty($lang_path)) {
$cache_path = \think\facade\Cache::get("lang_" . $lang_path . "/" . $default_lang . '/model.php');
if (empty($cache_path)) {
$cache_path = include $lang_path . "/" . $default_lang . '/model.php';
\think\facade\Cache::tag("lang")->set("lang_" . $lang_path . "/" . $default_lang, $cache_path);
}
$lang = array_merge($cache_common, $cache_path);
} else {
$lang = $cache_common;
}
return $lang;
}
/**
* 表名
* @var string
*/
protected $name = 'aikefu_conversation';
/**
* 主键
* @var string
*/
protected $pk = 'id';
/**
* 获取会话信息
* @param array $condition
* @param string $field
* @return array
*/
public function getConversationInfo($condition = [], $field = '*')
{
$info = $this->where($condition)->field($field)->find();
return empty($info) ? [] : $info->toArray();
}
/**
* 获取会话列表
* @param array $condition
* @param string $field
* @param string $order
* @param int $page
* @param int $limit
* @return array
*/
public function getConversationList($condition = [], $field = '*', $order = 'id desc', $page = 1, $limit = 10)
{
$list = $this->where($condition)->field($field)->order($order)->paginate([
'page' => $page,
'list_rows' => $limit
]);
return $this->pageFormat($list);
}
/**
* 分页数据格式化
* @param \think\Paginator $paginator
* @return array
*/
protected function pageFormat($paginator)
{
return [
'data' => $paginator->items(),
'total' => $paginator->total(),
'per_page' => $paginator->listRows(),
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage()
];
}
/**
* 添加会话
* @param array $data
* @return array
*/
public function addConversation($data)
{
$result = $this->insert($data);
return $this->success(['result' => $result]);
}
/**
* 更新会话
* @param array $data
* @param array $condition
* @return array
*/
public function updateConversation($data, $condition)
{
$result = $this->where($condition)->update($data);
return $this->success(['result' => $result]);
}
/**
* 删除会话
* @param array $condition
* @return array
*/
public function deleteConversation($condition)
{
$result = $this->where($condition)->delete();
return $this->success(['result' => $result]);
}
/**
* 获取用户会话列表
* @param int $site_id
* @param string $user_id
* @param int $page
* @param int $limit
* @return array
*/
public function getUserConversationList($site_id, $user_id, $page = 1, $limit = 10)
{
$condition = [
['site_id', '=', $site_id],
['user_id', '=', $user_id]
];
return $this->getConversationList($condition, '*', 'update_time desc', $page, $limit);
}
}

View File

@@ -0,0 +1,208 @@
<?php
namespace addon\aikefu\model;
use think\Model;
class Message extends Model
{
/**
* 操作成功返回值函数
* @param string $data
* @param string $code_var
* @return array
*/
public function success($data = '', $code_var = 'SUCCESS')
{
$lang_array = $this->getLang();
$lang_var = $lang_array[$code_var] ?? $code_var;
if ($code_var == 'SUCCESS') {
$code_var = 0;
} else {
$code_array = array_keys($lang_array);
$code_index = array_search($code_var, $code_array);
if ($code_index != false) {
$code_var = 10000 + $code_index;
}
}
return success($code_var, $lang_var, $data);
}
/**
* 操作失败返回值函数
* @param string $data
* @param string $code_var
* @return array
*/
public function error($data = '', $code_var = 'FAIL')
{
$lang_array = $this->getLang();
if (isset($lang_array[$code_var])) {
$lang_var = $lang_array[$code_var];
} else {
$lang_var = $code_var;
$code_var = 'FAIL';
}
$code_array = array_keys($lang_array);
$code_index = array_search($code_var, $code_array);
if ($code_index != false) {
$code_var = -10000 - $code_index;
}
return error($code_var, $lang_var, $data);
}
/**
* 获取语言包数组
* @return Ambigous <multitype:, unknown>
*/
public function getLang()
{
$default_lang = config("lang.default_lang");
$cache_common = \think\facade\Cache::get("lang_app/lang/" . $default_lang . '/model.php');
if (empty($cache_common)) {
$cache_common = include 'app/lang/' . $default_lang . '/model.php';
\think\facade\Cache::tag("lang")->set("lang_app/lang/" . $default_lang, $cache_common);
}
$lang_path = $this->lang ?? '';
if (!empty($lang_path)) {
$cache_path = \think\facade\Cache::get("lang_" . $lang_path . "/" . $default_lang . '/model.php');
if (empty($cache_path)) {
$cache_path = include $lang_path . "/" . $default_lang . '/model.php';
\think\facade\Cache::tag("lang")->set("lang_" . $lang_path . "/" . $default_lang, $cache_path);
}
$lang = array_merge($cache_common, $cache_path);
} else {
$lang = $cache_common;
}
return $lang;
}
/**
* 表名
* @var string
*/
protected $name = 'aikefu_message';
/**
* 主键
* @var string
*/
protected $pk = 'id';
/**
* 获取消息信息
* @param array $condition
* @param string $field
* @return array
*/
public function getMessageInfo($condition = [], $field = '*')
{
$info = $this->where($condition)->field($field)->find();
return empty($info) ? [] : $info->toArray();
}
/**
* 获取消息列表
* @param array $condition
* @param string $field
* @param string $order
* @param int $page
* @param int $limit
* @return array
*/
public function getMessageList($condition = [], $field = '*', $order = 'id asc', $page = 1, $limit = 20)
{
$list = $this->where($condition)->field($field)->order($order)->paginate([
'page' => $page,
'list_rows' => $limit
]);
return $this->pageFormat($list);
}
/**
* 分页数据格式化
* @param \think\Paginator $paginator
* @return array
*/
protected function pageFormat($paginator)
{
return [
'data' => $paginator->items(),
'total' => $paginator->total(),
'per_page' => $paginator->listRows(),
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage()
];
}
/**
* 添加消息
* @param array $data
* @return array
*/
public function addMessage($data)
{
$result = $this->insert($data);
return $this->success(['result' => $result]);
}
/**
* 更新消息
* @param array $data
* @param array $condition
* @return array
*/
public function updateMessage($data, $condition)
{
$result = $this->where($condition)->update($data);
return $this->success(['result' => $result]);
}
/**
* 删除消息
* @param array $condition
* @return array
*/
public function deleteMessage($condition)
{
$result = $this->where($condition)->delete();
return $this->success(['result' => $result]);
}
/**
* 获取会话消息记录
* @param int $site_id
* @param string $conversation_id
* @param int $limit
* @param int $offset
* @return array
*/
public function getConversationMessages($site_id, $conversation_id, $limit = 20, $offset = 0)
{
$condition = [
['site_id', '=', $site_id],
['conversation_id', '=', $conversation_id]
];
return $this->getMessageList($condition, '*', 'create_time asc', ($offset / $limit) + 1, $limit);
}
/**
* 获取用户消息总数
* @param int $site_id
* @param string $user_id
* @return array
*/
public function getUserMessageCount($site_id, $user_id)
{
$count = $this->where([
['site_id', '=', $site_id],
['user_id', '=', $user_id]
])->count();
return $this->success(['count' => $count]);
}
}

View File

@@ -0,0 +1,217 @@
<?php
/**
* 智能客服控制器
*/
namespace addon\aikefu\shop\controller;
use addon\aikefu\model\Config as KefuConfigModel;
use addon\aikefu\model\Conversation as KefuConversationModel;
use addon\aikefu\model\Message as KefuMessageModel;
use app\shop\controller\BaseShop;
use think\facade\View;
/**
* 智能客服 控制器
*/
class Kefu extends BaseShop
{
/**
* 智能客服配置页
* @return \think\response\View|\think\response\Json
*/
public function config()
{
$kefu_config_model = new KefuConfigModel();
if (request()->isJson()) {
$api_key = input("api_key", "");//Dify API密钥
$base_url = input("base_url", "https://api.dify.ai/v1");//API基础地址
$chat_endpoint = input("chat_endpoint", "/chat-messages");//聊天接口端点
$status = input("status", 0);//状态
$data = array(
"api_key" => $api_key,
"base_url" => $base_url,
"chat_endpoint" => $chat_endpoint,
"status" => $status
);
$result = $kefu_config_model->setConfig($data, $this->site_id, $this->app_module);
return $result;
} else {
$config_info = $kefu_config_model->getConfig($this->site_id, $this->app_module)['data']['value'] ?? [];
$this->assign("config_info", $config_info);
return $this->fetch("kefu/config");
}
}
/**
* 会话管理列表
* @return \think\response\View
*/
public function conversation()
{
return View::fetch("kefu/conversation");
}
/**
* 获取会话列表
* @return \think\response\Json
*/
public function getConversationList()
{
$page = input("page", 1);
$limit = input("limit", 10);
$user_id = input("user_id", "");
$status = input("status", "");
$kefu_conversation_model = new KefuConversationModel();
$condition = [['site_id', '=', $this->site_id]];
if (!empty($user_id)) {
$condition[] = ['user_id', '=', $user_id];
}
if ($status !== '') {
$condition[] = ['status', '=', $status];
}
$conversation_list = $kefu_conversation_model->getConversationList($condition, '*', 'update_time desc', $page, $limit);
return $this->success($conversation_list);
}
/**
* 获取会话信息
* @return \think\response\Json
*/
public function getConversationInfo()
{
$conversation_id = input("conversation_id", "");
if (empty($conversation_id)) {
return $this->error('会话ID不能为空');
}
$kefu_conversation_model = new KefuConversationModel();
$conversation_info = $kefu_conversation_model->getConversationInfo([
['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_id]
]);
if (empty($conversation_info)) {
return $this->error('会话不存在');
}
return $this->success($conversation_info);
}
/**
* 结束会话
* @return \think\response\Json
*/
public function endConversation()
{
$id = input("id", "");
if (empty($id)) {
return $this->error('会话ID不能为空');
}
$kefu_conversation_model = new KefuConversationModel();
$result = $kefu_conversation_model->updateConversation(
['status' => 0],
[
['id', '=', $id],
['site_id', '=', $this->site_id]
]
);
return $this->success($result);
}
/**
* 删除会话
* @return \think\response\Json
*/
public function deleteConversation()
{
$id = input("id", "");
if (empty($id)) {
return $this->error('会话ID不能为空');
}
$kefu_conversation_model = new KefuConversationModel();
$kefu_message_model = new KefuMessageModel();
// 开启事务
\think\facade\Db::startTrans();
try {
// 删除会话关联的消息
$conversation_info = $kefu_conversation_model->getConversationInfo([
['id', '=', $id],
['site_id', '=', $this->site_id]
]);
if (!empty($conversation_info)) {
$kefu_message_model->deleteMessage([
['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_info['conversation_id']]
]);
}
// 删除会话
$result = $kefu_conversation_model->deleteConversation([
['id', '=', $id],
['site_id', '=', $this->site_id]
]);
// 提交事务
\think\facade\Db::commit();
return $this->success($result);
} catch (\Exception $e) {
// 回滚事务
\think\facade\Db::rollback();
return $this->error($e->getMessage());
}
}
/**
* 消息管理列表
* @return \think\response\View
*/
public function message()
{
$conversation_id = input("conversation_id", "");
View::assign("conversation_id", $conversation_id);
return View::fetch("kefu/message");
}
/**
* 获取消息列表
* @return \think\response\Json
*/
public function getMessageList()
{
$page = input("page", 1);
$limit = input("limit", 50);
$conversation_id = input("conversation_id", "");
if (empty($conversation_id)) {
return $this->error('会话ID不能为空');
}
$kefu_message_model = new KefuMessageModel();
$condition = [
['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation_id]
];
$message_list = $kefu_message_model->getMessageList($condition, '*', 'create_time asc', $page, $limit);
return $this->success($message_list);
}
}

View File

@@ -0,0 +1,104 @@
<style>
.word-aux {
margin-left: 110px;
color: #999;
font-size: 12px;
margin-top: 5px;
}
.required {
color: red;
}
</style>
<div class="layui-form form-wrap">
<div class="layui-form-item">
<label class="layui-form-label"><span class="required">*</span>Dify API密钥</label>
<div class="layui-input-block">
<input type="text" name="api_key" placeholder="请输入Dify API密钥" value="{$config_info.api_key ?? ''}" class="layui-input">
</div>
<div class="word-aux">
从Dify平台获取的API密钥用于调用Dify聊天机器人API。
<a href="https://dify.ai/" target="_blank">前往Dify平台</a>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">API基础地址</label>
<div class="layui-input-block">
<input type="text" name="base_url" placeholder="请输入Dify API基础地址" value="{$config_info.base_url ?? 'https://api.dify.ai/v1'}" class="layui-input">
</div>
<div class="word-aux">Dify API的基础地址默认为https://api.dify.ai/v1</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">聊天接口端点:</label>
<div class="layui-input-block">
<input type="text" name="chat_endpoint" placeholder="请输入聊天接口端点" value="{$config_info.chat_endpoint ?? '/chat-messages'}" class="layui-input">
</div>
<div class="word-aux">聊天接口的端点,默认为/chat-messages</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="required">*</span>状态:</label>
<div class="layui-input-block">
<input type="checkbox" name="status" value="1" lay-skin="switch" {if condition="isset($config_info.status) && $config_info.status == 1"} checked {/if}>
</div>
<div class="word-aux">启用或禁用智能客服功能</div>
</div>
<div class="form-row">
<button class="layui-btn" lay-submit lay-filter="save">保存</button>
<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
</div>
</div>
<script>
layui.use('form', function() {
var form = layui.form;
var repeat_flag = false; //防重复标识
form.render();
/**
* 监听提交
*/
form.on('submit(save)', function(data) {
if (repeat_flag) return false;
repeat_flag = true;
$.ajax({
url: ns.url("aikefu://shop/kefu/config"),
type: 'POST',
data: data.field,
dataType: 'json',
success: function(res) {
repeat_flag = false;
if (res.code === 0) {
layer.confirm('保存成功', {
title: '操作提示',
btn: ['返回列表', '继续编辑'],
yes: function(index, layero) {
location.reload();
layer.close(index);
},
btn2: function(index, layero) {
layer.close(index);
}
});
} else {
layer.msg(res.message, {icon: 2});
}
},
error: function() {
repeat_flag = false;
layer.msg('请求失败,请稍后重试', {icon: 2});
}
});
return false;
});
});
function back() {
window.history.back();
}
</script>

View File

@@ -0,0 +1,214 @@
<style>
.search-box {
padding: 10px 0;
border-bottom: 1px solid #eee;
margin-bottom: 15px;
}
.search-item {
display: inline-block;
margin-right: 15px;
vertical-align: middle;
}
.search-item label {
display: inline-block;
width: 80px;
text-align: right;
margin-right: 10px;
vertical-align: middle;
}
.search-item input,
.search-item select {
width: 150px;
display: inline-block;
vertical-align: middle;
}
.layui-btn-container {
margin-bottom: 15px;
text-align: right;
}
.layui-btn-sm {
margin-left: 5px;
}
.status-active {
color: #52c41a;
}
.status-inactive {
color: #faad14;
}
</style>
<div class="layui-card-body">
<!-- 搜索区域 -->
<div class="search-box">
<form class="layui-form" id="searchForm">
<div class="search-item">
<label for="user_id">用户ID</label>
<input type="text" name="user_id" id="user_id" placeholder="请输入用户ID" class="layui-input">
</div>
<div class="search-item">
<label for="status">状态</label>
<select name="status" id="status" class="layui-select">
<option value="">全部</option>
<option value="1">活跃</option>
<option value="0">已结束</option>
</select>
</div>
<div class="search-item">
<button type="button" class="layui-btn layui-btn-primary" id="searchBtn">搜索</button>
<button type="button" class="layui-btn layui-btn-primary" id="resetBtn">重置</button>
</div>
</form>
</div>
<!-- 表格区域 -->
<table class="layui-table" id="conversationTable" lay-filter="conversationTable"></table>
</div>
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<!-- 可以在这里添加按钮 -->
</div>
</script>
<script type="text/html" id="statusTpl">
{{# if(d.status === 1) {
return '<span class="status-active">活跃</span>';
} else {
return '<span class="status-inactive">已结束</span>';
}}}
</script>
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-xs" lay-event="view">查看消息</a>
<a class="layui-btn layui-btn-xs layui-btn-warning" lay-event="end">结束会话</a>
<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
</script>
<script>
layui.use(['table', 'form', 'layer'], function() {
var table = layui.table;
var form = layui.form;
var layer = layui.layer;
// 渲染表格
var tableIns = table.render({
elem: '#conversationTable',
url: ns.url("aikefu://shop/kefu/getConversationList"),
method: 'POST',
toolbar: '#toolbarDemo',
defaultToolbar: ['filter', 'exports', 'print'],
title: '会话管理',
cols: [[
{field: 'id', title: 'ID', width: 80, align: 'center', fixed: 'left'},
{field: 'conversation_id', title: '会话ID', width: 200, align: 'center'},
{field: 'user_id', title: '用户ID', width: 150, align: 'center'},
{field: 'name', title: '会话名称', width: 180, align: 'center'},
{field: 'status', title: '状态', width: 100, align: 'center', templet: '#statusTpl'},
{field: 'create_time', title: '创建时间', width: 180, align: 'center'},
{field: 'update_time', title: '更新时间', width: 180, align: 'center'},
{fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}
]],
page: true,
limit: 10,
limits: [10, 20, 30, 50, 100],
height: 'full-200',
text: {
none: '暂无会话数据'
}
});
// 搜索按钮点击事件
$('#searchBtn').click(function() {
// 执行搜索
tableIns.reload({
page: {
curr: 1
},
where: {
user_id: $('#user_id').val(),
status: $('#status').val()
}
});
});
// 重置按钮点击事件
$('#resetBtn').click(function() {
$('#user_id').val('');
$('#status').val('');
form.render('select');
// 执行重置后的搜索
tableIns.reload({
page: {
curr: 1
},
where: {
user_id: '',
status: ''
}
});
});
// 监听行工具事件
table.on('tool(conversationTable)', function(obj) {
var data = obj.data;
var layEvent = obj.event;
if (layEvent === 'view') {
// 查看消息
layer.open({
type: 2,
title: '消息记录',
content: ns.url("aikefu://shop/kefu/message", {conversation_id: data.conversation_id}),
area: ['90%', '90%']
});
} else if (layEvent === 'end') {
// 结束会话
layer.confirm('确定要结束该会话吗?', function(index) {
$.ajax({
url: ns.url("aikefu://shop/kefu/endConversation"),
type: 'POST',
data: {id: data.id},
dataType: 'json',
success: function(res) {
if (res.code === 0) {
layer.msg('会话已结束', {icon: 1});
// 重新加载表格数据
tableIns.reload();
} else {
layer.msg('操作失败:' + res.message, {icon: 2});
}
},
error: function() {
layer.msg('请求失败,请稍后重试', {icon: 2});
}
});
layer.close(index);
});
} else if (layEvent === 'delete') {
// 删除会话
layer.confirm('确定要删除该会话吗?删除后将无法恢复', function(index) {
$.ajax({
url: ns.url("aikefu://shop/kefu/deleteConversation"),
type: 'POST',
data: {id: data.id},
dataType: 'json',
success: function(res) {
if (res.code === 0) {
layer.msg('会话已删除', {icon: 1});
// 重新加载表格数据
tableIns.reload();
} else {
layer.msg('操作失败:' + res.message, {icon: 2});
}
},
error: function() {
layer.msg('请求失败,请稍后重试', {icon: 2});
}
});
layer.close(index);
});
}
});
});
</script>

View File

@@ -0,0 +1,274 @@
<style>
.message-list {
max-height: 600px;
overflow-y: auto;
padding: 20px;
background-color: #f5f7fa;
border-radius: 8px;
margin-bottom: 20px;
border: 1px solid #e6e6e6;
}
.message-item {
margin-bottom: 20px;
display: flex;
align-items: flex-start;
}
.message-item.user {
justify-content: flex-end;
}
.message-item.assistant {
justify-content: flex-start;
}
.message-avatar {
width: 40px;
height: 40px;
border-radius: 50%;
margin: 0 10px;
}
.message-content {
max-width: 70%;
padding: 12px 16px;
border-radius: 18px;
word-wrap: break-word;
line-height: 1.5;
}
.message-item.user .message-content {
background-color: #1E9FFF;
color: white;
border-bottom-right-radius: 4px;
}
.message-item.assistant .message-content {
background-color: white;
color: #333;
border-bottom-left-radius: 4px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.message-time {
font-size: 12px;
color: #909399;
margin-top: 5px;
text-align: center;
}
.message-role {
font-size: 12px;
color: #909399;
margin-bottom: 5px;
}
.conversation-info {
margin-bottom: 20px;
padding: 15px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
border: 1px solid #e6e6e6;
}
.conversation-info h3 {
margin: 0 0 10px 0;
font-size: 16px;
color: #333;
font-weight: bold;
}
.conversation-info p {
margin: 5px 0;
font-size: 14px;
color: #606266;
}
.status-active {
color: #52c41a;
}
.status-inactive {
color: #faad14;
}
.search-box {
margin-bottom: 20px;
padding: 15px;
background-color: white;
border-radius: 8px;
border: 1px solid #e6e6e6;
}
.search-item {
margin-right: 15px;
}
.search-item label {
display: inline-block;
width: 80px;
text-align: right;
margin-right: 10px;
}
.layui-btn-sm {
margin-left: 5px;
}
</style>
<div class="layui-card-body">
<!-- 搜索区域 -->
<div class="search-box">
<form class="layui-form" id="searchForm">
<div class="search-item">
<label for="conversation_id">会话ID</label>
<input type="text" name="conversation_id" id="conversation_id" placeholder="请输入会话ID" value="{$conversation_id ?? ''}" class="layui-input" style="width: 200px; display: inline-block;">
</div>
<div class="search-item">
<button type="button" class="layui-btn layui-btn-primary" id="searchBtn">搜索</button>
</div>
</form>
</div>
<!-- 会话信息 -->
<div id="conversationInfo" class="conversation-info">
<!-- 会话信息将通过JavaScript动态加载 -->
</div>
<!-- 消息列表 -->
<div class="message-list" id="messageList">
<!-- 消息列表将通过JavaScript动态加载 -->
</div>
<!-- 分页 -->
<div class="layui-fixbar" id="pagination"></div>
</div>
<script>
layui.use(['laypage', 'layer'], function() {
var laypage = layui.laypage;
var layer = layui.layer;
// 分页参数
var page = 1;
var limit = 50;
var total = 0;
var conversation_id = $('#conversation_id').val();
// 加载会话信息
function loadConversationInfo() {
if (!conversation_id) {
$('#conversationInfo').html('<h3>会话详情</h3><p>请输入会话ID进行搜索</p>');
return;
}
$.ajax({
url: ns.url("aikefu://shop/kefu/getConversationInfo"),
type: 'POST',
data: {
conversation_id: conversation_id
},
dataType: 'json',
success: function(res) {
if (res.code === 0) {
var info = res.data;
var html = '<h3>会话详情</h3>';
html += '<p><strong>会话ID</strong>' + info.conversation_id + '</p>';
html += '<p><strong>用户ID</strong>' + info.user_id + '</p>';
html += '<p><strong>会话名称:</strong>' + info.name + '</p>';
html += '<p><strong>状态:</strong>' + (info.status === 1 ? '<span class="status-active">活跃</span>' : '<span class="status-inactive">已结束</span>') + '</p>';
html += '<p><strong>创建时间:</strong>' + info.create_time + '</p>';
html += '<p><strong>更新时间:</strong>' + info.update_time + '</p>';
$('#conversationInfo').html(html);
} else {
$('#conversationInfo').html('<h3>会话详情</h3><p>未找到会话信息</p>');
}
},
error: function() {
$('#conversationInfo').html('<h3>会话详情</h3><p>加载会话信息失败</p>');
}
});
}
// 加载消息列表
function loadMessageList() {
if (!conversation_id) {
$('#messageList').html('<div style="text-align: center; padding: 50px; color: #999;">请输入会话ID进行搜索</div>');
return;
}
$.ajax({
url: ns.url("aikefu://shop/kefu/getMessageList"),
type: 'POST',
data: {
page: page,
limit: limit,
conversation_id: conversation_id
},
dataType: 'json',
success: function(res) {
if (res.code === 0) {
var list = res.data.list;
total = res.data.total;
var html = '';
if (list.length > 0) {
list.forEach(function(item) {
var role = item.role === 'user' ? '用户' : '机器人';
var roleClass = item.role === 'user' ? 'user' : 'assistant';
var avatar = item.role === 'user' ? '/__STATIC__/admin/img/user.png' : '/__STATIC__/admin/img/robot.png';
html += '<div class="message-item ' + roleClass + '">';
if (item.role === 'assistant') {
html += '<img src="' + avatar + '" class="message-avatar">';
}
html += '<div>';
html += '<div class="message-role">' + role + '</div>';
html += '<div class="message-content">' + item.content + '</div>';
html += '<div class="message-time">' + item.create_time + '</div>';
html += '</div>';
if (item.role === 'user') {
html += '<img src="' + avatar + '" class="message-avatar">';
}
html += '</div>';
});
} else {
html += '<div style="text-align: center; padding: 50px; color: #999;">暂无消息记录</div>';
}
$('#messageList').html(html);
// 滚动到底部
$('#messageList').scrollTop($('#messageList')[0].scrollHeight);
// 渲染分页
renderPagination();
} else {
layer.msg('加载失败:' + res.message, {icon: 2});
}
},
error: function() {
layer.msg('请求失败,请稍后重试', {icon: 2});
}
});
}
// 渲染分页
function renderPagination() {
if (total <= limit) {
$('#pagination').html('');
return;
}
laypage.render({
elem: 'pagination',
count: total,
limit: limit,
curr: page,
layout: ['prev', 'page', 'next', 'count', 'skip'],
jump: function(obj, first) {
if (!first) {
page = obj.curr;
loadMessageList();
}
}
});
}
// 搜索按钮点击事件
$('#searchBtn').click(function() {
conversation_id = $('#conversation_id').val();
page = 1;
loadConversationInfo();
loadMessageList();
});
// 初始化加载
if (conversation_id) {
loadConversationInfo();
loadMessageList();
}
});
</script>

View File

@@ -1,38 +1,30 @@
<?php
/**
*/
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [],
// 自定义页面路径
'link' => [],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
<?php
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [],
// 自定义页面路径
'link' => [],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
];

View File

@@ -1,21 +1,12 @@
<?php
/**
*/
return [
'name' => 'alioss',
'title' => '阿里云OSS',
'description' => '阿里云OSS',
'type' => 'system', //插件类型 system :系统插件(自动安装), business:业务插件 promotion:营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '5.3.1',
'version_no' => '525231212001',
'content' => '',
<?php
return [
'name' => 'alioss',
'title' => '阿里云OSS',
'description' => '阿里云OSS',
'type' => 'system', //插件类型 system :系统插件(自动安装), business:业务插件 promotion:营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '5.3.1',
'version_no' => '525231212001',
'content' => '',
];

View File

@@ -1,39 +1,31 @@
<?php
/**
*/
namespace addon\alioss\event;
use addon\alioss\model\Alioss;
use addon\alioss\model\Config;
/**
* 删除阿里云图片
*/
class ClearAlbumPic
{
public function handle($params)
{
$config_model = new Config();
$alioss_model = new Alioss();
$config = $config_model->getAliossConfig($params[ 'site_id' ]);
if (!empty($config[ 'data' ])) {
if (!empty($config[ 'data' ][ 'value' ][ 'endpoint' ]) && strpos($params[ 'pic_path' ], $config[ 'data' ][ 'value' ][ 'endpoint' ]) === 0) {
$result = $alioss_model->deleteAlbumPic($params[ 'pic_path' ], $config[ 'data' ][ 'value' ][ 'endpoint' ]);
return $result;
}
if (!empty($config[ 'data' ][ 'value' ][ 'domain' ]) && strpos($params[ 'pic_path' ], $config[ 'data' ][ 'value' ][ 'domain' ]) === 0) {
$result = $alioss_model->deleteAlbumPic($params[ 'pic_path' ], $config[ 'data' ][ 'value' ][ 'domain' ]);
return $result;
}
}
}
<?php
namespace addon\alioss\event;
use addon\alioss\model\Alioss;
use addon\alioss\model\Config;
/**
* 删除阿里云图片
*/
class ClearAlbumPic
{
public function handle($params)
{
$config_model = new Config();
$alioss_model = new Alioss();
$config = $config_model->getAliossConfig($params[ 'site_id' ]);
if (!empty($config[ 'data' ])) {
if (!empty($config[ 'data' ][ 'value' ][ 'endpoint' ]) && strpos($params[ 'pic_path' ], $config[ 'data' ][ 'value' ][ 'endpoint' ]) === 0) {
$result = $alioss_model->deleteAlbumPic($params[ 'pic_path' ], $config[ 'data' ][ 'value' ][ 'endpoint' ]);
return $result;
}
if (!empty($config[ 'data' ][ 'value' ][ 'domain' ]) && strpos($params[ 'pic_path' ], $config[ 'data' ][ 'value' ][ 'domain' ]) === 0) {
$result = $alioss_model->deleteAlbumPic($params[ 'pic_path' ], $config[ 'data' ][ 'value' ][ 'domain' ]);
return $result;
}
}
}
}

View File

@@ -1,27 +1,19 @@
<?php
/**
*/
namespace addon\alioss\event;
use addon\alioss\model\Config;
/**
* 关闭云上传
*/
class CloseOss
{
public function handle()
{
$config_model = new Config();
$result = $config_model->modifyConfigIsUse(0);
return $result;
}
<?php
namespace addon\alioss\event;
use addon\alioss\model\Config;
/**
* 关闭云上传
*/
class CloseOss
{
public function handle()
{
$config_model = new Config();
$result = $config_model->modifyConfigIsUse(0);
return $result;
}
}

View File

@@ -1,26 +1,18 @@
<?php
/**
*/
namespace addon\alioss\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
return success();
}
<?php
namespace addon\alioss\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
return success();
}
}

View File

@@ -1,33 +1,25 @@
<?php
/**
*/
namespace addon\alioss\event;
/**
* 云上传方式
*/
class OssType
{
/**
* 短信发送方式方式及配置
*/
public function handle()
{
$info = array(
"sms_type" => "alioss",
"sms_type_name" => "阿里云上传",
"edit_url" => "alioss://shop/config/config",
"shop_url" => "alioss://shop/config/config",
"desc" => "阿里云上传"
);
return $info;
}
<?php
namespace addon\alioss\event;
/**
* 云上传方式
*/
class OssType
{
/**
* 短信发送方式方式及配置
*/
public function handle()
{
$info = array(
"sms_type" => "alioss",
"sms_type_name" => "阿里云上传",
"edit_url" => "alioss://shop/config/config",
"shop_url" => "alioss://shop/config/config",
"desc" => "阿里云上传"
);
return $info;
}
}

View File

@@ -1,31 +1,23 @@
<?php
/**
*/
namespace addon\alioss\event;
use addon\alioss\model\Alioss;
/**
* 云上传方式
*/
class Put
{
/**
* @param $param
* @return array
*/
public function handle($param)
{
$qiniu_model = new Alioss();
$result = $qiniu_model->putFile($param);
return $result;
}
<?php
namespace addon\alioss\event;
use addon\alioss\model\Alioss;
/**
* 云上传方式
*/
class Put
{
/**
* @param $param
* @return array
*/
public function handle($param)
{
$qiniu_model = new Alioss();
$result = $qiniu_model->putFile($param);
return $result;
}
}

View File

@@ -1,27 +1,18 @@
<?php
/**
*/
namespace addon\alioss\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
return success();
}
<?php
namespace addon\alioss\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
return success();
}
}

View File

@@ -1,146 +1,138 @@
<?php
/**
*/
namespace addon\alioss\model;
use app\model\BaseModel;
use OSS\Core\OssException;
use OSS\OssClient;
use think\facade\Log;
/**
* 阿里云OSS上传
*/
class Alioss extends BaseModel
{
/**
* 字节组上传
* @param $data
* @param $key
* @return array
*/
public function put($param)
{
$data = $param['data'];
$key = $param['key'];
$config_model = new Config();
$config_result = $config_model->getAliossConfig();
$config = $config_result['data'];
if ($config['is_use'] == 1) {
$config = $config['value'];
$access_key_id = $config['access_key_id'];
$access_key_secret = $config['access_key_secret'];
$bucket = $config['bucket'];
$endpoint = $config['endpoint'];
try {
$ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
$result = $ossClient->putObject($bucket, $key, $data);
$is_domain = $config[ 'is_domain' ] ?? 0;
$path = $is_domain > 0 ? $config[ 'domain' ] . '/' . $key : $result['info']['url'];
$data = array (
'path' => $path,
// "path" => $result["info"]["url"],
'domain' => $endpoint,
'bucket' => $bucket
);
return $this->success($data);
} catch (OssException $e) {
return $this->error('', $e->getErrorMessage());
}
}
}
/**
* 设置阿里云OSS参数配置
* @param unknown $filePath 上传图片路径
* @param unknown $key 上传到阿里云后保存的文件名
*/
public function putFile($param)
{
$file_path = $param['file_path'];
$key = $param['key'];
$config_model = new Config();
$config = $config_model->getAliossConfig()['data'];
if ($config['is_use'] == 1) {
$config = $config['value'];
$access_key_id = $config['access_key_id'];
$access_key_secret = $config['access_key_secret'];
$bucket = $config['bucket'];
//要上传的空间
$endpoint = $config['endpoint'];
try {
$ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
$result = $ossClient->uploadFile($bucket, $key, $file_path);
$is_domain = $config[ 'is_domain' ] ?? 0;
$path = $is_domain > 0 ? $config[ 'domain' ] . '/' . $key : $result['info']['url'];
$path = str_replace('http://', 'https://', $path);
//返回图片的完整URL
$data = array (
// "path" => $this->subEndpoint($endpoint, $bucket)."/". $key,
'path' => $path,
'domain' => $endpoint,
'bucket' => $bucket
);
return $this->success($data);
} catch (\Exception $e) {
return $this->error('', $e->getMessage());
}
}
}
public function subEndpoint($endpoint, $bucket)
{
if (strpos($endpoint, 'http://') === 0) {
$temp = 'http://';
} else {
$temp = 'https://';
}
$temp_array = explode($temp, $endpoint);
return $temp . $bucket . '.' . $temp_array[ 1 ];
}
/**
* @param $file_path
* @return array
* 删除阿里云图片
*/
public function deleteAlbumPic($file_path, $prefix)
{
$config_model = new Config();
$config_result = $config_model->getAliossConfig();
$config = $config_result['data'];
if (!empty($config)) {
$config = $config['value'];
$access_key_id = $config['access_key_id'];
$access_key_secret = $config['access_key_secret'];
$bucket = $config['bucket'];
//要上传的空间
$endpoint = $config['endpoint'];
try {
$ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
$ossClient->deleteObject($bucket, str_replace($prefix . '/', '', $file_path));
$ossClient->deleteObject($bucket, str_replace($prefix . '/', '', img($file_path, 'big')));
$ossClient->deleteObject($bucket, str_replace($prefix . '/', '', img($file_path, 'mid')));
$ossClient->deleteObject($bucket, str_replace($prefix . '/', '', img($file_path, 'small')));
return $this->success();
} catch (OssException $e) {
return $this->error('', $e->getErrorMessage());
}
}
}
<?php
namespace addon\alioss\model;
use app\model\BaseModel;
use OSS\Core\OssException;
use OSS\OssClient;
use think\facade\Log;
/**
* 阿里云OSS上传
*/
class Alioss extends BaseModel
{
/**
* 字节组上传
* @param $data
* @param $key
* @return array
*/
public function put($param)
{
$data = $param['data'];
$key = $param['key'];
$config_model = new Config();
$config_result = $config_model->getAliossConfig();
$config = $config_result['data'];
if ($config['is_use'] == 1) {
$config = $config['value'];
$access_key_id = $config['access_key_id'];
$access_key_secret = $config['access_key_secret'];
$bucket = $config['bucket'];
$endpoint = $config['endpoint'];
try {
$ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
$result = $ossClient->putObject($bucket, $key, $data);
$is_domain = $config[ 'is_domain' ] ?? 0;
$path = $is_domain > 0 ? $config[ 'domain' ] . '/' . $key : $result['info']['url'];
$data = array (
'path' => $path,
// "path" => $result["info"]["url"],
'domain' => $endpoint,
'bucket' => $bucket
);
return $this->success($data);
} catch (OssException $e) {
return $this->error('', $e->getErrorMessage());
}
}
}
/**
* 设置阿里云OSS参数配置
* @param unknown $filePath 上传图片路径
* @param unknown $key 上传到阿里云后保存的文件名
*/
public function putFile($param)
{
$file_path = $param['file_path'];
$key = $param['key'];
$config_model = new Config();
$config = $config_model->getAliossConfig()['data'];
if ($config['is_use'] == 1) {
$config = $config['value'];
$access_key_id = $config['access_key_id'];
$access_key_secret = $config['access_key_secret'];
$bucket = $config['bucket'];
//要上传的空间
$endpoint = $config['endpoint'];
try {
$ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
$result = $ossClient->uploadFile($bucket, $key, $file_path);
$is_domain = $config[ 'is_domain' ] ?? 0;
$path = $is_domain > 0 ? $config[ 'domain' ] . '/' . $key : $result['info']['url'];
$path = str_replace('http://', 'https://', $path);
//返回图片的完整URL
$data = array (
// "path" => $this->subEndpoint($endpoint, $bucket)."/". $key,
'path' => $path,
'domain' => $endpoint,
'bucket' => $bucket
);
return $this->success($data);
} catch (\Exception $e) {
return $this->error('', $e->getMessage());
}
}
}
public function subEndpoint($endpoint, $bucket)
{
if (strpos($endpoint, 'http://') === 0) {
$temp = 'http://';
} else {
$temp = 'https://';
}
$temp_array = explode($temp, $endpoint);
return $temp . $bucket . '.' . $temp_array[ 1 ];
}
/**
* @param $file_path
* @return array
* 删除阿里云图片
*/
public function deleteAlbumPic($file_path, $prefix)
{
$config_model = new Config();
$config_result = $config_model->getAliossConfig();
$config = $config_result['data'];
if (!empty($config)) {
$config = $config['value'];
$access_key_id = $config['access_key_id'];
$access_key_secret = $config['access_key_secret'];
$bucket = $config['bucket'];
//要上传的空间
$endpoint = $config['endpoint'];
try {
$ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
$ossClient->deleteObject($bucket, str_replace($prefix . '/', '', $file_path));
$ossClient->deleteObject($bucket, str_replace($prefix . '/', '', img($file_path, 'big')));
$ossClient->deleteObject($bucket, str_replace($prefix . '/', '', img($file_path, 'mid')));
$ossClient->deleteObject($bucket, str_replace($prefix . '/', '', img($file_path, 'small')));
return $this->success();
} catch (OssException $e) {
return $this->error('', $e->getErrorMessage());
}
}
}
}

View File

@@ -1,56 +1,48 @@
<?php
/**
*/
namespace addon\alioss\model;
use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
/**
* 阿里云配置
*/
class Config extends BaseModel
{
/**
* 设置阿里云OSS上传配置
* array $data
*/
public function setAliossConfig($data, $status, $site_id = 1, $app_module = 'shop')
{
if ($status == 1) {
event('CloseOss', []);//同步关闭所有云上传
}
$config = new ConfigModel();
$res = $config->setConfig($data, '阿里云OSS上传配置', $status, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALIOSS_CONFIG' ] ]);
return $res;
}
/**
* 获取阿里云上传配置
*/
public function getAliossConfig($site_id = 1, $app_module = 'shop')
{
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALIOSS_CONFIG' ] ]);
return $res;
}
/**
* 配置阿里云开关状态
* @param $status
*/
public function modifyConfigIsUse($status, $site_id = 1, $app_module = 'shop')
{
$config = new ConfigModel();
$res = $config->modifyConfigIsUse($status, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALIOSS_CONFIG' ] ]);
return $res;
}
<?php
namespace addon\alioss\model;
use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
/**
* 阿里云配置
*/
class Config extends BaseModel
{
/**
* 设置阿里云OSS上传配置
* array $data
*/
public function setAliossConfig($data, $status, $site_id = 1, $app_module = 'shop')
{
if ($status == 1) {
event('CloseOss', []);//同步关闭所有云上传
}
$config = new ConfigModel();
$res = $config->setConfig($data, '阿里云OSS上传配置', $status, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALIOSS_CONFIG' ] ]);
return $res;
}
/**
* 获取阿里云上传配置
*/
public function getAliossConfig($site_id = 1, $app_module = 'shop')
{
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALIOSS_CONFIG' ] ]);
return $res;
}
/**
* 配置阿里云开关状态
* @param $status
*/
public function modifyConfigIsUse($status, $site_id = 1, $app_module = 'shop')
{
$config = new ConfigModel();
$res = $config->modifyConfigIsUse($status, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALIOSS_CONFIG' ] ]);
return $res;
}
}

View File

@@ -1,56 +1,48 @@
<?php
/**
*/
namespace addon\alioss\shop\controller;
use addon\alioss\model\Config as ConfigModel;
use app\shop\controller\BaseShop;
/**
* 七牛云上传管理
*/
class Config extends BaseShop
{
/**
* 云上传配置
* @return mixed
*/
public function config()
{
$config_model = new ConfigModel();
if (request()->isJson()) {
$bucket = input('bucket', '');
$access_key_id = input('access_key_id', '');
$access_key_secret = input('access_key_secret', '');
$endpoint = input('endpoint', '');
$status = input('status', 0);
$domain = input('domain', '');
$is_domain = input('is_domain', 0);
$data = array (
'bucket' => $bucket,
'access_key_id' => $access_key_id,
'access_key_secret' => $access_key_secret,
'endpoint' => $endpoint,
'domain' => $domain,
'is_domain' => $is_domain
);
$result = $config_model->setAliossConfig($data, $status, $this->site_id, $this->app_module);
return $result;
} else {
$info_result = $config_model->getAliossConfig($this->site_id, $this->app_module);
$info = $info_result['data'];
$this->assign('info', $info);
return $this->fetch('config/config');
}
}
<?php
namespace addon\alioss\shop\controller;
use addon\alioss\model\Config as ConfigModel;
use app\shop\controller\BaseShop;
/**
* 七牛云上传管理
*/
class Config extends BaseShop
{
/**
* 云上传配置
* @return mixed
*/
public function config()
{
$config_model = new ConfigModel();
if (request()->isJson()) {
$bucket = input('bucket', '');
$access_key_id = input('access_key_id', '');
$access_key_secret = input('access_key_secret', '');
$endpoint = input('endpoint', '');
$status = input('status', 0);
$domain = input('domain', '');
$is_domain = input('is_domain', 0);
$data = array (
'bucket' => $bucket,
'access_key_id' => $access_key_id,
'access_key_secret' => $access_key_secret,
'endpoint' => $endpoint,
'domain' => $domain,
'is_domain' => $is_domain
);
$result = $config_model->setAliossConfig($data, $status, $this->site_id, $this->app_module);
return $result;
} else {
$info_result = $config_model->getAliossConfig($this->site_id, $this->app_module);
$info = $info_result['data'];
$this->assign('info', $info);
return $this->fetch('config/config');
}
}
}

View File

@@ -1,38 +1,30 @@
<?php
/**
*/
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [],
// 自定义页面路径
'link' => [],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
<?php
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [],
// 自定义页面路径
'link' => [],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
];

View File

@@ -1,50 +1,42 @@
<?php
/**
*/
return [
'bind' => [
],
'listen' => [
//支付异步回调
'PayNotify' => [
'addon\alipay\event\PayNotify'
],
//支付方式,后台查询
'PayType' => [
'addon\alipay\event\PayType'
],
//支付,前台应用
'Pay' => [
'addon\alipay\event\Pay'
],
'PayClose' => [
'addon\alipay\event\PayClose'
],
'PayRefund' => [
'addon\alipay\event\PayRefund'
],
'PayTransfer' => [
'addon\alipay\event\PayTransfer'
],
'TransferType' => [
'addon\alipay\event\TransferType'
],
'AuthcodePay' => [
'addon\alipay\event\AuthcodePay'
],
'PayOrderQuery' => [
'addon\alipay\event\PayOrderQuery'
],
],
'subscribe' => [
],
];
<?php
return [
'bind' => [
],
'listen' => [
//支付异步回调
'PayNotify' => [
'addon\alipay\event\PayNotify'
],
//支付方式,后台查询
'PayType' => [
'addon\alipay\event\PayType'
],
//支付,前台应用
'Pay' => [
'addon\alipay\event\Pay'
],
'PayClose' => [
'addon\alipay\event\PayClose'
],
'PayRefund' => [
'addon\alipay\event\PayRefund'
],
'PayTransfer' => [
'addon\alipay\event\PayTransfer'
],
'TransferType' => [
'addon\alipay\event\TransferType'
],
'AuthcodePay' => [
'addon\alipay\event\AuthcodePay'
],
'PayOrderQuery' => [
'addon\alipay\event\PayOrderQuery'
],
],
'subscribe' => [
],
];

View File

@@ -1,21 +1,12 @@
<?php
/**
*/
return [
'name' => 'alipay',
'title' => '支付宝支付',
'description' => '支付宝支付功能',
'type' => 'system', //插件类型 system :系统插件(自动安装), promotion:营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '5.3.1',
'version_no' => '525231212001',
'content' => '',
<?php
return [
'name' => 'alipay',
'title' => '支付宝支付',
'description' => '支付宝支付功能',
'type' => 'system', //插件类型 system :系统插件(自动安装), promotion:营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '5.3.1',
'version_no' => '525231212001',
'content' => '',
];

View File

@@ -1,23 +1,15 @@
<?php
/**
*/
return [
[
'name' => 'ALI_PAY_CONFIG',
'title' => '支付宝支付编辑',
'url' => 'alipay://shop/pay/config',
'parent' => 'CONFIG_PAY',
'is_show' => 0,
'is_control' => 1,
'is_icon' => 0,
'picture' => '',
'picture_select' => '',
'sort' => 1,
],
];
<?php
return [
[
'name' => 'ALI_PAY_CONFIG',
'title' => '支付宝支付编辑',
'url' => 'alipay://shop/pay/config',
'parent' => 'CONFIG_PAY',
'is_show' => 0,
'is_control' => 1,
'is_icon' => 0,
'picture' => '',
'picture_select' => '',
'sort' => 1,
],
];

View File

@@ -1,27 +1,18 @@
<?php
/**
*/
namespace addon\alipay\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
return success();
}
<?php
namespace addon\alipay\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
return success();
}
}

View File

@@ -1,33 +1,25 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 生成支付
*/
class Pay
{
/**
* 支付方式及配置
*/
public function handle($param)
{
if ($param[ "pay_type" ] == "alipay") {
if (in_array($param[ "app_type" ], [ "h5", "app", "pc", "aliapp", 'wechat' ])) {
$pay_model = new PayModel($param[ 'site_id' ], $param[ "app_type" ] == 'aliapp');
$res = $pay_model->pay($param);
return $res;
}
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 生成支付
*/
class Pay
{
/**
* 支付方式及配置
*/
public function handle($param)
{
if ($param[ "pay_type" ] == "alipay") {
if (in_array($param[ "app_type" ], [ "h5", "app", "pc", "aliapp", 'wechat' ])) {
$pay_model = new PayModel($param[ 'site_id' ], $param[ "app_type" ] == 'aliapp');
$res = $pay_model->pay($param);
return $res;
}
}
}
}

View File

@@ -1,39 +1,31 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 关闭支付
*/
class PayClose
{
/**
* 关闭支付
* @param $params
* @return \addon\alipay\model\multitype|array
*/
public function handle($params)
{
// if ($params["pay_type"] == "alipay") {
try {
$pay_model = new PayModel($params[ 'site_id' ]);
$result = $pay_model->close($params);
return $result;
} catch (\Exception $e) {
return error(-1, $e->getMessage());
} catch (\Throwable $e) {
return error(-1, $e->getMessage());
}
// }
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 关闭支付
*/
class PayClose
{
/**
* 关闭支付
* @param $params
* @return \addon\alipay\model\multitype|array
*/
public function handle($params)
{
if ($params["pay_type"] == "alipay") {
try {
$pay_model = new PayModel($params[ 'site_id' ]);
$result = $pay_model->close($params);
return $result;
} catch (\Exception $e) {
return error(-1, $e->getMessage());
} catch (\Throwable $e) {
return error(-1, $e->getMessage());
}
}
}
}

View File

@@ -1,41 +1,35 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
use app\model\system\Pay as PayCommon;
/**
* 支付回调
*/
class PayNotify
{
/**
* 支付方式及配置
*/
public function handle()
{
if (isset($_POST[ 'out_trade_no' ])) {
$out_trade_no = $_POST[ 'out_trade_no' ];
$pay = new PayCommon();
$pay_info = $pay->getPayInfo($out_trade_no)[ 'data' ];
if (empty($pay_info)) return false;
if ($_POST[ 'total_amount' ] != $pay_info[ 'pay_money' ]) {
return false;
}
$mch_info = empty($pay_info[ 'mch_info' ]) ? [] : json_decode($pay_info[ 'mch_info' ], true);
$pay_model = new PayModel($pay_info[ 'site_id' ], $mch_info[ 'is_aliapp' ] ?? 0);
$pay_model->payNotify();
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
use app\model\system\Pay as PayCommon;
/**
* 支付回调
*/
class PayNotify
{
/**
* 支付方式及配置
*/
public function handle($param)
{
if ($param[ "pay_type" ] != "alipay") return false;
if (isset($_POST[ 'out_trade_no' ])) {
$out_trade_no = $_POST[ 'out_trade_no' ];
$pay = new PayCommon();
$pay_info = $pay->getPayInfo($out_trade_no)[ 'data' ];
if (empty($pay_info)) return false;
if ($_POST[ 'total_amount' ] != $pay_info[ 'pay_money' ]) {
return false;
}
$mch_info = empty($pay_info[ 'mch_info' ]) ? [] : json_decode($pay_info[ 'mch_info' ], true);
$pay_model = new PayModel($pay_info[ 'site_id' ], $mch_info[ 'is_aliapp' ] ?? 0);
$pay_model->payNotify();
}
}
}

View File

@@ -1,34 +1,26 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Config as ConfigModel;
use addon\alipay\model\Pay as PayModel;
use app\model\system\Pay;
/**
* 查询支付结果
*/
class PayOrderQuery
{
public function handle(array $params)
{
$pay_info = ( new Pay() )->getInfo([ [ 'id', '=', $params[ 'relate_id' ] ] ])[ 'data' ];
if (!empty($pay_info)) {
$config_model = new ConfigModel();
$pay_config = $config_model->getPayConfig($pay_info[ 'site_id' ])[ 'data' ][ 'value' ];
if (!empty($pay_config) && $pay_config[ 'pay_status' ] != 2) {
$pay_common = new PayModel($pay_info[ 'site_id' ]);
$pay_common->orderQuery($pay_info);
}
}
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Config as ConfigModel;
use addon\alipay\model\Pay as PayModel;
use app\model\system\Pay;
/**
* 查询支付结果
*/
class PayOrderQuery
{
public function handle(array $params)
{
$pay_info = ( new Pay() )->getInfo([ [ 'id', '=', $params[ 'relate_id' ] ] ])[ 'data' ];
if (!empty($pay_info)) {
$config_model = new ConfigModel();
$pay_config = $config_model->getPayConfig($pay_info[ 'site_id' ])[ 'data' ][ 'value' ];
if (!empty($pay_config) && $pay_config[ 'pay_status' ] != 2) {
$pay_common = new PayModel($pay_info[ 'site_id' ]);
$pay_common->orderQuery($pay_info);
}
}
}
}

View File

@@ -1,33 +1,25 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 原路退款
*/
class PayRefund
{
/**
* 关闭支付
*/
public function handle($params)
{
if ($params[ "pay_info" ][ "pay_type" ] == "alipay") {
$mch_info = empty($params[ 'pay_info' ][ 'mch_info' ]) ? [] : json_decode($params[ 'pay_info' ][ 'mch_info' ], true);
$pay_model = new PayModel($params[ 'site_id' ], $mch_info[ 'is_aliapp' ] ?? 0);
$result = $pay_model->refund($params);
return $result;
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 原路退款
*/
class PayRefund
{
/**
* 关闭支付
*/
public function handle($params)
{
if ($params[ "pay_info" ][ "pay_type" ] == "alipay") {
$mch_info = empty($params[ 'pay_info' ][ 'mch_info' ]) ? [] : json_decode($params[ 'pay_info' ][ 'mch_info' ], true);
$pay_model = new PayModel($params[ 'site_id' ], $mch_info[ 'is_aliapp' ] ?? 0);
$result = $pay_model->refund($params);
return $result;
}
}
}

View File

@@ -1,42 +1,34 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay;
use addon\alipay\model\Config;
class PayTransfer
{
public function handle(array $params)
{
if ($params[ 'transfer_type' ] == 'alipay') {
$pay = new Pay($params[ 'site_id' ]);
$config_model = new Config();
$config_result = $config_model->getPayConfig($params[ 'site_id' ]);
$config = $config_result[ "data" ];
if (!empty($config[ 'value' ])) {
$config_info = $config[ "value" ];
$countersign_type = $config_info['countersign_type'] ?? 0;
if ($countersign_type == 0) {
$res = $pay->payTransfer($params);
return $res;
} else {
$res = $pay->payNewTransfer($params);
return $res;
}
} else {
$res = $pay->payTransfer($params);
return $res;
}
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay;
use addon\alipay\model\Config;
class PayTransfer
{
public function handle(array $params)
{
if ($params[ 'transfer_type' ] == 'alipay') {
$pay = new Pay($params[ 'site_id' ]);
$config_model = new Config();
$config_result = $config_model->getPayConfig($params[ 'site_id' ]);
$config = $config_result[ "data" ];
if (!empty($config[ 'value' ])) {
$config_info = $config[ "value" ];
$countersign_type = $config_info['countersign_type'] ?? 0;
if ($countersign_type == 0) {
$res = $pay->payTransfer($params);
return $res;
} else {
$res = $pay->payNewTransfer($params);
return $res;
}
} else {
$res = $pay->payTransfer($params);
return $res;
}
}
}
}

View File

@@ -1,51 +1,43 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Config;
/**
* 支付方式 (后台调用)
*/
class PayType
{
/**
* 支付方式及配置
*/
public function handle($param)
{
$app_type = $param['app_type'] ?? '';
if (!empty($app_type)) {
if (!in_array($app_type, [ "h5", "app", "pc", "aliapp", 'wechat' ])) {
return '';
}
if ($app_type != 'aliapp') {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
$config = $config_result[ "data" ][ "value" ] ?? [];
$pay_status = $config[ "pay_status" ] ?? 0;
if ($pay_status == 0) {
return '';
}
}
}
$info = array (
"pay_type" => "alipay",
"pay_type_name" => "支付宝支付",
"edit_url" => "alipay://shop/pay/config",
"shop_url" => "alipay://shop/pay/config",
"logo" => "addon/alipay/icon.png",
"desc" => "支付宝网站(www.alipay.com) 是国内先进的网上支付平台。"
);
return $info;
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Config;
/**
* 支付方式 (后台调用)
*/
class PayType
{
/**
* 支付方式及配置
*/
public function handle($param)
{
$app_type = $param['app_type'] ?? '';
if (!empty($app_type)) {
if (!in_array($app_type, [ "h5", "app", "pc", "aliapp", 'wechat' ])) {
return '';
}
if ($app_type != 'aliapp') {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
$config = $config_result[ "data" ][ "value" ] ?? [];
$pay_status = $config[ "pay_status" ] ?? 0;
if ($pay_status == 0) {
return '';
}
}
}
$info = array (
"pay_type" => "alipay",
"pay_type_name" => "支付宝支付",
"edit_url" => "alipay://shop/pay/config",
"shop_url" => "alipay://shop/pay/config",
"logo" => "addon/alipay/icon.png",
"desc" => "支付宝网站(www.alipay.com) 是国内先进的网上支付平台。"
);
return $info;
}
}

View File

@@ -1,39 +1,31 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Config;
class TransferType
{
public function handle(array $param)
{
$app_type = $param['app_type'] ?? '';
if (!empty($app_type)) {
if (!in_array($app_type, [ "h5", "app", "pc", "aliapp" ])) {
return '';
}
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
$config = $config_result[ "data" ][ "value" ] ?? [];
$transfer_status = $config[ "transfer_status" ] ?? 0;
if ($transfer_status == 0) {
return '';
}
}
$info = array (
"type" => "alipay",
"type_name" => "支付宝",
);
return $info;
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Config;
class TransferType
{
public function handle(array $param)
{
$app_type = $param['app_type'] ?? '';
if (!empty($app_type)) {
if (!in_array($app_type, [ "h5", "app", "pc", "aliapp" ])) {
return '';
}
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
$config = $config_result[ "data" ][ "value" ] ?? [];
$transfer_status = $config[ "transfer_status" ] ?? 0;
if ($transfer_status == 0) {
return '';
}
}
$info = array (
"type" => "alipay",
"type_name" => "支付宝",
);
return $info;
}
}

View File

@@ -1,26 +1,17 @@
<?php
/**
*/
namespace addon\alipay\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
return error(-1, "系统插件不得删除");
}
<?php
namespace addon\alipay\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
return error(-1, "系统插件不得删除");
}
}

View File

@@ -1,96 +1,88 @@
<?php
/**
*/
namespace addon\alipay\model;
use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
/**
* 支付宝支付配置
*/
class Config extends BaseModel
{
private $encrypt = '******';
/**
* 设置支付配置
* @param $data
* @param int $site_id
* @param string $app_module
* @return array
*/
public function setPayConfig($data, $site_id = 0, $app_module = 'shop')
{
$config = new ConfigModel();
// 未加密前的数据
$original_config = $this->getPayConfig($site_id)[ 'data' ][ 'value' ];
// 检测数据是否发生变化,如果没有变化,则保持未加密前的数据
if (!empty($data[ 'private_key' ]) && $data[ 'private_key' ] == $this->encrypt) {
$data[ 'private_key' ] = $original_config[ 'private_key' ]; // 应用私钥
}
if (!empty($data[ 'public_key' ]) && $data[ 'public_key' ] == $this->encrypt) {
$data[ 'public_key' ] = $original_config[ 'public_key' ]; // 应用公钥
}
if (!empty($data[ 'alipay_public_key' ]) && $data[ 'alipay_public_key' ] == $this->encrypt) {
$data[ 'alipay_public_key' ] = $original_config[ 'alipay_public_key' ]; // 支付宝公钥
}
if (!empty($data[ 'public_key_crt' ]) && $data[ 'public_key_crt' ] == $this->encrypt) {
$data[ 'public_key_crt' ] = $original_config[ 'public_key_crt' ]; // 应用公钥证书
}
if (!empty($data[ 'alipay_public_key_crt' ]) && $data[ 'alipay_public_key_crt' ] == $this->encrypt) {
$data[ 'alipay_public_key_crt' ] = $original_config[ 'alipay_public_key_crt' ]; // 支付宝公钥证书
}
if (!empty($data[ 'alipay_with_crt' ]) && $data[ 'alipay_with_crt' ] == $this->encrypt) {
$data[ 'alipay_with_crt' ] = $original_config[ 'alipay_with_crt' ]; // 支付宝根证书
}
$res = $config->setConfig($data, '支付宝支付配置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALI_PAY_CONFIG' ] ]);
return $res;
}
/**
* 获取支付配置
* @param int $site_id
* @param string $app_module
* @param bool $need_encrypt 是否需要加密数据true加密、false不加密
* @return array
*/
public function getPayConfig($site_id = 0, $app_module = 'shop', $need_encrypt = false)
{
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALI_PAY_CONFIG' ] ]);
if ($need_encrypt) {
// 加密敏感信息
if (!empty($res[ 'data' ][ 'value' ][ 'private_key' ])) {
$res[ 'data' ][ 'value' ][ 'private_key' ] = $this->encrypt; // 应用私钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'public_key' ])) {
$res[ 'data' ][ 'value' ][ 'public_key' ] = $this->encrypt; // 应用公钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_public_key' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_public_key' ] = $this->encrypt; // 支付宝公钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'public_key_crt' ])) {
$res[ 'data' ][ 'value' ][ 'public_key_crt' ] = $this->encrypt; // 应用公钥证书
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_public_key_crt' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_public_key_crt' ] = $this->encrypt; // 支付宝公钥证书
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_with_crt' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_with_crt' ] = $this->encrypt; // 支付宝根证书
}
}
return $res;
}
<?php
namespace addon\alipay\model;
use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
/**
* 支付宝支付配置
*/
class Config extends BaseModel
{
private $encrypt = '******';
/**
* 设置支付配置
* @param $data
* @param int $site_id
* @param string $app_module
* @return array
*/
public function setPayConfig($data, $site_id = 0, $app_module = 'shop')
{
$config = new ConfigModel();
// 未加密前的数据
$original_config = $this->getPayConfig($site_id)[ 'data' ][ 'value' ];
// 检测数据是否发生变化,如果没有变化,则保持未加密前的数据
if (!empty($data[ 'private_key' ]) && $data[ 'private_key' ] == $this->encrypt) {
$data[ 'private_key' ] = $original_config[ 'private_key' ]; // 应用私钥
}
if (!empty($data[ 'public_key' ]) && $data[ 'public_key' ] == $this->encrypt) {
$data[ 'public_key' ] = $original_config[ 'public_key' ]; // 应用公钥
}
if (!empty($data[ 'alipay_public_key' ]) && $data[ 'alipay_public_key' ] == $this->encrypt) {
$data[ 'alipay_public_key' ] = $original_config[ 'alipay_public_key' ]; // 支付宝公钥
}
if (!empty($data[ 'public_key_crt' ]) && $data[ 'public_key_crt' ] == $this->encrypt) {
$data[ 'public_key_crt' ] = $original_config[ 'public_key_crt' ]; // 应用公钥证书
}
if (!empty($data[ 'alipay_public_key_crt' ]) && $data[ 'alipay_public_key_crt' ] == $this->encrypt) {
$data[ 'alipay_public_key_crt' ] = $original_config[ 'alipay_public_key_crt' ]; // 支付宝公钥证书
}
if (!empty($data[ 'alipay_with_crt' ]) && $data[ 'alipay_with_crt' ] == $this->encrypt) {
$data[ 'alipay_with_crt' ] = $original_config[ 'alipay_with_crt' ]; // 支付宝根证书
}
$res = $config->setConfig($data, '支付宝支付配置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALI_PAY_CONFIG' ] ]);
return $res;
}
/**
* 获取支付配置
* @param int $site_id
* @param string $app_module
* @param bool $need_encrypt 是否需要加密数据true加密、false不加密
* @return array
*/
public function getPayConfig($site_id = 0, $app_module = 'shop', $need_encrypt = false)
{
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALI_PAY_CONFIG' ] ]);
if ($need_encrypt) {
// 加密敏感信息
if (!empty($res[ 'data' ][ 'value' ][ 'private_key' ])) {
$res[ 'data' ][ 'value' ][ 'private_key' ] = $this->encrypt; // 应用私钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'public_key' ])) {
$res[ 'data' ][ 'value' ][ 'public_key' ] = $this->encrypt; // 应用公钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_public_key' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_public_key' ] = $this->encrypt; // 支付宝公钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'public_key_crt' ])) {
$res[ 'data' ][ 'value' ][ 'public_key_crt' ] = $this->encrypt; // 应用公钥证书
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_public_key_crt' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_public_key_crt' ] = $this->encrypt; // 支付宝公钥证书
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_with_crt' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_with_crt' ] = $this->encrypt; // 支付宝根证书
}
}
return $res;
}
}

View File

@@ -1,486 +1,478 @@
<?php
/**
*/
namespace addon\alipay\model;
use addon\alipay\data\sdk\AopClient;
use addon\alipay\data\sdk\request\AlipayFundTransToaccountTransferRequest;
use addon\alipay\data\sdk\request\AlipayTradeAppPayRequest;
use addon\alipay\data\sdk\request\AlipayTradeCloseRequest;
use addon\alipay\data\sdk\request\AlipayTradeCreateRequest;
use addon\alipay\data\sdk\request\AlipayTradePagePayRequest;
use addon\alipay\data\sdk\request\AlipayTradeRefundRequest;
use addon\alipay\data\sdk\request\AlipayTradeWapPayRequest;
use addon\alipay\data\sdk\request\AlipayTradePrecreateRequest;
use addon\alipay\data\sdk\request\AlipayTradePayRequest;
use addon\alipay\data\sdk\request\AlipayTradeQueryRequest;
use app\model\BaseModel;
use app\model\system\Cron;
use app\model\system\Pay as PayCommon;
use addon\alipay\data\sdk\request\AlipayFundTransUniTransferRequest;
use addon\alipay\data\sdk\AopCertClient;
use app\model\system\Pay as PayModel;
use addon\aliapp\model\Config as AliappConfig;
use think\facade\Log;
/**
* 支付宝支付配置
*/
class Pay extends BaseModel
{
public $aop;
private $is_aliapp = 0;
/**
*
* @param $site_id
* @param int $is_aliapp 是否是小程序
*/
function __construct($site_id, $is_aliapp = 0)
{
$this->is_aliapp = $is_aliapp;
try {
// 获取支付宝支付参数(统一支付到平台账户)
if ($is_aliapp) {
$config_info = ( new AliappConfig() )->getAliappConfig($site_id)[ 'data' ][ 'value' ];
} else {
$config_info = ( new Config() )->getPayConfig($site_id)[ 'data' ][ 'value' ];
}
if (!empty($config_info)) {
$countersign_type = $config_info[ 'countersign_type' ] ?? 0;
if ($countersign_type == 1) {
$appCertPath = $config_info[ "public_key_crt" ] ?? "";
$alipayCertPath = $config_info[ "alipay_public_key_crt" ] ?? "";
$rootCertPath = $config_info[ "alipay_with_crt" ] ?? "";
$this->aop = new AopCertClient();
//调用getPublicKey从支付宝公钥证书中提取公钥
$this->aop->alipayrsaPublicKey = $this->aop->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$this->aop->isCheckAlipayPublicCert = false;
//调用getCertSN获取证书序列号
$this->aop->appCertSN = $this->aop->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$this->aop->alipayRootCertSN = $this->aop->getRootCertSN($rootCertPath);
} else {
// 获取支付宝支付参数(统一支付到平台账户)
$this->aop = new AopClient();
$this->aop->alipayrsaPublicKey = $config_info[ 'public_key' ] ?? "";
$this->aop->alipayPublicKey = $config_info[ 'alipay_public_key' ] ?? "";
}
$this->aop->appId = $config_info[ "app_id" ] ?? "";
$this->aop->rsaPrivateKey = $config_info[ 'private_key' ] ?? "";
$this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$this->aop->apiVersion = '1.0';
$this->aop->signType = 'RSA2';
$this->aop->postCharset = 'UTF-8';
$this->aop->format = 'json';
}
// else{
// return $this->error('', '支付宝支付未配置');
// }
} catch (\Exception $e) {
return $this->error('', '支付宝配置错误');
}
}
/**
* 生成支付
* @param $param
* @return array
*/
public function pay($param)
{
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
"subject" => str_sub($param[ "pay_body" ], 15),
"total_amount" => (float) $param[ "pay_money" ],
"body" => str_sub($param[ "pay_body" ], 60),
"product_code" => 'FAST_INSTANT_TRADE_PAY',
);
switch ( $param[ "app_type" ] ) {
case "h5":
$request = new AlipayTradeWapPayRequest();
break;
case "pc":
$request = new AlipayTradePagePayRequest();
break;
case "app":
$request = new AlipayTradeAppPayRequest();
break;
case 'wechat':
$request = new AlipayTradeWapPayRequest();
break;
case 'cashier':
$request = new AlipayTradePrecreateRequest();
break;
case 'aliapp':
$parameter[ 'product_code' ] = 'FACE_TO_FACE_PAYMENT';
$member_info = model('member')->getInfo([ [ "member_id", "=", $param[ "member_id" ] ] ], 'ali_openid');
if (empty($member_info)) return $this->error(-1, '未获取到会员信息');
$parameter[ 'buyer_id' ] = $member_info[ 'ali_openid' ];
$request = new AlipayTradeCreateRequest();
break;
}
$parameter = json_encode($parameter);
$request->setBizContent($parameter);
$request->SetReturnUrl($param[ "return_url" ]);
$request->SetNotifyUrl($param[ "notify_url" ]);
///绑定商户数据
$pay_model = new PayModel();
$pay_model->bindMchPay($param[ "out_trade_no" ], [ "is_aliapp" => $this->is_aliapp ]);
try {
if ($param[ "app_type" ] == 'h5' || $param[ "app_type" ] == 'wechat' || $param[ "app_type" ] == 'pc') {
$result = $this->aop->pageExecute($request, 'get');
return $this->success([
'type' => 'url',
'data' => $result
]);
} elseif ($param[ "app_type" ] == 'app') {
$result = $this->aop->sdkExecute($request);
if (strpos(get_class($this->aop), 'AopClient') !== false) {
return $this->success([
'type' => 'url',
'data' => $result
]);
}
} else {
$result = $this->aop->execute($request);
}
if ($result === false) return $this->error('', '支付宝发起支付失败');
} catch (\Exception $e) {
return $this->error('', $e->getMessage());
}
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
switch ( $param[ "app_type" ] ) {
case 'cashier':
return $this->success([
'type' => 'qrcode',
'data' => [
'qrcode' => $result->$responseNode->qr_code
]
]);
break;
case 'aliapp':
return $this->success([
'type' => 'data',
'data' => [
'orderInfo' => $result->$responseNode->trade_no
]
]);
break;
default:
return $this->success();
}
} else {
return $this->error("", $result->$responseNode->sub_msg);
}
}
/**
* 支付关闭
* @param $param
* @return array
* @throws \think\Exception
*/
public function close($param)
{
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ]
);
// 建立请求
$request = new AlipayTradeCloseRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success();
} else {
$sub_code = $result->$responseNode->sub_code;
$data = [];
if(in_array($sub_code, ['ACQ.TRADE_STATUS_ERROR', 'ACQ.REASON_TRADE_STATUS_INVALID', 'ACQ.REASON_ILLEGAL_STATUS'])){
$pay_order_result = $this->get($param[ "out_trade_no" ]);
if(!empty($pay_order_result) && $pay_order_result['code'] >= 0){
if($pay_order_result['data']['trade_status'] == 'TRADE_SUCCESS' || $pay_order_result['data']['trade_status'] == 'TRADE_FINISHED'){
$data['is_paid'] = 1;
}
}
}
return $this->error($data, $result->$responseNode->sub_msg);
}
}
/**
* 支付宝支付原路返回
* @param array $param 支付参数
* @return array
* @throws \think\Exception
*/
public function refund($param)
{
$pay_info = $param[ "pay_info" ];
$refund_no = $param[ "refund_no" ];
$out_trade_no = $pay_info[ "trade_no" ] ?? '';
$refund_fee = $param[ "refund_fee" ];
$parameter = array (
'trade_no' => $out_trade_no,
'refund_amount' => sprintf("%.2f", $refund_fee),
'out_request_no' => $refund_no
);
// 建立请求
$request = new AlipayTradeRefundRequest ();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success();
} else {
return $this->error("", $result->$responseNode->sub_msg);
}
}
/**
* 支付宝转账
* @param $param
* @return array
*/
public function payTransfer($param)
{
try {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
if ($config_result[ 'code' ] < 0) return $config_result;
$config = $config_result[ 'data' ][ 'value' ];
if (empty($config)) return $this->error([], '未配置支付宝支付');
if (!$config[ 'transfer_status' ]) return $this->error([], '未启用支付宝转账');
$parameter = [
'out_biz_no' => $param[ 'out_trade_no' ],
'payee_type' => 'ALIPAY_LOGONID',
'payee_account' => $param[ "account_number" ],
'amount' => sprintf("%.2f", $param[ 'amount' ]),
'payee_real_name' => $param[ "real_name" ],
'remark' => $param[ "desc" ]
];
// 建立请求
$request = new AlipayFundTransToaccountTransferRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success([
'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
'payment_no' => $result->$responseNode->order_id, // 微信付款单号
'payment_time' => date_to_time($result->$responseNode->pay_date) // 付款成功时间
]);
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 异步完成支付
* @param $param
*/
public function payNotify()
{
// Log::write('pay_notifiy_log:alipay:'.json_encode(input()), 'notice');
try {
$res = $this->aop->rsaCheckV1($_POST, $this->aop->alipayrsaPublicKey, $this->aop->signType);
if ($res) { // 验证成功
$out_trade_no = $_POST[ 'out_trade_no' ];
// 支付宝交易号
$trade_no = $_POST[ 'trade_no' ];
// 交易状态
$trade_status = $_POST[ 'trade_status' ];
$pay_common = new PayCommon();
if ($trade_status == "TRADE_SUCCESS") {
$retval = $pay_common->onlinePay($out_trade_no, "alipay", $trade_no, "alipay");
}
echo "success";
} else {
// 验证失败
echo "fail";
}
} catch (\Exception $e) {
echo "fail";
}
}
public function payNewTransfer($param)
{
try {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
if ($config_result[ 'code' ] < 0) return $config_result;
$config = $config_result[ 'data' ][ 'value' ];
if (empty($config)) return $this->error([], '未配置支付宝支付');
if (!$config[ 'transfer_status' ]) return $this->error([], '未启用支付宝转账');
$parameter = [
'out_biz_no' => $param[ 'out_trade_no' ],
'trans_amount' => sprintf("%.2f", $param[ 'amount' ]),
'product_code' => 'TRANS_ACCOUNT_NO_PWD',
'biz_scene' => 'DIRECT_TRANSFER',
'order_title' => '支付宝转账',
'remark' => $param[ "desc" ],
'payee_info' => [
'identity' => $param[ "account_number" ],
'identity_type' => "ALIPAY_LOGON_ID",
'name' => $param[ "real_name" ]
]
];
// 建立请求
$request = new AlipayFundTransUniTransferRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success([
'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
'payment_no' => $result->$responseNode->order_id, // 微信付款单号
'payment_time' => date_to_time($result->$responseNode->trans_date) // 付款成功时间
]);
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 付款码支付
* @param $param
* @return array|mixed|void
*/
public function micropay($param)
{
try {
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
"subject" => str_sub($param[ "pay_body" ], 15),
"total_amount" => (float) $param[ "pay_money" ],
"scene" => "bar_code",
"auth_code" => $param[ 'auth_code' ],
);
$parameter = json_encode($parameter);
$request = new AlipayTradePayRequest();
$request->setBizContent($parameter);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
Log::write('支付宝—付款码支付result' . json_encode($result));
Log::write('支付宝—付款码支付resultCode' . json_encode($resultCode));
if (!empty($resultCode)) {
if ($resultCode == 10000) {
$pay_common = new PayModel();
return $res = $pay_common->onlinePay($param[ 'out_trade_no' ], 'alipay', $result->$responseNode->trade_no, 'alipay');
} else if ($resultCode == 10003) {
// 等待用户付款
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
// todo 查询交易信息【AlipayTradeQueryRequest】 https://opendocs.alipay.com/open/194/106039?pathHash=5b8cf9e6
public function orderQuery($param)
{
try {
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
);
$parameter = json_encode($parameter);
$request = new AlipayTradeQueryRequest();
$request->setBizContent($parameter);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
Log::write('alipay_orderQuery' . json_encode($result));
Log::write('alipay_orderQuery_$resultCode' . json_encode($resultCode));
if (!empty($resultCode) && $resultCode == 10000) {
if ($result->$responseNode->trade_status == 'TRADE_SUCCESS') {
$pay_common = new PayModel();
return $res = $pay_common->onlinePay($param[ 'out_trade_no' ], 'alipay', $result->$responseNode->trade_no, 'alipay');
} else {
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 查询订单信息
* @param $out_trade_no
* @return array
* @throws \think\Exception
*/
public function get($out_trade_no)
{
$parameter = array (
"out_trade_no" => $out_trade_no
);
// 建立请求
$request = new AlipayTradeQueryRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success(json_decode(json_encode($result->$responseNode), true));
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
}
<?php
namespace addon\alipay\model;
use addon\alipay\data\sdk\AopClient;
use addon\alipay\data\sdk\request\AlipayFundTransToaccountTransferRequest;
use addon\alipay\data\sdk\request\AlipayTradeAppPayRequest;
use addon\alipay\data\sdk\request\AlipayTradeCloseRequest;
use addon\alipay\data\sdk\request\AlipayTradeCreateRequest;
use addon\alipay\data\sdk\request\AlipayTradePagePayRequest;
use addon\alipay\data\sdk\request\AlipayTradeRefundRequest;
use addon\alipay\data\sdk\request\AlipayTradeWapPayRequest;
use addon\alipay\data\sdk\request\AlipayTradePrecreateRequest;
use addon\alipay\data\sdk\request\AlipayTradePayRequest;
use addon\alipay\data\sdk\request\AlipayTradeQueryRequest;
use app\model\BaseModel;
use app\model\system\Cron;
use app\model\system\Pay as PayCommon;
use addon\alipay\data\sdk\request\AlipayFundTransUniTransferRequest;
use addon\alipay\data\sdk\AopCertClient;
use app\model\system\Pay as PayModel;
use addon\aliapp\model\Config as AliappConfig;
use think\facade\Log;
/**
* 支付宝支付配置
*/
class Pay extends BaseModel
{
public $aop;
private $is_aliapp = 0;
/**
*
* @param $site_id
* @param int $is_aliapp 是否是小程序
*/
function __construct($site_id, $is_aliapp = 0)
{
$this->is_aliapp = $is_aliapp;
try {
// 获取支付宝支付参数(统一支付到平台账户)
if ($is_aliapp) {
$config_info = ( new AliappConfig() )->getAliappConfig($site_id)[ 'data' ][ 'value' ];
} else {
$config_info = ( new Config() )->getPayConfig($site_id)[ 'data' ][ 'value' ];
}
if (!empty($config_info)) {
$countersign_type = $config_info[ 'countersign_type' ] ?? 0;
if ($countersign_type == 1) {
$appCertPath = $config_info[ "public_key_crt" ] ?? "";
$alipayCertPath = $config_info[ "alipay_public_key_crt" ] ?? "";
$rootCertPath = $config_info[ "alipay_with_crt" ] ?? "";
$this->aop = new AopCertClient();
//调用getPublicKey从支付宝公钥证书中提取公钥
$this->aop->alipayrsaPublicKey = $this->aop->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$this->aop->isCheckAlipayPublicCert = false;
//调用getCertSN获取证书序列号
$this->aop->appCertSN = $this->aop->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$this->aop->alipayRootCertSN = $this->aop->getRootCertSN($rootCertPath);
} else {
// 获取支付宝支付参数(统一支付到平台账户)
$this->aop = new AopClient();
$this->aop->alipayrsaPublicKey = $config_info[ 'public_key' ] ?? "";
$this->aop->alipayPublicKey = $config_info[ 'alipay_public_key' ] ?? "";
}
$this->aop->appId = $config_info[ "app_id" ] ?? "";
$this->aop->rsaPrivateKey = $config_info[ 'private_key' ] ?? "";
$this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$this->aop->apiVersion = '1.0';
$this->aop->signType = 'RSA2';
$this->aop->postCharset = 'UTF-8';
$this->aop->format = 'json';
}
// else{
// return $this->error('', '支付宝支付未配置');
// }
} catch (\Exception $e) {
return $this->error('', '支付宝配置错误');
}
}
/**
* 生成支付
* @param $param
* @return array
*/
public function pay($param)
{
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
"subject" => str_sub($param[ "pay_body" ], 15),
"total_amount" => (float) $param[ "pay_money" ],
"body" => str_sub($param[ "pay_body" ], 60),
"product_code" => 'FAST_INSTANT_TRADE_PAY',
);
switch ( $param[ "app_type" ] ) {
case "h5":
$request = new AlipayTradeWapPayRequest();
break;
case "pc":
$request = new AlipayTradePagePayRequest();
break;
case "app":
$request = new AlipayTradeAppPayRequest();
break;
case 'wechat':
$request = new AlipayTradeWapPayRequest();
break;
case 'cashier':
$request = new AlipayTradePrecreateRequest();
break;
case 'aliapp':
$parameter[ 'product_code' ] = 'FACE_TO_FACE_PAYMENT';
$member_info = model('member')->getInfo([ [ "member_id", "=", $param[ "member_id" ] ] ], 'ali_openid');
if (empty($member_info)) return $this->error(-1, '未获取到会员信息');
$parameter[ 'buyer_id' ] = $member_info[ 'ali_openid' ];
$request = new AlipayTradeCreateRequest();
break;
}
$parameter = json_encode($parameter);
$request->setBizContent($parameter);
$request->SetReturnUrl($param[ "return_url" ]);
$request->SetNotifyUrl($param[ "notify_url" ]);
///绑定商户数据
$pay_model = new PayModel();
$pay_model->bindMchPay($param[ "out_trade_no" ], [ "is_aliapp" => $this->is_aliapp ]);
try {
if ($param[ "app_type" ] == 'h5' || $param[ "app_type" ] == 'wechat' || $param[ "app_type" ] == 'pc') {
$result = $this->aop->pageExecute($request, 'get');
return $this->success([
'type' => 'url',
'data' => $result
]);
} elseif ($param[ "app_type" ] == 'app') {
$result = $this->aop->sdkExecute($request);
if (strpos(get_class($this->aop), 'AopClient') !== false) {
return $this->success([
'type' => 'url',
'data' => $result
]);
}
} else {
$result = $this->aop->execute($request);
}
if ($result === false) return $this->error('', '支付宝发起支付失败');
} catch (\Exception $e) {
return $this->error('', $e->getMessage());
}
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
switch ( $param[ "app_type" ] ) {
case 'cashier':
return $this->success([
'type' => 'qrcode',
'data' => [
'qrcode' => $result->$responseNode->qr_code
]
]);
break;
case 'aliapp':
return $this->success([
'type' => 'data',
'data' => [
'orderInfo' => $result->$responseNode->trade_no
]
]);
break;
default:
return $this->success();
}
} else {
return $this->error("", $result->$responseNode->sub_msg);
}
}
/**
* 支付关闭
* @param $param
* @return array
* @throws \think\Exception
*/
public function close($param)
{
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ]
);
// 建立请求
$request = new AlipayTradeCloseRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success();
} else {
$sub_code = $result->$responseNode->sub_code;
$data = [];
if(in_array($sub_code, ['ACQ.TRADE_STATUS_ERROR', 'ACQ.REASON_TRADE_STATUS_INVALID', 'ACQ.REASON_ILLEGAL_STATUS'])){
$pay_order_result = $this->get($param[ "out_trade_no" ]);
if(!empty($pay_order_result) && $pay_order_result['code'] >= 0){
if($pay_order_result['data']['trade_status'] == 'TRADE_SUCCESS' || $pay_order_result['data']['trade_status'] == 'TRADE_FINISHED'){
$data['is_paid'] = 1;
}
}
}
return $this->error($data, $result->$responseNode->sub_msg);
}
}
/**
* 支付宝支付原路返回
* @param array $param 支付参数
* @return array
* @throws \think\Exception
*/
public function refund($param)
{
$pay_info = $param[ "pay_info" ];
$refund_no = $param[ "refund_no" ];
$out_trade_no = $pay_info[ "trade_no" ] ?? '';
$refund_fee = $param[ "refund_fee" ];
$parameter = array (
'trade_no' => $out_trade_no,
'refund_amount' => sprintf("%.2f", $refund_fee),
'out_request_no' => $refund_no
);
// 建立请求
$request = new AlipayTradeRefundRequest ();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success();
} else {
return $this->error("", $result->$responseNode->sub_msg);
}
}
/**
* 支付宝转账
* @param $param
* @return array
*/
public function payTransfer($param)
{
try {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
if ($config_result[ 'code' ] < 0) return $config_result;
$config = $config_result[ 'data' ][ 'value' ];
if (empty($config)) return $this->error([], '未配置支付宝支付');
if (!$config[ 'transfer_status' ]) return $this->error([], '未启用支付宝转账');
$parameter = [
'out_biz_no' => $param[ 'out_trade_no' ],
'payee_type' => 'ALIPAY_LOGONID',
'payee_account' => $param[ "account_number" ],
'amount' => sprintf("%.2f", $param[ 'amount' ]),
'payee_real_name' => $param[ "real_name" ],
'remark' => $param[ "desc" ]
];
// 建立请求
$request = new AlipayFundTransToaccountTransferRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success([
'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
'payment_no' => $result->$responseNode->order_id, // 微信付款单号
'payment_time' => date_to_time($result->$responseNode->pay_date) // 付款成功时间
]);
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 异步完成支付
* @param $param
*/
public function payNotify()
{
// Log::write('pay_notifiy_log:alipay:'.json_encode(input()), 'notice');
try {
$res = $this->aop->rsaCheckV1($_POST, $this->aop->alipayrsaPublicKey, $this->aop->signType);
if ($res) { // 验证成功
$out_trade_no = $_POST[ 'out_trade_no' ];
// 支付宝交易号
$trade_no = $_POST[ 'trade_no' ];
// 交易状态
$trade_status = $_POST[ 'trade_status' ];
$pay_common = new PayCommon();
if ($trade_status == "TRADE_SUCCESS") {
$retval = $pay_common->onlinePay($out_trade_no, "alipay", $trade_no, "alipay");
}
echo "success";
} else {
// 验证失败
echo "fail";
}
} catch (\Exception $e) {
echo "fail";
}
}
public function payNewTransfer($param)
{
try {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
if ($config_result[ 'code' ] < 0) return $config_result;
$config = $config_result[ 'data' ][ 'value' ];
if (empty($config)) return $this->error([], '未配置支付宝支付');
if (!$config[ 'transfer_status' ]) return $this->error([], '未启用支付宝转账');
$parameter = [
'out_biz_no' => $param[ 'out_trade_no' ],
'trans_amount' => sprintf("%.2f", $param[ 'amount' ]),
'product_code' => 'TRANS_ACCOUNT_NO_PWD',
'biz_scene' => 'DIRECT_TRANSFER',
'order_title' => '支付宝转账',
'remark' => $param[ "desc" ],
'payee_info' => [
'identity' => $param[ "account_number" ],
'identity_type' => "ALIPAY_LOGON_ID",
'name' => $param[ "real_name" ]
]
];
// 建立请求
$request = new AlipayFundTransUniTransferRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success([
'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
'payment_no' => $result->$responseNode->order_id, // 微信付款单号
'payment_time' => date_to_time($result->$responseNode->trans_date) // 付款成功时间
]);
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 付款码支付
* @param $param
* @return array|mixed|void
*/
public function micropay($param)
{
try {
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
"subject" => str_sub($param[ "pay_body" ], 15),
"total_amount" => (float) $param[ "pay_money" ],
"scene" => "bar_code",
"auth_code" => $param[ 'auth_code' ],
);
$parameter = json_encode($parameter);
$request = new AlipayTradePayRequest();
$request->setBizContent($parameter);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
Log::write('支付宝—付款码支付result' . json_encode($result));
Log::write('支付宝—付款码支付resultCode' . json_encode($resultCode));
if (!empty($resultCode)) {
if ($resultCode == 10000) {
$pay_common = new PayModel();
return $res = $pay_common->onlinePay($param[ 'out_trade_no' ], 'alipay', $result->$responseNode->trade_no, 'alipay');
} else if ($resultCode == 10003) {
// 等待用户付款
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
// todo 查询交易信息【AlipayTradeQueryRequest】 https://opendocs.alipay.com/open/194/106039?pathHash=5b8cf9e6
public function orderQuery($param)
{
try {
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
);
$parameter = json_encode($parameter);
$request = new AlipayTradeQueryRequest();
$request->setBizContent($parameter);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
Log::write('alipay_orderQuery' . json_encode($result));
Log::write('alipay_orderQuery_$resultCode' . json_encode($resultCode));
if (!empty($resultCode) && $resultCode == 10000) {
if ($result->$responseNode->trade_status == 'TRADE_SUCCESS') {
$pay_common = new PayModel();
return $res = $pay_common->onlinePay($param[ 'out_trade_no' ], 'alipay', $result->$responseNode->trade_no, 'alipay');
} else {
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 查询订单信息
* @param $out_trade_no
* @return array
* @throws \think\Exception
*/
public function get($out_trade_no)
{
$parameter = array (
"out_trade_no" => $out_trade_no
);
// 建立请求
$request = new AlipayTradeQueryRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success(json_decode(json_encode($result->$responseNode), true));
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
}
}

View File

@@ -1,94 +1,86 @@
<?php
/**
*/
namespace addon\alipay\shop\controller;
use addon\alipay\model\Config as ConfigModel;
use app\shop\controller\BaseShop;
use think\facade\Config;
use app\model\upload\Upload;
/**
* 支付宝 控制器
*/
class Pay extends BaseShop
{
public function config()
{
$config_model = new ConfigModel();
if (request()->isJson()) {
$app_id = input("app_id", "");//支付宝应用ID (支付宝分配给开发者的应用ID)
$private_key = input("private_key", "");//应用私钥
$public_key = input("public_key", "");//应用公钥
$alipay_public_key = input("alipay_public_key", "");//支付宝公钥
$app_type = input("app_type", "");//支持端口 如web app
$pay_status = input("pay_status", 0);//支付启用状态
$refund_status = input("refund_status", 0);//退款启用状态
$transfer_status = input("transfer_status", 0);//转账启用状态
$public_key_crt = input("public_key_crt", "");
$alipay_public_key_crt = input("alipay_public_key_crt", "");
$alipay_with_crt = input("alipay_with_crt", "");
$countersign_type = input("countersign_type", 0);//加签模式
$data = array (
"app_id" => $app_id,
"private_key" => $private_key,
"public_key" => $public_key,
"alipay_public_key" => $alipay_public_key,
"refund_status" => $refund_status,
"pay_status" => $pay_status,
"transfer_status" => $transfer_status,
"app_type" => $app_type,
"public_key_crt" => $public_key_crt,
"alipay_public_key_crt" => $alipay_public_key_crt,
"alipay_with_crt" => $alipay_with_crt,
"countersign_type" => $countersign_type
);
$result = $config_model->setPayConfig($data, $this->site_id, $this->app_module);
return $result;
} else {
$info = $config_model->getPayConfig($this->site_id, $this->app_module, true)[ 'data' ][ 'value' ];
if (!empty($info)) {
$app_type_arr = [];
if (!empty($info[ 'app_type' ])) {
$app_type_arr = explode(',', $info[ 'app_type' ]);
}
$info[ 'app_type_arr' ] = $app_type_arr;
if (empty($info[ 'countersign_type' ])) {
$info[ 'countersign_type' ] = 0;
}
}
$this->assign("info", $info);
$this->assign("app_type", Config::get("app_type"));
return $this->fetch("pay/config");
}
}
/**
* 上传微信支付证书
*/
public function uploadAlipayCrt()
{
$upload_model = new Upload();
$site_id = request()->siteid();
$name = input("name", "");
$extend_type = [ 'crt' ];
$param = array (
"name" => "file",
"extend_type" => $extend_type
);
$site_id = max($site_id, 0);
$result = $upload_model->setPath("common/alipay/crt/" . $site_id . "/")->file($param);
return $result;
}
<?php
namespace addon\alipay\shop\controller;
use addon\alipay\model\Config as ConfigModel;
use app\shop\controller\BaseShop;
use think\facade\Config;
use app\model\upload\Upload;
/**
* 支付宝 控制器
*/
class Pay extends BaseShop
{
public function config()
{
$config_model = new ConfigModel();
if (request()->isJson()) {
$app_id = input("app_id", "");//支付宝应用ID (支付宝分配给开发者的应用ID)
$private_key = input("private_key", "");//应用私钥
$public_key = input("public_key", "");//应用公钥
$alipay_public_key = input("alipay_public_key", "");//支付宝公钥
$app_type = input("app_type", "");//支持端口 如web app
$pay_status = input("pay_status", 0);//支付启用状态
$refund_status = input("refund_status", 0);//退款启用状态
$transfer_status = input("transfer_status", 0);//转账启用状态
$public_key_crt = input("public_key_crt", "");
$alipay_public_key_crt = input("alipay_public_key_crt", "");
$alipay_with_crt = input("alipay_with_crt", "");
$countersign_type = input("countersign_type", 0);//加签模式
$data = array (
"app_id" => $app_id,
"private_key" => $private_key,
"public_key" => $public_key,
"alipay_public_key" => $alipay_public_key,
"refund_status" => $refund_status,
"pay_status" => $pay_status,
"transfer_status" => $transfer_status,
"app_type" => $app_type,
"public_key_crt" => $public_key_crt,
"alipay_public_key_crt" => $alipay_public_key_crt,
"alipay_with_crt" => $alipay_with_crt,
"countersign_type" => $countersign_type
);
$result = $config_model->setPayConfig($data, $this->site_id, $this->app_module);
return $result;
} else {
$info = $config_model->getPayConfig($this->site_id, $this->app_module, true)[ 'data' ][ 'value' ];
if (!empty($info)) {
$app_type_arr = [];
if (!empty($info[ 'app_type' ])) {
$app_type_arr = explode(',', $info[ 'app_type' ]);
}
$info[ 'app_type_arr' ] = $app_type_arr;
if (empty($info[ 'countersign_type' ])) {
$info[ 'countersign_type' ] = 0;
}
}
$this->assign("info", $info);
$this->assign("app_type", Config::get("app_type"));
return $this->fetch("pay/config");
}
}
/**
* 上传微信支付证书
*/
public function uploadAlipayCrt()
{
$upload_model = new Upload();
$site_id = request()->siteid();
$name = input("name", "");
$extend_type = [ 'crt' ];
$param = array (
"name" => "file",
"extend_type" => $extend_type
);
$site_id = max($site_id, 0);
$result = $upload_model->setPath("common/alipay/crt/" . $site_id . "/")->file($param);
return $result;
}
}

View File

@@ -1,48 +1,40 @@
<?php
/**
*/
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [],
// 自定义页面路径
'link' => [
[
'name' => 'CASES_INFO',
'title' => '案例展示',
'parent' => 'BASICS_LINK',
'wap_url' => '/pages_tool/cases/index',
'web_url' => '',
'sort' => 0
]
],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
<?php
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [],
// 自定义页面路径
'link' => [
[
'name' => 'CASES_INFO',
'title' => '案例展示',
'parent' => 'BASICS_LINK',
'wap_url' => '/pages_tool/cases/index',
'web_url' => '',
'sort' => 0
]
],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
];

View File

@@ -1,21 +1,12 @@
<?php
/**
*/
return [
'name' => 'cases',
'title' => '案例展示',
'description' => '展示案例信息',
'type' => 'tool', //插件类型 system :系统插件(自动安装), promotion:扩展营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '1.0.0',
'version_no' => '2025051923121',
'content' => '',
<?php
return [
'name' => 'cases',
'title' => '案例展示',
'description' => '展示案例信息',
'type' => 'tool', //插件类型 system :系统插件(自动安装), promotion:扩展营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '1.0.0',
'version_no' => '2025051923121',
'content' => '',
];

View File

@@ -1,25 +1,17 @@
<?php
/**
*/
namespace addon\cases\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
return success();
}
<?php
namespace addon\cases\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
return success();
}
}

View File

@@ -1,30 +1,21 @@
<?php
/**
*/
namespace addon\cases\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
try {
return error('', "系统插件不允许删除");
} catch (\Exception $e) {
return error('', $e->getMessage());
}
}
<?php
namespace addon\cases\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
try {
return error('', "系统插件不允许删除");
} catch (\Exception $e) {
return error('', $e->getMessage());
}
}
}

View File

@@ -253,7 +253,7 @@ class Enterprise extends BaseModel
*/
public function deleteVideo($condition)
{
file_put_contents(__DIR__ . '/debug.txt', var_export($condition,true));
// file_put_contents(__DIR__ . '/debug.txt', var_export($condition,true));
$check_condition = array_column($condition, 2, 0);
$site_id = $check_condition['site_id'] ?? '';
if ($site_id === '') {

View File

@@ -1,351 +1,343 @@
<?php
/**
*/
namespace addon\coupon\api\controller;
use app\api\controller\BaseApi;
use addon\coupon\model\Coupon as CouponModel;
use addon\coupon\model\CouponType as CouponTypeModel;
use addon\coupon\model\MemberCoupon;
use think\facade\Db;
/**
* 优惠券
*/
class Coupon extends BaseApi
{
/**
* 优惠券类型信息
*/
public function typeinfo()
{
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
if (empty($coupon_type_id)) {
return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
}
$app_type = $this->params['app_type'] ?? 'h5';
$coupon_model = new CouponModel();
$condition = [
[ 'coupon_type_id', '=', $coupon_type_id ],
[ 'is_show', '=', 1 ],
[ 'site_id', '=', $this->site_id ]
];
$coupon_type_model = new CouponTypeModel();
$qrcode = $coupon_type_model->qrcode($coupon_type_id, $app_type, $this->site_id)[ 'data' ];
$info = $coupon_model->getCouponTypeInfo($condition);
if (!empty($info[ 'data' ]) && !empty($qrcode)) {
$info[ 'data' ][ 'qrcode' ] = $qrcode[ 'path' ];
}
return $this->response($info);
}
/**
* 列表信息
*/
public function memberpage()
{
$token = $this->checkToken();
if ($token[ 'code' ] < 0) return $this->response($token);
$page = $this->params['page'] ?? 1;
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
$state = $this->params['state'] ?? 1;//优惠券状态 1已领用未使用 2已使用 3已过期
$coupon_model = new CouponModel();
$condition = [
[ 'npc.member_id', '=', $token[ 'data' ][ 'member_id' ] ],
[ 'npc.state', '=', $state ]
];
//按类型筛选
$type = $this->params['type'] ?? '';
$related_id = $this->params['related_id'] ?? 0;
switch ( $type ) {
case 'reward'://满减
$condition[] = ['npc.type', '=', 'reward'];
break;
case 'discount'://折扣
$condition[] = ['npc.type', '=', 'discount'];
break;
case 'no_threshold'://无门槛
$condition[] = ['npc.at_least', '=', 0 ];
break;
}
if (!empty($related_id)) {
$condition[] = [ 'related_id', '=', $related_id ];
}
$list = $coupon_model->getMemberCouponPageList($condition, $page, $page_size);
return $this->response($list);
}
/**
* 优惠券类型列表
*/
public function typelists()
{
$num = $this->params['num'] ?? 0;
$coupon_type_id_arr = $this->params['coupon_type_id_arr'] ?? '';//coupon_type_id数组
$can_receive = $this->params['can_receive'] ?? 0;// 是否只查询可领取的
$token = $this->checkToken();
$coupon_model = new CouponModel();
$condition = [
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'site_id', '=', $this->site_id ]
];
//按类型查询
$type = $this->params['type'] ?? '';
switch ( $type ) {
case 'reward'://满减
$condition[] = ['type', '=', 'reward'];
break;
case 'discount'://折扣
$condition[] = ['type', '=', 'discount'];
break;
case 'no_threshold'://无门槛
$condition[] = ['at_least', '=', 0 ];
break;
}
if (!empty($coupon_type_id_arr)) {
$condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
}
$field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
if ($can_receive == 1) {
$condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
}
$order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
$list = $coupon_model->getCouponTypeList($condition, $field, $order, $num);
if (!empty($list[ 'data' ]) && $this->member_id) {
foreach ($list[ 'data' ] as $k => $v) {
$list[ 'data' ][ $k ][ 'member_coupon_num' ] = $coupon_model->getCouponCount([
[ 'get_type', '=', 2 ],
[ 'member_id', '=', $this->member_id ],
[ 'coupon_type_id', '=', $v[ 'coupon_type_id' ] ]
])[ 'data' ];
}
}
return $this->response($list);
}
/**
* 优惠券类型分页列表
*/
public function typepagelists()
{
$page = $this->params['page'] ?? 1;
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
$coupon_type_id_arr = $this->params['coupon_type_id_arr'] ?? '';//coupon_type_id数组
$can_receive = $this->params['can_receive'] ?? 0;// 是否只查询可领取的
$token = $this->checkToken();
$coupon_model = new CouponModel();
$condition = [
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'site_id', '=', $this->site_id ]
];
//按类型查询
$type = $this->params['type'] ?? '';
switch ( $type ) {
case 'reward'://满减
$condition[] = ['type', '=', 'reward'];
break;
case 'discount'://折扣
$condition[] = ['type', '=', 'discount'];
break;
case 'no_threshold'://无门槛
$condition[] = ['at_least', '=', 0 ];
break;
}
if (!empty($coupon_type_id_arr)) {
$condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
}
$field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
if ($can_receive == 1) {
$condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
}
if ($this->member_id) {
$prefix = config('database.connections.mysql.prefix');
$field .= ', (select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = ct.coupon_type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
}
$order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
$list = $coupon_model->getCouponTypePageList($condition, $page, $page_size, $order, $field, 'ct');
return $this->response($list);
}
/**
* 获取优惠券
*/
public function receive()
{
$token = $this->checkToken();
if ($token[ 'code' ] < 0) return $this->response($token);
$site_id = $this->site_id;
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
$get_type = $this->params['get_type'] ?? 2;//获取方式:1订单2.直接领取3.活动领取
if (empty($coupon_type_id)) {
return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
}
$coupon_model = new CouponModel();
$res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $token[ 'data' ][ 'member_id' ], $get_type);
$res[ 'data' ] = [];
//判断一下用户是否拥有当前优惠券
$coupon = $coupon_model->getCouponInfo([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ], [ 'member_id', '=', $token[ 'data' ][ 'member_id' ] ] ], 'coupon_id')[ 'data' ];
$res[ 'data' ][ 'is_exist' ] = empty($coupon) ? 0 : 1;
return $this->response($res);
}
/**
* 会员优惠券数量
* @return string
*/
public function num()
{
$token = $this->checkToken();
if ($token[ 'code' ] < 0) return $this->response($token);
$state = $this->params[ 'state' ] ?? 1;
$coupon_model = new MemberCoupon();
$count = $coupon_model->getMemberCouponNum($token[ 'data' ][ 'member_id' ], $state);
return $this->response($count);
}
/**
* 是否可以领取
*/
public function receivedNum()
{
$token = $this->checkToken();
if ($token[ 'code' ] < 0) return $this->response($token);
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
$coupon_model = new MemberCoupon();
$res = $coupon_model->receivedNum($coupon_type_id, $this->member_id);
return $this->response($res);
}
/**
* 查询商品可用的优惠券
* @param int $id
* @return false|string
*/
public function goodsCoupon($id = 0)
{
$this->checkToken();
$coupon_model = new CouponModel();
$goods_id = $this->params[ 'goods_id' ] ?? 0;
if (!empty($id)) {
$goods_id = $id;
}
// 查询全部商品参与
$condition = [
[ 'site_id', '=', $this->site_id ],
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'goods_type', '=', 1 ]
];
$field = 'count,lead_count,coupon_type_id,coupon_type_id as type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit';
if ($this->member_id) {
$prefix = config('database.connections.mysql.prefix');
$field .= ',(select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
}
$list = $coupon_model->getCouponTypeList($condition, $field, 'money desc', null, 'ct');
// 查询指定商品参与
$goods_condition = [
[ 'site_id', '=', $this->site_id ],
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'goods_type', '=', 2 ],
[ 'goods_ids', 'like', "%,$goods_id,%" ]
];
$goods_coupon = $coupon_model->getCouponTypeList($goods_condition, $field, 'money desc', null, 'ct');
if (!empty($goods_coupon[ 'data' ])) {
$list[ 'data' ] = array_merge($list[ 'data' ], $goods_coupon[ 'data' ]);
}
// 查询指定商品不参与
$not_goods_condition = [
[ 'site_id', '=', $this->site_id ],
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'goods_type', '=', 3 ],
[ 'goods_ids', 'not like', "%,$goods_id,%" ]
];
$not_goods_coupon = $coupon_model->getCouponTypeList($not_goods_condition, $field, 'money desc', null, 'ct');
if (!empty($not_goods_coupon[ 'data' ])) {
$list[ 'data' ] = array_merge($list[ 'data' ], $not_goods_coupon[ 'data' ]);
}
if ($list[ 'data' ] && $this->member_id) {
foreach ($list[ 'data' ] as $k => $v) {
// 已抢光
if ($v[ 'count' ] == $v[ 'lead_count' ]) {
unset($list[ 'data' ][ $k ]);
} elseif ($v[ 'max_fetch' ] != 0 && $v[ 'member_coupon_num' ] > 0 && $v[ 'member_coupon_num' ] >= $v[ 'max_fetch' ]) {
// 已领取
unset($list[ 'data' ][ $k ]);
}
}
$list[ 'data' ] = array_values($list[ 'data' ]);
}
return $this->response($list);
}
/**
* 查询优惠券通过优惠券类型id
*/
public function couponById()
{
$id = $this->params[ 'id' ] ?? 0;
$coupon_model = new CouponModel();
$condition = [
[ 'site_id', '=', $this->site_id ],
[ 'status', '=', 1 ],
[ 'coupon_type_id', 'in', $id ]
];
$list = $coupon_model->getCouponTypeList($condition, 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit', 'money desc', '');
return $this->response($list);
}
<?php
namespace addon\coupon\api\controller;
use app\api\controller\BaseApi;
use addon\coupon\model\Coupon as CouponModel;
use addon\coupon\model\CouponType as CouponTypeModel;
use addon\coupon\model\MemberCoupon;
use think\facade\Db;
/**
* 优惠券
*/
class Coupon extends BaseApi
{
/**
* 优惠券类型信息
*/
public function typeinfo()
{
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
if (empty($coupon_type_id)) {
return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
}
$app_type = $this->params['app_type'] ?? 'h5';
$coupon_model = new CouponModel();
$condition = [
[ 'coupon_type_id', '=', $coupon_type_id ],
[ 'is_show', '=', 1 ],
[ 'site_id', '=', $this->site_id ]
];
$coupon_type_model = new CouponTypeModel();
$qrcode = $coupon_type_model->qrcode($coupon_type_id, $app_type, $this->site_id)[ 'data' ];
$info = $coupon_model->getCouponTypeInfo($condition);
if (!empty($info[ 'data' ]) && !empty($qrcode)) {
$info[ 'data' ][ 'qrcode' ] = $qrcode[ 'path' ];
}
return $this->response($info);
}
/**
* 列表信息
*/
public function memberpage()
{
$token = $this->checkToken();
if ($token[ 'code' ] < 0) return $this->response($token);
$page = $this->params['page'] ?? 1;
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
$state = $this->params['state'] ?? 1;//优惠券状态 1已领用未使用 2已使用 3已过期
$coupon_model = new CouponModel();
$condition = [
[ 'npc.member_id', '=', $token[ 'data' ][ 'member_id' ] ],
[ 'npc.state', '=', $state ]
];
//按类型筛选
$type = $this->params['type'] ?? '';
$related_id = $this->params['related_id'] ?? 0;
switch ( $type ) {
case 'reward'://满减
$condition[] = ['npc.type', '=', 'reward'];
break;
case 'discount'://折扣
$condition[] = ['npc.type', '=', 'discount'];
break;
case 'no_threshold'://无门槛
$condition[] = ['npc.at_least', '=', 0 ];
break;
}
if (!empty($related_id)) {
$condition[] = [ 'related_id', '=', $related_id ];
}
$list = $coupon_model->getMemberCouponPageList($condition, $page, $page_size);
return $this->response($list);
}
/**
* 优惠券类型列表
*/
public function typelists()
{
$num = $this->params['num'] ?? 0;
$coupon_type_id_arr = $this->params['coupon_type_id_arr'] ?? '';//coupon_type_id数组
$can_receive = $this->params['can_receive'] ?? 0;// 是否只查询可领取的
$token = $this->checkToken();
$coupon_model = new CouponModel();
$condition = [
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'site_id', '=', $this->site_id ]
];
//按类型查询
$type = $this->params['type'] ?? '';
switch ( $type ) {
case 'reward'://满减
$condition[] = ['type', '=', 'reward'];
break;
case 'discount'://折扣
$condition[] = ['type', '=', 'discount'];
break;
case 'no_threshold'://无门槛
$condition[] = ['at_least', '=', 0 ];
break;
}
if (!empty($coupon_type_id_arr)) {
$condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
}
$field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
if ($can_receive == 1) {
$condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
}
$order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
$list = $coupon_model->getCouponTypeList($condition, $field, $order, $num);
if (!empty($list[ 'data' ]) && $this->member_id) {
foreach ($list[ 'data' ] as $k => $v) {
$list[ 'data' ][ $k ][ 'member_coupon_num' ] = $coupon_model->getCouponCount([
[ 'get_type', '=', 2 ],
[ 'member_id', '=', $this->member_id ],
[ 'coupon_type_id', '=', $v[ 'coupon_type_id' ] ]
])[ 'data' ];
}
}
return $this->response($list);
}
/**
* 优惠券类型分页列表
*/
public function typepagelists()
{
$page = $this->params['page'] ?? 1;
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
$coupon_type_id_arr = $this->params['coupon_type_id_arr'] ?? '';//coupon_type_id数组
$can_receive = $this->params['can_receive'] ?? 0;// 是否只查询可领取的
$token = $this->checkToken();
$coupon_model = new CouponModel();
$condition = [
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'site_id', '=', $this->site_id ]
];
//按类型查询
$type = $this->params['type'] ?? '';
switch ( $type ) {
case 'reward'://满减
$condition[] = ['type', '=', 'reward'];
break;
case 'discount'://折扣
$condition[] = ['type', '=', 'discount'];
break;
case 'no_threshold'://无门槛
$condition[] = ['at_least', '=', 0 ];
break;
}
if (!empty($coupon_type_id_arr)) {
$condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
}
$field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
if ($can_receive == 1) {
$condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
}
if ($this->member_id) {
$prefix = config('database.connections.mysql.prefix');
$field .= ', (select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = ct.coupon_type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
}
$order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
$list = $coupon_model->getCouponTypePageList($condition, $page, $page_size, $order, $field, 'ct');
return $this->response($list);
}
/**
* 获取优惠券
*/
public function receive()
{
$token = $this->checkToken();
if ($token[ 'code' ] < 0) return $this->response($token);
$site_id = $this->site_id;
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
$get_type = $this->params['get_type'] ?? 2;//获取方式:1订单2.直接领取3.活动领取
if (empty($coupon_type_id)) {
return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
}
$coupon_model = new CouponModel();
$res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $token[ 'data' ][ 'member_id' ], $get_type);
$res[ 'data' ] = [];
//判断一下用户是否拥有当前优惠券
$coupon = $coupon_model->getCouponInfo([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ], [ 'member_id', '=', $token[ 'data' ][ 'member_id' ] ] ], 'coupon_id')[ 'data' ];
$res[ 'data' ][ 'is_exist' ] = empty($coupon) ? 0 : 1;
return $this->response($res);
}
/**
* 会员优惠券数量
* @return string
*/
public function num()
{
$token = $this->checkToken();
if ($token[ 'code' ] < 0) return $this->response($token);
$state = $this->params[ 'state' ] ?? 1;
$coupon_model = new MemberCoupon();
$count = $coupon_model->getMemberCouponNum($token[ 'data' ][ 'member_id' ], $state);
return $this->response($count);
}
/**
* 是否可以领取
*/
public function receivedNum()
{
$token = $this->checkToken();
if ($token[ 'code' ] < 0) return $this->response($token);
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
$coupon_model = new MemberCoupon();
$res = $coupon_model->receivedNum($coupon_type_id, $this->member_id);
return $this->response($res);
}
/**
* 查询商品可用的优惠券
* @param int $id
* @return false|string
*/
public function goodsCoupon($id = 0)
{
$this->checkToken();
$coupon_model = new CouponModel();
$goods_id = $this->params[ 'goods_id' ] ?? 0;
if (!empty($id)) {
$goods_id = $id;
}
// 查询全部商品参与
$condition = [
[ 'site_id', '=', $this->site_id ],
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'goods_type', '=', 1 ]
];
$field = 'count,lead_count,coupon_type_id,coupon_type_id as type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit';
if ($this->member_id) {
$prefix = config('database.connections.mysql.prefix');
$field .= ',(select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
}
$list = $coupon_model->getCouponTypeList($condition, $field, 'money desc', null, 'ct');
// 查询指定商品参与
$goods_condition = [
[ 'site_id', '=', $this->site_id ],
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'goods_type', '=', 2 ],
[ 'goods_ids', 'like', "%,$goods_id,%" ]
];
$goods_coupon = $coupon_model->getCouponTypeList($goods_condition, $field, 'money desc', null, 'ct');
if (!empty($goods_coupon[ 'data' ])) {
$list[ 'data' ] = array_merge($list[ 'data' ], $goods_coupon[ 'data' ]);
}
// 查询指定商品不参与
$not_goods_condition = [
[ 'site_id', '=', $this->site_id ],
[ 'status', '=', 1 ],
[ 'is_show', '=', 1 ],
[ 'goods_type', '=', 3 ],
[ 'goods_ids', 'not like', "%,$goods_id,%" ]
];
$not_goods_coupon = $coupon_model->getCouponTypeList($not_goods_condition, $field, 'money desc', null, 'ct');
if (!empty($not_goods_coupon[ 'data' ])) {
$list[ 'data' ] = array_merge($list[ 'data' ], $not_goods_coupon[ 'data' ]);
}
if ($list[ 'data' ] && $this->member_id) {
foreach ($list[ 'data' ] as $k => $v) {
// 已抢光
if ($v[ 'count' ] == $v[ 'lead_count' ]) {
unset($list[ 'data' ][ $k ]);
} elseif ($v[ 'max_fetch' ] != 0 && $v[ 'member_coupon_num' ] > 0 && $v[ 'member_coupon_num' ] >= $v[ 'max_fetch' ]) {
// 已领取
unset($list[ 'data' ][ $k ]);
}
}
$list[ 'data' ] = array_values($list[ 'data' ]);
}
return $this->response($list);
}
/**
* 查询优惠券通过优惠券类型id
*/
public function couponById()
{
$id = $this->params[ 'id' ] ?? 0;
$coupon_model = new CouponModel();
$condition = [
[ 'site_id', '=', $this->site_id ],
[ 'status', '=', 1 ],
[ 'coupon_type_id', 'in', $id ]
];
$list = $coupon_model->getCouponTypeList($condition, 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit', 'money desc', '');
return $this->response($list);
}
}

View File

@@ -1,67 +1,59 @@
<?php
/**
*/
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [
[
'name' => 'Coupon',
'title' => '优惠券',
'type' => 'PROMOTION',
'value' => '{"style":1,"sources":"initial","styleName":"风格一","couponIds":[],"count":6,"previewList":[],"nameColor":"","moneyColor":"#FFFFFF","limitColor":"#FFFFFF","btnStyle":{"maxLen": 4,"textColor":"#FFFFFF","bgColor":"","text":"立即领取","aroundRadius":0,"isBgColor":false,"isAroundRadius":false},"isName":false,"couponBgColor":"","couponBgUrl":"","couponType":"img","ifNeedBg":true}',
'sort' => '30000',
'support_diy_view' => '',
'max_count' => 0,
'icon' => 'iconfont iconyouhuiquan',
],
],
// 自定义页面路径
'link' => [
[
'name' => 'COUPON_LIST',
'title' => '优惠券',
'parent' => 'MARKETING_LINK',
'wap_url' => '',
'web_url' => '',
'sort' => 0,
'child_list' => [
[
'name' => 'COUPON_PREFECTURE',
'title' => '优惠券专区',
'wap_url' => '/pages_tool/goods/coupon',
'web_url' => '',
'sort' => 0
]
]
],
],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
<?php
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [
[
'name' => 'Coupon',
'title' => '优惠券',
'type' => 'PROMOTION',
'value' => '{"style":1,"sources":"initial","styleName":"风格一","couponIds":[],"count":6,"previewList":[],"nameColor":"","moneyColor":"#FFFFFF","limitColor":"#FFFFFF","btnStyle":{"maxLen": 4,"textColor":"#FFFFFF","bgColor":"","text":"立即领取","aroundRadius":0,"isBgColor":false,"isAroundRadius":false},"isName":false,"couponBgColor":"","couponBgUrl":"","couponType":"img","ifNeedBg":true}',
'sort' => '30000',
'support_diy_view' => '',
'max_count' => 0,
'icon' => 'iconfont iconyouhuiquan',
],
],
// 自定义页面路径
'link' => [
[
'name' => 'COUPON_LIST',
'title' => '优惠券',
'parent' => 'MARKETING_LINK',
'wap_url' => '',
'web_url' => '',
'sort' => 0,
'child_list' => [
[
'name' => 'COUPON_PREFECTURE',
'title' => '优惠券专区',
'wap_url' => '/pages_tool/goods/coupon',
'web_url' => '',
'sort' => 0
]
]
],
],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
];

View File

@@ -1,21 +1,12 @@
<?php
/**
*/
return [
'name' => 'coupon',
'title' => '优惠券',
'description' => '会员优惠券功能',
'type' => 'system', //插件类型 system :系统插件(自动安装), promotion:扩展营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '5.3.1',
'version_no' => '525231212001',
'content' => '',
<?php
return [
'name' => 'coupon',
'title' => '优惠券',
'description' => '会员优惠券功能',
'type' => 'system', //插件类型 system :系统插件(自动安装), promotion:扩展营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '5.3.1',
'version_no' => '525231212001',
'content' => '',
];

View File

@@ -1,55 +1,46 @@
<?php
/**
*/
namespace addon\coupon\dict;
/**
* 订单公共属性
*/
class CouponDict
{
const normal = 1;
const used = 2;
const expire = 3;
const close = 4;
/**
* 优惠券状态
* @param $status
* @return string|string[]
*/
public static function getStatus($status = ''){
$list = [
self::normal => '待使用',
self::used => '已使用',
self::expire => '已过期',
self::close => '已关闭',
];
if($status) return $list[$status] ?? '';
return $list;
}
const all = 1;
const selected = 2;
const selected_out = 3;
public static function getGoodsType($type = ''){
$list = [
self::all => '全部商品参与',
self::selected => '指定商品参与',
self::selected_out => '指定不参与商品'
];
if($type) return $list[$type] ?? '';
return $list;
}
}
<?php
namespace addon\coupon\dict;
/**
* 订单公共属性
*/
class CouponDict
{
const normal = 1;
const used = 2;
const expire = 3;
const close = 4;
/**
* 优惠券状态
* @param $status
* @return string|string[]
*/
public static function getStatus($status = ''){
$list = [
self::normal => '待使用',
self::used => '已使用',
self::expire => '已过期',
self::close => '已关闭',
];
if($status) return $list[$status] ?? '';
return $list;
}
const all = 1;
const selected = 2;
const selected_out = 3;
public static function getGoodsType($type = ''){
$list = [
self::all => '全部商品参与',
self::selected => '指定商品参与',
self::selected_out => '指定不参与商品'
];
if($type) return $list[$type] ?? '';
return $list;
}
}

View File

@@ -1,27 +1,19 @@
<?php
/**
*/
namespace addon\coupon\event;
use addon\coupon\model\Coupon;
/**
* 启动活动
*/
class CronCouponEnd
{
public function handle($params = [])
{
$coupon = new Coupon();
$res = $coupon->cronCouponEnd();
return $res;
}
<?php
namespace addon\coupon\event;
use addon\coupon\model\Coupon;
/**
* 启动活动
*/
class CronCouponEnd
{
public function handle($params = [])
{
$coupon = new Coupon();
$res = $coupon->cronCouponEnd();
return $res;
}
}

View File

@@ -1,27 +1,19 @@
<?php
/**
*/
namespace addon\coupon\event;
use addon\coupon\model\CouponType;
/**
* 优惠券定时结束
*/
class CronCouponTypeEnd
{
public function handle($params = [])
{
$coupon = new CouponType();
$res = $coupon->couponCronEnd($params[ 'relate_id' ]);
return $res;
}
<?php
namespace addon\coupon\event;
use addon\coupon\model\CouponType;
/**
* 优惠券定时结束
*/
class CronCouponTypeEnd
{
public function handle($params = [])
{
$coupon = new CouponType();
$res = $coupon->couponCronEnd($params[ 'relate_id' ]);
return $res;
}
}

View File

@@ -1,38 +1,29 @@
<?php
/**
*/
namespace addon\coupon\event;
use app\model\system\Cron;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
try {
execute_sql('addon/coupon/data/install.sql');
$cron = new Cron();
$cron->deleteCron([ ['event', '=', 'CronCouponEnd'] ]);
$cron->addCron(2, 1, '优惠券过期自动关闭', 'CronCouponEnd', time(), 0);
return success();
} catch (\Exception $e) {
return error('', $e->getMessage());
}
}
<?php
namespace addon\coupon\event;
use app\model\system\Cron;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
try {
execute_sql('addon/coupon/data/install.sql');
$cron = new Cron();
$cron->deleteCron([ ['event', '=', 'CronCouponEnd'] ]);
$cron->addCron(2, 1, '优惠券过期自动关闭', 'CronCouponEnd', time(), 0);
return success();
} catch (\Exception $e) {
return error('', $e->getMessage());
}
}
}

View File

@@ -1,46 +1,38 @@
<?php
/**
*/
namespace addon\coupon\event;
/**
* 店铺活动
*/
class ShowPromotion
{
/**
* 活动展示
* @return array
*/
public function handle()
{
$data = [
'shop' => [
[
//插件名称
'name' => 'coupon',
//展示分类根据平台端设置admin平台营销shop店铺营销member:会员营销, tool:应用工具)
'show_type' => 'shop',
//展示主题
'title' => '优惠券',
//展示介绍
'description' => '设置商家优惠券',
//展示图标
'icon' => 'addon/coupon/icon.png',
//跳转链接
'url' => 'coupon://shop/coupon/lists',
]
]
];
return $data;
}
<?php
namespace addon\coupon\event;
/**
* 店铺活动
*/
class ShowPromotion
{
/**
* 活动展示
* @return array
*/
public function handle()
{
$data = [
'shop' => [
[
//插件名称
'name' => 'coupon',
//展示分类根据平台端设置admin平台营销shop店铺营销member:会员营销, tool:应用工具)
'show_type' => 'shop',
//展示主题
'title' => '优惠券',
//展示介绍
'description' => '设置商家优惠券',
//展示图标
'icon' => 'addon/coupon/icon.png',
//跳转链接
'url' => 'coupon://shop/coupon/lists',
]
]
];
return $data;
}
}

View File

@@ -1,32 +1,23 @@
<?php
/**
*/
namespace addon\coupon\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
try {
return error('', "系统插件不允许删除");
//execute_sql('addon/coupon/data/uninstall.sql');
//return success();
} catch (\Exception $e) {
return error('', $e->getMessage());
}
}
<?php
namespace addon\coupon\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
try {
return error('', "系统插件不允许删除");
//execute_sql('addon/coupon/data/uninstall.sql');
//return success();
} catch (\Exception $e) {
return error('', $e->getMessage());
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,64 +1,56 @@
<?php
/**
*/
namespace addon\coupon\model;
use app\model\BaseModel;
use app\model\system\Stat;
/**
* 优惠券统计
*/
class CouponStat extends BaseModel
{
/**
* 领取优惠券统计
* @param $params
* @return array
*/
public function addReceiveCouponStat($params)
{
$coupon_id = $params[ 'coupon_id' ];
$site_id = $params[ 'site_id' ] ?? 0;
$order_condition = array (
[ 'coupon_id', '=', $coupon_id ],
[ 'site_id', '=', $site_id ]
);
$info = model('promotion_coupon')->getInfo($order_condition);
if (empty($info))
return $this->error();
$stat_data = array (
'site_id' => $site_id,
'coupon_count' => 1
);
$member_id = $info[ 'member_id' ];
//如果是第一笔订单才能累加下单会员数
$time_region = getDayStartAndEndTime();
$today_start_time = $time_region[ 'start_time' ];
$today_end_time = $time_region[ 'end_time' ];
$today_order_condition = array (
[ 'member_id', '=', $member_id ],
[ 'fetch_time', 'between', [ $today_start_time, $today_end_time ] ],
[ 'coupon_id', '<>', $coupon_id ]
);
$count = model('promotion_coupon')->getCount($today_order_condition);
if ($count == 0) {
$stat_data[ 'coupon_member_count' ] = 1;
}
//发布统计
$stat_model = new Stat();
$result = $stat_model->addShopStat($stat_data);
return $result;
}
<?php
namespace addon\coupon\model;
use app\model\BaseModel;
use app\model\system\Stat;
/**
* 优惠券统计
*/
class CouponStat extends BaseModel
{
/**
* 领取优惠券统计
* @param $params
* @return array
*/
public function addReceiveCouponStat($params)
{
$coupon_id = $params[ 'coupon_id' ];
$site_id = $params[ 'site_id' ] ?? 0;
$order_condition = array (
[ 'coupon_id', '=', $coupon_id ],
[ 'site_id', '=', $site_id ]
);
$info = model('promotion_coupon')->getInfo($order_condition);
if (empty($info))
return $this->error();
$stat_data = array (
'site_id' => $site_id,
'coupon_count' => 1
);
$member_id = $info[ 'member_id' ];
//如果是第一笔订单才能累加下单会员数
$time_region = getDayStartAndEndTime();
$today_start_time = $time_region[ 'start_time' ];
$today_end_time = $time_region[ 'end_time' ];
$today_order_condition = array (
[ 'member_id', '=', $member_id ],
[ 'fetch_time', 'between', [ $today_start_time, $today_end_time ] ],
[ 'coupon_id', '<>', $coupon_id ]
);
$count = model('promotion_coupon')->getCount($today_order_condition);
if ($count == 0) {
$stat_data[ 'coupon_member_count' ] = 1;
}
//发布统计
$stat_model = new Stat();
$result = $stat_model->addShopStat($stat_data);
return $result;
}
}

View File

@@ -1,336 +1,328 @@
<?php
/**
*/
namespace addon\coupon\model;
use app\model\BaseModel;
use app\model\system\Config as ConfigModel;
use app\model\system\Cron;
use app\model\upload\Upload;
/**
* 优惠券活动
*/
class CouponType extends BaseModel
{
//优惠券类型状态
private $coupon_type_status = [
1 => '进行中',
2 => '已结束',
-1 => '已关闭',
];
public function getCouponTypeStatus()
{
return $this->coupon_type_status;
}
/**
* 添加优惠券活动
* @param $data
* @return array
*/
public function addCouponType($data)
{
//只要创建了就是进行中
$data[ 'status' ] = 1;
$data[ 'create_time' ] = time();
//获取商品id
if ($data[ 'goods_type' ] == 1) {//全部商品参与
$data[ 'goods_ids' ] = '';
}
$data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
$res = model('promotion_coupon_type')->add($data);
if ($data[ 'validity_type' ] == 0) {
$cron = new Cron();
$cron->addCron(1, 1, '优惠券活动定时结束', 'CronCouponTypeEnd', $data[ 'end_time' ], $res);
}
$this->qrcode($res, 'all', $data[ 'site_id' ]);
return $this->success($res);
}
/**
* 编辑优惠券活动
* @param $data
* @param $coupon_type_id
* @return array
*/
public function editCouponType($data, $coupon_type_id)
{
$data[ 'update_time' ] = time();
//获取商品id
if ($data[ 'goods_type' ] == 1) {//全部商品参与
$data[ 'goods_ids' ] = '';
}
$coupon_info = model('promotion_coupon_type')->getInfo([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
if (!empty($coupon_info[ 'image' ]) && !empty($data[ 'image' ]) && $coupon_info[ 'image' ] != $data[ 'image' ]) {
$upload_model = new Upload();
$upload_model->deletePic($coupon_info[ 'image' ], $coupon_info[ 'site_id' ]);
}
$data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
$res = model('promotion_coupon_type')->update($data, [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
model('promotion_coupon')->update([ 'goods_ids' => $data[ 'goods_ids' ], 'goods_type' => $data[ 'goods_type' ] ], [ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'state', '=', 1 ] ]);
$cron = new Cron();
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
if ($data[ 'validity_type' ] == 0) {
$cron->addCron(1, 1, '优惠券活动定时结束', 'CronCouponTypeEnd', $data[ 'end_time' ], $coupon_type_id);
}
return $this->success($res);
}
/**
* 关闭优惠券
* @param $coupon_type_id
* @param $site_id
* @return array
*/
public function closeCouponType($coupon_type_id, $site_id)
{
$res = model('promotion_coupon_type')->update([ 'status' => -1 ], [ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
// if ($res) {
// model("promotion_coupon")->update(['state' => 3], [['coupon_type_id', '=', $coupon_type_id], ['site_id', '=', $site_id]]);
// }
$cron = new Cron();
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
return $this->success($res);
}
/**
* 删除优惠券活动
* @param $coupon_type_id
* @param $site_id
* @return array
*/
public function deleteCouponType($coupon_type_id, $site_id)
{
$coupon_info = model('promotion_coupon_type')->getInfo([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
if ($coupon_info['status'] == 1) return $this->error('', '进行中的优惠卷无法删除,请先关闭');
if (!empty($coupon_info[ 'image' ])) {
$upload_model = new Upload();
$upload_model->deletePic($coupon_info[ 'image' ], $coupon_info[ 'site_id' ]);
}
$res = model('promotion_coupon_type')->delete([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
if ($res) {
model('promotion_coupon')->delete([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
}
$cron = new Cron();
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
return $this->success($res);
}
/**
* 获取优惠券活动详情
* @param $coupon_type_id
* @param $site_id
* @return array
*/
public function getCouponTypeInfo($coupon_type_id, $site_id)
{
$res = model('promotion_coupon_type')->getList([ [ 'coupon_type_id', 'in', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
if (!empty($res)) {
foreach ($res as $k => $v) {
if ($v[ 'goods_type' ] == 2 || $v[ 'goods_type' ] == 3) {
$field[ $k ] = 'goods_id,goods_name,FLOOR(goods_stock) as goods_stock,goods_image,price,sort';
$goods_ids[ $k ] = substr($v[ 'goods_ids' ], '1', '-1');
$goods_list[ $k ] = model('goods')->getList([ [ 'goods_id', 'in', $goods_ids[ $k ] ] ], $field[ $k ]);
}
$res[ $k ][ 'goods_list' ] = $goods_list[$k] ?? [];
$res[ $k ][ 'goods_list_count' ] = count($res[ $k ][ 'goods_list' ]);
}
}
return $this->success($res);
}
/**
* 获取优惠券活动信息
* @param array $where
* @param bool $field
* @param string $alias
* @param null $join
* @param null $data
* @return array
*/
public function getInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
{
$res = model('promotion_coupon_type')->getInfo($where, $field, $alias, $join, $data);
return $this->success($res);
}
/**
* 获取优惠券类型列表
* @param array $condition
* @param string $field
* @param string $order
* @param null $limit
* @return array
*/
public function getCouponTypeList($condition = [], $field = '*', $order = 'create_time desc', $limit = null)
{
$res = model('promotion_coupon_type')->getList($condition, $field, $order, '', '', '', $limit);
return $this->success($res);
}
/**
* 获取优惠券活动分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @return array
*/
public function getCouponTypePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
{
$condition[] = [ 'promotion_type', '=', 0 ];
$list = model('promotion_coupon_type')->pageList($condition, $field, $order, $page, $page_size);
return $this->success($list);
}
/**
* 排序
* @param $coupon_type_id
* @param $sort
* @return array
*/
public function couponSort($coupon_type_id, $sort)
{
$res = model('promotion_coupon_type')->update([ 'sort' => $sort ], [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
return $this->success($res);
}
/**
* 生成优惠券二维码
* @param $coupon_type_id
* @param string $app_type all为全部
* @param string $type 类型 create创建 get获取
* @return mixed|array
*/
public function qrcode($coupon_type_id, $app_type, $site_id, $type = 'create')
{
$res = event('Qrcode', [
'site_id' => $site_id,
'app_type' => $app_type,
'type' => $type,
'data' => [
'coupon_type_id' => $coupon_type_id
],
'page' => '/pages_tool/goods/coupon_receive',
'qrcode_path' => 'upload/qrcode/coupon',
'qrcode_name' => 'coupon_type_code_' . $coupon_type_id . '_' . $site_id,
], true);
return $res;
}
/**
* 优惠券定时结束
* @param $coupon_type_id
* @return array
*/
public function couponCronEnd($coupon_type_id)
{
$res = model('promotion_coupon_type')->update([ 'status' => 2 ], [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
return $this->success($res);
}
public function spread($coupon_type_id, $name, $site_id, $type = 'create')
{
$data = [
'site_id' => $site_id,
'app_type' => 'all', // all为全部
'type' => $type, // 类型 create创建 get获取
'data' => [
'coupon_type_id' => $coupon_type_id
],
'page' => '/pages_tool/goods/coupon_receive',
'qrcode_path' => 'upload/qrcode/coupon',
'qrcode_name' => 'coupon_type_code_' . $coupon_type_id . '_' . $site_id,
];
event('Qrcode', $data, true);
$app_type_list = config('app_type');
$path = [];
foreach ($app_type_list as $k => $v) {
switch ( $k ) {
case 'h5':
$wap_domain = getH5Domain();
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'url' ] = $wap_domain . $data[ 'page' ] . '?coupon_type_id=' . $coupon_type_id;
$path[ $k ][ 'img' ] = 'upload/qrcode/coupon/coupon_type_code_' . $coupon_type_id . '_' . $site_id . '_' . $k . '.png';
break;
case 'weapp' :
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ] ]);
if (!empty($res[ 'data' ])) {
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信小程序';
} else {
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
}
} else {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信小程序';
}
break;
case 'wechat' :
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
if (!empty($res[ 'data' ])) {
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信公众号';
} else {
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
}
} else {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信公众号';
}
break;
}
}
$return = [
'path' => $path,
'name' => $name,
];
return $this->success($return);
}
public function urlQrcode($page, $qrcode_param, $promotion_type, $app_type, $site_id)
{
$params = [
'site_id' => $site_id,
'data' => $qrcode_param,
'page' => $page,
'promotion_type' => $promotion_type,
'app_type' => $app_type,
'h5_path' => $page . '?coupon_type_id=' . $qrcode_param[ 'coupon_type_id' ],
'qrcode_path' => 'upload/qrcode/coupon',
'qrcode_name' => 'coupon_type_code_' . $promotion_type . '_' . $qrcode_param[ 'coupon_type_id' ] . '_' . $site_id,
];
$solitaire = event('PromotionQrcode', $params, true);
return $this->success($solitaire);
}
<?php
namespace addon\coupon\model;
use app\model\BaseModel;
use app\model\system\Config as ConfigModel;
use app\model\system\Cron;
use app\model\upload\Upload;
/**
* 优惠券活动
*/
class CouponType extends BaseModel
{
//优惠券类型状态
private $coupon_type_status = [
1 => '进行中',
2 => '已结束',
-1 => '已关闭',
];
public function getCouponTypeStatus()
{
return $this->coupon_type_status;
}
/**
* 添加优惠券活动
* @param $data
* @return array
*/
public function addCouponType($data)
{
//只要创建了就是进行中
$data[ 'status' ] = 1;
$data[ 'create_time' ] = time();
//获取商品id
if ($data[ 'goods_type' ] == 1) {//全部商品参与
$data[ 'goods_ids' ] = '';
}
$data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
$res = model('promotion_coupon_type')->add($data);
if ($data[ 'validity_type' ] == 0) {
$cron = new Cron();
$cron->addCron(1, 1, '优惠券活动定时结束', 'CronCouponTypeEnd', $data[ 'end_time' ], $res);
}
$this->qrcode($res, 'all', $data[ 'site_id' ]);
return $this->success($res);
}
/**
* 编辑优惠券活动
* @param $data
* @param $coupon_type_id
* @return array
*/
public function editCouponType($data, $coupon_type_id)
{
$data[ 'update_time' ] = time();
//获取商品id
if ($data[ 'goods_type' ] == 1) {//全部商品参与
$data[ 'goods_ids' ] = '';
}
$coupon_info = model('promotion_coupon_type')->getInfo([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
if (!empty($coupon_info[ 'image' ]) && !empty($data[ 'image' ]) && $coupon_info[ 'image' ] != $data[ 'image' ]) {
$upload_model = new Upload();
$upload_model->deletePic($coupon_info[ 'image' ], $coupon_info[ 'site_id' ]);
}
$data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
$res = model('promotion_coupon_type')->update($data, [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
model('promotion_coupon')->update([ 'goods_ids' => $data[ 'goods_ids' ], 'goods_type' => $data[ 'goods_type' ] ], [ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'state', '=', 1 ] ]);
$cron = new Cron();
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
if ($data[ 'validity_type' ] == 0) {
$cron->addCron(1, 1, '优惠券活动定时结束', 'CronCouponTypeEnd', $data[ 'end_time' ], $coupon_type_id);
}
return $this->success($res);
}
/**
* 关闭优惠券
* @param $coupon_type_id
* @param $site_id
* @return array
*/
public function closeCouponType($coupon_type_id, $site_id)
{
$res = model('promotion_coupon_type')->update([ 'status' => -1 ], [ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
// if ($res) {
// model("promotion_coupon")->update(['state' => 3], [['coupon_type_id', '=', $coupon_type_id], ['site_id', '=', $site_id]]);
// }
$cron = new Cron();
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
return $this->success($res);
}
/**
* 删除优惠券活动
* @param $coupon_type_id
* @param $site_id
* @return array
*/
public function deleteCouponType($coupon_type_id, $site_id)
{
$coupon_info = model('promotion_coupon_type')->getInfo([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
if ($coupon_info['status'] == 1) return $this->error('', '进行中的优惠卷无法删除,请先关闭');
if (!empty($coupon_info[ 'image' ])) {
$upload_model = new Upload();
$upload_model->deletePic($coupon_info[ 'image' ], $coupon_info[ 'site_id' ]);
}
$res = model('promotion_coupon_type')->delete([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
if ($res) {
model('promotion_coupon')->delete([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
}
$cron = new Cron();
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
return $this->success($res);
}
/**
* 获取优惠券活动详情
* @param $coupon_type_id
* @param $site_id
* @return array
*/
public function getCouponTypeInfo($coupon_type_id, $site_id)
{
$res = model('promotion_coupon_type')->getList([ [ 'coupon_type_id', 'in', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
if (!empty($res)) {
foreach ($res as $k => $v) {
if ($v[ 'goods_type' ] == 2 || $v[ 'goods_type' ] == 3) {
$field[ $k ] = 'goods_id,goods_name,FLOOR(goods_stock) as goods_stock,goods_image,price,sort';
$goods_ids[ $k ] = substr($v[ 'goods_ids' ], '1', '-1');
$goods_list[ $k ] = model('goods')->getList([ [ 'goods_id', 'in', $goods_ids[ $k ] ] ], $field[ $k ]);
}
$res[ $k ][ 'goods_list' ] = $goods_list[$k] ?? [];
$res[ $k ][ 'goods_list_count' ] = count($res[ $k ][ 'goods_list' ]);
}
}
return $this->success($res);
}
/**
* 获取优惠券活动信息
* @param array $where
* @param bool $field
* @param string $alias
* @param null $join
* @param null $data
* @return array
*/
public function getInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
{
$res = model('promotion_coupon_type')->getInfo($where, $field, $alias, $join, $data);
return $this->success($res);
}
/**
* 获取优惠券类型列表
* @param array $condition
* @param string $field
* @param string $order
* @param null $limit
* @return array
*/
public function getCouponTypeList($condition = [], $field = '*', $order = 'create_time desc', $limit = null)
{
$res = model('promotion_coupon_type')->getList($condition, $field, $order, '', '', '', $limit);
return $this->success($res);
}
/**
* 获取优惠券活动分页列表
* @param array $condition
* @param int $page
* @param int $page_size
* @param string $order
* @param string $field
* @return array
*/
public function getCouponTypePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
{
$condition[] = [ 'promotion_type', '=', 0 ];
$list = model('promotion_coupon_type')->pageList($condition, $field, $order, $page, $page_size);
return $this->success($list);
}
/**
* 排序
* @param $coupon_type_id
* @param $sort
* @return array
*/
public function couponSort($coupon_type_id, $sort)
{
$res = model('promotion_coupon_type')->update([ 'sort' => $sort ], [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
return $this->success($res);
}
/**
* 生成优惠券二维码
* @param $coupon_type_id
* @param string $app_type all为全部
* @param string $type 类型 create创建 get获取
* @return mixed|array
*/
public function qrcode($coupon_type_id, $app_type, $site_id, $type = 'create')
{
$res = event('Qrcode', [
'site_id' => $site_id,
'app_type' => $app_type,
'type' => $type,
'data' => [
'coupon_type_id' => $coupon_type_id
],
'page' => '/pages_tool/goods/coupon_receive',
'qrcode_path' => 'upload/qrcode/coupon',
'qrcode_name' => 'coupon_type_code_' . $coupon_type_id . '_' . $site_id,
], true);
return $res;
}
/**
* 优惠券定时结束
* @param $coupon_type_id
* @return array
*/
public function couponCronEnd($coupon_type_id)
{
$res = model('promotion_coupon_type')->update([ 'status' => 2 ], [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
return $this->success($res);
}
public function spread($coupon_type_id, $name, $site_id, $type = 'create')
{
$data = [
'site_id' => $site_id,
'app_type' => 'all', // all为全部
'type' => $type, // 类型 create创建 get获取
'data' => [
'coupon_type_id' => $coupon_type_id
],
'page' => '/pages_tool/goods/coupon_receive',
'qrcode_path' => 'upload/qrcode/coupon',
'qrcode_name' => 'coupon_type_code_' . $coupon_type_id . '_' . $site_id,
];
event('Qrcode', $data, true);
$app_type_list = config('app_type');
$path = [];
foreach ($app_type_list as $k => $v) {
switch ( $k ) {
case 'h5':
$wap_domain = getH5Domain();
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'url' ] = $wap_domain . $data[ 'page' ] . '?coupon_type_id=' . $coupon_type_id;
$path[ $k ][ 'img' ] = 'upload/qrcode/coupon/coupon_type_code_' . $coupon_type_id . '_' . $site_id . '_' . $k . '.png';
break;
case 'weapp' :
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ] ]);
if (!empty($res[ 'data' ])) {
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信小程序';
} else {
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
}
} else {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信小程序';
}
break;
case 'wechat' :
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
if (!empty($res[ 'data' ])) {
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信公众号';
} else {
$path[ $k ][ 'status' ] = 1;
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
}
} else {
$path[ $k ][ 'status' ] = 2;
$path[ $k ][ 'message' ] = '未配置微信公众号';
}
break;
}
}
$return = [
'path' => $path,
'name' => $name,
];
return $this->success($return);
}
public function urlQrcode($page, $qrcode_param, $promotion_type, $app_type, $site_id)
{
$params = [
'site_id' => $site_id,
'data' => $qrcode_param,
'page' => $page,
'promotion_type' => $promotion_type,
'app_type' => $app_type,
'h5_path' => $page . '?coupon_type_id=' . $qrcode_param[ 'coupon_type_id' ],
'qrcode_path' => 'upload/qrcode/coupon',
'qrcode_name' => 'coupon_type_code_' . $promotion_type . '_' . $qrcode_param[ 'coupon_type_id' ] . '_' . $site_id,
];
$solitaire = event('PromotionQrcode', $params, true);
return $this->success($solitaire);
}
}

View File

@@ -1,243 +1,235 @@
<?php
/**
*/
namespace addon\coupon\model;
use addon\coupon\dict\CouponDict;
use app\model\BaseModel;
/**
* 优惠券
*/
class MemberCoupon extends BaseModel
{
/**
* 获取会员已领取优惠券
* @param $member_id
* @param $state
* @param int $site_id
* @param string $order
* @return array
*/
public function getMemberCouponList($member_id, $state, $site_id = 0, $order = "fetch_time desc")
{
$condition = array (
[ "member_id", "=", $member_id ],
[ "state", "=", $state ],
);
if ($site_id > 0) {
$condition[] = [ "site_id", "=", $site_id ];
}
$list = model("promotion_coupon")->getList($condition, "*", $order, '', '', '', 0);
return $this->success($list);
}
/**
* 使用优惠券
* @param $coupon_id
* @param $member_id
* @param int $order_id
* @return array
*/
public function useMemberCoupon($coupon_id, $member_id, $order_id = 0)
{
//优惠券处理方案
$result = model('promotion_coupon')->update([ 'use_order_id' => $order_id, 'state' => 2, 'use_time' => time() ], [ [ 'coupon_id', '=', $coupon_id ], [ "member_id", "=", $member_id ], [ 'state', '=', 1 ] ]);
if ($result === false) {
return $this->error();
}
return $this->success();
}
/**
* 获取会员已领取优惠券数量
* @param $member_id
* @param $state
* @param int $site_id
* @return array
*/
public function getMemberCouponNum($member_id, $state, $site_id = 0)
{
$condition = array (
[ "member_id", "=", $member_id ],
[ "state", "=", $state ],
);
if ($site_id > 0) {
$condition[] = [ "site_id", "=", $site_id ];
}
$num = model("promotion_coupon")->getCount($condition);
return $this->success($num);
}
/**
* 会员是否可领取该优惠券
* @param $coupon_type_id
* @param $member_id
* @return array
*/
public function receivedNum($coupon_type_id, $member_id)
{
$received_num = model('promotion_coupon')->getCount([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'member_id', '=', $member_id ] ]);
return $this->success($received_num);
}
/**
* 获取编码
*/
public function getCode()
{
return random_keys(8);
}
/**
* 会员批量发放优惠券
* @param $coupon_type_ids
* @param $site_id
* @param $member_id
* @param int $get_type
* @param int $is_stock
* @param int $related_id
* @return array
*/
public function sendCoupon($coupon_type_ids, $site_id, $member_id, $get_type = 4, $is_stock = 0, $related_id = 0)
{
//已选优惠券提交数组
if (!empty($coupon_type_ids)) {
$res = 0;
foreach ($coupon_type_ids as $coupon_type_id) {
$coupon_type_info = model('promotion_coupon_type')->getInfo([ 'coupon_type_id' => $coupon_type_id, 'site_id' => $site_id, 'status' => 1 ]);
if (!empty($coupon_type_info)) {
if ($coupon_type_info[ 'count' ] != -1 || $is_stock == 0) {
if ($coupon_type_info[ 'count' ] == $coupon_type_info[ 'lead_count' ]) {
return $this->error('', '来迟了该优惠券已被领取完了');
}
}
if ($coupon_type_info[ 'max_fetch' ] != 0 && $get_type == 2) {
//限制领取
$member_receive_num = model('promotion_coupon')->getCount([
'coupon_type_id' => $coupon_type_id,
'member_id' => $member_id,
'get_type' => 2
]);
if ($member_receive_num >= $coupon_type_info[ 'max_fetch' ] ) {
return $this->error('', '该优惠券领取已达到上限');
}
}
$data = [
'coupon_type_id' => $coupon_type_id,
'site_id' => $site_id,
'coupon_code' => $this->getCode(),
'member_id' => $member_id,
'money' => $coupon_type_info[ 'money' ],
'state' => 1,
'get_type' => $get_type,
'goods_type' => $coupon_type_info[ 'goods_type' ],
'fetch_time' => time(),
'coupon_name' => $coupon_type_info[ 'coupon_name' ],
'at_least' => $coupon_type_info[ 'at_least' ],
'type' => $coupon_type_info[ 'type' ],
'discount' => $coupon_type_info[ 'discount' ],
'discount_limit' => $coupon_type_info[ 'discount_limit' ],
'goods_ids' => $coupon_type_info[ 'goods_ids' ],
'related_id' => $related_id
];
if ($coupon_type_info[ 'validity_type' ] == 0) {
$data[ 'end_time' ] = $coupon_type_info[ 'end_time' ];
} elseif ($coupon_type_info[ 'validity_type' ] == 1) {
$data[ 'end_time' ] = ( time() + $coupon_type_info[ 'fixed_term' ] * 86400 );
}
$res = model('promotion_coupon')->add($data);
if ($is_stock == 0) {
model('promotion_coupon_type')->setInc([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count');
}
}
}
if ($res) {
return $this->success($res);
} else {
return $this->error();
}
} else {
return $this->error();
}
}
/**
* 回收优惠券
* @param array $coupon_list
* @param $site_id
* @return array
*/
public function recoveryCoupon(array $coupon_list, $site_id)
{
$coupon = [];
foreach ($coupon_list as $coupon_item) {
if (isset($coupon[ $coupon_item[ 'coupon_type_id' ] ])) {
$coupon[$coupon_item['coupon_type_id']][] = $coupon_item['coupon_id'];
} else {
$coupon[ $coupon_item[ 'coupon_type_id' ] ] = [ $coupon_item[ 'coupon_id' ] ];
}
}
if (!count($coupon)) return $this->error();
model('promotion_coupon')->startTrans();
try {
foreach ($coupon as $coupon_type_id => $coupon_ids) {
$num = model('promotion_coupon')->delete([ [ 'coupon_id', 'in', $coupon_ids ], [ 'site_id', '=', $site_id ], [ 'state', '=', 1 ] ]);
if ($num) model('promotion_coupon_type')->setDec([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count', $num);
}
model('promotion_coupon')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_coupon')->rollback();
return $this->error('', '回收失败');
}
}
/**
* 专用于撤回活动赠送的优惠券
* @return void
*/
public function cancelByPromotion($data){
$member_id = $data['member_id'];
$coupon_data = $data['coupon_data'];//优惠券id相关项
$coupon_ids = array_column($coupon_data, 'coupon_type_id');
$member_coupon_list = model('promotion_coupon')->getList([
['member_id', '=', $member_id],
['coupon_type_id', 'in', $coupon_ids],
['state', '=', CouponDict::normal]
], '*');
$member_coupon_type_group_list = [];
foreach($member_coupon_list as $v){
$member_coupon_type_group_list[$v['coupon_type_id']][] = $v['coupon_id'];
}
$cancel_ids = [];
foreach ($coupon_data as $item) {
$coupon_type_id = $item['coupon_type_id'];
$num = $item['num'];
$item_coupon_type_group = $member_coupon_type_group_list[$coupon_type_id] ?? [];
if($item_coupon_type_group){
if(count($item_coupon_type_group) > $num){
$cancel_ids = array_merge($cancel_ids, array_slice($item_coupon_type_group, 0, $num));
}else{
$cancel_ids = array_merge($cancel_ids, $item_coupon_type_group);
}
}
}
model('promotion_coupon')->update(['state' => CouponDict::close], [['coupon_id', 'in', $cancel_ids]]);
return $this->success();
}
<?php
namespace addon\coupon\model;
use addon\coupon\dict\CouponDict;
use app\model\BaseModel;
/**
* 优惠券
*/
class MemberCoupon extends BaseModel
{
/**
* 获取会员已领取优惠券
* @param $member_id
* @param $state
* @param int $site_id
* @param string $order
* @return array
*/
public function getMemberCouponList($member_id, $state, $site_id = 0, $order = "fetch_time desc")
{
$condition = array (
[ "member_id", "=", $member_id ],
[ "state", "=", $state ],
);
if ($site_id > 0) {
$condition[] = [ "site_id", "=", $site_id ];
}
$list = model("promotion_coupon")->getList($condition, "*", $order, '', '', '', 0);
return $this->success($list);
}
/**
* 使用优惠券
* @param $coupon_id
* @param $member_id
* @param int $order_id
* @return array
*/
public function useMemberCoupon($coupon_id, $member_id, $order_id = 0)
{
//优惠券处理方案
$result = model('promotion_coupon')->update([ 'use_order_id' => $order_id, 'state' => 2, 'use_time' => time() ], [ [ 'coupon_id', '=', $coupon_id ], [ "member_id", "=", $member_id ], [ 'state', '=', 1 ] ]);
if ($result === false) {
return $this->error();
}
return $this->success();
}
/**
* 获取会员已领取优惠券数量
* @param $member_id
* @param $state
* @param int $site_id
* @return array
*/
public function getMemberCouponNum($member_id, $state, $site_id = 0)
{
$condition = array (
[ "member_id", "=", $member_id ],
[ "state", "=", $state ],
);
if ($site_id > 0) {
$condition[] = [ "site_id", "=", $site_id ];
}
$num = model("promotion_coupon")->getCount($condition);
return $this->success($num);
}
/**
* 会员是否可领取该优惠券
* @param $coupon_type_id
* @param $member_id
* @return array
*/
public function receivedNum($coupon_type_id, $member_id)
{
$received_num = model('promotion_coupon')->getCount([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'member_id', '=', $member_id ] ]);
return $this->success($received_num);
}
/**
* 获取编码
*/
public function getCode()
{
return random_keys(8);
}
/**
* 会员批量发放优惠券
* @param $coupon_type_ids
* @param $site_id
* @param $member_id
* @param int $get_type
* @param int $is_stock
* @param int $related_id
* @return array
*/
public function sendCoupon($coupon_type_ids, $site_id, $member_id, $get_type = 4, $is_stock = 0, $related_id = 0)
{
//已选优惠券提交数组
if (!empty($coupon_type_ids)) {
$res = 0;
foreach ($coupon_type_ids as $coupon_type_id) {
$coupon_type_info = model('promotion_coupon_type')->getInfo([ 'coupon_type_id' => $coupon_type_id, 'site_id' => $site_id, 'status' => 1 ]);
if (!empty($coupon_type_info)) {
if ($coupon_type_info[ 'count' ] != -1 || $is_stock == 0) {
if ($coupon_type_info[ 'count' ] == $coupon_type_info[ 'lead_count' ]) {
return $this->error('', '来迟了该优惠券已被领取完了');
}
}
if ($coupon_type_info[ 'max_fetch' ] != 0 && $get_type == 2) {
//限制领取
$member_receive_num = model('promotion_coupon')->getCount([
'coupon_type_id' => $coupon_type_id,
'member_id' => $member_id,
'get_type' => 2
]);
if ($member_receive_num >= $coupon_type_info[ 'max_fetch' ] ) {
return $this->error('', '该优惠券领取已达到上限');
}
}
$data = [
'coupon_type_id' => $coupon_type_id,
'site_id' => $site_id,
'coupon_code' => $this->getCode(),
'member_id' => $member_id,
'money' => $coupon_type_info[ 'money' ],
'state' => 1,
'get_type' => $get_type,
'goods_type' => $coupon_type_info[ 'goods_type' ],
'fetch_time' => time(),
'coupon_name' => $coupon_type_info[ 'coupon_name' ],
'at_least' => $coupon_type_info[ 'at_least' ],
'type' => $coupon_type_info[ 'type' ],
'discount' => $coupon_type_info[ 'discount' ],
'discount_limit' => $coupon_type_info[ 'discount_limit' ],
'goods_ids' => $coupon_type_info[ 'goods_ids' ],
'related_id' => $related_id
];
if ($coupon_type_info[ 'validity_type' ] == 0) {
$data[ 'end_time' ] = $coupon_type_info[ 'end_time' ];
} elseif ($coupon_type_info[ 'validity_type' ] == 1) {
$data[ 'end_time' ] = ( time() + $coupon_type_info[ 'fixed_term' ] * 86400 );
}
$res = model('promotion_coupon')->add($data);
if ($is_stock == 0) {
model('promotion_coupon_type')->setInc([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count');
}
}
}
if ($res) {
return $this->success($res);
} else {
return $this->error();
}
} else {
return $this->error();
}
}
/**
* 回收优惠券
* @param array $coupon_list
* @param $site_id
* @return array
*/
public function recoveryCoupon(array $coupon_list, $site_id)
{
$coupon = [];
foreach ($coupon_list as $coupon_item) {
if (isset($coupon[ $coupon_item[ 'coupon_type_id' ] ])) {
$coupon[$coupon_item['coupon_type_id']][] = $coupon_item['coupon_id'];
} else {
$coupon[ $coupon_item[ 'coupon_type_id' ] ] = [ $coupon_item[ 'coupon_id' ] ];
}
}
if (!count($coupon)) return $this->error();
model('promotion_coupon')->startTrans();
try {
foreach ($coupon as $coupon_type_id => $coupon_ids) {
$num = model('promotion_coupon')->delete([ [ 'coupon_id', 'in', $coupon_ids ], [ 'site_id', '=', $site_id ], [ 'state', '=', 1 ] ]);
if ($num) model('promotion_coupon_type')->setDec([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count', $num);
}
model('promotion_coupon')->commit();
return $this->success();
} catch (\Exception $e) {
model('promotion_coupon')->rollback();
return $this->error('', '回收失败');
}
}
/**
* 专用于撤回活动赠送的优惠券
* @return void
*/
public function cancelByPromotion($data){
$member_id = $data['member_id'];
$coupon_data = $data['coupon_data'];//优惠券id相关项
$coupon_ids = array_column($coupon_data, 'coupon_type_id');
$member_coupon_list = model('promotion_coupon')->getList([
['member_id', '=', $member_id],
['coupon_type_id', 'in', $coupon_ids],
['state', '=', CouponDict::normal]
], '*');
$member_coupon_type_group_list = [];
foreach($member_coupon_list as $v){
$member_coupon_type_group_list[$v['coupon_type_id']][] = $v['coupon_id'];
}
$cancel_ids = [];
foreach ($coupon_data as $item) {
$coupon_type_id = $item['coupon_type_id'];
$num = $item['num'];
$item_coupon_type_group = $member_coupon_type_group_list[$coupon_type_id] ?? [];
if($item_coupon_type_group){
if(count($item_coupon_type_group) > $num){
$cancel_ids = array_merge($cancel_ids, array_slice($item_coupon_type_group, 0, $num));
}else{
$cancel_ids = array_merge($cancel_ids, $item_coupon_type_group);
}
}
}
model('promotion_coupon')->update(['state' => CouponDict::close], [['coupon_id', 'in', $cancel_ids]]);
return $this->success();
}
}

Some files were not shown because too many files have changed in this diff Show More