CdkeyLibrary.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant\store\product;
  12. use app\common\repositories\store\product\CdkeyLibraryRepository;
  13. use crmeb\basic\BaseController;
  14. use crmeb\exceptions\UploadException;
  15. use think\App;
  16. use think\exception\ValidateException;
  17. /**
  18. * Class CdkeyLibrary
  19. * app\controller\merchant\store\product
  20. * 卡密库
  21. */
  22. class CdkeyLibrary extends BaseController
  23. {
  24. protected $repository ;
  25. /**
  26. * ProductGroup constructor.
  27. * @param App $app
  28. * @param CdkeyLibraryRepository $repository
  29. */
  30. public function __construct(App $app ,CdkeyLibraryRepository $repository)
  31. {
  32. parent::__construct($app);
  33. $this->repository = $repository;
  34. }
  35. /**
  36. * 列表
  37. * @return \think\response\Json
  38. * @author Qinii
  39. */
  40. public function lst()
  41. {
  42. [$page, $limit] = $this->getPage();
  43. $where = $this->request->params(['keyword','status','date','productName','name']);
  44. $where['mer_id'] = $this->request->merId();
  45. $data = $this->repository->getList($where,$page,$limit);
  46. return app('json')->success($data);
  47. }
  48. /**
  49. * 创建表单
  50. *
  51. * @return \Illuminate\Http\JsonResponse
  52. */
  53. public function createForm()
  54. {
  55. // 调用 formToData 方法将表单转换为数据并返回 JSON 格式的响应
  56. return app('json')->success(formToData($this->repository->form()));
  57. }
  58. /**
  59. * 添加表单
  60. * @return \think\response\Json
  61. * @author Qinii
  62. */
  63. public function create()
  64. {
  65. $data = $this->checkParams();
  66. $this->repository->create($data);
  67. return app('json')->success('添加成功');
  68. }
  69. /**
  70. * 修改表单
  71. * @param $id
  72. * @return \think\response\Json
  73. * @author Qinii
  74. */
  75. public function updateForm($id)
  76. {
  77. $data = $this->repository->merHas($this->request->merId(),$id, null);
  78. if (!$data) return app('json')->fail('数据不存在');
  79. return app('json')->success(formToData($this->repository->form($id)));
  80. }
  81. /**
  82. * 修改
  83. * @param $id
  84. * @return \think\response\Json
  85. * @author Qinii
  86. */
  87. public function update($id)
  88. {
  89. $data = $this->repository->merHas($this->request->merId(),$id, null);
  90. if (!$data) return app('json')->fail('数据不存在');
  91. $data = $this->checkParams();
  92. $this->repository->update($id,$data);
  93. return app('json')->success('修改成功');
  94. }
  95. /**
  96. * 删除
  97. * @param $id
  98. * @return \think\response\Json
  99. * @author Qinii
  100. */
  101. public function delete($id)
  102. {
  103. $this->repository->destory($id, $this->request->merId());
  104. return app('json')->success('删除成功');
  105. }
  106. /**
  107. * 验证
  108. * @param $id
  109. * @return \think\response\Json
  110. * @author Qinii
  111. */
  112. public function checkParams()
  113. {
  114. $data = $this->request->params(['name','remark']);
  115. if (empty($data['name'])) {
  116. throw new ValidateException('请输入名称');
  117. }
  118. $data['mer_id'] = $this->request->merId();
  119. return $data;
  120. }
  121. /**
  122. * 获取选项列表
  123. *
  124. * @return \think\response\Json
  125. */
  126. public function options()
  127. {
  128. // 调用 repository 类的 getOptions 方法获取选项列表,并通过 json 组件返回成功状态和数据
  129. return app('json')->success($this->repository->getOptions($this->request->merId()));
  130. }
  131. /**
  132. * 下载快递模板文件
  133. *
  134. * @return \think\response\Json
  135. */
  136. public function downloadExpress()
  137. {
  138. try {
  139. // 设置文件名和路径
  140. $file['name'] = 'express';
  141. $path = app()->getRootPath() . 'extend/express.xlsx';
  142. // 判断文件是否存在,若不存在则返回失败状态
  143. if (!$file || !file_exists($path)) return app('json')->fail('文件不存在');
  144. // 调用 download 函数下载文件,并返回成功状态
  145. return download($path, $file['name']);
  146. } catch (UploadException $e) {
  147. // 捕获上传异常,返回失败状态
  148. return app('json')->fail('下载失败');
  149. }
  150. }
  151. }