colder

1983 经验值

boundary是内容的分隔标记浏览器自动生成的。mutipart嘛就是多个part之间的分隔符在上传文件时服务器端根据分隔符从post的数据中获取每一个提交的参数和每一个上传的文件内容并保存为不同的文件。一个完整的POST请求的raw数据如下

POST https://api2.888zbd.com/user/upload_image HTTP/1.1
Host: api2.888zbd.com
Connection: keep-alive
Content-Length: 6056
Pragma: no-cache
Cache-Control: no-cache
Accept: application/vnd.shopcrm.v1+json
Origin: https://vip.888zbd.com
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwaTIuODg4emJkLmNvbS91c2VyL3JlZnJlc2hfdG9rZW4iLCJpYXQiOjE1MzY3MTUzMjksImV4cCI6MTUzNjcyNDQyNywibmJmIjoxNTM2NzIwODI3LCJqdGkiOiJIcXJSWm5BVWJjaGZ3a2dDIiwic3ViIjoxMzA4LCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTkasdfasdfyJpZCI6MTMwOCwidasdfasdfYW5nd2VuamllIiwiZ3VhcmQiOiJ1c2VyIn19.EDH-TpJ7uZKSRi-XSIxQQRVJ2XjnU_s3Uix5fRbkoM8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBOvYAnsSYYC1x0Zy
Referer: https://vip.888zbd.com/shops/549/edit
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9

------WebKitFormBoundaryBOvYAnsSYYC1x0Zy
Content-Disposition: form-data; name="crop_x"

0
------WebKitFormBoundaryBOvYAnsSYYC1x0Zy
Content-Disposition: form-data; name="crop_y"

0
------WebKitFormBoundaryBOvYAnsSYYC1x0Zy
Content-Disposition: form-data; name="crop_width"

0
------WebKitFormBoundaryBOvYAnsSYYC1x0Zy
Content-Disposition: form-data; name="crop_height"

0
------WebKitFormBoundaryBOvYAnsSYYC1x0Zy
Content-Disposition: form-data; name="file"; filename="t01877bf8efa9727b5f.jpg"
Content-Type: image/jpeg

 JFIF  H H     C      

!"$"$   C         "                                           h N;vy   =  7  6d |           m   -        }ywu3 x       (<Ԙ -  } P <v4k   H      _  ϧ _   K g  Frj      QqSŔ>  Ϳg  _ ]- 

j e4ks "` I Gv h ns
-= 띞 (9 x ~z c fq ]Yie S 'F y&=55z/ k
v[ [EdL
y q < 1 &X q= = # + ߣ~ S1 #
------WebKitFormBoundaryBOvYAnsSYYC1x0Zy--

如果需要定义全局方法和全局常量,自己摸索了一点方法:

1、在bootstrap目录中,或者任意你喜欢的位置创建public.php,里面编写你的全局方法

<?php

// 生成一个随机的用户名
if (! function_exists('generate_username')) {
    function generate_username() {
        return str_random(16);
    }
}

2、在composer.json中的autoload中添加files

    "autoload": {
        "files": [
            "bootstrap/public.php"    //这里指向全局方法或全局常量代码文件
        ]
    },

3、重新生成autoload

composer dumpautoload

4、检查vendor/composer/autoload_files.php,发现文件public.php已经自动加载

return array(
    '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php',
    '667aeda72477189d0494fecd327c3641' => $vendorDir . '/symfony/var-dumper/Resources/functions/dump.php',
    '5255c38a0faeba867671b61dfda6d864' => $vendorDir . '/paragonie/random_compat/lib/random.php',
    '72579e7bd17821bb1321b87411366eae' => $vendorDir . '/illuminate/support/helpers.php',
    '1d1b89d124cc9cb8219922c9d5569199' => $vendorDir . '/hamcrest/hamcrest-php/hamcrest/Hamcrest.php',
    '2c102faa651ef8ea5874edb585946bce' => $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php',
    'c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php',
    'a0edc8309cc5e1d60e3047b5df6b7052' => $vendorDir . '/guzzlehttp/psr7/src/functions_include.php',
    'e7223560d890eab89cda23685e711e2c' => $vendorDir . '/psy/psysh/src/Psy/functions.php',
    'decc78cc4436b1292c6c0d151b19445c' => $vendorDir . '/phpseclib/phpseclib/phpseclib/bootstrap.php',
    '37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php',
    'f0906e6318348a765ffb6eb24e0d0938' => $vendorDir . '/laravel/framework/src/Illuminate/Foundation/helpers.php',
    '58571171fd5812e6e447dce228f52f4d' => $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php',
    'f4efd79f57e8418cfb06b27ba8753cd5' => $baseDir . '/bootstrap/public.php',  //我们的文件
);

4、可以在代码里任何地方使用此方法了

Route::get('/username', function () {
    return generate_username();
});

我最近搞过这个
首先,生成一个验证码保存在redis里,派发一个发送短信异步任务

$mobile = $request->mobile;
        $verifyCode = random_int(10000, 99999);

        $key = 'smsVerifyCode_' . $mobile;
        Redis::set($key, $verifyCode, 'EX', 600);

        //派发发短信的任务到队列
        $this->dispatch(new SendSmsVerifyCodeJob($mobile, $verifyCode));

然后,在发送短信任务类里发送短信:

public function handle(UserMessenger $messenger)
    {
        $messenger->sendRegisterVerifyCode($this->mobile, $this->verifyCode);
    }

然后,就是通过SendCloud发送短信。这个比较复杂,需要好好研究SendCloud的API。

最后,通过系统定时任务执行php artisan queue:listen或者php artisan queue:work执行发送短信的任务。

有点复杂,说的不是很清楚

这个问题我解决了,首先你要使用laravel-cors包并正确配置,如果你要支持跨域的cookie,还要配置supportsCredentials = true,且ajax也要配置,可参阅:http://www.cnblogs.com/Brose/p/cors_ajax_withCredentials_firefox.html
最后,如果还不行,那就配置nginx,参阅:http://www.ttlsa.com/nginx/how-to-allow-cross-domain-ajax-requests-on-nginx/

这个问题我解决了,首先你要使用laravel-cors包并正确配置,如果你要支持跨域的cookie,还要配置supportsCredentials = true,且ajax也要配置,可参阅:http://www.cnblogs.com/Brose/p/cors_ajax_withCredentials_firefox.html
最后,如果还不行,那就配置nginx,参阅:http://www.ttlsa.com/nginx/how-to-allow-cross-domain-ajax-requests-on-nginx/

也有好处啊,可以重用啊。不过我的问题是:可以把这些方法直接写到Model里吗?

<?php

namespace App\Serializer;

use League\Fractal\Serializer\ArraySerializer;


class CustomSerializer extends ArraySerializer
{
    /**
     * 重新封装Dingo API返回的data,加入status_code和message
     *
     * @param string $resourceKey
     * @param array $data
     * @return array
     */
    public function collection($resourceKey, array $data)
    {
        return [
            'message' => '操作成功',
            'status_code' => 200,
            'data' => $data
        ];
    }

    public function item($resourceKey, array $data)
    {
        return [
            'message' => '操作成功',
            'status_code' => 200,
            'data' => $data
        ];
    }
}