123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\Repositories\Eloquent;
- use App\Repositories\Eloquent\BaseRepository;
- use GuzzleHttp\Client as httpClient;
- use App\Repositories\Contracts\ThirdScaleInterface;
- /**
- *
- * 用户做远程量表步骤
- * 1:通过统一微信授权拿到微信用户openid
- * 2: 创建用户(customerCreate)得到远程的系统中的openid
- * 3: 创建远程量表实例(createPush)
- * 4: 远程登陆获取测评页面(appLogin)
- *
- */
- class ThirdScaleFacadeRepository extends BaseRepository implements ThirdScaleInterface
- {
- public function sign(string $op, string $task, string $version, array $parameter)
- {
- $appid = config('console.thirdScale.appid');
- $key = config('console.thirdScale.key');
- ksort($parameter);
- $pStr = '';
- foreach ($parameter as $v) {
- $pStr .= $v;
- }
- $str = $appid . $op . $task . $version . $pStr . $key;
- return md5($str);
- }
- public function fixedParameter($op, $task, $parameter, $version = '1.0.0')
- {
- $fixedParameter = [
- 'op' => $op,
- 'task' => $task,
- 'version' => $version,
- 'appid' => config('console.thirdScale.appid')
- ];
- //密钥
- $sign = $this->sign($fixedParameter['op'], $fixedParameter['task'], $fixedParameter['version'], $parameter);
- //加入
- $fixedParameter['sign'] = $sign;
- return $fixedParameter;
- }
- public function getScale(int $start, int $count, string $keyword)
- {
- //请求参数
- $parameter = ['start'=>$start, 'count'=>$count, 'keyword'=>$keyword];
- //固定参数
- $fixedParameter = $this->fixedParameter('module', 'moduleList', $parameter);
- //请求地址
- $url = config('console.thirdScale.url');
- try {
- $parameter = array_merge($fixedParameter, $parameter);
- $client = new httpClient();
- $res = $client->request('post', $url, ['form_params'=>$parameter]);
- $body = $res->getBody();
- $data = json_decode($body);
- return $this->response($data);
- } catch (\Exception $e) {
- $this->setLog(self::TYPENAME.'getAccessToken 远程请求失败', $this->startTime, [$e->getMessage()]);
- $this->error()->fail();
- }
- }
- public function createPush(string $openid, string $mid)
- {
- //请求参数
- $parameter = ['open_id'=>$openid, 'mid'=>$mid];
- //固定参数
- $fixedParameter = $this->fixedParameter('module', 'createPush', $parameter);
- //请求地址
- $url = config('console.thirdScale.url');
- try {
- $parameter = array_merge($fixedParameter, $parameter);
- $client = new httpClient();
- $res = $client->request('post', $url, ['form_params'=>$parameter]);
- $body = $res->getBody();
- $data = json_decode($body);
- $this->setLog(self::TYPENAME.'createPush 远程请求结果', $this->startTime, [$parameter]);
- $this->setLog(self::TYPENAME.'createPush 远程请求结果', $this->startTime, [$url]);
- $this->setLog(self::TYPENAME.'createPush 远程请求结果', $this->startTime, [$data]);
- return $data->data->push_id;
- // return $this->response($data);
- } catch (\Exception $e) {
- $this->setLog(self::TYPENAME.'createPush 远程请求失败', $this->startTime, [$e->getMessage()]);
- $this->error()->fail();
- }
- }
- public function customerCreate(string $name, string $openId = null, int $sex = 1)
- {
- //请求参数
- // $parameter = ['name'=>$openId];
- $parameter = [
- 'name'=>$name,
- 'wechat_uid'=>$openId,
- ];
- //固定参数
- $fixedParameter = $this->fixedParameter('customer', 'customerCreate', $parameter);
- //请求地址
- $url = config('console.thirdScale.url');
- try {
- $parameter = array_merge($fixedParameter, $parameter);
- $client = new httpClient();
- $res = $client->request('post', $url, ['form_params'=>$parameter]);
- $body = $res->getBody();
- $data = json_decode($body);
- self::setLog(self::TYPENAME.'customerCreate 远程返回结果', $this->startTime, [$data]);
- // echo json_encode($url);
- // echo json_encode($parameter);
- // echo "<br />";
- // echo json_encode($data); exit;
- return $data->data->open_id;
- // return $this->response($data);
- } catch (\Exception $e) {
- $this->setLog(self::TYPENAME.'customerCreate 远程请求失败', $this->startTime, [$e->getMessage()]);
- $this->error()->fail($e->getMessage());
- }
- }
- public function appLogin(string $thirdOpenId, string $pushId, string $reportId)
- {
- //请求参数
- if ($pushId){
- //$parameter = ['appid'=>config('console.thirdScale.appid'), 'push_id'=>$pushId, 'open_id'=>$thirdOpenId, 'version'=>'1.0.0'];
- $parameter = ['appid'=>config('console.thirdScale.appid'), 'push_id'=>$pushId, 'open_id'=>$thirdOpenId, 'version'=>'1.1.0', 'is_report'=>'1', 's_timestamp'=>time()];
- }
- if ($reportId){
- $parameter = ['report_id'=>$reportId, 'open_id'=>$thirdOpenId];
- }
- //$parameter['sign'] = $this->sign('core', 'appLogin', '1.0.0', ['push_id'=>$pushId, 'open_id'=>$thirdOpenId]);
- $parameter['sign'] = $this->sign('core', 'appLogin', '1.1.0', ['push_id'=>$pushId, 'open_id'=>$thirdOpenId, 'is_report'=>'1', 's_timestamp'=>time()]);
- $str = '';
- foreach ($parameter as $key=>$v){
- $str.=$key.'='.urlencode($v).'&';
- }
- //请求地址
- $url = config('console.thirdScale.do_url');
- return $url.'&'.$str;
- }
- }
|