SystemOutAccount.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if (!$data['appsecret']) {
  89. unset($data['appsecret']);
  90. } else {
  91. $data['appsecret'] = password_hash($data['appsecret'], PASSWORD_DEFAULT);
  92. }
  93. $data['add_time'] = time();
  94. $data['rules'] = implode(',', $data['rules']);
  95. if (!$this->services->save($data)) {
  96. return app('json')->fail(100006);
  97. } else {
  98. return app('json')->success(100000);
  99. }
  100. }
  101. /**
  102. * 修改
  103. * @param string $id
  104. * @return mixed
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. public function update($id = '')
  110. {
  111. $data = $this->request->postMore([
  112. [['appsecret', 's'], ''],
  113. [['title', 's'], ''],
  114. ['rules', []],
  115. ]);
  116. $this->validate($data, StoreOutAccountValidate::class, 'update');
  117. if (!$data['appsecret']) {
  118. unset($data['appsecret']);
  119. } else {
  120. $data['appsecret'] = password_hash($data['appsecret'], PASSWORD_DEFAULT);
  121. }
  122. if (!$this->services->getOne(['id' => $id])) return app('json')->fail('没有此账号');
  123. $data['rules'] = implode(',', $data['rules']);
  124. $res = $this->services->update($id, $data);
  125. if (!$res) {
  126. return app('json')->fail(100006);
  127. } else {
  128. return app('json')->success(100000);
  129. }
  130. }
  131. /**
  132. * 设置账号推送接口
  133. * @param $id
  134. * @return mixed
  135. */
  136. public function outSetUpSave($id)
  137. {
  138. $data = $this->request->postMore([
  139. ['push_open', 0],
  140. ['push_account', ''],
  141. ['push_password', ''],
  142. ['push_token_url', ''],
  143. ['user_update_push', ''],
  144. ['order_create_push', ''],
  145. ['order_pay_push', ''],
  146. ['refund_create_push', ''],
  147. ['refund_cancel_push', ''],
  148. ]);
  149. $this->services->outSetUpSave($id, $data);
  150. return app('json')->success(100000);
  151. }
  152. /**
  153. * 对外接口列表
  154. * @param OutInterfaceServices $service
  155. * @return mixed
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\DbException
  158. * @throws \think\db\exception\ModelNotFoundException
  159. */
  160. public function outInterfaceList(OutInterfaceServices $service)
  161. {
  162. return app('json')->success($service->outInterfaceList());
  163. }
  164. /**
  165. * 保存接口文档
  166. * @param $id
  167. * @param OutInterfaceServices $service
  168. * @return mixed
  169. */
  170. public function saveInterface($id, OutInterfaceServices $service)
  171. {
  172. $data = $this->request->postMore([
  173. ['pid', 0], //上级id
  174. ['type', 0], //类型 0菜单 1接口
  175. ['name', ''], //名称
  176. ['describe', ''], //说明
  177. ['method', ''], //方法
  178. ['url', ''], //链接地址
  179. ['request_params', []], //请求参数
  180. ['return_params', []], //返回参数
  181. ['request_example', ''], //请求示例
  182. ['return_example', ''], //返回示例
  183. ['error_code', []] //错误码
  184. ]);
  185. $service->saveInterface((int)$id, $data);
  186. return app('json')->success(100000);
  187. }
  188. /**
  189. * 对外接口文档
  190. * @param $id
  191. * @param OutInterfaceServices $service
  192. * @return mixed
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\DbException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. */
  197. public function interfaceInfo($id, OutInterfaceServices $service)
  198. {
  199. return app('json')->success($service->interfaceInfo($id));
  200. }
  201. /**
  202. * 修改接口名称
  203. * @param OutInterfaceServices $service
  204. * @return mixed
  205. */
  206. public function editInterfaceName(OutInterfaceServices $service)
  207. {
  208. $data = $this->request->postMore([
  209. ['id', 0], //上级id
  210. ['name', ''], //名称
  211. ]);
  212. if (!$data['id'] || !$data['name']) {
  213. return app('json')->success(100100);
  214. }
  215. $service->editInterfaceName($data);
  216. return app('json')->success(100001);
  217. }
  218. /**
  219. * 删除接口
  220. * @param $id
  221. * @param OutInterfaceServices $service
  222. * @return mixed
  223. */
  224. public function delInterface($id, OutInterfaceServices $service)
  225. {
  226. if (!$id) return app('json')->success(100100);
  227. $service->delInterface($id);
  228. return app('json')->success(100002);
  229. }
  230. /**
  231. * 测试获取token接口
  232. * @return mixed
  233. * @throws \think\db\exception\DataNotFoundException
  234. * @throws \think\db\exception\DbException
  235. * @throws \think\db\exception\ModelNotFoundException
  236. */
  237. public function textOutUrl()
  238. {
  239. $data = $this->request->postMore([
  240. ['push_account', 0],
  241. ['push_password', 0],
  242. ['push_token_url', '']
  243. ]);
  244. return app('json')->success('100014',$this->services->textOutUrl($data));
  245. }
  246. }