| 
									
										
										
										
											2025-09-26 14:38:44 +08:00
										 |  |  |  | //供应商接口
 | 
					
						
							|  |  |  |  | const express = require('express'); | 
					
						
							|  |  |  |  | const {getDB} = require('../database'); | 
					
						
							|  |  |  |  | const router = express.Router(); | 
					
						
							|  |  |  |  | const {SelectBuilder, InsertBuilder, UpdateBuilder} = require('../config/dbv2') | 
					
						
							|  |  |  |  | const bcrypt = require('bcryptjs'); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | /* | 
					
						
							|  |  |  |  | * username  账号 | 
					
						
							|  |  |  |  | * password  密码 | 
					
						
							|  |  |  |  | * avatar 头像 | 
					
						
							|  |  |  |  | * real_name  姓名 | 
					
						
							|  |  |  |  | * id_card 身份证号 | 
					
						
							|  |  |  |  | * wechat_qr  微信二维码 | 
					
						
							|  |  |  |  | * alipay_qr 支付宝收款码 | 
					
						
							|  |  |  |  | * bank_card  银行卡号 | 
					
						
							|  |  |  |  | * unionpay_qr 云闪付收款码 | 
					
						
							|  |  |  |  | * phone 手机号 | 
					
						
							|  |  |  |  | * | 
					
						
							|  |  |  |  | * */ | 
					
						
							| 
									
										
										
										
											2025-10-10 17:30:49 +08:00
										 |  |  |  | router.post('/add', async (req, res) => { | 
					
						
							| 
									
										
										
										
											2025-09-26 14:38:44 +08:00
										 |  |  |  |     const db = getDB(); | 
					
						
							|  |  |  |  |     try { | 
					
						
							|  |  |  |  |         const { | 
					
						
							|  |  |  |  |             username, | 
					
						
							|  |  |  |  |             password, | 
					
						
							|  |  |  |  |             avatar, | 
					
						
							|  |  |  |  |             real_name, | 
					
						
							|  |  |  |  |             id_card, | 
					
						
							|  |  |  |  |             wechat_qr, | 
					
						
							|  |  |  |  |             alipay_qr, | 
					
						
							|  |  |  |  |             bank_card, | 
					
						
							|  |  |  |  |             unionpay_qr, | 
					
						
							|  |  |  |  |             phone | 
					
						
							|  |  |  |  |         } = req.body; | 
					
						
							|  |  |  |  |         console.log('12333333') | 
					
						
							|  |  |  |  |         // 验证手机号格式
 | 
					
						
							|  |  |  |  |         const phoneRegex = /^1[3-9]\d{9}$/; | 
					
						
							|  |  |  |  |         if (!phoneRegex.test(phone)) { | 
					
						
							|  |  |  |  |             return res.status(400).json({success: false, message: '手机号格式不正确'}); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         let userCountQuery = new SelectBuilder() | 
					
						
							|  |  |  |  |             .from('users') | 
					
						
							|  |  |  |  |             .select('COUNT(*) as total') | 
					
						
							|  |  |  |  |             .where(`(username=? or phone=?)`, username, phone) | 
					
						
							|  |  |  |  |             .where('is_delete=?', false) | 
					
						
							|  |  |  |  |         let [user] = await userCountQuery.execute(db); | 
					
						
							|  |  |  |  |         if (user.total > 0) { | 
					
						
							|  |  |  |  |             res.status(400).send({ | 
					
						
							|  |  |  |  |                 success: false, message: '手机号或者用户名重复' | 
					
						
							|  |  |  |  |             }) | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         const hashedPassword = await bcrypt.hash(password, 10); | 
					
						
							|  |  |  |  |         let insetObj = { | 
					
						
							|  |  |  |  |             username, | 
					
						
							|  |  |  |  |             password: hashedPassword, | 
					
						
							|  |  |  |  |             real_name, | 
					
						
							|  |  |  |  |             id_card, | 
					
						
							|  |  |  |  |             wechat_qr, | 
					
						
							|  |  |  |  |             alipay_qr, | 
					
						
							|  |  |  |  |             bank_card, | 
					
						
							|  |  |  |  |             unionpay_qr, | 
					
						
							|  |  |  |  |             avatar, | 
					
						
							|  |  |  |  |             phone, | 
					
						
							|  |  |  |  |             user_type: 'supplier', | 
					
						
							| 
									
										
										
										
											2025-10-20 17:21:40 +08:00
										 |  |  |  |             role: 'supplier', | 
					
						
							|  |  |  |  |             audit_status: 'approved', | 
					
						
							|  |  |  |  |             payment_status: 'paid' | 
					
						
							| 
									
										
										
										
											2025-09-26 14:38:44 +08:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |         console.log(insetObj, '111') | 
					
						
							|  |  |  |  |         await db.query('START TRANSACTION'); | 
					
						
							|  |  |  |  |         let sqlResult = new InsertBuilder() | 
					
						
							|  |  |  |  |             .into('users') | 
					
						
							|  |  |  |  |             .values(insetObj) | 
					
						
							|  |  |  |  |         let result = await sqlResult.execute(db); | 
					
						
							|  |  |  |  |         console.log(result) | 
					
						
							|  |  |  |  |         await db.query('COMMIT'); | 
					
						
							|  |  |  |  |         if (result.affectedRows > 0) { | 
					
						
							|  |  |  |  |             res.json({success: true, message: '创建成功'}) | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             res.json({success: false, message: '系统错误请联系管理员'}) | 
					
						
							|  |  |  |  |             await db.query('ROLLBACK'); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     } catch (err) { | 
					
						
							|  |  |  |  |         console.log(err) | 
					
						
							|  |  |  |  |         await db.query('ROLLBACK'); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | }) | 
					
						
							|  |  |  |  | /* | 
					
						
							|  |  |  |  | * 修改供应商 | 
					
						
							|  |  |  |  | * username  账号 | 
					
						
							|  |  |  |  | * password  密码 | 
					
						
							|  |  |  |  | * avatar 头像 | 
					
						
							|  |  |  |  | * real_name  姓名 | 
					
						
							|  |  |  |  | * id_card 身份证号 | 
					
						
							|  |  |  |  | * wechat_qr  微信二维码 | 
					
						
							|  |  |  |  | * alipay_qr 支付宝收款码 | 
					
						
							|  |  |  |  | * bank_card  银行卡号 | 
					
						
							|  |  |  |  | * unionpay_qr 云闪付收款码 | 
					
						
							|  |  |  |  | * phone 手机号 | 
					
						
							|  |  |  |  | * id 供应商id | 
					
						
							|  |  |  |  | * audit_status 审核状态   'pending','approved','rejected' | 
					
						
							|  |  |  |  | * */ | 
					
						
							| 
									
										
										
										
											2025-10-10 17:30:49 +08:00
										 |  |  |  | router.put('/edit', async (req, res) => { | 
					
						
							| 
									
										
										
										
											2025-09-26 14:38:44 +08:00
										 |  |  |  |     const db = getDB(); | 
					
						
							|  |  |  |  |     try { | 
					
						
							|  |  |  |  |         const { | 
					
						
							|  |  |  |  |             username, | 
					
						
							|  |  |  |  |             password, | 
					
						
							|  |  |  |  |             id, | 
					
						
							|  |  |  |  |             avatar, | 
					
						
							|  |  |  |  |             real_name, | 
					
						
							|  |  |  |  |             id_card, | 
					
						
							|  |  |  |  |             wechat_qr, | 
					
						
							|  |  |  |  |             bank_card, | 
					
						
							|  |  |  |  |             unionpay_qr, | 
					
						
							|  |  |  |  |             phone, | 
					
						
							|  |  |  |  |             audit_status | 
					
						
							|  |  |  |  |         } = req.body; | 
					
						
							|  |  |  |  |         if (!id) { | 
					
						
							|  |  |  |  |             return res.status(400).send({ | 
					
						
							|  |  |  |  |                 success: false, | 
					
						
							|  |  |  |  |                 message: '供应商id未填写' | 
					
						
							|  |  |  |  |             }) | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         let userCountQuery = new SelectBuilder() | 
					
						
							|  |  |  |  |             .from('users') | 
					
						
							|  |  |  |  |             .select('COUNT(*) as total') | 
					
						
							|  |  |  |  |             .where(`(username=? or phone=?)`, username, phone) | 
					
						
							|  |  |  |  |             .where('id!=?', id) | 
					
						
							|  |  |  |  |             .where('is_delete=?', false) | 
					
						
							|  |  |  |  |         let userInfo = await userCountQuery.execute(db); | 
					
						
							|  |  |  |  |         if (userInfo.total > 0) { | 
					
						
							|  |  |  |  |             res.status(400).send({ | 
					
						
							|  |  |  |  |                 success: false, | 
					
						
							|  |  |  |  |                 message: '手机号或用户名重复请重新填写' | 
					
						
							|  |  |  |  |             }) | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         // 动态组装更新对象
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         let upUser = new UpdateBuilder() | 
					
						
							|  |  |  |  |             .update('users') | 
					
						
							|  |  |  |  |             .where('id=?', id) | 
					
						
							|  |  |  |  |         const fields = [ | 
					
						
							|  |  |  |  |             'username', | 
					
						
							|  |  |  |  |             'avatar', | 
					
						
							|  |  |  |  |             'real_name', | 
					
						
							|  |  |  |  |             'id_card', | 
					
						
							|  |  |  |  |             'wechat_qr', | 
					
						
							|  |  |  |  |             'bank_card', | 
					
						
							|  |  |  |  |             'unionpay_qr', | 
					
						
							|  |  |  |  |             'phone', | 
					
						
							|  |  |  |  |             'audit_status' | 
					
						
							|  |  |  |  |         ]; | 
					
						
							|  |  |  |  |         // 遍历允许的字段,存在才 set
 | 
					
						
							|  |  |  |  |         for (const field of fields) { | 
					
						
							|  |  |  |  |             if (req.body[field]) { | 
					
						
							|  |  |  |  |                 upUser.set(field, req.body[field]); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 密码单独处理(异步 hash)
 | 
					
						
							|  |  |  |  |         if (password) { | 
					
						
							|  |  |  |  |             upUser.set('password', await bcrypt.hash(password, 10)); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         await upUser.execute(db); | 
					
						
							|  |  |  |  |         res.json({success: true, message: '更新成功'}); | 
					
						
							|  |  |  |  |     } catch (e) { | 
					
						
							|  |  |  |  |         console.log(e) | 
					
						
							|  |  |  |  |         res.status(500).send({ | 
					
						
							|  |  |  |  |             success: false, | 
					
						
							|  |  |  |  |             message: '系统错误请联系管理员' | 
					
						
							|  |  |  |  |         }) | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | }) | 
					
						
							|  |  |  |  | /* | 
					
						
							|  |  |  |  | * 供应商详情 | 
					
						
							|  |  |  |  | * id 供应商id | 
					
						
							|  |  |  |  | * */ | 
					
						
							|  |  |  |  | router.get('/details', async (req, res) => { | 
					
						
							|  |  |  |  |     const {id} = req.query; | 
					
						
							|  |  |  |  |     const db = getDB(); | 
					
						
							|  |  |  |  |     try { | 
					
						
							|  |  |  |  |         if (!id) { | 
					
						
							|  |  |  |  |             return res.status(400).send({ | 
					
						
							|  |  |  |  |                 success: false, | 
					
						
							|  |  |  |  |                 message: '供应商id不能为空' | 
					
						
							|  |  |  |  |             }) | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         let [user] = await new SelectBuilder() | 
					
						
							|  |  |  |  |             .from('users') | 
					
						
							|  |  |  |  |             .where('id=?', id) | 
					
						
							|  |  |  |  |             .where('user_type=?', 'supplier') | 
					
						
							|  |  |  |  |             .where('is_delete=?', false) | 
					
						
							|  |  |  |  |             .execute(db) | 
					
						
							|  |  |  |  |         if (user) { | 
					
						
							|  |  |  |  |             delete user.password | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             return res.status(400).json({success: false, message: '无此用户'}) | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         res.json({data: user, success: true, message: '查询成功'}) | 
					
						
							|  |  |  |  |     } catch (err) { | 
					
						
							|  |  |  |  |         return res.status(500).json({success: false, message: '系统错误,请联系管理员'}) | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | }) | 
					
						
							|  |  |  |  | /* | 
					
						
							|  |  |  |  | * 供应商列表 | 
					
						
							|  |  |  |  | * page  1 页数 | 
					
						
							|  |  |  |  | * limit 20 每页数量 | 
					
						
							|  |  |  |  | * name 手机号,账号 | 
					
						
							|  |  |  |  | * */ | 
					
						
							|  |  |  |  | router.get('/list', async (req, res) => { | 
					
						
							|  |  |  |  |     const db = getDB(); | 
					
						
							|  |  |  |  |     try { | 
					
						
							|  |  |  |  |         let {page = 1, limit = 20, name} = req.query; | 
					
						
							|  |  |  |  |         console.log(page, limit, name); | 
					
						
							|  |  |  |  |         let userCountQuery = new SelectBuilder() | 
					
						
							|  |  |  |  |             .from('users') | 
					
						
							|  |  |  |  |             .where('user_type=?', 'supplier') | 
					
						
							|  |  |  |  |             .where('is_delete=?', false) | 
					
						
							|  |  |  |  |         if (name) { | 
					
						
							| 
									
										
										
										
											2025-09-26 15:56:10 +08:00
										 |  |  |  |             userCountQuery.where('(username LIKE ? or phone LIKE ?)', `%${name}%`, `%${name}%`) | 
					
						
							| 
									
										
										
										
											2025-09-26 14:38:44 +08:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |         const data = await userCountQuery.paginateWithCount(db, page, limit); | 
					
						
							|  |  |  |  |         return res.json(data) | 
					
						
							|  |  |  |  |     } catch (err) { | 
					
						
							|  |  |  |  |         console.log(err) | 
					
						
							|  |  |  |  |         return res.status(500).json({success: false, message: err}) | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | }) | 
					
						
							|  |  |  |  | /* | 
					
						
							|  |  |  |  | * 删除供应商 | 
					
						
							|  |  |  |  | * id | 
					
						
							|  |  |  |  | * */ | 
					
						
							| 
									
										
										
										
											2025-10-10 17:30:49 +08:00
										 |  |  |  | router.delete('/delete', async (req, res) => { | 
					
						
							| 
									
										
										
										
											2025-09-26 14:38:44 +08:00
										 |  |  |  |     const db = getDB(); | 
					
						
							|  |  |  |  |     try { | 
					
						
							|  |  |  |  |         const {id} = req.query; | 
					
						
							|  |  |  |  |         await new UpdateBuilder() | 
					
						
							|  |  |  |  |             .update('users') | 
					
						
							|  |  |  |  |             .set('is_delete', true) | 
					
						
							|  |  |  |  |             .where('id=?', id) | 
					
						
							|  |  |  |  |             .execute(db) | 
					
						
							|  |  |  |  |         res.json({success: true, message: '删除成功'}) | 
					
						
							|  |  |  |  |     } catch (err) { | 
					
						
							|  |  |  |  |         res.status(500).send({error: err}) | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | }) | 
					
						
							|  |  |  |  | module.exports = router; |