Product.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. class Product extends Api
  6. {
  7. protected $noNeedLogin = ['*'];
  8. protected $noNeedRight = ['*'];
  9. /**
  10. * 商户菜单
  11. * @return void
  12. * @throws \think\db\exception\DataNotFoundException
  13. * @throws \think\db\exception\ModelNotFoundException
  14. * @throws \think\exception\DbException
  15. */
  16. public function merchantGetab()
  17. {
  18. if (!$this->request->isGet()) {
  19. $this->error('请求错误');
  20. }
  21. $list = \db('product_merchant')
  22. ->where([
  23. 'status' => 1,
  24. 'deleteTime' => null,
  25. ])
  26. ->field('merchant_code,merchant_name')
  27. ->select();
  28. foreach ($list as $k => $v) {
  29. $list[$k]['tab'] = $v['merchant_name'].'('.$v['merchant_code'].')';
  30. }
  31. if (empty($list)) {
  32. $this->error('未找到商户数据');
  33. }else{
  34. $this->success('成功', $list);
  35. }
  36. }
  37. /**
  38. * 产品列表
  39. * @return void
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. * @throws \think\exception\DbException
  43. */
  44. public function productList()
  45. {
  46. // 1. 请求方法验证优化
  47. if (!$this->request->isGet()) {
  48. $this->error('请求方法错误');
  49. }
  50. $param = $this->request->param();
  51. // 2. 参数验证优化
  52. if (empty($param['code']) || !is_string($param['code'])) {
  53. $this->error('商户编码参数错误');
  54. }
  55. // 3. 参数安全处理
  56. $merchantCode = trim($param['code']);
  57. $searchKeyword = isset($param['search']) ? trim($param['search']) : '';
  58. // 4. 分页参数处理
  59. $page = isset($param['page']) ? intval($param['page']) : 1;
  60. $pageSize = isset($param['pageSize']) ? intval($param['pageSize']) : 15;
  61. // 验证分页参数
  62. if ($page < 1) $page = 1;
  63. if ($pageSize < 1 || $pageSize > 100) $pageSize = 15; // 限制最大每页100条
  64. // 5. 构建查询条件
  65. $where = [
  66. 'b.merchant_code' => $merchantCode
  67. ];
  68. if (!empty($searchKeyword)) {
  69. // 使用更安全的查询方式
  70. $where['a.product_name|a.product_code'] = ['like', '%' . addslashes($searchKeyword) . '%'];
  71. }
  72. // 6. 查询数据(带分页)
  73. try {
  74. // 首先获取总记录数
  75. $total = \db('product')
  76. ->alias('a')
  77. ->join('product_merchant b', 'a.merchant_id = b.id')
  78. ->where($where)
  79. ->count();
  80. // 分页查询
  81. $list = \db('product')
  82. ->alias('a')
  83. ->join('product_merchant b', 'a.merchant_id = b.id')
  84. ->where($where)
  85. ->field([
  86. 'a.product_name as 产品名称',
  87. 'a.product_code as 产品编码',
  88. 'a.product_img',
  89. 'a.product_new_img',
  90. 'a.createTime as 创建时间',
  91. 'a.create_name as 创建人',
  92. 'b.merchant_code as 商户编码',
  93. 'a.id'
  94. ])
  95. ->order('a.createTime', 'desc')
  96. ->page($page, $pageSize)
  97. ->select();
  98. } catch (\Exception $e) {
  99. $this->error('查询数据失败:' . $e->getMessage());
  100. }
  101. // 7. 优化数据处理逻辑
  102. if (!empty($list)) {
  103. foreach ($list as &$item) {
  104. // 产品图片
  105. if (!empty($item['product_img'])) {
  106. $item['产品图片'] = ltrim($item['product_img'], '/');
  107. unset($item['product_img']); // 移除原始字段
  108. } else {
  109. $item['产品图片'] = ''; // 设置默认值
  110. }
  111. // 产品效果图
  112. if (!empty($item['product_new_img'])) {
  113. $item['产品效果图'] = ltrim($item['product_new_img'], '/');
  114. unset($item['product_new_img']); // 移除原始字段
  115. } else {
  116. $item['产品效果图'] = ''; // 设置默认值
  117. }
  118. }
  119. unset($item); // 解除引用
  120. }
  121. $result = [
  122. 'list' => $list,
  123. 'total' => $total,
  124. ];
  125. // 10. 统一返回格式
  126. $this->success('查询成功', $result);
  127. }
  128. /**
  129. * 产品原图图片上传
  130. * @return \think\response\Json|void
  131. */
  132. public function ImgUpload()
  133. {
  134. // 处理 CORS OPTIONS 预检请求
  135. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  136. header('Access-Control-Allow-Origin: *');
  137. header('Access-Control-Allow-Methods: POST, OPTIONS');
  138. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  139. header('Access-Control-Max-Age: 86400');
  140. exit(204);
  141. }
  142. // 实际请求必须返回 CORS 头
  143. header('Access-Control-Allow-Origin: *');
  144. // 获取上传的文件
  145. $file = request()->file('image');
  146. $param = $this->request->param();
  147. if ($file) {
  148. // 生成日期格式的文件夹名 image_YYYYMMDD
  149. $dateFolder = 'image_' . date('Ymd');
  150. // 指定目标目录(包含日期文件夹)
  151. $targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'merchant' . DS . $param['merchant_code'] . DS . $param['product_code'] . DS . 'oldimg' . DS . $dateFolder;
  152. // 若目录不存在则创建
  153. if (!is_dir($targetPath)) {
  154. mkdir($targetPath, 0755, true);
  155. }
  156. // 获取原始文件名(或自定义新文件名)
  157. $originalName = $file->getInfo('name'); // 原始文件名
  158. $extension = pathinfo($originalName, PATHINFO_EXTENSION); // 文件扩展名
  159. $newFileName = uniqid() . '.' . $extension; // 生成唯一文件名(避免冲突)
  160. // 移动文件到指定目录,并验证大小/格式,同时指定自定义文件名
  161. $info = $file->validate([
  162. 'size' => 10485760, // 最大10MB
  163. 'ext' => 'jpg,png'
  164. ])->move($targetPath, $newFileName); // 关键:手动指定文件名,避免自动生成日期目录
  165. if ($info) {
  166. // 直接拼接路径,不依赖 getSaveName() 的返回值
  167. $imageUrl = '/uploads/merchant/'.$param['merchant_code'].'/'.$param['product_code'].'/oldimg/' . $dateFolder . '/' . $newFileName;
  168. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  169. } else {
  170. $res = $file->getError();
  171. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  172. }
  173. }
  174. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  175. }
  176. /**
  177. * 产品详情
  178. * @return void
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. * @throws \think\exception\DbException
  182. */
  183. public function productDetail()
  184. {
  185. // 1. 请求方法验证
  186. if (!$this->request->isGet()) {
  187. $this->error('只支持GET请求');
  188. }
  189. // 2. 参数获取与验证
  190. $param = $this->request->param();
  191. if (empty($param['id']) || !is_numeric($param['id'])) {
  192. $this->error('产品ID参数错误');
  193. }
  194. // 3. 参数安全处理
  195. $productId = intval($param['id']);
  196. if ($productId <= 0) {
  197. $this->error('产品ID必须为正整数');
  198. }
  199. // 4. 查询数据
  200. try {
  201. $product = \db('product')
  202. ->field([
  203. 'id',
  204. 'product_name as 产品名称',
  205. 'product_code as 产品编码',
  206. 'product_img',
  207. 'product_new_img',
  208. 'createTime as 创建时间',
  209. 'create_name as 创建人',
  210. ])
  211. ->where('id', $productId)
  212. ->whereNull('deleteTime')
  213. ->find();
  214. } catch (\Exception $e) {
  215. $this->error('查询产品详情失败:' . $e->getMessage());
  216. }
  217. // 5. 检查查询结果
  218. if (empty($product)) {
  219. $this->error('产品不存在或已被删除');
  220. }
  221. // 6. 优化图片路径处理
  222. // 产品图片
  223. if (!empty($product['product_img'])) {
  224. $product['产品图片'] = ltrim($product['product_img'], '/');
  225. } else {
  226. $product['产品图片'] = ''; // 设置默认空值
  227. }
  228. // 产品效果图 - 修复变量名错误(原代码中使用了$v)
  229. if (!empty($product['product_new_img'])) {
  230. $product['产品效果图'] = ltrim($product['product_new_img'], '/');
  231. } else {
  232. $product['产品效果图'] = ''; // 设置默认空值
  233. }
  234. $newImg = \db('product_new_img')
  235. ->where('product_id', $productId)
  236. ->whereNull('deleteTime')
  237. ->column('img_address');
  238. $product['newImg'] = $newImg;
  239. // 7. 移除原始图片字段,保持返回数据整洁
  240. unset($product['product_img'], $product['product_new_img']);
  241. // 8. 返回成功结果
  242. $this->success('获取产品详情成功', $product);
  243. }
  244. /**
  245. * 获取单条产品数据信息
  246. */
  247. public function GetProductFind(){
  248. if (!$this->request->isGet()) {
  249. $this->error('只支持GET请求');
  250. }
  251. $param = $this->request->param();
  252. if (empty($param['id']) || !is_numeric($param['id'])) {
  253. $this->error('产品ID参数错误');
  254. }
  255. $product = Db::name('product')->where('id', $param['id'])->find();
  256. if (empty($product)) {
  257. return json([
  258. 'code' => 1,
  259. 'msg' => '产品不存在',
  260. 'data' => null
  261. ]);
  262. }
  263. $this->success('获取成功', $product);
  264. }
  265. /**
  266. * 新增产品
  267. * @return void
  268. */
  269. public function productAdd()
  270. {
  271. // 1. 请求方法验证(可提取为公共方法)
  272. if (!$this->request->isPost()) {
  273. throw new \Exception('非法请求');
  274. }
  275. // 2. 获取并验证参数
  276. $param = $this->request->post();
  277. $this->validateProductParams($param, ['product_name', 'product_code']);
  278. // 3. 准备数据(使用助手函数处理时间)
  279. $data = [
  280. 'product_name' => $param['product_name'],
  281. 'product_code' => $param['product_code'],
  282. 'createTime' => date('Y-m-d H:i:s', time()),
  283. 'create_name' => isset($param['create_name']) ? $param['create_name'] : '',
  284. 'merchant_id' => isset($param['merchant_id']) ? intval($param['merchant_id']) : 0,
  285. 'product_img' => isset($param['product_img']) ? $param['product_img'] : '',
  286. ];
  287. // 4. 验证产品编码唯一性
  288. if (Db::name('product')->where('product_code', $data['product_code'])->count() > 0) {
  289. $this->error('产品编码已存在');
  290. }
  291. // 5. 使用事务确保数据一致性
  292. $result = Db::name('product')->insert($data);
  293. if ($result) {
  294. $this->success('新增成功');
  295. } else {
  296. $this->error('新增失败');
  297. }
  298. }
  299. /**
  300. * 修改产品数据
  301. * @return void
  302. */
  303. public function productEdit()
  304. {
  305. // 1. 请求方法验证
  306. if (!$this->request->isPost()) {
  307. throw new \Exception('非法请求');
  308. }
  309. // 2. 获取并验证参数
  310. $param = $this->request->post();
  311. $this->validateProductParams($param, ['id', 'product_name', 'product_code']);
  312. // 3. 检查产品是否存在
  313. $product = Db::name('product')->where('id', intval($param['id']))->find();
  314. if (!$product) {
  315. $this->error('产品不存在');
  316. }
  317. // 4. 准备更新数据
  318. $updateData = [
  319. 'product_name' => $param['product_name'],
  320. 'product_code' => $param['product_code'],
  321. ];
  322. // 5. 可选字段更新
  323. $optionalFields = ['create_name', 'merchant_id', 'product_img', 'product_new_img'];
  324. foreach ($optionalFields as $field) {
  325. if (isset($param[$field])) {
  326. $updateData[$field] = $param[$field];
  327. }
  328. }
  329. // 6. 验证产品编码唯一性(排除自身)
  330. $codeExists = Db::name('product')
  331. ->where('product_code', $updateData['product_code'])
  332. ->where('id', '<>', intval($param['id']))
  333. ->count();
  334. if ($codeExists > 0) {
  335. $this->error('产品编码已被其他产品使用');
  336. }
  337. // 7. 使用事务更新
  338. $result = Db::name('product')
  339. ->where('id', intval($param['id']))
  340. ->update($updateData);
  341. if ($result !== false) {
  342. $this->success('更新成功');
  343. } else {
  344. $this->error('更新失败');
  345. }
  346. }
  347. /**
  348. * 验证产品参数(私有方法,供内部使用)
  349. * @param array $param 参数数组
  350. * @param array $requiredFields 必要字段
  351. * @throws \Exception
  352. */
  353. private function validateProductParams($param, $requiredFields)
  354. {
  355. foreach ($requiredFields as $field) {
  356. if (!isset($param[$field]) || trim($param[$field]) === '') {
  357. throw new \Exception("缺少必要参数:{$field}");
  358. }
  359. }
  360. }
  361. /**
  362. * 获取商户ID
  363. * @return void
  364. */
  365. public function getMerchantId()
  366. {
  367. if (!$this->request->isGet()) {
  368. $this->error('请求错误');
  369. }
  370. $param = $this->request->param();
  371. if (empty($param['merchant_code'])) {
  372. $this->error('参数错误');
  373. }
  374. $id = \db('product_merchant')
  375. ->where('merchant_code', $param['merchant_code'])
  376. ->value('id');
  377. if (!empty($id)) {
  378. $this->success('成功', $id);
  379. }else{
  380. $this->error('失败');
  381. }
  382. }
  383. }