CpService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * ${CARET}
  4. * @file:CpService.php
  5. * @Created by gnitif
  6. * @Date: 2023/3/21
  7. * @Time: 20:24
  8. */
  9. namespace Modules\ContentManage\Services\CpManage;
  10. use GuzzleHttp\Client;
  11. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. use Modules\ContentManage\Models\Book;
  15. use Modules\ContentManage\Models\Cp\Cps;
  16. use function Symfony\Component\Translation\t;
  17. class CpService
  18. {
  19. public static function reportCpUser($user)
  20. {
  21. $params = [
  22. 'username' => $user['cp_name'],
  23. 'is_enabled' => 1,
  24. 'is_show_total_amount' => $user['is_show_total_amount'] ?? 0,
  25. 'is_union_sign' => $user['is_union_sign'] ?? 0,
  26. 'timestamp' => time(),
  27. ];
  28. $params['sign'] = self::getSign($params['timestamp']);
  29. $url = config('contentManage.zhushuyunpublicapi.public_domain', 'https://pubapi.zhuishuyun.com') . '/inapi/cpUers/createUser';
  30. $client = new Client(['base_uri' => $url,'timeout' => 2]);
  31. $response = $client->request('post','',['query' => $params]);
  32. $result = $response->getBody()->getContents();
  33. $result = json_decode($result, true);
  34. if (!isset($result['code']) || $result['code'] != 0) {
  35. Log::error("cp创建用户接口请求异常,请求结果为:" . var_export($result, true));
  36. throw new \Exception("cp创建用户接口请求异常");
  37. }
  38. return true;
  39. }
  40. private static function getSign(mixed $timestamp)
  41. {
  42. $key = config('contentManage.zhushuyunpublicapi.external_private_key', 'ZSY_2021!KEY');
  43. $string = md5($timestamp . $key) . $key;
  44. return md5($string);
  45. }
  46. public static function selectOptions($cpName = "")
  47. {
  48. $list = Cps::select('cp_id','cp_name','cp_nick','cp_company');
  49. if (!empty($cpName)){
  50. $list->where('cp_name','like',"%{$cpName}%");
  51. }
  52. return $list->get();
  53. }
  54. /**
  55. * 查询产品列表
  56. * name: getCpList
  57. * @param $where
  58. * @param int $pageSize
  59. * @return LengthAwarePaginator
  60. * date 2023/03/22 20:49
  61. */
  62. public static function getCpList($where , int $pageSize = 20)
  63. {
  64. $list = DB::table('cps')->where($where)->orderBy('cp_id','desc')->paginate($pageSize);
  65. if(!$list->isEmpty()){
  66. foreach ($list as $value){
  67. $value->book_count = Book::where('cp_id', $value->cp_id)->count();
  68. }
  69. }
  70. return $list;
  71. }
  72. }