SystemOutAccount.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\outapi\validate\StoreOutAccountValidate;
  14. use app\services\out\OutAccountServices;
  15. use app\services\out\OutInterfaceServices;
  16. use think\facade\App;
  17. /**
  18. * 对外接口账户
  19. * Class SystemOutAccount
  20. * @package app\adminapi\controller\v1\setting
  21. */
  22. class SystemOutAccount extends AuthController
  23. {
  24. /**
  25. * 构造方法
  26. * SystemOut constructor.
  27. * @param App $app
  28. * @param OutAccountServices $services
  29. */
  30. public function __construct(App $app, OutAccountServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. }
  35. /**
  36. * 账号信息
  37. * @return string
  38. * @throws \Exception
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['name', '', ''],
  44. ['status', ''],
  45. ]);
  46. return app('json')->success($this->services->getList($where));
  47. }
  48. /**
  49. * 修改状态
  50. * @param string $status
  51. * @param string $id
  52. * @return mixed
  53. */
  54. public function set_status($id = '', $status = '')
  55. {
  56. if ($status == '' || $id == '') return app('json')->fail(100100);
  57. $this->services->update($id, ['status' => $status]);
  58. return app('json')->success($status == 1 ? 100012 : 100013);
  59. }
  60. /**
  61. * 删除
  62. * @param $id
  63. * @return mixed
  64. */
  65. public function delete($id)
  66. {
  67. if ($id == '') return app('json')->fail(100100);
  68. $this->services->update($id, ['is_del' => 1]);
  69. return app('json')->success(100002);
  70. }
  71. /**
  72. * 保存
  73. * @return mixed
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function save()
  79. {
  80. $data = $this->request->postMore([
  81. [['appid', 's'], ''],
  82. [['appsecret', 's'], ''],
  83. [['title', 's'], ''],
  84. ['rules', []],
  85. ]);
  86. $this->validate($data, StoreOutAccountValidate::class, 'save');
  87. if ($this->services->getOne(['appid' => $data['appid']])) return app('json')->fail('账号重复');
  88. $data['apppwd'] = $data['appsecret'];
  89. $data['appsecret'] = password_hash($data['appsecret'], PASSWORD_DEFAULT);
  90. $data['add_time'] = time();
  91. $data['rules'] = implode(',', $data['rules']);
  92. if (!$this->services->save($data)) {
  93. return app('json')->fail(100006);
  94. } else {
  95. return app('json')->success(100000);
  96. }
  97. }
  98. /**
  99. * 修改
  100. * @param string $id
  101. * @return mixed
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function update($id = '')
  107. {
  108. $data = $this->request->postMore([
  109. [['appsecret', 's'], ''],
  110. [['title', 's'], ''],
  111. ['rules', []],
  112. ]);
  113. $this->validate($data, StoreOutAccountValidate::class, 'update');
  114. if (!$this->services->getOne(['id' => $id])) return app('json')->fail('没有此账号');
  115. $data['apppwd'] = $data['appsecret'];
  116. $data['appsecret'] = password_hash($data['appsecret'], PASSWORD_DEFAULT);
  117. $data['rules'] = implode(',', $data['rules']);
  118. $res = $this->services->update($id, $data);
  119. if (!$res) {
  120. return app('json')->fail(100006);
  121. } else {
  122. return app('json')->success(100000);
  123. }
  124. }
  125. /**
  126. * 设置账号推送接口
  127. * @param $id
  128. * @return mixed
  129. */
  130. public function outSetUpSave($id)
  131. {
  132. $data = $this->request->postMore([
  133. ['push_open', 0],
  134. ['push_account', ''],
  135. ['push_password', ''],
  136. ['push_token_url', ''],
  137. ['user_update_push', ''],
  138. ['order_create_push', ''],
  139. ['order_pay_push', ''],
  140. ['refund_create_push', ''],
  141. ['refund_cancel_push', ''],
  142. ]);
  143. $this->services->outSetUpSave($id, $data);
  144. return app('json')->success(100000);
  145. }
  146. /**
  147. * 对外接口列表
  148. * @param OutInterfaceServices $service
  149. * @return mixed
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function outInterfaceList(OutInterfaceServices $service)
  155. {
  156. return app('json')->success($service->outInterfaceList());
  157. }
  158. /**
  159. * 保存接口文档
  160. * @param $id
  161. * @param OutInterfaceServices $service
  162. * @return mixed
  163. */
  164. public function saveInterface($id, OutInterfaceServices $service)
  165. {
  166. $data = $this->request->postMore([
  167. ['pid', 0], //上级id
  168. ['type', 0], //类型 0菜单 1接口
  169. ['name', ''], //名称
  170. ['describe', ''], //说明
  171. ['method', ''], //方法
  172. ['url', ''], //链接地址
  173. ['request_params', []], //请求参数
  174. ['return_params', []], //返回参数
  175. ['request_example', ''], //请求示例
  176. ['return_example', ''], //返回示例
  177. ['error_code', []] //错误码
  178. ]);
  179. $service->saveInterface((int)$id, $data);
  180. return app('json')->success(100000);
  181. }
  182. /**
  183. * 对外接口文档
  184. * @param $id
  185. * @param OutInterfaceServices $service
  186. * @return mixed
  187. * @throws \think\db\exception\DataNotFoundException
  188. * @throws \think\db\exception\DbException
  189. * @throws \think\db\exception\ModelNotFoundException
  190. */
  191. public function interfaceInfo($id, OutInterfaceServices $service)
  192. {
  193. return app('json')->success($service->interfaceInfo($id));
  194. }
  195. /**
  196. * 修改接口名称
  197. * @param OutInterfaceServices $service
  198. * @return mixed
  199. */
  200. public function editInterfaceName(OutInterfaceServices $service)
  201. {
  202. $data = $this->request->postMore([
  203. ['id', 0], //上级id
  204. ['name', ''], //名称
  205. ]);
  206. if (!$data['id'] || !$data['name']) {
  207. return app('json')->success(100100);
  208. }
  209. $service->editInterfaceName($data);
  210. return app('json')->success(100001);
  211. }
  212. /**
  213. * 删除接口
  214. * @param $id
  215. * @param OutInterfaceServices $service
  216. * @return mixed
  217. */
  218. public function delInterface($id, OutInterfaceServices $service)
  219. {
  220. if (!$id) return app('json')->success(100100);
  221. $service->delInterface($id);
  222. return app('json')->success(100002);
  223. }
  224. /**
  225. * 测试获取token接口
  226. * @return mixed
  227. * @throws \think\db\exception\DataNotFoundException
  228. * @throws \think\db\exception\DbException
  229. * @throws \think\db\exception\ModelNotFoundException
  230. */
  231. public function textOutUrl()
  232. {
  233. $data = $this->request->postMore([
  234. ['push_account', 0],
  235. ['push_password', 0],
  236. ['push_token_url', '']
  237. ]);
  238. return app('json')->success('100014', $this->services->textOutUrl($data));
  239. }
  240. }