ThirdScaleFacadeRepository.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Repositories\Eloquent\BaseRepository;
  4. use GuzzleHttp\Client as httpClient;
  5. use App\Repositories\Contracts\ThirdScaleInterface;
  6. /**
  7. *
  8. * 用户做远程量表步骤
  9. * 1:通过统一微信授权拿到微信用户openid
  10. * 2: 创建用户(customerCreate)得到远程的系统中的openid
  11. * 3: 创建远程量表实例(createPush)
  12. * 4: 远程登陆获取测评页面(appLogin)
  13. *
  14. */
  15. class ThirdScaleFacadeRepository extends BaseRepository implements ThirdScaleInterface
  16. {
  17. public function sign(string $op, string $task, string $version, array $parameter)
  18. {
  19. $appid = config('console.thirdScale.appid');
  20. $key = config('console.thirdScale.key');
  21. ksort($parameter);
  22. $pStr = '';
  23. foreach ($parameter as $v) {
  24. $pStr .= $v;
  25. }
  26. $str = $appid . $op . $task . $version . $pStr . $key;
  27. return md5($str);
  28. }
  29. public function fixedParameter($op, $task, $parameter, $version = '1.0.0')
  30. {
  31. $fixedParameter = [
  32. 'op' => $op,
  33. 'task' => $task,
  34. 'version' => $version,
  35. 'appid' => config('console.thirdScale.appid')
  36. ];
  37. //密钥
  38. $sign = $this->sign($fixedParameter['op'], $fixedParameter['task'], $fixedParameter['version'], $parameter);
  39. //加入
  40. $fixedParameter['sign'] = $sign;
  41. return $fixedParameter;
  42. }
  43. public function getScale(int $start, int $count, string $keyword)
  44. {
  45. //请求参数
  46. $parameter = ['start'=>$start, 'count'=>$count, 'keyword'=>$keyword];
  47. //固定参数
  48. $fixedParameter = $this->fixedParameter('module', 'moduleList', $parameter);
  49. //请求地址
  50. $url = config('console.thirdScale.url');
  51. try {
  52. $parameter = array_merge($fixedParameter, $parameter);
  53. $client = new httpClient();
  54. $res = $client->request('post', $url, ['form_params'=>$parameter]);
  55. $body = $res->getBody();
  56. $data = json_decode($body);
  57. return $this->response($data);
  58. } catch (\Exception $e) {
  59. $this->setLog(self::TYPENAME.'getAccessToken 远程请求失败', $this->startTime, [$e->getMessage()]);
  60. $this->error()->fail();
  61. }
  62. }
  63. public function createPush(string $openid, string $mid)
  64. {
  65. //请求参数
  66. $parameter = ['open_id'=>$openid, 'mid'=>$mid];
  67. //固定参数
  68. $fixedParameter = $this->fixedParameter('module', 'createPush', $parameter);
  69. //请求地址
  70. $url = config('console.thirdScale.url');
  71. try {
  72. $parameter = array_merge($fixedParameter, $parameter);
  73. $client = new httpClient();
  74. $res = $client->request('post', $url, ['form_params'=>$parameter]);
  75. $body = $res->getBody();
  76. $data = json_decode($body);
  77. $this->setLog(self::TYPENAME.'createPush 远程请求结果', $this->startTime, [$parameter]);
  78. $this->setLog(self::TYPENAME.'createPush 远程请求结果', $this->startTime, [$url]);
  79. $this->setLog(self::TYPENAME.'createPush 远程请求结果', $this->startTime, [$data]);
  80. return $data->data->push_id;
  81. // return $this->response($data);
  82. } catch (\Exception $e) {
  83. $this->setLog(self::TYPENAME.'createPush 远程请求失败', $this->startTime, [$e->getMessage()]);
  84. $this->error()->fail();
  85. }
  86. }
  87. public function customerCreate(string $name, string $openId = null, int $sex = 1)
  88. {
  89. //请求参数
  90. // $parameter = ['name'=>$openId];
  91. $parameter = [
  92. 'name'=>$name,
  93. 'wechat_uid'=>$openId,
  94. ];
  95. //固定参数
  96. $fixedParameter = $this->fixedParameter('customer', 'customerCreate', $parameter);
  97. //请求地址
  98. $url = config('console.thirdScale.url');
  99. try {
  100. $parameter = array_merge($fixedParameter, $parameter);
  101. $client = new httpClient();
  102. $res = $client->request('post', $url, ['form_params'=>$parameter]);
  103. $body = $res->getBody();
  104. $data = json_decode($body);
  105. self::setLog(self::TYPENAME.'customerCreate 远程返回结果', $this->startTime, [$data]);
  106. // echo json_encode($url);
  107. // echo json_encode($parameter);
  108. // echo "<br />";
  109. // echo json_encode($data); exit;
  110. return $data->data->open_id;
  111. // return $this->response($data);
  112. } catch (\Exception $e) {
  113. $this->setLog(self::TYPENAME.'customerCreate 远程请求失败', $this->startTime, [$e->getMessage()]);
  114. $this->error()->fail($e->getMessage());
  115. }
  116. }
  117. public function appLogin(string $thirdOpenId, string $pushId, string $reportId)
  118. {
  119. //请求参数
  120. if ($pushId){
  121. //$parameter = ['appid'=>config('console.thirdScale.appid'), 'push_id'=>$pushId, 'open_id'=>$thirdOpenId, 'version'=>'1.0.0'];
  122. $parameter = ['appid'=>config('console.thirdScale.appid'), 'push_id'=>$pushId, 'open_id'=>$thirdOpenId, 'version'=>'1.1.0', 'is_report'=>'1', 's_timestamp'=>time()];
  123. }
  124. if ($reportId){
  125. $parameter = ['report_id'=>$reportId, 'open_id'=>$thirdOpenId];
  126. }
  127. //$parameter['sign'] = $this->sign('core', 'appLogin', '1.0.0', ['push_id'=>$pushId, 'open_id'=>$thirdOpenId]);
  128. $parameter['sign'] = $this->sign('core', 'appLogin', '1.1.0', ['push_id'=>$pushId, 'open_id'=>$thirdOpenId, 'is_report'=>'1', 's_timestamp'=>time()]);
  129. $str = '';
  130. foreach ($parameter as $key=>$v){
  131. $str.=$key.'='.urlencode($v).'&';
  132. }
  133. //请求地址
  134. $url = config('console.thirdScale.do_url');
  135. return $url.'&'.$str;
  136. }
  137. }