<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>赵达的个人网站 - Zhao Da&apos;s Personal Website</title>
    <description>一名程序员</description>
    <link>https://zhaoda.net/</link>
    <atom:link href="https://zhaoda.net/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Thu, 11 Jun 2026 01:51:57 +0800</pubDate>
    <lastBuildDate>Thu, 11 Jun 2026 01:51:57 +0800</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Discuz 论坛附件、头像等资源迁移到阿里云 OSS 并开启 CDN 的解决方案</title>
        <description>&lt;p&gt;Discuz 论坛的附件、头像等资源不断增长，如果和主程序一起存储在云服务器上，就会导致云盘要定期进行扩容操作；附件如果需要进行 CDN 加速也只能使用回源策略进行 CDN 配置。为了一劳永逸的解决附件存储和加速问题，将附件等资源迁移到对象存储服务上是一个好的选择，本文以阿里云 ECS、OSS 服务为背景，其他云计算平台也可以参考。&lt;/p&gt;

&lt;h2 id=&quot;创建-oss-bucket&quot;&gt;创建 OSS Bucket&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;在 &lt;a href=&quot;https://oss.console.aliyun.com/&quot;&gt;OSS&lt;/a&gt; 管理页面分别给论坛附件、头像创建 Bucket，名称为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;img-bucket&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;avatar-bucket&lt;/code&gt;（如果自定为其他名字，后文提到的 Bucket 名称请自行替换），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;区域&lt;/code&gt;选择和论坛所在云服务器 ESC 一致的区域，记录下该区域的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Endpoint&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;存储类型&lt;/code&gt;选择标准存储，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;读取权限&lt;/code&gt;选择私有&lt;/li&gt;
  &lt;li&gt;到 &lt;a href=&quot;https://ram.console.aliyun.com/#/user/list&quot;&gt;访问控制&lt;/a&gt; 创建网站用户，生成 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AccessKey&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AccessKeySecret&lt;/code&gt;，给该用户授权 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AliyunOSSFullAccess&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;开启-cdn&quot;&gt;开启 CDN&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;到刚创建好的 Bucket 的域名管理中绑定用户域名，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;img.example.com&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;avatar.example.com&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;勾选&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;阿里云 CDN 加速&lt;/code&gt;，如果域名 dns 解析在阿里云，可以勾选&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;自动添加 CNAME 记录&lt;/code&gt;，如果不在请自行添加域名的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cname&lt;/code&gt;为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;阿里云 CDN 加速域名&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;勾选&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CDN 缓存自动刷新&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;建议单独为 CDN 申请单独的根域名，这样 CDN 请求不会带上网站的 cookie 等信息&lt;/li&gt;
  &lt;li&gt;到 &lt;a href=&quot;https://cdn.console.aliyun.com/&quot;&gt;CDN&lt;/a&gt; 域名管理中对新创建的域名进行配置
    &lt;ul&gt;
      &lt;li&gt;开启&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;私有Bucket回源&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;缓存过期时间&lt;/code&gt;添加规则，内容目录&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;过期时间为3年，权重99&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;设置HTTP头&lt;/code&gt;添加规则，Cache-Control 设置为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max-age=315360000&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;开启&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;智能压缩&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;其他设置请根据实际需求自行修改&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;迁移附件和头像&quot;&gt;迁移附件和头像&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;安装 ossfs，该工具能让您在 Linux 系统中把 OSS Bucket 挂载到本地文件系统中&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;ossfs 基于s3fs 构建，具有s3fs 的全部功能。主要功能包括&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;支持POSIX 文件系统的大部分功能，包括文件读写，目录，链接操作，权限，uid/gid，以及扩展属性（extended attributes）&lt;/li&gt;
    &lt;li&gt;通过OSS 的multipart 功能上传大文件&lt;/li&gt;
    &lt;li&gt;MD5 校验保证数据完整性&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 下载并安装&lt;/span&gt;
wget http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/32196/cn_zh/1524809958556/ossfs_1.80.4_centos7.0_x86_64.rpm?spm&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;a2c4g.11186623.2.6.XJB3Dd&amp;amp;file&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;ossfs_1.80.4_centos7.0_x86_64.rpm
&lt;span class=&quot;nb&quot;&gt;mv &lt;/span&gt;ossfs_1.80.4_centos7.0_x86_64.rpm?spm&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;a2c4g.11186623.2.6.QwMtDE ossfs_1.80.4_centos7.0_x86_64.rpm
yum localinstall ossfs_1.80.4_centos7.0_x86_64.rpm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;将每个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bucket name&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AccessKey&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AccessKeySecret&lt;/code&gt;用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;连接后填写到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/passwd-ossfs&lt;/code&gt;，每行一个，并设置文件权限为 640&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-conf highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;img&lt;/span&gt;-&lt;span class=&quot;n&quot;&gt;bucket&lt;/span&gt;:&lt;span class=&quot;n&quot;&gt;AccessKey&lt;/span&gt;:&lt;span class=&quot;n&quot;&gt;AccessKeySecret&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my&lt;/span&gt;-&lt;span class=&quot;n&quot;&gt;bucket&lt;/span&gt;:&lt;span class=&quot;n&quot;&gt;AccessKey&lt;/span&gt;:&lt;span class=&quot;n&quot;&gt;AccessKeySecret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;chmod &lt;/span&gt;640 /etc/passwd-ossfs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;安装 mailcap，解决 bucket 中的文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content-Type&lt;/code&gt; 全是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;application/octet-stream&lt;/code&gt; 的问题&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yum &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;mailcap
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;将论坛附件和头像剪切到其他临时目录&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 假设论坛根目录为 /data/htdocs/www ，后续脚本将以此为准，请根据实际情况修改&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; /data/htdocs/www/data/attachment /data/
&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; /data/htdocs/www/uc_server/data/avatar /data/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;挂载 OSS Bucket&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 重新创建被剪的目录&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; /data/htdocs/www/data/attachment
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; /data/htdocs/www/uc_server/data/avatar

&lt;span class=&quot;c&quot;&gt;# 获取运行 php-fpm 和 nginx 的系统用户 uid 和 gid，比如 www 用户&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# uid=1000(www) gid=1000(www) 组=1000(www)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;id &lt;/span&gt;www

&lt;span class=&quot;c&quot;&gt;# 挂载 bucket&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Endpoint：请使用内网地址，速度快且流量免费&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -o noxattr：如果你没有使用eCryptFs等需要XATTR的文件系统，可以提升性能&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -o kernel_cache：使用文件系统的 page cache&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -o allow_other：允许其他用户访问挂载文件夹&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -ouid -ogid：制定挂载目录的用户和组权限&lt;/span&gt;
ossfs img-bucket /data/htdocs/www/data/attachment &lt;span class=&quot;nt&quot;&gt;-ourl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;Endpoint &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; noxattr &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; kernel_cache &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; allow_other &lt;span class=&quot;nt&quot;&gt;-ouid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000 &lt;span class=&quot;nt&quot;&gt;-ogid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000
ossfs avatar-bucket /data/htdocs/www/uc_server/data/avatar &lt;span class=&quot;nt&quot;&gt;-ourl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;Endpoint &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; noxattr &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; kernel_cache &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; allow_other &lt;span class=&quot;nt&quot;&gt;-ouid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000 &lt;span class=&quot;nt&quot;&gt;-ogid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;复制附件、头像到 bucket&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; /data/attachment/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /data/htdocs/www/data/attachment
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; /data/avatar/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /data/htdocs/www/uc_server/data/avatar
&lt;span class=&quot;c&quot;&gt;# 到 oss bucket 文件管理验证数据没有问题后可以删除临时拷贝&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; /data/attachment/
&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; /data/avatar/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;如何卸载 bucket&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# root用户&lt;/span&gt;
umount /data/htdocs/www/data/attachment
umount /data/htdocs/www/uc_server/data/avatar
&lt;span class=&quot;c&quot;&gt;# 非root用户&lt;/span&gt;
fusermount &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; /data/htdocs/www/data/attachment
fusermount &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; /data/htdocs/www/uc_server/data/avatar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;开机启动，以 CentOS 7.0 为例，其他系统参考 &lt;a href=&quot;https://github.com/aliyun/ossfs/wiki/FAQ#14&quot;&gt;FAQ&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 根据下面的模板创建启动脚本&lt;/span&gt;
vi /etc/init.d/ossfs
&lt;span class=&quot;c&quot;&gt;# 添加执行权限&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;chmod &lt;/span&gt;a+x /etc/init.d/ossfs
&lt;span class=&quot;c&quot;&gt;# 设置开机启动&lt;/span&gt;
chkconfig ossfs on
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#! /bin/bash&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ossfs      Automount Aliyun OSS Bucket in the specified direcotry.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# chkconfig: 2345 90 10&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# description: Activates/Deactivates ossfs configured to start at boot time.&lt;/span&gt;

