OpenApi.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\system\openapi;
  12. use app\common\repositories\openapi\OpenAuthRepository;
  13. use app\validate\merchant\OpenAuthValidate;
  14. use crmeb\basic\BaseController;
  15. use think\App;
  16. class OpenApi extends BaseController
  17. {
  18. protected $repository;
  19. /**
  20. * 构造函数
  21. *
  22. * @param App $app 应用实例
  23. * @param OpenAuthRepository $repository 开放认证仓库实例
  24. */
  25. public function __construct(App $app, OpenAuthRepository $repository)
  26. {
  27. // 调用父类构造函数
  28. parent::__construct($app);
  29. // 初始化仓库实例
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * 获取列表数据
  34. *
  35. * @return \think\response\Json
  36. */
  37. public function lst()
  38. {
  39. // 获取分页参数
  40. [$page, $limit] = $this->getPage();
  41. $where = $this->request->params(['title', 'access_key','status']);
  42. // 添加商家ID条件
  43. $where['mer_id'] = $this->request->merId();
  44. // 获取数据列表
  45. $data = $this->repository->getList($where, $page, $limit);
  46. // 返回成功响应
  47. return app('json')->success($data);
  48. }
  49. /**
  50. * 创建表单数据
  51. *
  52. * @return \think\response\Json
  53. */
  54. public function createForm()
  55. {
  56. // 获取表单数据并转换为数组
  57. return app('json')->success(formToData($this->repository->form($this->request->merId())));
  58. }
  59. /**
  60. * 创建数据
  61. *
  62. * @return \Illuminate\Http\JsonResponse
  63. */
  64. public function create()
  65. {
  66. // 检查参数
  67. $data = $this->checkParams();
  68. $this->repository->create($this->request->merId(), $data);
  69. // 返回成功信息
  70. return app('json')->success('添加成功');
  71. }
  72. /**
  73. * 获取表单数据
  74. *
  75. * @param int $id 数据 ID
  76. * @return \Illuminate\Http\JsonResponse
  77. */
  78. public function updateForm($id)
  79. {
  80. return app('json')->success(formToData($this->repository->form($this->request->merId(), $id)));
  81. }
  82. /**
  83. * 更新数据
  84. *
  85. * @param int $id 数据 ID
  86. * @return \Illuminate\Http\JsonResponse
  87. */
  88. public function update($id)
  89. {
  90. // 检查参数
  91. $data = $this->checkParams();
  92. $data['update_time'] = date('Y-m-d H:i:s', time());
  93. $data['auth'] = implode(',', $data['auth']);
  94. $this->repository->update($id, $data);
  95. return app('json')->success('编辑成功');
  96. }
  97. /**
  98. * 根据ID切换状态
  99. * @param int $id ID
  100. * @return \think\response\Json 返回JSON格式的修改结果
  101. */
  102. public function switchWithStatus($id)
  103. {
  104. // 获取状态参数,默认为0
  105. $status = $this->request->param('status', 0) == 1 ?: 0;
  106. // 调用repository的update方法更新状态
  107. $this->repository->update($id, ['status' => $status]);
  108. // 返回JSON格式的修改成功结果
  109. return app('json')->success('修改成功');
  110. }
  111. /**
  112. * 根据ID删除记录
  113. * @param int $id ID
  114. * @return \think\response\Json 返回JSON格式的删除结果
  115. */
  116. public function delete($id)
  117. {
  118. // 调用repository的update方法更新is_del和delete_time字段
  119. $this->repository->update($id, ['is_del' => 1, 'delete_time' => date('Y-m-d H:i:s', time())]);
  120. // 返回JSON格式的删除成功结果
  121. return app('json')->success('删除成功');
  122. }
  123. /**
  124. * 根据ID获取密钥
  125. * @param int $id ID
  126. * @return \think\response\Json 返回JSON格式的密钥数据
  127. */
  128. public function getSecretKey($id)
  129. {
  130. // 调用repository的getSecretKey方法获取密钥数据
  131. $data = $this->repository->getSecretKey($id);
  132. // 返回JSON格式的密钥数据
  133. return app('json')->success($data);
  134. }
  135. /**
  136. * 设置密钥
  137. *
  138. * @param int $id 密钥ID
  139. * @return \Illuminate\Http\JsonResponse 返回JSON格式的数据
  140. */
  141. public function setSecretKey($id)
  142. {
  143. // 调用 repository 类的 setSecretKey 方法,传入 ID 和 merId,获取数据并存储到 data 变量中
  144. $data = $this->repository->setSecretKey($id, $this->request->merId());
  145. return app('json')->success('重置成功', $data);
  146. }
  147. /**
  148. * 检查参数
  149. *
  150. * @return array 返回检查后的参数数组
  151. */
  152. public function checkParams()
  153. {
  154. $data = $this->request->params(['title', 'status', 'mark', 'auth', 'sort']);
  155. // 实例化 OpenAuthValidate 类,并调用其 check 方法,传入 $data 作为参数,检查参数是否合法
  156. app()->make(OpenAuthValidate::class)->check($data);
  157. // 返回检查后的参数数组
  158. return $data;
  159. }
  160. }