Przeglądaj źródła

添加内容小号验证

wzq 5 lat temu
rodzic
commit
0e43bc817d

+ 44 - 0
app/Helper/helper.php

@@ -37,4 +37,48 @@ function subtext($text, $length)
     } else {
         return $text;
     }
+}
+
+function http($url, $param, $isCheck = true, $method = 'post')
+{
+    try {
+        $client = new \GuzzleHttp\Client();
+        $response = $client->request($method, $url, $param);
+        $result = json_decode($response->getBody()->getContents(), true);
+        if ($isCheck == true) {
+            if ($result['code'] == 0) {
+                return $result['data'];
+            } else {
+                return [];
+            }
+        } else {
+            return $result;
+        }
+
+    } catch (\Exception $exception) {
+        return [];
+    }
+
+}
+
+function generateSign(array $params, $secret_key)
+{
+    unset($params['sign']);
+    // 将删除参数组中所有等值为FALSE的参数(包括:NULL, 空字符串,0, false)
+    $params = array_filter($params);
+
+    // 按照键名对参数数组进行升序排序
+    ksort($params);
+
+    // 给参数数组追加密钥,键名为 key, 值为签名配置中配置的 secret_key 的值
+    $params['chxq_key'] = $secret_key;
+    \Illuminate\Support\Facades\Log::debug($params);
+    // 生成 URL-encode 之后的请求字符串
+    $str = http_build_query($params);
+    $str = urldecode($str);
+    \Illuminate\Support\Facades\Log::debug($str);
+    //$str = "address=计算机啊手机壳阿看见手机卡&address_type=1&area_id=2&area_name=西安市&city_id=2&city_name=西安市&contact_mobile
+    //=18458881890&contact_name=刘德华&province_id=1&province_name=陕西省&uid=0&zipcode=1000000";
+    // 将请求字符串使用MD5加密后,再转换成大写,并返回
+    return strtoupper(MD5($str));
 }

+ 12 - 4
app/Repositories/Post/PostRepository.php

@@ -16,6 +16,7 @@ use App\Models\PostImgs;
 use App\Models\PostLog;
 use App\Models\Topic;
 use App\Traits\PostTrait;
+use App\Traits\UserTrait;
 use Illuminate\Database\QueryException;
 use Dingo\Api\Http\Response;
 use Illuminate\Support\Carbon;
@@ -30,6 +31,7 @@ use League\Csv\CannotInsertRecord;
 class PostRepository
 {
     use PostTrait;
+    use UserTrait;
 
     public function __construct(Post $post,
                                 PostData $postData,
@@ -54,7 +56,13 @@ class PostRepository
     public function create($request)
     {
         //验证小号
-
+        $userInfo = $this->getUserInfo($request['uid']);
+        if(!$userInfo || $userInfo['type'] != 1){
+            return Response::create([
+                'message'  => '所选小号信息有误',
+                'status_code'   => 500
+            ]);
+        }
         //验证话题
         $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
         $topicCount = count($topicIds);
@@ -68,9 +76,9 @@ class PostRepository
 
         $data = [
             'uid' => $request['uid'],
-            'username' => '暂无',
-            'mobile' => '暂无',
-            'avatar' => '暂无',
+            'username' => $userInfo['username'],
+            'mobile' => $userInfo['mobile'],
+            'avatar' => $userInfo['avatar'],
             'type' => $request['type'],
             'img' => $request['img'],
             'video' => $request['video']??'',

+ 26 - 0
app/Traits/UserTrait.php

@@ -0,0 +1,26 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/11
+ * Time: 17:50
+ */
+
+namespace App\Traits;
+use Tymon\JWTAuth\Facades\JWTAuth;
+
+trait UserTrait
+{
+    public function getUserInfo($uid) {
+        try {
+            $url = config("customer.manage_service_url").'/user/memberView';
+            $array = [
+                'json' => ['uid' => $uid], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+            ];
+            return http($url,$array, true, 'get');
+        } catch (\Exception $e) {
+            return [];
+        }
+
+    }
+}