ossfs img-bucket /data/htdocs/www/data/attachment &lt;span class=&quot;nt&quot;&gt;-ourl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;Endpoint &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; noxattr &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; kernel_cache &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; allow_other &lt;span class=&quot;nt&quot;&gt;-ouid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000 &lt;span class=&quot;nt&quot;&gt;-ogid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000
ossfs avatar-bucket /data/htdocs/www/uc_server/data/avatar &lt;span class=&quot;nt&quot;&gt;-ourl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;Endpoint &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; noxattr &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; kernel_cache &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; allow_other &lt;span class=&quot;nt&quot;&gt;-ouid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000 &lt;span class=&quot;nt&quot;&gt;-ogid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;修改论坛附件和头像地址&quot;&gt;修改论坛附件和头像地址&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;到论坛后台 -&amp;gt; 全局 -&amp;gt; 上传设置 -&amp;gt; 基本设置 中修改 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;本地附件 URL 地址&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2018/discuz-attachment-url.jpg&quot; alt=&quot;论坛附件url地址&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;到论坛后台 -&amp;gt; 站长 -&amp;gt; UCenter 设置 中修改头像调用方式为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;使用静态地址调用头像&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2018/discuz-avatar-url.jpg&quot; alt=&quot;论坛头像调用方式&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;由于有用户没有上传头像而使用默认头像，到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;avatar&lt;/code&gt; bucket 的根目录添加3个默认头像文件 noavatar_big.gif、noavatar_middle.gif 和 noavatar_small.gif&lt;/li&gt;
  &lt;li&gt;修改&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source/function/function_core.php&lt;/code&gt;中头像地址&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 查找下面的代码并修改&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// $file = $ucenterurl.&apos;/data/avatar/&apos;.$dir1.&apos;/&apos;.$dir2.&apos;/&apos;.$dir3.&apos;/&apos;.substr($uid, -2).($real ? &apos;_real&apos; : &apos;&apos;).&apos;_avatar_&apos;.$size.&apos;.jpg&apos;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// return $returnsrc ? $file : &apos;&amp;lt;img src=&quot;&apos;.$file.&apos;&quot; onerror=&quot;this.onerror=null;this.src=\&apos;&apos;.$ucenterurl.&apos;/images/noavatar_&apos;.$size.&apos;.gif\&apos;&quot; /&amp;gt;&apos;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 使用头像cdn地址&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$cdnurl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;http://avatar.example.com/&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$cdnurl&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$dir1&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;/&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$dir2&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;/&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$dir3&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;/&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;substr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$uid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$real&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;_real&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;_avatar_&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$size&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;.jpg&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$returnsrc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&amp;lt;img src=&quot;&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;&quot; onerror=&quot;this.onerror=null;this.src=\&apos;&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$cdnurl&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;noavatar_&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$size&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;.gif\&apos;&quot; /&amp;gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;修改&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uc_server/avatar.php&lt;/code&gt;中头像重定向地址&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$avatar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;./data/avatar/&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_avatar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$uid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file_exists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dirname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;__FILE__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;/&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$avatar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;nv&quot;&gt;$random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9999&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// rewrite avatar url&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$avatar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;http://avatar.example.com/&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;substr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$avatar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$avatar_url&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$avatar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$avatar&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;?random=&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;nv&quot;&gt;$size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;in_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;big&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;middle&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;small&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;middle&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// $avatar_url = &apos;images/noavatar_&apos;.$size.&apos;.gif&apos;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// rewrite avatar url&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$avatar_url&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;http://avatar.example.com/noavatar_&apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$size&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;.gif&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;HTTP/1.1 301 Moved Permanently&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Last-Modified:&quot;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;r&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Expires: &quot;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;r&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;86400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// header(&apos;Location: &apos;.UC_API.&apos;/&apos;.$avatar_url);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;Location: &apos;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$avatar_url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;论坛-jscss样式图片等资源接入-cdn&quot;&gt;论坛 JS、CSS、样式图片等资源接入 CDN&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;到 &lt;a href=&quot;https://cdn.console.aliyun.com/&quot;&gt;CDN&lt;/a&gt; 域名管理中添加域名 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static.example.com&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;回源设置中源站信息类型选择&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IP&lt;/code&gt;，并填写&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;源站地址IP&lt;/code&gt;为论坛外网 ip，端口 80&lt;/li&gt;
  &lt;li&gt;其他缓存和 HTTP 头设置参考上面 OSS Bucket 的 CDN 设置&lt;/li&gt;
  &lt;li&gt;添加论坛 Nginx 的 vhost 配置，如果是其他 web server 请参考添加&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-conf highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;
{
	&lt;span class=&quot;n&quot;&gt;listen&lt;/span&gt;	&lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;server_name&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;static&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;;

	&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;htm&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;  /&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;htdocs&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;www&lt;/span&gt;;
	&lt;span class=&quot;c&quot;&gt;# error_page  404 = /topic-1.html;
&lt;/span&gt;	&lt;span class=&quot;n&quot;&gt;expires&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;;

	&lt;span class=&quot;n&quot;&gt;location&lt;/span&gt; ~ /\.&lt;span class=&quot;n&quot;&gt;git&lt;/span&gt;
	{
		&lt;span class=&quot;n&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;404&lt;/span&gt;;
	}

	&lt;span class=&quot;n&quot;&gt;location&lt;/span&gt; ~ ^/.*\.(&lt;span class=&quot;n&quot;&gt;php&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;php5&lt;/span&gt;)$
	{
		&lt;span class=&quot;n&quot;&gt;deny&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all&lt;/span&gt;;
	}
	
	&lt;span class=&quot;n&quot;&gt;access_log&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;off&lt;/span&gt;;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;ul&gt;
      &lt;li&gt;到论坛后台 -&amp;gt; 全局 -&amp;gt; 性能优化 -&amp;gt; 服务器优化 中修改 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JS 文件 URL&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CSS 文件 URL&lt;/code&gt; 为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;自定义 URL&lt;/code&gt; 并填写 CDN 地址 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://static.example.com/data/cache/&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2018/discuz-static-url.jpg&quot; alt=&quot;论坛 JS、CSS、样式图片等资源接入 CDN&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;小云-app-缩略图接入-cdn&quot;&gt;小云 APP 缩略图接入 CDN&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;如果论坛使用小云 APP 开发了客户端程序，且开启了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;生成缩略图&lt;/code&gt;功能，同样可以参照上面附件、头像的方式接入 OSS 和 CDN&lt;/li&gt;
  &lt;li&gt;创建 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thumb&lt;/code&gt; bucket 并开启 CDN 加速域名 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thumb.example.com&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;用 ossfs 将 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/data/htdocs/www/data/appbyme/thumb&lt;/code&gt; 目录挂载到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thumb&lt;/code&gt; bucket，并将已有缩略图拷贝进去&lt;/li&gt;
  &lt;li&gt;复制一份 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/data/htdocs/www/mobcent/app/config/mobcent.php&lt;/code&gt; 为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/data/htdocs/www/mobcent/app/config/my_mobcent.php&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;修改 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my_mobcent.php&lt;/code&gt; 中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdndomain&lt;/code&gt; 的值为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thumb.example.com&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;修改 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/data/htdocs/www/mobcent/app/components/web/ImageUtils.php&lt;/code&gt; 中缩略图地址&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_getThumbUrlFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$thumb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//支持自定义CDN域名&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$cacheurl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Yii&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;mobcent&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;cache&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;cdndomain&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$cacheurl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)){&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$cacheurl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Yii&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dzRootUrl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// return sprintf(&apos;%s/%s/%s/%s_%s&apos;, &lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//     $cacheurl,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//     MOBCENT_THUMB_URL_PATH,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//     self::_getThumbTempPath($image),&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//     (isset($_GET[&apos;sdkVersion&apos;]) &amp;amp;&amp;amp; $_GET[&apos;sdkVersion&apos;] &amp;gt; &apos;1.0.0&apos;) ? &apos;xgsize&apos; : &apos;mobcentSmallPreview&apos;,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//     $thumb&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// );&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 修改缩略图地址&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;%s/%s/%s_%s&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;nv&quot;&gt;$cacheurl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;_getThumbTempPath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;sdkVersion&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;sdkVersion&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;1.0.0&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;xgsize&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;mobcentSmallPreview&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$thumb&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;后记&quot;&gt;后记&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;至此，Discuz 论坛全站资源、附件、头像都接入了 OSS 和 CDN，将降低论坛主服务器 http 请求量压力并提高页面打开速度&lt;/li&gt;
  &lt;li&gt;除了本文使用的 ossfs 方案将附件接入 OSS，还有一些其他方案将附件接入 OSS 或 CDN；比如最简单的 CDN 回源方式将附件接入 CDN；还可以通过 &lt;a href=&quot;https://help.aliyun.com/document_detail/32190.html?spm=a2c4g.11186623.6.1061.3oclSk&quot;&gt;ossftp&lt;/a&gt; 工具绑定 OSS，然后&lt;a href=&quot;https://help.aliyun.com/document_detail/32191.html?spm=a2c4g.11186623.6.1067.nBMco5&quot;&gt;开启论坛远程附件方式&lt;/a&gt;来将附件接入 OSS 和 CDN；但是这些方案都没有本文的方案更具有通用性和易用性&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;参考&quot;&gt;参考&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://help.aliyun.com/document_detail/32196.html?spm=a2c4g.11186623.6.1073.za6nvQ&quot;&gt;ossfs 安装和使用&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/aliyun/ossfs/blob/master/README-CN.md&quot;&gt;ossfs 中文文档&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/aliyun/ossfs/wiki/FAQ&quot;&gt;ossfs 常见问题&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://help.aliyun.com/document_detail/32191.html?spm=a2c4g.11186623.6.1067.nBMco5&quot;&gt;Discuz如何存储远程附件到OSS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://lvtao.net/tool/discuz-file-move-aliyun-oss.html&quot;&gt;discuz附件图片迁移阿里云OSS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://help.aliyun.com/document_detail/32190.html?spm=a2c4g.11186623.6.1061.3oclSk&quot;&gt;ossftp 安装和使用&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Wed, 23 May 2018 22:30:00 +0800</pubDate>
        <link>https://zhaoda.net/2018/05/23/discuz-attachment-oss/</link>
        <guid isPermaLink="true">https://zhaoda.net/2018/05/23/discuz-attachment-oss/</guid>
        
        <category>discuz</category>
        
        <category>oss</category>
        
        <category>ossfs</category>
        
        <category>附件</category>
        
        
      </item>
    
      <item>
        <title>CentOS 7.5 + PHP 5.6.36 + Nginx 1.14.0 配置笔记</title>
        <description>&lt;h2 id=&quot;创建用户组和目录&quot;&gt;创建用户、组和目录&lt;/h2&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# web 用户和组&lt;/span&gt;
groupadd www
useradd &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; www www &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; /sbin/nologin

&lt;span class=&quot;c&quot;&gt;# 网站目录&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /data/htdocs

&lt;span class=&quot;c&quot;&gt;# 日志目录&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /data/logs

&lt;span class=&quot;c&quot;&gt;# 创建软件包下载和编译目录，后续软件都下载到这里&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /data/software

&lt;span class=&quot;c&quot;&gt;# 创建软件安装目录，PHP、Nginx 将安装到这里&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /usr/local/webserver

&lt;span class=&quot;c&quot;&gt;# 可写目录进行如下设置&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# chown -R www:www /path&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;服务器基础环境和依赖安装&quot;&gt;服务器基础环境和依赖安装&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 如果系统自带 Apache、PHP、MySQL，先卸载&lt;/span&gt;
yum remove httpd
yum remove php
yum remove mysql

&lt;span class=&quot;c&quot;&gt;# 升级所有软件包&lt;/span&gt;
yum update &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 安装可能用到的软件包，大部分其实已经内置在系统中&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# gcc：GNU 编译器套装&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# gcc-c++：GCC 的 C++ 支持&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# autoconf：在sh下制作供编译、安装和打包软件的配置脚本的工具&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# libjpeg-turbo-static：处理 JPEG 图像数据格式的自由库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# libpng：处理 PNG 图像数据格式的自由库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# freetype: 字体光栅化库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# libxml2: 解析XML文档的函数库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# zlib: 提供数据压缩之用的库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# glibc：C函数库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# glib2：跨平台的、用C语言编写的五个底层库的集合&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# bzip2：比传统的 gzip 或者 ZIP 的压缩效率更高的库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ncurses：虚拟终端中的“类GUI”应用软件工具箱&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# curl：利用URL语法在命令行下工作的文件传输工具&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# e2fsprogs：用以维护ext2，ext3和ext4文件系统的工具程序集&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# krb5-libs：网络身份验证系统&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# libidn：通过IETF国际域名（IDN）实施字符串预处理、Punycode 和 IDNA规格定义的工具&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# openssl：安全通信软件包&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# openldap：轻型目录访问协议&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# nss-pam-ldapd：使用目录服务器的 nsswitch 模块&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# openldap-clients：LDAP 客户端实用工具&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# openldap-servers：LDAP 服务&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# bison：自动生成语法分析器程序&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# lrzsz：远程上传和下载文件&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# libmcrypt：mcrypt 算法库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# mhash：mhash 算法库&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ImageMagick：用于查看、编辑位图文件以及进行图像格式转换的开放源代码软件套装&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# libmemcached: memcache 客户端&lt;/span&gt;
yum &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;gcc gcc-c++ autoconf libjpeg-turbo-static libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel pcre pcre-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-libs krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss-pam-ldapd openldap-clients openldap-servers bison lrzsz libmcrypt libmcrypt-devel mcrypt mhash ImageMagick ImageMagick-devel libmemcached libmemcached-devel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;安装其他-php-依赖库&quot;&gt;安装其他 PHP 依赖库&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# libiconv：提供了一个iconv()的函数，以实现一个字符编码到另一个字符编码的转换&lt;/span&gt;
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;zxvf libiconv-1.15.tar.gz
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;libiconv-1.15
./configure &lt;span class=&quot;nt&quot;&gt;--prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local
make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 在 /etc/ld.so.conf 加一行 /usr/local/lib，再执行如下&lt;/span&gt;
/sbin/ldconfig

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;安装-php&quot;&gt;安装 PHP&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 下载并解压&lt;/span&gt;
wget http://cn2.php.net/get/php-5.6.36.tar.gz/from/this/mirror &lt;span class=&quot;nt&quot;&gt;-O&lt;/span&gt; php-5.6.36.tar.gz
&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;zxvf php-5.6.36.tar.gz
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;php-5.6.36

&lt;span class=&quot;c&quot;&gt;# 编译安装&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 内存如果小于1G，增加 --disable-fileinfo&lt;/span&gt;
./configure &lt;span class=&quot;nt&quot;&gt;--prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local/webserver/php &lt;span class=&quot;nt&quot;&gt;--with-config-file-path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local/webserver/php/etc &lt;span class=&quot;nt&quot;&gt;--with-mysql&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-mysqli&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-curl&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-mcrypt&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-gd&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-openssl&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-mhash&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-xmlrpc&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-gettext&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-bz2&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-zlib&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-iconv-dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local &lt;span class=&quot;nt&quot;&gt;--with-freetype-dir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-jpeg-dir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-png-dir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-libxml-dir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-xml&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-bcmath&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-shmop&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-sysvsem&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-inline-optimization&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-mbregex&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-mbstring&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-gd-native-ttf&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-pcntl&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-sockets&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-zip&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-soap&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-ftp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-exif&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-opcache&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-fpm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-fpm-user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;www &lt;span class=&quot;nt&quot;&gt;--with-fpm-group&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;www &lt;span class=&quot;nt&quot;&gt;--without-pear&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 如果遇到 undefined reference to `libiconv_open&apos; 错误&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# make 后面增加 ZEND_EXTRA_LIBS=&apos;-liconv&apos;&lt;/span&gt;
make &lt;span class=&quot;nv&quot;&gt;ZEND_EXTRA_LIBS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;-liconv&apos;&lt;/span&gt;
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 创建配置文件&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cp &lt;/span&gt;php.ini-production /usr/local/webserver/php/etc/php.ini

&lt;span class=&quot;c&quot;&gt;# 复制启动脚本到 init.d 目录并修改权限&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cp &lt;/span&gt;sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
&lt;span class=&quot;nb&quot;&gt;chmod &lt;/span&gt;700 /etc/init.d/php-fpm

&lt;span class=&quot;c&quot;&gt;# 设置开机启动&lt;/span&gt;
chkconfig php-fpm on

&lt;span class=&quot;c&quot;&gt;# 查看开机启动服务&lt;/span&gt;
chkconfig &lt;span class=&quot;nt&quot;&gt;--list&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 启动服务&lt;/span&gt;
service php-fpm start

&lt;span class=&quot;c&quot;&gt;# 停止服务&lt;/span&gt;
service php-fpm stop

&lt;span class=&quot;c&quot;&gt;# 重启服务&lt;/span&gt;
service php-fpm reload

&lt;span class=&quot;c&quot;&gt;# 创建软连接，方便全局执行 php-fpm start | stop | reload&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;ln&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; /usr/local/webserver/php/bin/php /usr/bin/php
&lt;span class=&quot;nb&quot;&gt;ln&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; /etc/init.d/php-fpm /usr/bin/php-fpm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;安装-php-扩展&quot;&gt;安装 PHP 扩展&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# memcache 扩展&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# memcached 支持 Binary Protocol，而 memcache 不支持，意味着 memcached 会有更高的性能&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# https://pecl.php.net/package/memcached&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 3.x.x 支持 php 7，2.x.x 支持 php 5.2-5.6&lt;/span&gt;
wget https://pecl.php.net/get/memcached-2.2.0.tgz
&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;zxvf memcached-2.2.0.tgz
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;memcached-2.2.0
/usr/local/webserver/php/bin/phpize
./configure &lt;span class=&quot;nt&quot;&gt;--with-php-config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local/webserver/php/bin/php-config
make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 如果需要再本地缓存业务侧数据，安装 apcu&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# apc 包含 opcode 缓存和 KV 数据缓存&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# PHP 5.5.0 及后续版本中已经绑定了 OPcache 扩展，所以不需要安装 apc&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# apcu 只包含 KV 数据缓存&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# https://pecl.php.net/package/APCu&lt;/span&gt;
wget https://pecl.php.net/get/apcu-4.0.11.tgz
&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;zxvf apcu-4.0.11.tgz
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;apcu-4.0.11
/usr/local/webserver/php/bin/phpize
./configure &lt;span class=&quot;nt&quot;&gt;--with-php-config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local/webserver/php/bin/php-config
make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 图片处理 ImageMagick 扩展&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# https://pecl.php.net/package/imagick&lt;/span&gt;
wget https://pecl.php.net/get/imagick-3.4.3.tgz
&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;zxvf imagick-3.4.3.tgz
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;imagick-3.4.3
/usr/local/webserver/php/bin/phpize
./configure &lt;span class=&quot;nt&quot;&gt;--with-php-config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local/webserver/php/bin/php-config
make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;修改-phpini-配置文件&quot;&gt;修改 php.ini 配置文件&lt;/h2&gt;

&lt;div class=&quot;language-ini highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;; vi /usr/local/webserver/php/etc/php.init
&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 该选项设置为 On 时，将在所有的脚本中使用输出控制
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;output_buffering&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;On&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 将 PHP 所能打开的文件限制在指定的目录树
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;open_basedir&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/data/htdocs/:/tmp/&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 禁用函数
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;disable_functions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,proc_get_status,dl,chroot,show_source,syslog,readlink,symlink,popepassthru,stream_socket_server&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 禁止暴露 PHP 被安装在服务器上
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;expose_php&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Off&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 禁止错误信息输出
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;display_errors&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Off&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 错误信息记录到服务器错误日志
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;log_errors&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;On&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 设置脚本错误将被记录到的文件
; 该文件必须是web服务器用户可写的
; mkdir /data/logs
; touch /data/logs/php_error.log
; chown www:www /data/logs/php_error.log
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;error_log&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/data/logs/php_error.log&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 传递给存储处理器的参数
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;session.save_path&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/tmp&quot;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 防止 Nginx 文件类型错误解析漏洞
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;cgi.fix_pathinfo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 文件上传临时目录
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;upload_tmp_dir&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/tmp&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 时区
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;date.timezone&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Asia/Shanghai&quot;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 允许使用 PHP 代码开始标志的缩写形式
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;short_open_tag&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;On&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; php 扩展目录
; php 5.2
; extension_dir = &quot;/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/&quot;
; php 5.4
; extension_dir = &quot;/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/&quot;
; php 5.5
; extension_dir = &quot;/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20121212/&quot;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;extension_dir&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20131226/&quot;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;; 文件结尾添加扩展配置，按需添加
&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;[apcu]&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;apcu.so&quot;&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;apc.enabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;on&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;apc.shm_size&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;128M&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; apc.enable_cli = on
&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;[memcached]&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;memcached.so&quot;&lt;/span&gt;
&lt;span class=&quot;nn&quot;&gt;[imagick]&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;imagick.so&quot;&lt;/span&gt;
&lt;span class=&quot;nn&quot;&gt;[opcache]&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;zend_extension&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;opcache.so&quot;&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;opcache.enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 具体参数配置根据实际情况，默认不配置
; opcache.memory_consumption=128
; opcache.interned_strings_buffer=8
; opcache.max_accelerated_files=4000
; opcache.revalidate_freq=60
; opcache.fast_shutdown=1
; opcache.enable_cli=1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;修改-php-fpmconf-配置文件&quot;&gt;修改 php-fpm.conf 配置文件&lt;/h2&gt;

&lt;div class=&quot;language-ini highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;; cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf
; vi /usr/local/webserver/php/etc/php-fpm.conf
; 查找并修改如下配置，其他保持默认
&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 错误日志的位置
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;error_log&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/data/logs/php-fpm.log&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 错误级别
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;log_level&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;error&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 如果子进程在设定的时间内收到该参数设定次数的 SIGSEGV 或者 SIGBUS退出信息号，则FPM会重新启动
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;emergency_restart_threshold&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 用于设定平滑重启的间隔时间
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;emergency_restart_interval&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1m&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置子进程接受主进程复用信号的超时时间
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;process_control_timeout&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;5s&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置 FPM 在后台运行
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;daemonize&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;yes&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置允许连接到 FastCGI 的服务器 IPV4 地址
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;listen.allowed_clients&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;127.0.0.1&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 子进程的数量是固定的
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;pm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;static&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; pm 设置为 static 时表示创建的子进程的数量
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;pm.max_children&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;64&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置启动时创建的子进程数目。仅在 pm 设置为 dynamic 时使用
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;pm.start_servers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置空闲服务进程的最低数目。仅在 pm 设置为 dynamic 时使用
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;pm.min_spare_servers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置空闲服务进程的最大数目。仅在 pm 设置为 dynamic 时使用
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;pm.max_spare_servers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置每个子进程重生之前服务的请求数
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;pm.max_requests&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;500&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; FPM 状态页面的网址
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;pm.status_path&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/phpfpm_status&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置单个请求的超时中止时间
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;request_terminate_timeout&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 设置文件打开描述符的 rlimit 限制
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;rlimit_files&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;65535&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;; 禁止输出错误信息
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;php_flag&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;[display_errors]&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;off&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;启动-php&quot;&gt;启动 php&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 修改文件句柄数为 65535&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;ulimit&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-SHn&lt;/span&gt; 65535

&lt;span class=&quot;c&quot;&gt;# 启动 php-cgi 进程&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 监听 127.0.0.1 的 9000 端口&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 进程数为 64（如果服务器内存小于3GB，可以只开启64个进程）&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 用户为www&lt;/span&gt;
php-fpm start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;安装-nginx&quot;&gt;安装 nginx&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget http://nginx.org/download/nginx-1.14.0.tar.gz
&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;zxvf nginx-1.14.0.tar.gz
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;nginx-1.14.0/
&lt;span class=&quot;c&quot;&gt;# --prefix=PATH：指定nginx的安装目录&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# --user=name：设置nginx工作进程的用户&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# --group=name：设置nginx工作进程的组&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# --with-pcre：设置PCRE库的源码路径，如果已通过 yum 方式安装，使用 --with-pcre 自动找到库文件&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# --with-http_stub_status_module：用来监控 Nginx 的当前状态&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# --with-http_ssl_module：使用https协议模块&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# --with-http_realip_module：通过这个模块允许我们改变客户端请求头中客户端IP地址值&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 更多配置参考 https://tengine.taobao.org/nginx_docs/cn/docs/install.html&lt;/span&gt;

./configure &lt;span class=&quot;nt&quot;&gt;--prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local/webserver/nginx &lt;span class=&quot;nt&quot;&gt;--user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;www &lt;span class=&quot;nt&quot;&gt;--group&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;www &lt;span class=&quot;nt&quot;&gt;--with-pcre&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-http_stub_status_module&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-http_ssl_module&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-http_realip_module&lt;/span&gt;
make
make &lt;span class=&quot;nb&quot;&gt;install

ln&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; /usr/local/webserver/nginx/sbin/nginx /usr/bin/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;升级-nginx&quot;&gt;升级 nginx&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# make 之后不要 make install&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; /usr/local/webserver/nginx/sbin/nginx /usr/local/webserver/nginx/sbin/nginx.old
&lt;span class=&quot;nb&quot;&gt;cp &lt;/span&gt;objs/nginx /usr/local/webserver/nginx/sbin/
nginx &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;kill&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-USR2&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /usr/local/webserver/nginx/logs/nginx.pid&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;kill&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-QUIT&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /usr/local/webserver/nginx/logs/nginx.pid.oldbin&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
nginx &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;修改-nginxconf-配置文件&quot;&gt;修改 nginx.conf 配置文件&lt;/h2&gt;

&lt;p&gt;Nginx 配置文件主要分成四部分：main（全局设置）、server（主机设置）、upstream（上游服务器设置，主要为反向代理、负载均衡相关配置）和 location（URL匹配特定位置后的设置），每部分包含若干个指令。main 部分设置的指令将影响其它所有部分的设置；server 部分的指令主要用于指定虚拟主机域名、IP 和端口；upstream 的指令用于设置一系列的后端服务器，设置反向代理及后端服务器的负载均衡；location 部分用于匹配网页位置（比如，根目录“/”,“/images”,等等）。他们之间的关系式：server 继承 main，location 继承 server；upstream 既不会继承指令也不会被继承，它有自己的特殊指令，不需要在其他地方的应用。&lt;/p&gt;

&lt;div class=&quot;language-conf highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# vi /usr/local/webserver/nginx/conf/nginx.conf
# 查找并修改如下配置，其他保持默认
&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 运行的用户和用户组
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;www&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;www&lt;/span&gt;;
&lt;span class=&quot;c&quot;&gt;# 全局错误日志和级别
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error_log&lt;/span&gt;  /&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;nginx_error&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;;
&lt;span class=&quot;c&quot;&gt;# 进程文件
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;  /&lt;span class=&quot;n&quot;&gt;usr&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;webserver&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;nginx&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;logs&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;nginx&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;;
&lt;span class=&quot;c&quot;&gt;# 单个进程打开的最多文件描述符数目
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worker_rlimit_nofile&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;65535&lt;/span&gt;;

&lt;span class=&quot;n&quot;&gt;events&lt;/span&gt; 
{
    &lt;span class=&quot;c&quot;&gt;# 事件模型，epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoll&lt;/span&gt;;
    &lt;span class=&quot;c&quot;&gt;# 单个进程可以处理的最大连接数
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;worker_connections&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;65535&lt;/span&gt;;
}

&lt;span class=&quot;n&quot;&gt;http&lt;/span&gt; {
    &lt;span class=&quot;c&quot;&gt;# 日志名称和格式
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;log_format&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;  &lt;span class=&quot;s1&quot;&gt;&apos;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &apos;&lt;/span&gt;
                      &lt;span class=&quot;s1&quot;&gt;&apos;$status $body_bytes_sent &quot;$http_referer&quot; &apos;&lt;/span&gt;
                      &lt;span class=&quot;s1&quot;&gt;&apos;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&apos;&lt;/span&gt;;
    &lt;span class=&quot;c&quot;&gt;# 允许客户端请求的最大单文件字节数
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;client_max_body_size&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;;
    &lt;span class=&quot;c&quot;&gt;# 防止网络阻塞
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tcp_nopush&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt;;
    &lt;span class=&quot;c&quot;&gt;# 开启gzip压缩
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;gzip&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt;;
    &lt;span class=&quot;c&quot;&gt;# 隐藏 nginx 的版本信息
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;server_tokens&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;off&lt;/span&gt;;
    &lt;span class=&quot;c&quot;&gt;# 包含其它自定义虚拟主机配置文件
&lt;/span&gt;    &lt;span class=&quot;c&quot;&gt;# mkdir /usr/local/webserver/nginx/conf/vhost
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vhost&lt;/span&gt;/*.&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;;

    &lt;span class=&quot;c&quot;&gt;# 注释掉 server { } 部分的默认配置
&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;nginx-开机启动&quot;&gt;nginx 开机启动&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# vi /etc/rc.local&lt;/span&gt;
/usr/local/webserver/nginx/sbin/nginx

&lt;span class=&quot;c&quot;&gt;# 测试&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# nginx -t&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 重启和停止&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# nginx -s reload | stop&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;nginx-站点加密&quot;&gt;Nginx 站点加密&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;创建脚本 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi /usr/local/sbin/htpasswd.pl&lt;/code&gt;，输入
    &lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/perl&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;strict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;my&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$pw&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ARGV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;crypt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$pw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$pw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;生成密码
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x /usr/local/sbin/htpasswd.pl
&lt;span class=&quot;c&quot;&gt;# passwd 是要生成的密码&lt;/span&gt;
/usr/local/sbin/htpasswd.pl passwd
&lt;span class=&quot;c&quot;&gt;# 创建完成后删除 htpasswd.pl&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;创建存放用户名和密码的文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi /usr/local/webserver/nginx/conf/.htpasswd&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;输入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user:passwd&lt;/code&gt;，user 是用户名，passwd 是刚才生成的密码&lt;/li&gt;
  &lt;li&gt;在需要加密的 server 或者 location 中增加如下配置
    &lt;div class=&quot;language-conf highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;auth_basic&lt;/span&gt;	&lt;span class=&quot;s2&quot;&gt;&quot;login...&quot;&lt;/span&gt;;
&lt;span class=&quot;n&quot;&gt;auth_basic_user_file&lt;/span&gt;	/&lt;span class=&quot;n&quot;&gt;usr&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;webserver&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;nginx&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;/.&lt;span class=&quot;n&quot;&gt;htpasswd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;https-ssl-证书配置&quot;&gt;https ssl 证书配置&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;如果需要创建一个服务器监控和管理站点，用来查看 phpinfo 或 nginx 状态等信息，建议将该站点设置密码和 https 访问&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 替换下面的 example.com 为你的服务器运维域名&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /usr/local/webserver/nginx/conf/ssl
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /usr/local/webserver/nginx/conf/ssl
openssl genrsa &lt;span class=&quot;nt&quot;&gt;-des3&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-out&lt;/span&gt; example.com.key 1024 795dsl
&lt;span class=&quot;c&quot;&gt;## 输入密码、确认密码&lt;/span&gt;
openssl req &lt;span class=&quot;nt&quot;&gt;-new&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-key&lt;/span&gt; example.com.key &lt;span class=&quot;nt&quot;&gt;-out&lt;/span&gt; example.com.csr
&lt;span class=&quot;c&quot;&gt;## 分别输入之前设置的密码、CN、Beijing、Beijing、example.com、example.com、*.example.com、邮箱、回车、回车&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cp &lt;/span&gt;example.com.key example.com.key.org
openssl rsa &lt;span class=&quot;nt&quot;&gt;-in&lt;/span&gt; example.com.key.org &lt;span class=&quot;nt&quot;&gt;-out&lt;/span&gt; example.com.key
&lt;span class=&quot;c&quot;&gt;## 输入之前设置的密码&lt;/span&gt;
openssl x509 &lt;span class=&quot;nt&quot;&gt;-req&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-days&lt;/span&gt; 365 &lt;span class=&quot;nt&quot;&gt;-in&lt;/span&gt; example.com.csr &lt;span class=&quot;nt&quot;&gt;-signkey&lt;/span&gt; example.com.key &lt;span class=&quot;nt&quot;&gt;-out&lt;/span&gt; example.com.crt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;在需要开启 https 的 server 或者 location 中增加如下配置
    &lt;div class=&quot;language-conf highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt;;
  &lt;span class=&quot;n&quot;&gt;ssl_certificate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;crt&lt;/span&gt;;
  &lt;span class=&quot;n&quot;&gt;ssl_certificate_key&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;配置运维站点&quot;&gt;配置运维站点&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;替换下面的 example.com 为你的服务器运维域名&lt;/li&gt;
  &lt;li&gt;创建站点配置文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi /usr/local/webserver/nginx/conf/vhost/example.com&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-conf highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;
{
	&lt;span class=&quot;n&quot;&gt;listen&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;server_name&lt;/span&gt;	&lt;span class=&quot;err&quot;&gt;_&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;access_log&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;off&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;deny&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all&lt;/span&gt;;
}

&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;
{
	&lt;span class=&quot;n&quot;&gt;listen&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;server_name&lt;/span&gt;	&lt;span class=&quot;err&quot;&gt;_&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;access_log&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;off&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;deny&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all&lt;/span&gt;;
}

&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt; {
	&lt;span class=&quot;n&quot;&gt;listen&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;server_name&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;htm&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;php&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;  /&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;htdocs&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;;

	&lt;span class=&quot;n&quot;&gt;location&lt;/span&gt; / {
		&lt;span class=&quot;n&quot;&gt;rewrite&lt;/span&gt; ^(.*) &lt;span class=&quot;n&quot;&gt;https&lt;/span&gt;://$&lt;span class=&quot;n&quot;&gt;server_name&lt;/span&gt;$&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;permanent&lt;/span&gt;;
	}
	&lt;span class=&quot;n&quot;&gt;access_log&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;off&lt;/span&gt;;
}

&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;
{
	&lt;span class=&quot;n&quot;&gt;listen&lt;/span&gt;	&lt;span class=&quot;m&quot;&gt;443&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;server_name&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;if&lt;/span&gt; ($&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt; != &lt;span class=&quot;s1&quot;&gt;&apos;example.com&apos;&lt;/span&gt;) {
		&lt;span class=&quot;n&quot;&gt;rewrite&lt;/span&gt;	^/(.*)$	&lt;span class=&quot;n&quot;&gt;http&lt;/span&gt;://&lt;span class=&quot;n&quot;&gt;www&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;/	&lt;span class=&quot;n&quot;&gt;permanent&lt;/span&gt;;
	}
	&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;htm&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;php&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;  /&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;htdocs&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;;

	&lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;ssl_certificate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;crt&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;ssl_certificate_key&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;com&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;;
	
	&lt;span class=&quot;n&quot;&gt;auth_basic&lt;/span&gt;	&lt;span class=&quot;s2&quot;&gt;&quot;login...&quot;&lt;/span&gt;;
	&lt;span class=&quot;n&quot;&gt;auth_basic_user_file&lt;/span&gt;  /&lt;span class=&quot;n&quot;&gt;usr&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;webserver&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;nginx&lt;/span&gt;/&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;/.&lt;span class=&quot;n&quot;&gt;htpasswd&lt;/span&gt;;

	&lt;span class=&quot;n&quot;&gt;location&lt;/span&gt; /&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; {
		&lt;span class=&quot;n&quot;&gt;stub_status&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt;;
	}

	&lt;span class=&quot;n&quot;&gt;location&lt;/span&gt; /&lt;span class=&quot;n&quot;&gt;phpfpm_status&lt;/span&gt; {
		&lt;span class=&quot;n&quot;&gt;fastcgi_pass&lt;/span&gt;  &lt;span class=&quot;m&quot;&gt;127&lt;/span&gt;.&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;.&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;.&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;:&lt;span class=&quot;m&quot;&gt;9000&lt;/span&gt;;
		&lt;span class=&quot;n&quot;&gt;fastcgi_index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;php&lt;/span&gt;;
		&lt;span class=&quot;n&quot;&gt;fastcgi_param&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;HTTPS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt;;
		&lt;span class=&quot;n&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastcgi&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;;	
	}

	&lt;span class=&quot;n&quot;&gt;location&lt;/span&gt; ~ \.(&lt;span class=&quot;n&quot;&gt;php&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;php5&lt;/span&gt;)?$
	{      
		&lt;span class=&quot;n&quot;&gt;fastcgi_pass&lt;/span&gt;  &lt;span class=&quot;m&quot;&gt;127&lt;/span&gt;.&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;.&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;.&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;:&lt;span class=&quot;m&quot;&gt;9000&lt;/span&gt;;
		&lt;span class=&quot;n&quot;&gt;fastcgi_index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;php&lt;/span&gt;;
		&lt;span class=&quot;n&quot;&gt;fastcgi_param&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;HTTPS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt;;
		&lt;span class=&quot;n&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastcgi&lt;/span&gt;.&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;;
	}
	
	&lt;span class=&quot;n&quot;&gt;location&lt;/span&gt; ~ \.(&lt;span class=&quot;n&quot;&gt;gif&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;jpg&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;jpeg&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;png&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;bmp&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;swf&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;js&lt;/span&gt;|&lt;span class=&quot;n&quot;&gt;css&lt;/span&gt;)$
	{
		&lt;span class=&quot;n&quot;&gt;expires&lt;/span&gt;      &lt;span class=&quot;m&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;;
	}

	&lt;span class=&quot;n&quot;&gt;access_log&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;off&lt;/span&gt;;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;创建站点目录 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkdir /data/htdocs/example.com&lt;/code&gt;，创建以下文件&lt;/li&gt;
  &lt;li&gt;index.html&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;example.com&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/status&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;target=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;_blank&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;status&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/phpfpm_status&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;target=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;_blank&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;phpfpm_status&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/phpinfo.php&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;target=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;_blank&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;phpinfo&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/ocp.php&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;target=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;_blank&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;opcache&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/apc.php&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;target=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;_blank&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;apcu&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&amp;lt;iframe&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/status&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;width: 400px; height: 100px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/iframe&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&amp;lt;iframe&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/phpfpm_status&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;width: 400px; height: 240px;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/iframe&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;phpinfo.php&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;ocp.php，&lt;a href=&quot;https://gist.github.com/ck-on/4959032&quot;&gt;https://gist.github.com/ck-on/4959032&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;apc.php，&lt;a href=&quot;https://github.com/krakjoe/apcu/blob/master/apc.php&quot;&gt;https://github.com/krakjoe/apcu/blob/master/apc.php&lt;/a&gt; ，修改文件中的用户名和密码&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;重启并测试&quot;&gt;重启并测试&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nginx &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; reload
php-fpm reload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面配置的运维域名如果没有解析，可以到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; 中配置，然后访问 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://运维域名&lt;/code&gt; 看个页面输出是否正确；如果一切顺利，继续到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/webserver/nginx/conf/vhost&lt;/code&gt; 中添加站点配置吧&lt;/p&gt;
</description>
        <pubDate>Tue, 22 May 2018 23:00:00 +0800</pubDate>
        <link>https://zhaoda.net/2018/05/22/centos-php-nginx/</link>
        <guid isPermaLink="true">https://zhaoda.net/2018/05/22/centos-php-nginx/</guid>
        
        <category>centos</category>
        
        <category>php</category>
        
        <category>nginx</category>
        
        <category>linux</category>
        
        
      </item>
    
      <item>
        <title>MySQL 常见问题汇总</title>
        <description>&lt;h2 id=&quot;mysql-56数据导入报-gtid-相关错误&quot;&gt;MySQL 5.6数据导入报 GTID 相关错误&lt;/h2&gt;

&lt;p&gt;从阿里云备份数据后还原到本地，用命令行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mysql -uroot -p --default-character-set=&amp;lt;character&amp;gt; -f &amp;lt;dbname&amp;gt; &amp;lt; &amp;lt;backup.sql&amp;gt;&lt;/code&gt; 方式会报如下错误：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ERROR 1839 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;HY000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at line 24: @@GLOBAL.GTID_PURGED can only be &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;when @@GLOBAL.GTID_MODE &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; ON.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; 方式导入解决。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;mysql &lt;span class=&quot;nt&quot;&gt;-uroot&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;use &amp;lt;dbname&amp;gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; &amp;lt;backup.sql&amp;gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;os-x-通过-brew-安装-mysql&quot;&gt;OS X 通过 brew 安装 MySQL&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 安装&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;brew &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;mysql &lt;span class=&quot;c&quot;&gt;# 5.6&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;brew &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;mysql55 &lt;span class=&quot;c&quot;&gt;# 5.5&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 然后查看 brew info mysql 进行后续操作&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;修改 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my.cnf&lt;/code&gt; 中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;datadir&lt;/code&gt; 切换数据库的存储位置。&lt;/p&gt;

&lt;p&gt;https://gist.github.com/mralexho/6cd3cf8b2ebbd4f33165&lt;/p&gt;

&lt;h2 id=&quot;查找本地-mycnf-位置&quot;&gt;查找本地 my.cnf 位置&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo&lt;/span&gt; /usr/libexec/locate.updatedb
&lt;span class=&quot;c&quot;&gt;# 可能要等待几分钟，然后继续&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;locate my.cnf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Thu, 12 Nov 2015 20:00:00 +0800</pubDate>
        <link>https://zhaoda.net/2015/11/12/mysql-issues/</link>
        <guid isPermaLink="true">https://zhaoda.net/2015/11/12/mysql-issues/</guid>
        
        <category>mysql</category>
        
        <category>issue</category>
        
        
      </item>
    
      <item>
        <title>Gitbook 的使用和常用插件</title>
        <description>&lt;p&gt;&lt;a href=&quot;https://github.com/GitbookIO/gitbook&quot;&gt;Gitbook&lt;/a&gt; 是基于 Node.js 的命令行工具，用来创建漂亮的电子书，它使用 Markdown 或 AsciiDoc 语法来撰写内容，用 Git 进行版本控制，且可以托管在 Github 上。Gitbook 可以将作品编译成网站、 PDF、 ePub 和 MOBI 等多重格式。&lt;/p&gt;

&lt;p&gt;如果你不擅长自己搭建 gitbook 环境，还可以使用 &lt;a href=&quot;https://www.gitbook.com&quot;&gt;gitbook.com&lt;/a&gt; 在线服务来创建和托管你的作品，他们还提供了基于桌面的&lt;a href=&quot;https://www.gitbook.com/editor&quot;&gt;编辑器&lt;/a&gt;。&lt;/p&gt;

&lt;h2 id=&quot;如何使用&quot;&gt;如何使用&lt;/h2&gt;

&lt;p&gt;首先在全局安装 gitbook 客户端工具：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;npm &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;gitbook-cli &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在你的作品目录中创建两个必需的文件 README.md 和 SUMMARY.md，README.md 是作品的介绍，SUMMARY.md 是作品的目录结构，里面要包含一个章节标题和文件索引的列表：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Summary&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;This&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;summary&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;my&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;book&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;section&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;section1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;README&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;section1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;example1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;section1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;example2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;section&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;section2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;README&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;section2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;example1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;根据 SUMMARY.md 的目录结构初始化各个章节文件：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gitbook init
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;运行服务，在编辑内容后实时预览：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gitbook serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;服务器启动后，浏览器打开 http://localhost:4000 查看，撰写完后可以生成静态网站用来发布：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gitbook build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用插件&quot;&gt;使用插件&lt;/h2&gt;

&lt;p&gt;Gitbook 本身功能丰富，但同时可以使用插件来进行个性化定制。&lt;a href=&quot;http://localhost:4000/start/plugin.html&quot;&gt;Gitbook 插件&lt;/a&gt; 里已经有100多个插件，可以在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;book.json&lt;/code&gt; 文件的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plugins&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pluginsConfig&lt;/code&gt; 字段添加插件及相关配置，添加后别忘了进行安装。&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// book.json&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Webpack 中文指南&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Webpack 是当下最热门的前端资源模块化管理和打包工具，本书大部分内容翻译自 Webpack 官网。&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;zh&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;plugins&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;disqus&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;github&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;editlink&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;prism&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;-highlight&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;baidu&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;splitter&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;sitemap&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;pluginsConfig&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;disqus&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;shortName&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;webpack-handbook&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;github&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;https://github.com/zhaoda/webpack-handbook&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;editlink&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;https://github.com/zhaoda/webpack-handbook/blob/master/content&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;编辑本页&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;baidu&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;a9787f0ab45d5e237bab522431d0a7ec&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;sitemap&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;hostname&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;http://zhaoda.net/&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 安装插件&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gitbook &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; ./
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;常用插件&quot;&gt;常用插件&lt;/h2&gt;

&lt;h5 id=&quot;editlink&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/editlink&quot;&gt;editlink&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;内容顶部显示 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;编辑本页&lt;/code&gt; 链接。&lt;/p&gt;

&lt;h5 id=&quot;ad&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/ad&quot;&gt;ad&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;在每个页面顶部和底部添加广告或任何自定义内容。&lt;/p&gt;

&lt;h5 id=&quot;splitter&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/splitter&quot;&gt;splitter&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;在左侧目录和右侧内容之间添加一个可以拖拽的栏，用来调整两边的宽度。&lt;/p&gt;

&lt;h5 id=&quot;image-captions&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/image-captions&quot;&gt;image-captions&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;抓取内容中图片的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alt&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;title&lt;/code&gt; 属性，在图片下面显示标题。&lt;/p&gt;

&lt;h5 id=&quot;github&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/github&quot;&gt;github&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;在右上角显示 github 仓库的图标链接。&lt;/p&gt;

&lt;h5 id=&quot;anchors&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/anchors&quot;&gt;anchors&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;标题带有 github 样式的锚点。&lt;/p&gt;

&lt;h5 id=&quot;chart&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/chart&quot;&gt;chart&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用 C3.js 图表。&lt;/p&gt;

&lt;h5 id=&quot;styles-sass&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/styles-sass&quot;&gt;styles-sass&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用 SASS 替换 CSS。&lt;/p&gt;

&lt;h5 id=&quot;styles-less&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/styles-less&quot;&gt;styles-less&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用 LESS 替换 CSS。&lt;/p&gt;

&lt;h5 id=&quot;ga&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/ga&quot;&gt;ga&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;添加 Google 统计代码。&lt;/p&gt;

&lt;h5 id=&quot;disqus&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/disqus&quot;&gt;disqus&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;添加 disqus 评论插件。&lt;/p&gt;

&lt;h5 id=&quot;sitemap&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/sitemap&quot;&gt;sitemap&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;生成站点地图。&lt;/p&gt;

&lt;h5 id=&quot;latex-codecogs&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/latex-codecogs&quot;&gt;latex-codecogs&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用数学方程式。&lt;/p&gt;

&lt;h5 id=&quot;mermaid&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/mermaid&quot;&gt;mermaid&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用流程图。&lt;/p&gt;

&lt;h5 id=&quot;book-summary-scroll-position-saver&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/book-summary-scroll-position-saver&quot;&gt;book-summary-scroll-position-saver&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;自动保存左侧目录区域导航条的位置。&lt;/p&gt;

&lt;h5 id=&quot;sharing&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/sharing&quot;&gt;sharing&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;默认的分享插件。&lt;/p&gt;

&lt;h5 id=&quot;fontsettings&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/fontsettings&quot;&gt;fontsettings&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;默认的字体、字号、颜色设置插件。&lt;/p&gt;

&lt;h5 id=&quot;search&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/search&quot;&gt;search&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;默认搜索插件。&lt;/p&gt;

&lt;h5 id=&quot;tbfed-pagefooter&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/tbfed-pagefooter&quot;&gt;tbfed-pagefooter&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;自定义页脚，显示版权和最后修订时间。&lt;/p&gt;

&lt;h5 id=&quot;prism&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/prism&quot;&gt;prism&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;基于 &lt;a href=&quot;http://prismjs.com/&quot;&gt;Prism&lt;/a&gt; 的代码高亮。&lt;/p&gt;

&lt;h5 id=&quot;atoc&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/atoc&quot;&gt;atoc&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;插入 TOC 目录。&lt;/p&gt;

&lt;h5 id=&quot;ace&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/ace&quot;&gt;ace&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;插入代码高亮编辑器。&lt;/p&gt;

&lt;h5 id=&quot;highlight&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/highlight&quot;&gt;highlight&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;默认的代码高亮插件，通常会使用 prism 来替换。&lt;/p&gt;

&lt;h5 id=&quot;github-buttons&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/github-buttons&quot;&gt;github-buttons&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;显示 github 仓库的 star 和 fork 按钮。&lt;/p&gt;

&lt;h5 id=&quot;sectionx&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/sectionx&quot;&gt;sectionx&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;分离各个段落，并提供一个展开收起的按钮。&lt;/p&gt;

&lt;h5 id=&quot;mcqx&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/mcqx&quot;&gt;mcqx&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用选择题。&lt;/p&gt;

&lt;h5 id=&quot;include-codeblock&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/include-codeblock&quot;&gt;include-codeblock&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;通过引用文件插入代码。&lt;/p&gt;

&lt;h5 id=&quot;fbqx&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/fbqx&quot;&gt;fbqx&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用填空题。&lt;/p&gt;

&lt;h5 id=&quot;spoiler&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/spoiler&quot;&gt;spoiler&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;隐藏答案，当鼠标划过时才显示。&lt;/p&gt;

&lt;h5 id=&quot;anchor-navigation&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/anchor-navigation&quot;&gt;anchor-navigation&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;锚点导航。&lt;/p&gt;

&lt;h5 id=&quot;youtubex&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/youtubex&quot;&gt;youtubex&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;插入 YouTube 视频。&lt;/p&gt;

&lt;h5 id=&quot;redirect&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/redirect&quot;&gt;redirect&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;页面跳转。&lt;/p&gt;

&lt;h5 id=&quot;expandable-chapters&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/expandable-chapters&quot;&gt;expandable-chapters&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;收起或展开章节目录中的父节点。&lt;/p&gt;

&lt;h5 id=&quot;baidu&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/baidu&quot;&gt;baidu&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用百度统计。&lt;/p&gt;

&lt;h5 id=&quot;duoshuo&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/duoshuo&quot;&gt;duoshuo&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;使用多说评论。&lt;/p&gt;

&lt;h5 id=&quot;jsfiddle&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/jsfiddle&quot;&gt;jsfiddle&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;插入 JSFiddle 组件。&lt;/p&gt;

&lt;h5 id=&quot;jsbin&quot;&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/jsbin&quot;&gt;jsbin&lt;/a&gt;&lt;/h5&gt;

&lt;p&gt;插入 JSBin 组件。&lt;/p&gt;

&lt;h5&gt;&lt;a href=&quot;https://plugins.gitbook.com/plugin/&quot;&gt;&lt;/a&gt;&lt;/h5&gt;

&lt;h2 id=&quot;开发插件&quot;&gt;开发插件&lt;/h2&gt;

&lt;p&gt;最好先查看别人的插件是怎么做的，然后再看&lt;a href=&quot;https://developer.gitbook.com/plugins/index.html&quot;&gt;官方文档&lt;/a&gt;。&lt;/p&gt;

</description>
        <pubDate>Mon, 09 Nov 2015 17:30:00 +0800</pubDate>
        <link>https://zhaoda.net/2015/11/09/gitbook-plugins/</link>
        <guid isPermaLink="true">https://zhaoda.net/2015/11/09/gitbook-plugins/</guid>
        
        <category>gitbook</category>
        
        <category>plugin</category>
        
        
      </item>
    
      <item>
        <title>Webpack 中文指南</title>
        <description>&lt;p&gt;Webpack 是迄今为止我觉得最棒的前端模块化工具，在我日常的开发工作中，也摸索出了一套基于 webpack 的前端构建工作流程。但是 webpack 的使用和配置确实有些复杂，尤其是它众多的 loader、插件和配置信息，如果能灵活使用，就能解决之前前端开发工作中许多难题。&lt;/p&gt;

&lt;p&gt;Webpack 抽象了文件的概念，所有文件（资源）都可以视作模块，尤其是打包后可以生成带有版本号和依赖关系的文件包，可以十分方便的完成生产环境的部署工作。&lt;/p&gt;

&lt;p&gt;在学习 Webpack 的过程中，我写了一本关于其使用的中文指南，目前已经完成了一些基础章节，接下来会继续添加一些进阶的内容，同时欢迎有兴趣的同学一起参与撰写和翻译官方文档。&lt;/p&gt;

&lt;p&gt;这本《Webpack 中文指南》使用 gitbook 撰写，托管在 github 仓库 https://github.com/zhaoda/webpack-handbook ，并通过网站 http://zhaoda.net/webpack-handbook/ 进行发布浏览。&lt;/p&gt;
</description>
        <pubDate>Wed, 04 Nov 2015 00:26:00 +0800</pubDate>
        <link>https://zhaoda.net/2015/11/04/webpack-handbook/</link>
        <guid isPermaLink="true">https://zhaoda.net/2015/11/04/webpack-handbook/</guid>
        
        <category>webpack</category>
        
        
      </item>
    
      <item>
        <title>Browserify 使用指南</title>
        <description>&lt;p&gt;&lt;img src=&quot;/assets/2015/browserify.png&quot; alt=&quot;browserify!&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;让浏览器加载nodejs模块&quot;&gt;让浏览器加载Nodejs模块&lt;/h1&gt;

&lt;p&gt;目前&lt;a href=&quot;https://www.npmjs.com/&quot;&gt;NPM&lt;/a&gt;上有二十多万个&lt;a href=&quot;http://nodejs.org&quot;&gt;NodeJS&lt;/a&gt;模块，它们都是通过CMD的方式打包的，除了特定的可以使用CMD模块加载器加载的模块，大部分nodejs模块无法直接使用到浏览器环境中。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://browserify.org/&quot;&gt;Browserify&lt;/a&gt;是一个供浏览器环境使用的模块打包工具，像在node环境一样，也是通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;require(&apos;modules&apos;)&lt;/code&gt;来组织模块之间的引用和依赖，既可以引用npm中的模块，也可以引用自己写的模块，然后打包成js文件，再在页面中通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&lt;/code&gt;标签加载。&lt;/p&gt;

&lt;p&gt;当然对于很多NodeJS模块，比如涉及到io操作的模块，就算通过browserify打包后肯定也无法运行在浏览器环境中，这种情况下就会用到为它们重写的支持浏览器端的分支模块，可以在&lt;a href=&quot;http://browserifysearch.org/&quot;&gt;browserify search&lt;/a&gt;搜索到这些模块。&lt;/p&gt;

&lt;h1 id=&quot;用例&quot;&gt;用例&lt;/h1&gt;

&lt;p&gt;比如写一个入口&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.js&lt;/code&gt;文件，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;require()&lt;/code&gt;来引用其它模块，包括相对目录中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;./foo.js&apos;&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;../lib/bar.js&apos;&lt;/code&gt;，还有在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node_modules/&lt;/code&gt;目录中的名为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;gamma&apos;&lt;/code&gt;的模块。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;browserify通过&lt;a href=&quot;https://github.com/substack/node-resolve&quot;&gt;node’s module lookup algorithm&lt;/a&gt;来查找&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node_modules/&lt;/code&gt;中的模块）&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// main.js&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;./foo.js&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;../lib/bar.js&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;gamma&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;gamma&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;elem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;baz&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;elem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;textContent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;gamma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;众所周知，这些被引用的模块会用通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module.exports&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exports&lt;/code&gt;输出它们的功能接口：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// foo.js&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;exports&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;111&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;接下来就可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;browserify&lt;/code&gt;命令对它们进行打包了:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;browserify main.js &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; bundle.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.js&lt;/code&gt;和它所引用的模块会按依赖序列整体打包到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundle.js&lt;/code&gt;。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;browserify使用&lt;a href=&quot;https://github.com/defunctzombie/node-required&quot;&gt;required&lt;/a&gt;来查找&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;require()&lt;/code&gt;依赖队列。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;接下来，就可以在html中加载这个文件&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;script src=&quot;bundle.js&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;来运行了。&lt;/p&gt;

&lt;h1 id=&quot;安装browserify&quot;&gt;安装Browserify&lt;/h1&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; browserify
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;通过命令行使用&quot;&gt;通过命令行使用&lt;/h1&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Usage: browserify [entry files] {OPTIONS}

常用选项:

    --outfile, -o  指定打包后的输出文件
                   如果不指定，将输到stdout，比如shell

    --require, -r  通过模块名或文件路径指定需要打包到bundle中的其他模块
                   Optionally use a colon separator to set the target

      --entry, -e  入口文件

     --ignore, -i  打包过程中忽略依赖的某个文件，当成空模块打包，引用它不会报错

    --exclude, -u  打包过程中排除依赖的某个文件，比忽略选项更严格，引用它会报错

   --external, -x  指定某个文件不要打包到bundle，它可能会打包在其他的bundle中，比如jquery会打包到公共的bundle中

  --transform, -t  指定打包过程中使用的转换模块

    --command, -c  指定打包过程中使用的命令行转换

  --standalone -s  生成UMD模式的bundle模块，可以在AMD、CMD中使用

       --debug -d  生成source maps，并被注释到bundle.js的尾部

       --help, -h  显示帮助

高级选项:

  --insert-globals, --ig, --fast    [default: false]

    如果设置为true，会跳过代码分析，强制在每个模块的闭包作用域中插入如下参数
    process, global, __filename, and __dirname

    优点: 打包速度快
    缺点: 生成多余的参数

  --insert-global-vars, --igv

    需要在代码分析中检测和定义的全局变量，需要和--insert-globals一起使用
    默认包括 __filename,__dirname,process,Buffer,global 用逗号分隔

  --detect-globals, --dg            [default: true]

    在代码分析过程中检测全局变量 process, global, __filename, and __dirname

    优点: 像npm模块那样工作
    缺点: 打包速度慢

  --ignore-missing, --im            [default: false]

    忽略 `require()` 引用

  --noparse=FILE

    对于默写确定不包含`require()`的文件，忽略他们的解析过程（比如jquery），加快打包速度

  --no-builtins

    如果打包后的文件要在node环境中使用，请使用这个选项，保证node内置模块不被打包到bundle中

  --no-commondir

    不是用相对路径，__filename,__dirname会被解析成绝对路径

  --no-bundle-external

    不打包扩展模块

  --bare

    --no-builtins --no-commondir --insert-global-vars &quot;__filename,__dirname&quot; 的别名
    如果bundle在node环境中运行，请使用--bare

  --no-browser-field, --no-bf

    忽略package.json中的browser属性
    如果bundle在node环境中运行

  --node

    --bare --no-browser-field 的别名

  --full-paths

    所有模块都是用绝对路径进行引用

  --deps

    输出模块的依赖序列（一个数组）

  --list

    输出所有模块的路径列表

  --extension=EXTENSION

    指定有效的模块文件扩展名

  --global-transform=MODULE, -g MODULE

    指定一个全局的转换模块

  --plugin=MODULE, -p MODULE

    注册一个插件模块

如何在转换模块和插件再插入参数项：

  使用subarg语法在中括号中插入子命令，比如：

    -t [ foo -x 3 --beep ]

  会这样执行

    foo(file, { x: 3, beep: true })

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;nodejs内置模块的转换&quot;&gt;nodejs内置模块的转换&lt;/h1&gt;

&lt;p&gt;理想情况下，大部分不涉及io操作的模块可以在浏览器中直接运行。&lt;/p&gt;

&lt;p&gt;对于nodejs内置模块，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;require()&lt;/code&gt;它们的时候，会被转换成如下模块来打包，以适配浏览器环境。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/assert&quot;&gt;assert&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/buffer&quot;&gt;buffer&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/console-browserify&quot;&gt;console&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/constants-browserify&quot;&gt;constants&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/crypto-browserify&quot;&gt;crypto&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/domain-browser&quot;&gt;domain&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/events&quot;&gt;events&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/stream-http&quot;&gt;http&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/https-browserify&quot;&gt;https&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/os-browserify&quot;&gt;os&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/path-browserify&quot;&gt;path&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/punycode&quot;&gt;punycode&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/querystring-es3&quot;&gt;querystring&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/stream-browserify&quot;&gt;stream&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/string_decoder&quot;&gt;string_decoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/timers-browserify&quot;&gt;timers&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/tty-browserify&quot;&gt;tty&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/url&quot;&gt;url&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/util&quot;&gt;util&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/vm-browserify&quot;&gt;vm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/browserify-zlib&quot;&gt;zlib&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外, 如果是用到了如下5个全局变量，它们会在打包过程中被改写成可以适配浏览器环境的。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/process&quot;&gt;process&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/buffer&quot;&gt;Buffer&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;global 全局变量window&lt;/li&gt;
  &lt;li&gt;__filename - 文件名&lt;/li&gt;
  &lt;li&gt;__dirname - 文件路径&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;其他用例&quot;&gt;其他用例&lt;/h1&gt;

&lt;h2 id=&quot;在全局作用域中模块外部引用指定模块&quot;&gt;在全局作用域中（模块外部）引用指定模块&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;browserify &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; through &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; duplexer &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; ./my-file.js:my-module &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; bundle.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在页面中直接引用这些外部模块&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bundle.js&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;through&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;through&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;duplexer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;duplexer&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myModule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;my-module&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;cm&quot;&gt;/* ... */&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;导出source-maps文件&quot;&gt;导出source maps文件&lt;/h2&gt;

&lt;p&gt;使用&lt;a href=&quot;https://github.com/thlorenz/exorcist&quot;&gt;exorcist&lt;/a&gt;导出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.js.map&lt;/code&gt;文件&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;browserify main.js &lt;span class=&quot;nt&quot;&gt;--debug&lt;/span&gt; | exorcist bundle.js.map &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; bundle.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;导出多个包&quot;&gt;导出多个包&lt;/h2&gt;

&lt;p&gt;如果多个页面的打包结果中包含公共的模块，可以将公共模块（common.js）单独打包出来，以节省请求流量并优化缓存策略&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;beep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;js&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;robot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;./robot.js&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;robot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;beep&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;boop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;js&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;robot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;./robot.js&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;robot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;boop&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这两个文件都引用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot.js&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;robot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;js&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;exports&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 把公共的模块打包成common.js
$ browserify -r ./robot.js &amp;gt; static/common.js
$ browserify -x ./robot.js beep.js &amp;gt; static/beep.js
$ browserify -x ./robot.js boop.js &amp;gt; static/boop.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如何引用？&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;&amp;lt;!--beep.html--&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;common.js&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;beep.js&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;&amp;lt;!--boop.html--&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;common.js&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;boop.js&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;上面这个用例，还可以用&lt;a href=&quot;https://github.com/substack/browserify-handbook#partitioning&quot;&gt;插件&lt;/a&gt;解决，会更方便&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;api&quot;&gt;api&lt;/h2&gt;

&lt;p&gt;Browserify本身也是一个nodejs模块，提供的方法大致和上面介绍的命令行选项相同，具体的api请参考&lt;a href=&quot;https://github.com/substack/node-browserify/blob/master/readme.markdown#methods&quot;&gt;browserify methods&lt;/a&gt;，这里不再敖述。&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;browserify&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;browserify&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;browserify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;./browser/main.js&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bundle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pipe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;events-api&quot;&gt;events api&lt;/h1&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;//当文件被进行打包时出发，可以用来监听文件变化来重新进行打包&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 当每个package.json被读取时触发&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pkg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pkg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 当执行`.bundle()`时触发&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;bundle&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bundle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 当执行`.reset()`时触发&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 当一个文件被转换时触&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;packagejson&quot;&gt;package.json&lt;/h1&gt;

&lt;p&gt;browserify会先通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.json&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;main&quot;&lt;/code&gt;属性来查找模块入口，如果没有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;main&quot;&lt;/code&gt;属性，就在目录中搜索&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;index.js&quot;&lt;/code&gt;文件。当然，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.json&lt;/code&gt;中还定义了一些browserify特有的属性：&lt;/p&gt;

&lt;h2 id=&quot;browser属性&quot;&gt;browser属性&lt;/h2&gt;

&lt;p&gt;“&lt;a href=&quot;https://gist.github.com/4339901&quot;&gt;browser&lt;/a&gt;“属性用来优先指定browserify打包是的模块入口文件，这样可以和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;main&quot;&lt;/code&gt;属性区分开&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nl&quot;&gt;&quot;browser&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;./browser.js&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;还可以指定细分的打包替换文件&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nl&quot;&gt;&quot;browser&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;fs&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;level-fs&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;./lib/ops.js&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;./browser/opts.js&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;browserifytransform&quot;&gt;browserify.transform&lt;/h2&gt;

&lt;p&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.json&lt;/code&gt;中指定转换模块，可以打包过程中自动执行&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nl&quot;&gt;&quot;browserify&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;transform&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;brfs&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;使用插件&quot;&gt;使用插件&lt;/h1&gt;

&lt;p&gt;对于一些更先进的用例，转换模块并不能满足需求，插件是更适合的选择。插件是以包实例为其第一个参数，其他属性作为后面的参数。插件可以用来做一些转换模块做不到的花哨功能。比如&lt;a href=&quot;https://www.npmjs.com/package/factor-bundle&quot;&gt;factor-bundle&lt;/a&gt;插件可以用来自动生成多个入口文件共用的common.js模块。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;browserify x.js y.js &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; factor-bundle &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; bundle/x.js &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; bundle/y.js &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; bundle/common.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;参考链接&quot;&gt;参考链接&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/browse/keyword/browserify-plugin&quot;&gt;Browserify插件列表&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/substack/node-browserify/wiki/list-of-transforms&quot;&gt;Browserify转换列表&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/substack/node-browserify/wiki/browserify-tools&quot;&gt;Browserify第三方工具&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/substack/browserify-handbook&quot;&gt;Browserify参考手册&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://javascript.ruanyifeng.com/tool/browserify.html&quot;&gt;Browserify：浏览器加载Node.js模块&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ibm.com/developerworks/cn/web/1501_chengfu_browserify&quot;&gt;通过Browserify在浏览器中使用NodeJS模块&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Fri, 16 Oct 2015 14:28:00 +0800</pubDate>
        <link>https://zhaoda.net/2015/10/16/browserify-guide/</link>
        <guid isPermaLink="true">https://zhaoda.net/2015/10/16/browserify-guide/</guid>
        
        <category>browserify</category>
        
        <category>nodejs</category>
        
        
      </item>
    
      <item>
        <title>移动端 HTML5 &amp;lt;video&gt; 视频播放优化实践</title>
        <description>&lt;h1 id=&quot;遇到的挑战&quot;&gt;遇到的挑战&lt;/h1&gt;

&lt;p&gt;移动端HTML5使用原生&amp;lt;video&amp;gt;标签播放视频，要做到两个基本原则，速度快和体验佳，先来分析一下这两个问题。&lt;/p&gt;

&lt;h2 id=&quot;下载速度&quot;&gt;下载速度&lt;/h2&gt;

&lt;p&gt;以一个8s短视频为例，wifi环境下提供的高清视频达到1000kbps，文件大小大约1MB；非wifi环境下提供的低码率视频是500kbps左右，文件大小大约500KB；参考QzoneTouch多普勒测速，2g网络的平均速度是14KB/s，那么下载一个低码率视频耗时35s；那么要想流畅播放视频，就需要一个加载等待的过程，这个过程要有明确的反馈，不能让用户有“坏掉了”的感觉。&lt;/p&gt;

&lt;h5 id=&quot;多普勒测速数据参考&quot;&gt;多普勒测速数据参考&lt;/h5&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;dns(s)&lt;/th&gt;
      &lt;th&gt;conn(s)&lt;/th&gt;
      &lt;th&gt;rtt(s)&lt;/th&gt;
      &lt;th&gt;tran(kb/s)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;2g&lt;/td&gt;
      &lt;td&gt;3.85785&lt;/td&gt;
      &lt;td&gt;2.33482&lt;/td&gt;
      &lt;td&gt;2.57478&lt;/td&gt;
      &lt;td&gt;14.0374&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3g&lt;/td&gt;
      &lt;td&gt;1.60643&lt;/td&gt;
      &lt;td&gt;0.743109&lt;/td&gt;
      &lt;td&gt;0.608047&lt;/td&gt;
      &lt;td&gt;60.1967&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;wifi&lt;/td&gt;
      &lt;td&gt;0.986921&lt;/td&gt;
      &lt;td&gt;0.550208&lt;/td&gt;
      &lt;td&gt;0.444332&lt;/td&gt;
      &lt;td&gt;70.8728&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;用户体验&quot;&gt;用户体验&lt;/h2&gt;

&lt;p&gt;视频是否可以自动播放，是否能循环播放，是否能显示下载进度，播放的时候如何隐藏控制条，暂停的时候又能显示出来呢。这些问题看上去貌似简单，但是由于PC/iOS/Android这些不同平台、不同的浏览器内核、甚至相同内核的不同版本，所实现的&amp;lt;video&amp;gt;属性、方法和事件差异较大，解决兼容性问题又给开发造成了很大困扰。&lt;/p&gt;

&lt;h1 id=&quot;分析原因&quot;&gt;分析原因&lt;/h1&gt;

&lt;h2 id=&quot;事件差异&quot;&gt;事件差异&lt;/h2&gt;

&lt;p&gt;下面是播放一个短视频，在不同平台触发事件和获取属性的差异表现。&lt;/p&gt;

&lt;h5 id=&quot;pc&quot;&gt;PC&lt;/h5&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;event&lt;/th&gt;
      &lt;th&gt;readyState&lt;/th&gt;
      &lt;th&gt;currentTime (s)&lt;/th&gt;
      &lt;th&gt;buffered (s)&lt;/th&gt;
      &lt;th&gt;duration (s)&lt;/th&gt;
      &lt;th&gt;视频状态&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;loadstart&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;suspend&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;play&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;waiting&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;durationchange&lt;/td&gt;
      &lt;td&gt;METADATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;5.35&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;获取到视频长度&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;loadedmetadata&lt;/td&gt;
      &lt;td&gt;METADATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0.66&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;获取到元数据&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;loadeddata&lt;/td&gt;
      &lt;td&gt;ENOUGHDATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0.66&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;canplay&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0.66&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;playing&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0.66&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;开始播放&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;canplaythrough&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0.66&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;可以流畅播放&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;11&lt;/td&gt;
      &lt;td&gt;progress&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0.11&lt;/td&gt;
      &lt;td&gt;3.68&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;持续下载&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;12&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0.14&lt;/td&gt;
      &lt;td&gt;4.44&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;播放进度变化&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;23&lt;/td&gt;
      &lt;td&gt;progress&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;1.77&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;下载完毕&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;24&lt;/td&gt;
      &lt;td&gt;suspend&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;1.77&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;25&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;1.9&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;继续播放中&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;48&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;7.7&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;49&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;50&lt;/td&gt;
      &lt;td&gt;seeking&lt;/td&gt;
      &lt;td&gt;METADATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;51&lt;/td&gt;
      &lt;td&gt;waiting&lt;/td&gt;
      &lt;td&gt;METADATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;52&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;53&lt;/td&gt;
      &lt;td&gt;seeked&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;播放完毕进度回到起点&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;54&lt;/td&gt;
      &lt;td&gt;canplay&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;55&lt;/td&gt;
      &lt;td&gt;playing&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;循环播放&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;56&lt;/td&gt;
      &lt;td&gt;canplaythrough&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;57&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0.19&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h5 id=&quot;ios&quot;&gt;iOS&lt;/h5&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;event&lt;/th&gt;
      &lt;th&gt;readyState&lt;/th&gt;
      &lt;th&gt;currentTime (s)&lt;/th&gt;
      &lt;th&gt;buffered (s)&lt;/th&gt;
      &lt;th&gt;duration (s)&lt;/th&gt;
      &lt;th&gt;视频状态&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;loadstart&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;play&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;waiting&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;durationchange&lt;/td&gt;
      &lt;td&gt;METADATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;获取到视频长度&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;loadedmetadata&lt;/td&gt;
      &lt;td&gt;METADATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;获取到元数据&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;loadeddata&lt;/td&gt;
      &lt;td&gt;ENOUGHDATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;canplay&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;canplaythrough&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;可以流畅播放&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;playing&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;开始播放&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;progress&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;下载完毕&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;11&lt;/td&gt;
      &lt;td&gt;suspend&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;12&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0.02&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;播放进度变化&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;43&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;7.8&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;44&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;45&lt;/td&gt;
      &lt;td&gt;seeked&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;播放完毕进度回到起点&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;46&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0.22&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;循环播放&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h5 id=&quot;android&quot;&gt;Android&lt;/h5&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;event&lt;/th&gt;
      &lt;th&gt;readyState&lt;/th&gt;
      &lt;th&gt;currentTime (s)&lt;/th&gt;
      &lt;th&gt;buffered (s)&lt;/th&gt;
      &lt;th&gt;duration (s)&lt;/th&gt;
      &lt;th&gt;视频状态&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;loadstart&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;play&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;waiting&lt;/td&gt;
      &lt;td&gt;NOTHING&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;durationchange&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;durationchange&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;获取到视频长度&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;loadedmetadata&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;获取到元数据&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;loadeddata&lt;/td&gt;
      &lt;td&gt;ENOUGHDATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;canplay&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;canplaythrough&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;playing&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;11&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;12&lt;/td&gt;
      &lt;td&gt;progress&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;3.57&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;下载中&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;13&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0.2&lt;/td&gt;
      &lt;td&gt;6.89&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;开始播放&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;14&lt;/td&gt;
      &lt;td&gt;progress&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;下载完毕&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
      &lt;td&gt;…&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;49&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;7.79&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;50&lt;/td&gt;
      &lt;td&gt;progress&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;7.87&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;51&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;52&lt;/td&gt;
      &lt;td&gt;seeking&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;播放完毕进度回到起点&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;53&lt;/td&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;54&lt;/td&gt;
      &lt;td&gt;seeked&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;循环播放失败卡住了&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;55&lt;/td&gt;
      &lt;td&gt;progress&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;56&lt;/td&gt;
      &lt;td&gt;stalled&lt;/td&gt;
      &lt;td&gt;ENOUGH_DATA&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;7.91&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h5 id=&quot;一些常用且需要重点关注的video事件&quot;&gt;一些常用且需要重点关注的&amp;lt;video&amp;gt;事件&lt;/h5&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;event&lt;/th&gt;
      &lt;th&gt;iOS&lt;/th&gt;
      &lt;th&gt;Android&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;play&lt;/td&gt;
      &lt;td&gt;只是要播放视频，响应的是video.play()方法，并不代表已经开始播放&lt;/td&gt;
      &lt;td&gt;和iOS一样，仅是响应video.play()方法&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;durationchange&lt;/td&gt;
      &lt;td&gt;会执行一次，一定会获取到视频的duration&lt;/td&gt;
      &lt;td&gt;可能会执行多次，只有最后一次才能获取到真实的duration，前面的duration都是0；但低版本Android可能获取到的duration是0或1；（本文提到的低版本Android大部分是4.1以下）&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;canplay&lt;/td&gt;
      &lt;td&gt;可以认为是视频元素没有问题，可以运行，没有更多含义了，基本用不上&lt;/td&gt;
      &lt;td&gt;同iOS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;canplaythrough&lt;/td&gt;
      &lt;td&gt;会有明确的缓冲，表示可以流畅播放了；&lt;/td&gt;
      &lt;td&gt;没有什么用，视频仍然会卡住，数据可能还没有开始加载；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;playing&lt;/td&gt;
      &lt;td&gt;明确表示播放开始了；&lt;/td&gt;
      &lt;td&gt;依然没有用，视频可能并没有开始播放；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;progress&lt;/td&gt;
      &lt;td&gt;有明确的下载，可以获取到当前的buffer，并且全部下载完毕后不在触发；&lt;/td&gt;
      &lt;td&gt;不一定有明确的数据下载，并且全部下载完毕后依然继续触发；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;timeupdate&lt;/td&gt;
      &lt;td&gt;会有明确的进度变化，可以获取到currentTime；&lt;/td&gt;
      &lt;td&gt;进度不一定变化，currentTime可能总是0，但是第一次有currentTime变化的timeupdate事件一定代表了视频开始播放了；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;error&lt;/td&gt;
      &lt;td&gt;iOS中会有明确的错误抛出；&lt;/td&gt;
      &lt;td&gt;Android中某些浏览器会莫名其妙的抛出error；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;stalled&lt;/td&gt;
      &lt;td&gt;网络状况不佳，导致视频下载中断；&lt;/td&gt;
      &lt;td&gt;在没有play之前，也可能会抛出该事件。&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;属性差异&quot;&gt;属性差异&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;attributes&lt;/th&gt;
      &lt;th&gt;iOS&lt;/th&gt;
      &lt;th&gt;android&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;poster&lt;br /&gt;封面图片&lt;/td&gt;
      &lt;td&gt;支持，但是加载速度明显比在&amp;lt;img&amp;gt;中要慢；&lt;/td&gt;
      &lt;td&gt;不一定支持（浏览器厂商的实现标准不统一）；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;preload&lt;br /&gt;预加载&lt;/td&gt;
      &lt;td&gt;iPhone不支持；&lt;/td&gt;
      &lt;td&gt;可能支持；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;autoplay&lt;br /&gt;自动播放&lt;/td&gt;
      &lt;td&gt;iPhone Safari中不支持，但在webview中可能被开启；iOS开发文档明确说明蜂窝网络下不允许autoplay；&lt;/td&gt;
      &lt;td&gt;可能支持；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;loop&lt;br /&gt;循环播放&lt;/td&gt;
      &lt;td&gt;支持&lt;/td&gt;
      &lt;td&gt;可能支持；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;controls&lt;br /&gt;控制条&lt;/td&gt;
      &lt;td&gt;支持，但是需要开始播放了才显示&lt;/td&gt;
      &lt;td&gt;基本都支持显示或者不显示&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;width和height&lt;/td&gt;
      &lt;td&gt;一定给出明确的属性设置，切不能为0；&lt;/td&gt;
      &lt;td&gt;如果不设置，仅仅通过CSS样式去控制视频大小，可能会导致&lt;video&gt;标签失效。&lt;/video&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;其他怪异bug和不友好表现&quot;&gt;其他怪异bug和不友好表现&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;iOS&lt;/th&gt;
      &lt;th&gt;android&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;物理位置覆盖在&amp;lt;video&amp;gt;区域上的元素，click和touch等事件会失效，比如一个&amp;lt;a&amp;gt;链接如果覆盖在&amp;lt;video&amp;gt;上，那么点击后没有任何效果。&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;iOS8.0+中，单页面播放视频超过16个，再播放的视频全部MediaError解码异常无法播放。&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;iPhone的Safari会弹出一个全屏的播放器来播放视频，iPad则支持内联播放。iOS7+ 如果webview（比如微信）开启了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;webview.allowsInlineMediaPlayback = YES;&lt;/code&gt;，可以通过设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;webkit-playsinline&lt;/code&gt;属性支持内联播放；&lt;/td&gt;
      &lt;td&gt;支持内联播放，但某些厂商会用自己的播放器劫持原生的视频播放；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;下载视频时，会先发送一个2字节的请求来获取视频元数据（比如时长），然后再不断的发送分包续传（206）请求来下载视频，抓包显示请求数和请求量至少有一倍的冗余（x2），这个严重的bug在iOS8中有明显的修复，但是分包的206请求仍然会有冗余数据的下载，浪费了流量。&lt;/td&gt;
      &lt;td&gt;比iOS的处理方式好，没有第一个2字节请求，没有流量损耗；&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;低版本Android（&amp;lt;=4.0.4）中，&amp;lt;video&amp;gt;如果在有相对和决定定位的层中，可能会导致整个页面错位。&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;某些浏览器厂商会劫持&amp;lt;video&amp;gt;，用其“自己”的播放器来播放视频，“破坏”了产品本身的播放体验，那么只能case by case的解决了。&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;加载视频时没有进度提示，视觉上看不出是播放完了还是卡住了；&lt;/td&gt;
      &lt;td&gt;加载视频时，大都会显示一个自带的loading UI（菊花）。&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h1 id=&quot;最佳实践&quot;&gt;最佳实践&lt;/h1&gt;

&lt;h2 id=&quot;视频初始化&quot;&gt;视频初始化&lt;/h2&gt;

&lt;p&gt;如果将一个&amp;lt;video&amp;gt;直接显示在页面中，那么就会看到各种五花八门的播放器初始效果；&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cloud.githubusercontent.com/assets/546659/4839249/700ca10a-5ffb-11e4-81d2-b11726e4ea38.jpg&quot; alt=&quot;五花八门的播放器初始效果&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这显然不是一个好的视觉体验，那么通常的做法是制作一个模拟的视频播放视图，比如一个封面加一个播放按钮。&lt;/p&gt;

&lt;p&gt;而真实的&amp;lt;video&amp;gt;视频元素要隐藏起来，如何隐藏呢？最好不要用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{display: none}&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{width:0;height:0;}&lt;/code&gt;的方式，因为这样视频元素会处于未激活的状态，给后续的处理带来麻烦。最佳的方式是将视频设置成1x1像素大小，放在视觉边缘的位置。&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;&amp;lt;!--iOS--&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;video&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;webkit-playsinline&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;vplayinside notaplink&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;x-webkit-airplay&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;controls&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;loop=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;loop&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;%=src%&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/video&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;&amp;lt;!--Android--&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;video&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;controls&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;loop=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;loop&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;%=src%&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/video&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://cloud.githubusercontent.com/assets/546659/4839441/d3f7fdec-5ffe-11e4-8904-f8dbe720ed84.jpg&quot; alt=&quot;default&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;自动播放&quot;&gt;自动播放&lt;/h2&gt;

&lt;p&gt;autoplay的支持依赖内核和网络状况，比如iPhone在蜂窝网络下明确禁用了autoplay；&lt;/p&gt;

&lt;p&gt;经过试验，在没有明确的用户操作的情况下，直接通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;video.play()&lt;/code&gt;也是无法激活播放的；&lt;/p&gt;

&lt;p&gt;并且在产品设计上，自动播放也不是一个舒服的用户体验，所以产品设计上尽量避免使用自动播放。&lt;/p&gt;

&lt;h2 id=&quot;点击播放&quot;&gt;点击播放&lt;/h2&gt;

&lt;p&gt;之前提到，视频最好通过1px大小隐藏起来，那么这时如何触发播放呢？&lt;/p&gt;

&lt;p&gt;经过试验，当在明确的用户操作（touch、click）时，通过这些用户行为事件的回调函数，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;video.play()&lt;/code&gt;是可以触发视频播放的，那么能否在用户操作后，再去同步的创建和播放视频呢？答案是肯定的，这无疑是一个视频元素初始化的最佳实践，但是有些差异需要注意。&lt;/p&gt;

&lt;h5 id=&quot;ios6&quot;&gt;iOS6+&lt;/h5&gt;

&lt;p&gt;可以在用户的touch时间中动态创建并播放视频。&lt;/p&gt;

&lt;h5 id=&quot;ios--6&quot;&gt;iOS &amp;lt; 6&lt;/h5&gt;

&lt;p&gt;可以在用户的touch时间中动态创建视频，但不能播放；要再追加一个click事件来启动播放；也就是说，给伪造的视频播放按钮同时绑定tap和click事件，在tap的时候创建，在之后300毫秒的click中去播放。&lt;/p&gt;

&lt;h5 id=&quot;android-1&quot;&gt;Android&lt;/h5&gt;

&lt;p&gt;大部分高版本Android可以像iOS6+那样去处理，但是低版本的不行，必须要通过click事件去传递&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;video.play()&lt;/code&gt;，为了保持兼容，最好是用帮tap和click两个事件来分别完成视频的初始化和播放。&lt;/p&gt;

&lt;p&gt;我们还发现，有些低版本Android中，无法通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;video.play()&lt;/code&gt;来播放视频，必须有真实的用户点击视频元素才能播放；这种情况，有一个技巧就是在tap的时候初始化并放大视频覆盖在播放视图中，让300毫秒后的真实点击行为穿透点击在视频元素上来实现播放。&lt;/p&gt;

&lt;h2 id=&quot;循环播放&quot;&gt;循环播放&lt;/h2&gt;

&lt;p&gt;如果视频需要循环播放，那么就增加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt;属性，是否能循环播放就看浏览器是否支持了，因为还没有找到hack技巧来强制循环播放；&lt;/p&gt;

&lt;p&gt;即使，在不支持循环播放的Android中，通过监听&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;seeked&lt;/code&gt;事件知道了播放进度到了终点或起点暂停了，此时也无法通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;video.play()&lt;/code&gt;来让视频重新播放。&lt;/p&gt;

&lt;h2 id=&quot;监控下载进度&quot;&gt;监控下载进度&lt;/h2&gt;

&lt;p&gt;如何获取视频时长和已经下载的时长？&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 视频时长&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 获取视频已经下载的时长&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getEnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;buffered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;parseInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;progress事件表示视频在加载，但是它的触发频率和时机并不规律，最佳做法是通过一个定时器去实时获取end，当end &amp;gt;= duration时，表示已经下载完毕，再终止定时器。&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;timer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getEnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;clearInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;timer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;全部下载后再播放&quot;&gt;全部下载后再播放&lt;/h2&gt;

&lt;p&gt;假设播放短视频，如果网络不佳，会造成播放断断续续，在iOS中这种停顿还没有一个明确的等待提示，这不是一个好的体验，那么是否可以将视频全部下载完毕再播放呢？&lt;/p&gt;

&lt;p&gt;在iOS中，可以在视频刚开始下载的时候马上暂停，此时下载还将继续，可以做一个loading的菊花告知视频正在加载，然后等到视频全部下载完再开始播放。&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;loadeddata&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 暂停，但下载还在继续&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pause&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// 启动定时器检测视频下载进度&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;timer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getEnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 下载完了，开始播放吧&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;width&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;clearInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;timer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://cloud.githubusercontent.com/assets/546659/4840851/454dabb2-6014-11e4-9651-06136a5a7332.gif&quot; alt=&quot;default&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;缓冲播放边下边播时选择开始播放的最佳时间点&quot;&gt;缓冲播放——边下边播时，选择开始播放的最佳时间点&lt;/h2&gt;

&lt;p&gt;当视频越来越长或者网络慢时，等待视频全部下载完再播放也不是好的体验，最好能边下边播，缓冲到流畅状态就开始播放，那什么时候播放才是最佳时间点呢？&lt;/p&gt;

&lt;p&gt;在iOS中，canplaythrough事件就是这个最佳时间点，它是通过动态计算缓冲量和下载速度得出的视频可以流畅播放的状态反馈。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;canplaythrough event: The user agent estimates that if playback were to be started now, the media resource could be rendered at the current playback rate all the way to its end without having to stop for further buffering.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;https://cloud.githubusercontent.com/assets/546659/4841077/8fd35acc-6016-11e4-8755-684ef2656ddf.gif&quot; alt=&quot;default&quot; /&gt;&lt;/p&gt;

&lt;p&gt;注意：下载完再播放和缓冲播放只适用于iOS。&lt;/p&gt;

&lt;h2 id=&quot;统计播放时间和播放次数&quot;&gt;统计播放时间和播放次数&lt;/h2&gt;

&lt;p&gt;要统计实际的播放时间，要累加timeupdate事件变化的时间，再减去中间可能暂停的时间。&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;playing&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 开始播放是打点&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data-updateTime&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;pause&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 暂停播放时清除打点&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;removeAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data-updateTime&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 累加播放时间&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;timeupdate&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;updateTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;parseInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data-updateTime&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;playingTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;parseInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data-playingTime&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;times&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;parseInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data-times&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;newtimes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;video&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;parseFloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data-duration&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// 播放时间&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;playingTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;playingTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;updateTime&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;// 播放次数&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;newtimes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ceil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;playingTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data-playingTime&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;playingTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;$video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data-updateTime&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;异常处理&quot;&gt;异常处理&lt;/h2&gt;

&lt;p&gt;对error事件做详细的上报；&lt;/p&gt;

&lt;p&gt;对stalled事件做统计上报，并提示用户网络慢等。&lt;/p&gt;

&lt;h1 id=&quot;参考数据&quot;&gt;参考数据&lt;/h1&gt;

&lt;p&gt;微视触屏版iOS视频测速&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;网络环境&lt;/th&gt;
      &lt;th&gt;视频码率&lt;/th&gt;
      &lt;th&gt;获取到视频时长&lt;br /&gt;时间点(s)&lt;/th&gt;
      &lt;th&gt;开始流畅播放&lt;br /&gt;时间点(s)&lt;/th&gt;
      &lt;th&gt;全部下载完毕&lt;br /&gt;时间点(s)&lt;/th&gt;
      &lt;th&gt;视频长度(s)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;wifi&lt;/td&gt;
      &lt;td&gt;1000kbps&lt;/td&gt;
      &lt;td&gt;2.86&lt;/td&gt;
      &lt;td&gt;3.97&lt;/td&gt;
      &lt;td&gt;5.85&lt;/td&gt;
      &lt;td&gt;8.69&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;非wifi&lt;/td&gt;
      &lt;td&gt;500kbps&lt;/td&gt;
      &lt;td&gt;4.56&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;10.62&lt;/td&gt;
      &lt;td&gt;8.67&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h1 id=&quot;参考资料&quot;&gt;参考资料&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;HTML5 Video Events and API检测工具 &lt;a href=&quot;http://www.w3.org/2010/05/video/mediaevents.html&quot;&gt;http://www.w3.org/2010/05/video/mediaevents.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;W3C video 标准 &lt;a href=&quot;http://www.w3.org/TR/html5/embedded-content-0.html#the-video-element&quot;&gt;http://www.w3.org/TR/html5/embedded-content-0.html#the-video-element&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;如何在iOS7+的webview中内联播放视频 &lt;a href=&quot;http://darktalker.com/2014/play-video-inline-iphone-ios7&quot;&gt;http://darktalker.com/2014/play-video-inline-iphone-ios7&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;视频事件流水查看工具 &lt;a href=&quot;http://z.weishi.qq.com/app/video.html&quot;&gt;http://z.weishi.qq.com/app/video.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HTML5 VIDEO bytes on iOS &lt;a href=&quot;http://www.stevesouders.com/blog/2013/04/21/html5-video-bytes-on-ios&quot;&gt;http://www.stevesouders.com/blog/2013/04/21/html5-video-bytes-on-ios&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 30 Oct 2014 22:59:00 +0800</pubDate>
        <link>https://zhaoda.net/2014/10/30/html5-video-optimization/</link>
        <guid isPermaLink="true">https://zhaoda.net/2014/10/30/html5-video-optimization/</guid>
        
        <category>html5</category>
        
        <category>video</category>
        
        
      </item>
    
      <item>
        <title>微视 WebView 的 JSAPI 介绍</title>
        <description>&lt;h1 id=&quot;简介&quot;&gt;简介&lt;/h1&gt;

&lt;p&gt;和&lt;a href=&quot;http://qydev.weixin.qq.com/wiki/index.php?title=WeixinJS%E6%8E%A5%E5%8F%A3&quot;&gt;微信的JSAPI&lt;/a&gt;一样，&lt;a href=&quot;http://www.weishi.com&quot;&gt;微视&lt;/a&gt;的webview中也内置了一套jsapi，当webview打开后，会初始化jsapi，初始化完成后会派发给document一个WeishiJSBridgeReady事件并构建好window.WeishiJSBridge对象，然后就可以使用window.WeishiJSBridge.invoke方法来调用jsapi的具体功能。&lt;/p&gt;

&lt;h1 id=&quot;初始化&quot;&gt;初始化&lt;/h1&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 定义微视JSAPI&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;$doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;WeishiJSBridgeReady&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wait&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;build&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;wait&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wait&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;responseCallback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;WSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 微视jsapi加载成功&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;使用&quot;&gt;使用&lt;/h1&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// apiname：jsapi接口名称&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// params：接口参数&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// callback：回调函数&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apiname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;返回值&quot;&gt;返回值&lt;/h1&gt;

&lt;p&gt;返回值通过回调函数的参数进行返回，类型是json对象，旧版微视android客户端返回的是json字符串，需要用JSON.parse兼容。&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 返回值实例&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 0为成功&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 接入层返回的数据&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;errcode&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;成功&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// 接入层返回的数据&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;权限&quot;&gt;权限&lt;/h1&gt;

&lt;p&gt;目前微视jsapi只对内部qq.com、weishi.com 域开放，暂不支持外域接入申请。&lt;/p&gt;

&lt;h1 id=&quot;数据请求类api&quot;&gt;数据请求类API&lt;/h1&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 请求转发&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 将通常web侧的ajax请求，通过客户端的jsapi进行代理转发，&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 这个请求会带上登录态和加密的token，&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 来解决web侧无法获取登录态和加密的问题，更加安全可靠。&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;request.forward&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;info/clientInfo.php&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 接入层的请求地址&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// get or post&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 参数&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;页面跳转类型api&quot;&gt;页面跳转类型API&lt;/h1&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 详情页&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.detail&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;tweetid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;2002018099834136&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 微视id&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 客人页&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.profile&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;uid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;1000047&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// uid&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 频道页&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.channel&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;美女&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 频道名&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;tweetid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 定位到指定微视的微视id&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 标签页&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.tag&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;中国好声音&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 标签名&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;tweetid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 定位到指定微视的微视id&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 我的二维码名片&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.weblogin&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 邀请好友&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.inviteFriends&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 推荐关注默认&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.suggestedPeopleDefault&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 推荐关注名人&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.suggestedPeopleStar&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 推荐关注达人&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.suggestedPeoplePopular&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 推荐关注朋友&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.suggestedPeopleFriends&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 扫一扫&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.qrscan&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 打开摄像头&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.camera&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 打开长视频&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.camera.longvideo&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 打开动态影集&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.camera.photo&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 打开本地视频&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.camera.localvideo&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 发现页&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.discovery&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 新的朋友&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.newFriends&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 独立的热门页面&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.popularweishi&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 独立的频道页面&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;view.channelhomepage&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;视图操作类api&quot;&gt;视图操作类API&lt;/h1&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 关闭当前webview&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;action.closeWindow&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 隐藏toolbar&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;action.hideToolbar&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 显示toolbar&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;action.showToolbar&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 设置页面标题&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;action.setTitle&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;新标题&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 标题&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 打开新页面&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;action.openURLInWindow&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://weishi.com&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 页面地址&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;hideToolbar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 是否隐藏toolbar&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 分享&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;WeishiJSBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;action.share&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://weishi.com&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 页面地址&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;imgurl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://mat1.gtimg.com/www/mb/webapp/weishi/image/medal/1002_detail.png&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 图片地址&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;一赞成名&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 标题&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;徽章分享&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 摘要&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;调试工具&quot;&gt;调试工具&lt;/h1&gt;

&lt;p&gt;http://z.weishi.qq.com/app/api.html&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cloud.githubusercontent.com/assets/546659/4806086/70ae1994-5e85-11e4-9290-597e12205206.png&quot; alt=&quot;24f9e0b5e71533d66f2e1cfa45d8859d&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;更新日志&quot;&gt;更新日志&lt;/h1&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;日期&lt;/th&gt;
      &lt;th&gt;日志&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;2014-09-16&lt;/td&gt;
      &lt;td&gt;文档初始化&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2014-09-19&lt;/td&gt;
      &lt;td&gt;增加分享功能api&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
</description>
        <pubDate>Tue, 28 Oct 2014 17:35:00 +0800</pubDate>
        <link>https://zhaoda.net/2014/10/28/weishi-webview-jsapi/</link>
        <guid isPermaLink="true">https://zhaoda.net/2014/10/28/weishi-webview-jsapi/</guid>
        
        <category>jsapi</category>
        
        <category>微视</category>
        
        
      </item>
    
      <item>
        <title>微视 URI Scheme 介绍</title>
        <description>&lt;p&gt;&lt;a href=&quot;http://www.weishi.com&quot;&gt;微视&lt;/a&gt;APP统一资源标识符。&lt;/p&gt;

&lt;h1 id=&quot;格式&quot;&gt;格式&lt;/h1&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;scheme name&amp;gt; : &amp;lt;path&amp;gt; [ ? &amp;lt;query&amp;gt; ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;：微视scheme协议名称，ios中是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;weishiiosscheme&lt;/code&gt;，android中是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;weishiandroidscheme&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt;：页面路径；&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;query&amp;gt;&lt;/code&gt;：可选项目，查询参数，用&amp;amp;隔开的键值对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;key&amp;gt;&lt;/code&gt;=&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;value&amp;gt;&lt;/code&gt;。&lt;/p&gt;

&lt;h1 id=&quot;功能&quot;&gt;功能&lt;/h1&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;序号&lt;/th&gt;
      &lt;th&gt;资源&lt;/th&gt;
      &lt;th&gt;URI Scheme&lt;/th&gt;
      &lt;th&gt;支持版本&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;详情页&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://detail?tweetid=微视id&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;客人页&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://userprofile?uid=uid&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;频道页&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://channel?name=频道名称&amp;amp;tweetid=定位到指定微视的微视id&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;标签页&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://tag?name=标签名称&amp;amp;tweetid=定位到指定微视的微视id&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;我的二维码&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://myqrcard&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;邀请好友&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://invitefriends&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;推荐关注名人&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://suggestedpeople?type=star&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;推荐关注达人&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://suggestedpeople?type=popular&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;推荐关注朋友&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://suggestedpeople?type=friends&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;扫一扫&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://qrscan&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;11&lt;/td&gt;
      &lt;td&gt;拍摄&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://opencamera&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;12&lt;/td&gt;
      &lt;td&gt;发现页&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://discovery&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;13&lt;/td&gt;
      &lt;td&gt;新的朋友&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://newfriends&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;14&lt;/td&gt;
      &lt;td&gt;发现-热门&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://popularweishi&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;15&lt;/td&gt;
      &lt;td&gt;发现-频道&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://channelhomepage&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;16&lt;/td&gt;
      &lt;td&gt;勋章墙&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://medalwall&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;17&lt;/td&gt;
      &lt;td&gt;打开webview&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://webview?url=打开的链接&lt;/td&gt;
      &lt;td&gt;2.7+&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;18&lt;/td&gt;
      &lt;td&gt;微视星语&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;scheme name&amp;gt;&lt;/code&gt;://pickstar&lt;/td&gt;
      &lt;td&gt;2.8+&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h1 id=&quot;调试工具&quot;&gt;调试工具&lt;/h1&gt;
&lt;p&gt;http://z.weishi.qq.com/app/scheme.html&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cloud.githubusercontent.com/assets/546659/4805999/1c65c374-5e84-11e4-9cbd-47b7cb478df6.png&quot; alt=&quot;512e7ff1578297f2052f8062bf7b9bc4&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;更新日志&quot;&gt;更新日志&lt;/h1&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;日期&lt;/th&gt;
      &lt;th&gt;日志&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;2014-09-16&lt;/td&gt;
      &lt;td&gt;微视2.7版本最新支持&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2014-10-28&lt;/td&gt;
      &lt;td&gt;微视2.8版本最新支持&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
</description>
        <pubDate>Tue, 28 Oct 2014 17:25:00 +0800</pubDate>
        <link>https://zhaoda.net/2014/10/28/weishi-uri-scheme/</link>
        <guid isPermaLink="true">https://zhaoda.net/2014/10/28/weishi-uri-scheme/</guid>
        
        <category>scheme</category>
        
        <category>微视</category>
        
        
      </item>
    
      <item>
        <title>NPM 国内镜像配置和 CNPM 使用</title>
        <description>&lt;h1 id=&quot;npm镜像配置&quot;&gt;&lt;a href=&quot;http://www.npmjs.com&quot;&gt;NPM&lt;/a&gt;镜像配置&lt;/h1&gt;

&lt;ol&gt;
  &lt;li&gt;通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config&lt;/code&gt; 配置&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;```bash
$ npm config set registry http://r.cnpmjs.org
```
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;命令行临时指定&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;```bash
$ npm --registry http://r.cnpmjs.org install koa
```
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;编辑 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.npmrc&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;```
registry = http://r.cnpmjs.org
```
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;其他npm配置&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;```bash
$ npm config set strict-ssl false # 关闭npm的https
$ npm config set proxy=http://ip:port # 设置npm获取的代理服务器地址
$ npm config delete http-proxy # 清除npm的http代理
$ npm config delete https-proxy # 清除npm的https代理
$ npm install -g koa --proxy http://ip:port # 临时使用代理
```
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;cnpmnode包管理器&quot;&gt;&lt;a href=&quot;http://cnpmjs.org/&quot;&gt;CNPM&lt;/a&gt;——node包管理器&lt;/h1&gt;

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; cnpm &lt;span class=&quot;nt&quot;&gt;--registry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;http://r.cnpmjs.org
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;alias &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cnpm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;npm --registry=http://r.cnpmjs.org &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;
--cache=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/.npm/.cache/cnpm &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;
--disturl=http://cnpmjs.org/dist &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;
--userconfig=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/.cnpmrc&quot;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#或者把该设置放到shell配置文件里 .bashrc or .zshrc&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;\n#alias for cnpm\nalias cnpm=&quot;npm --registry=http://r.cnpmjs.org \
  --cache=$HOME/.npm/.cache/cnpm \
  --disturl=http://cnpmjs.org/dist \
  --userconfig=$HOME/.cnpmrc&quot;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.zshrc &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用&quot;&gt;使用&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cnpm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;name] &lt;span class=&quot;c&quot;&gt;# 安装&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cnpm &lt;span class=&quot;nb&quot;&gt;sync &lt;/span&gt;connect &lt;span class=&quot;c&quot;&gt;# 同步&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;open http://cnpmjs.org/sync/connect &lt;span class=&quot;c&quot;&gt;# 通过web浏览器查看&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cnpm publish &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;name]
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cnpm unpublish &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;name]

&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cnpm info cnpm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Sun, 13 Apr 2014 16:00:00 +0800</pubDate>
        <link>https://zhaoda.net/2014/04/13/cnpm/</link>
        <guid isPermaLink="true">https://zhaoda.net/2014/04/13/cnpm/</guid>
        
        <category>npm</category>
        
        <category>nodejs</category>
        
        
      </item>
    
  </channel>
</rss>
