Product.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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('a.deleteTime', null)
  79. ->where($where)
  80. ->count();
  81. // 分页查询
  82. $list = \db('product')
  83. ->alias('a')
  84. ->join('product_merchant b', 'a.merchant_id = b.id')
  85. ->where($where)
  86. ->field([
  87. 'a.product_name as 产品名称',
  88. 'a.product_code as 产品编码',
  89. 'a.product_img',
  90. 'a.deleteTime',
  91. 'a.product_new_img',
  92. 'a.createTime as 创建时间',
  93. 'a.create_name as 创建人',
  94. 'b.merchant_code as 商户编码',
  95. 'a.id'
  96. ])
  97. ->where('a.deleteTime', null)
  98. ->order('a.createTime', 'desc')
  99. ->page($page, $pageSize)
  100. ->select();
  101. } catch (\Exception $e) {
  102. $this->error('查询数据失败:' . $e->getMessage());
  103. }
  104. // 7. 优化数据处理逻辑
  105. if (!empty($list)) {
  106. foreach ($list as &$item) {
  107. // 产品图片
  108. if (!empty($item['product_img'])) {
  109. $item['产品图片'] = ltrim($item['product_img'], '/');
  110. unset($item['product_img']); // 移除原始字段
  111. } else {
  112. $item['产品图片'] = ''; // 设置默认值
  113. }
  114. // 产品效果图
  115. if (!empty($item['product_new_img'])) {
  116. $item['产品效果图'] = ltrim($item['product_new_img'], '/');
  117. unset($item['product_new_img']); // 移除原始字段
  118. } else {
  119. $item['产品效果图'] = ''; // 设置默认值
  120. }
  121. }
  122. unset($item); // 解除引用
  123. }
  124. $result = [
  125. 'list' => $list,
  126. 'total' => $total,
  127. ];
  128. // 10. 统一返回格式
  129. $this->success('查询成功', $result);
  130. }
  131. /**
  132. * 产品详情
  133. * @return void
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. * @throws \think\exception\DbException
  137. */
  138. public function productDetail()
  139. {
  140. // 1. 请求方法验证
  141. if (!$this->request->isGet()) {
  142. $this->error('只支持GET请求');
  143. }
  144. // 2. 参数获取与验证
  145. $param = $this->request->param();
  146. if (empty($param['id']) || !is_numeric($param['id'])) {
  147. $this->error('产品ID参数错误');
  148. }
  149. // 3. 参数安全处理
  150. $productId = intval($param['id']);
  151. if ($productId <= 0) {
  152. $this->error('产品ID必须为正整数');
  153. }
  154. // 4. 查询数据
  155. try {
  156. $product = \db('product')
  157. ->field([
  158. 'id',
  159. 'product_name as 产品名称',
  160. 'product_code as 产品编码',
  161. 'product_img',
  162. 'product_new_img',
  163. 'createTime as 创建时间',
  164. 'create_name as 创建人',
  165. ])
  166. ->where('id', $productId)
  167. ->whereNull('deleteTime')
  168. ->find();
  169. } catch (\Exception $e) {
  170. $this->error('查询产品详情失败:' . $e->getMessage());
  171. }
  172. // 5. 检查查询结果
  173. if (empty($product)) {
  174. $this->error('产品不存在或已被删除');
  175. }
  176. // 6. 优化图片路径处理
  177. // 产品图片
  178. if (!empty($product['product_img'])) {
  179. $product['产品图片'] = ltrim($product['product_img']);
  180. } else {
  181. $product['产品图片'] = ''; // 设置默认空值
  182. }
  183. // 产品效果图 - 修复变量名错误(原代码中使用了$v)
  184. if (!empty($product['product_new_img'])) {
  185. $product['产品效果图'] = ltrim($product['product_new_img']);
  186. } else {
  187. $product['产品效果图'] = ''; // 设置默认空值
  188. }
  189. $newImg = \db('product_new_img')
  190. ->where('product_id', $productId)
  191. ->whereNull('deleteTime')
  192. ->column('img_address');
  193. $product['newImg'] = $newImg;
  194. // 7. 移除原始图片字段,保持返回数据整洁
  195. unset($product['product_img'], $product['product_new_img']);
  196. $product_image = \db('product_image')
  197. ->where('product_id', $productId)
  198. ->whereNull('mod_rq')
  199. ->order('id desc')
  200. ->select();
  201. return json([
  202. 'code' => 0,
  203. 'msg' => '获取产品详情成功',
  204. 'image' => $product_image,//历史图片
  205. 'data' => $product
  206. ]);
  207. }
  208. /**
  209. * 获取单条产品数据信息
  210. */
  211. public function GetProductFind(){
  212. if (!$this->request->isGet()) {
  213. $this->error('只支持GET请求');
  214. }
  215. $param = $this->request->param();
  216. if (empty($param['id']) || !is_numeric($param['id'])) {
  217. $this->error('产品ID参数错误');
  218. }
  219. $product = Db::name('product')->where('id', $param['id'])->find();
  220. if (empty($product)) {
  221. return json([
  222. 'code' => 1,
  223. 'msg' => '产品不存在',
  224. 'data' => null
  225. ]);
  226. }
  227. $this->success('获取成功', $product);
  228. }
  229. /**
  230. * 新增产品
  231. * @return void
  232. */
  233. public function productAdd()
  234. {
  235. // 请求方法验证(可提取为公共方法)
  236. if (!$this->request->isPost()) {
  237. throw new \Exception('非法请求');
  238. }
  239. // 获取并验证参数
  240. $param = $this->request->post();
  241. $this->validateProductParams($param, ['product_name', 'product_code']);
  242. //处理产品图片 base64 -> 保存到 uploads/merchant/{product_code前段}/{product_code}/{product_name}.png
  243. $productImgPath = '';
  244. if (!empty($param['product_img'])) {
  245. $base64Data = $param['product_img'];
  246. if (preg_match('/data:image\/(png|jpg|jpeg);base64,([A-Za-z0-9+\/=]+)/i', $base64Data, $m)) {
  247. $imageType = strtolower($m[1]);
  248. $imageData = base64_decode($m[2]);
  249. if ($imageData !== false && strlen($imageData) >= 100) {
  250. $productCode = $param['product_code'];
  251. $prefix = strlen($productCode) >= 4 ? substr($productCode, 0, -4) : $productCode; // 后四位前面的数值
  252. $safeName = preg_replace('/[\\\\\/:*?"<>|]/u', '_', $param['product_name']);
  253. $ext = ($imageType === 'jpeg') ? 'jpg' : $imageType;
  254. $fileName = $safeName . '.' . $ext;
  255. $saveDir = str_replace('\\', '/', ROOT_PATH . 'public/uploads/merchant/' . $prefix . '/' . $productCode . '/'. 'oldimg/');
  256. if (!is_dir($saveDir)) {
  257. mkdir($saveDir, 0755, true);
  258. }
  259. if (file_put_contents($saveDir . $fileName, $imageData)) {
  260. $productImgPath = 'uploads/merchant/' . $prefix . '/' . $productCode . '/' . 'oldimg/'. $fileName;
  261. }
  262. }
  263. }
  264. }
  265. $data = [
  266. 'product_name' => $param['product_name'],
  267. 'product_code' => $param['product_code'],
  268. 'createTime' => date('Y-m-d H:i:s', time()),
  269. 'create_name' => isset($param['create_name']) ? $param['create_name'] : '',
  270. 'merchant_id' => isset($param['merchant_id']) ? intval($param['merchant_id']) : 0,
  271. 'product_img' => $productImgPath,
  272. ];
  273. //验证产品编码唯一性
  274. if (Db::name('product')->where('product_code', $data['product_code'])->count() > 0) {
  275. $this->error('产品编码已存在');
  276. }
  277. $result = Db::name('product')->insert($data);
  278. if ($result) {
  279. $this->success('新增成功');
  280. } else {
  281. $this->error('新增失败');
  282. }
  283. }
  284. /**
  285. * 修改产品数据
  286. * @return void
  287. */
  288. public function productEdit()
  289. {
  290. // 1. 请求方法验证
  291. if (!$this->request->isPost()) {
  292. throw new \Exception('非法请求');
  293. }
  294. // 2. 获取并验证参数
  295. $param = $this->request->post();
  296. $this->validateProductParams($param, ['id', 'product_name', 'product_code']);
  297. // 3. 检查产品是否存在
  298. $product = Db::name('product')->where('id', intval($param['id']))->find();
  299. if (!$product) {
  300. $this->error('产品不存在');
  301. }
  302. // 4. 准备更新数据
  303. $updateData = [
  304. 'product_name' => $param['product_name'],
  305. 'product_code' => $param['product_code'],
  306. ];
  307. // 5. 可选字段更新
  308. $optionalFields = ['create_name', 'merchant_id', 'product_img', 'product_new_img'];
  309. foreach ($optionalFields as $field) {
  310. if (isset($param[$field])) {
  311. $updateData[$field] = $param[$field];
  312. }
  313. }
  314. // 6. 验证产品编码唯一性(排除自身)
  315. $codeExists = Db::name('product')
  316. ->where('product_code', $updateData['product_code'])
  317. ->where('id', '<>', intval($param['id']))
  318. ->count();
  319. if ($codeExists > 0) {
  320. $this->error('产品编码已被其他产品使用');
  321. }
  322. // 7. 使用事务更新
  323. $result = Db::name('product')
  324. ->where('id', intval($param['id']))
  325. ->update($updateData);
  326. if ($result !== false) {
  327. $this->success('更新成功');
  328. } else {
  329. $this->error('更新失败');
  330. }
  331. }
  332. /**
  333. * 验证产品参数(私有方法,供内部使用)
  334. * @param array $param 参数数组
  335. * @param array $requiredFields 必要字段
  336. * @throws \Exception
  337. */
  338. private function validateProductParams($param, $requiredFields)
  339. {
  340. foreach ($requiredFields as $field) {
  341. if (!isset($param[$field]) || trim($param[$field]) === '') {
  342. throw new \Exception("缺少必要参数:{$field}");
  343. }
  344. }
  345. }
  346. /**
  347. * 获取商户ID
  348. * @return void
  349. */
  350. public function getMerchantId()
  351. {
  352. if (!$this->request->isGet()) {
  353. $this->error('请求错误');
  354. }
  355. $param = $this->request->param();
  356. if (empty($param['merchant_code'])) {
  357. $this->error('参数错误');
  358. }
  359. $id = \db('product_merchant')
  360. ->where('merchant_code', $param['merchant_code'])
  361. ->value('id');
  362. if (!empty($id)) {
  363. $this->success('成功', $id);
  364. }else{
  365. $this->error('失败');
  366. }
  367. }
  368. /**
  369. * 产品删除(软删除,设置 deleteTime)
  370. */
  371. public function Product_Del()
  372. {
  373. $params = $this->request->param();
  374. $record['deleteTime'] = date('Y-m-d H:i:s');
  375. $res = Db::name('product')->where('id', $params['id'])->update($record);
  376. if (!$res) {
  377. return json([
  378. 'code' => 1,
  379. 'msg' => '删除失败',
  380. 'data' => ''
  381. ]);
  382. }
  383. return json([
  384. 'code' => 0,
  385. 'msg' => '删除成功'
  386. ]);
  387. }
  388. }