Giter VIP home page Giter VIP logo

isv_demo_php's Introduction

Getting Started

注意!注意!注意!demo中的数据库存储一定要修改为mysql等持久化存储。

demo部署后,一定要确认isv.log和filecache.php两个文件有写权限。

###创建套件前

登录到 http://open-dev.dingtalk.com/#/index?_k=lluyaq 创建套件(需要先注册开发者账号和钉钉企业才能创建套件)

###创建套件 3.填写套件信息 其中:

  • Token: 可以随意填写,填写完之后,打开工程的isv/config.php文件,把Token的值复制给TOKEN
  • 数据加密密钥:点击自动生成,然后打开工程的isv/config.php文件,把值复制给给ENCODING_AES_KEY
  • 应用ID:把应用ID的值复制给APPID
  • IP白名单: 调用钉钉API的合法IP列表(例如,工程部署在ip地址为123.xxx.xxx.xxx的主机上,那就填写"123.xxx.xxx.xxx")
  • 回调URL: url为工程地址/receive.php(例如,工程将部署在ip地址为123.xxx.xxx.xxx的主机上,端口为8080,那么我的回调URL即为:http://123.xxx.xxx.xxx:8080/receive.php,假如你有域名的话,也可以把IP地址换成域名)

4.配置PHP服务器环境(php+apache/nginx),安装mcrypt扩展(注意,一定要安装mcrypt扩展),保证apache服务根目录与可写权限(存储json数据)

5.将demo工程(isv)部署到服务器上

6.部署成功之后,点击『创建套件』弹窗中的『验证有效性』。

具体是如何验证回调URL有效性的,请查看(isv/receive.php)

7.创建套件成功之后,将得到的SuiteKey和SuiteSecret填写到工程的config.php中。

8.点击['测试企业和文档'],注册测试企业,注册完成后,点击『登录管理』到oa.dingtalk.com完成测试企业的激活

9.测试企业激活完成后,进入套件『管理』,在页面底部选择要授权的测试企业进行授权

10.修改微应用主页地址和PC主页地址

点击应用最右侧的编辑,编辑微应用信息,例如,工程部署在ip地址为123.xxx.xxx.xxx的主机上,端口为8080,那么微应用首页地址即为:http://123.xxx.xxx.xxx:8080/index.php?corpid=$CORPID$,PC版首页地址为:http://123.xxx.xxx.xxx:8080/indexpc.php?corpid=$CORPID$,点击保存。

11.打开钉钉,进入对应企业,即可看到微应用,点击进入

注意:Ticket推送状态成功之后,再授权企业

###创建企业应用 1.进入https://oa.dingtalk.com/#/microApp/microAppList,点击『新建应用』

2.配置PHP服务器环境(php+apache/nginx),安装mcrypt扩展(注意,一定要安装mcrypt扩展),保证apache服务根目录与可写权限(存储json数据)

3.微应用主页地址填写。地址为根目录/index.php,(例如,工程部署在ip地址为123.xxx.xxx.xxx的主机上,端口为8080,那么微应用首页地址即为:http://123.xxx.xxx.xxx:8080/index.php,PC版首页地址为:http://123.xxx.xxx.xxx:8080/indexpc.php,假如你有域名的话,也可以把IP地址换成域名) 修改config.php中的CORPID,SECRET,AGENTID,其中CORPID,SECRET在微应用设置页面https://oa.dingtalk.com/#/microApp/microAppSet获取,AGENTID在创建微应用的时候可以获取

4.微应用创建成功后,需要把微应用首页地址改为'根目录/index.php'

5.打开钉钉,进入对应企业,即可看到微应用,点击进入

###本DEMO具体实现

1.URL回调流程

请查看文档

2.jsapi权限验证配置流程

请查看文档

3.免登流程

请查看文档

isv_demo_php's People

Contributors

liningwangyi avatar opendingtalk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

isv_demo_php's Issues

php 7.2

mcrypt_module_open已经被废弃

php7加解密需要修改

按照下列示例代码修改:

class DingtalkCrypt
{

    private $key;//加密key
    private $iv;//加密向量

    private $corpid;//合作ID

    public static $block_size = 32;

    public function __construct($key, $corpid)
    {
        $this->corpid = $corpid;
        $this->key = base64_decode($key . "=");
    }


    public function pkcs7_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    public function _unpad($text)
    {
        $pad = ord(substr($text, -1));//取最后一个字符的ASCII 码值
        if ($pad < 1 || $pad > strlen($text)) {
            $pad = 0;
        }
        return substr($text, 0, (strlen($text) - $pad));
    }

    /**
     * 秘钥key和向量iv填充算法:大于block_size则截取,小于则填充"\0"
     * @param $str
     * @param $block_size
     * @return string
     */
    private function _pad0($str, $block_size)
    {
        return str_pad(substr($str, 0, $block_size), $block_size, chr(0)); //chr(0) 与 "\0" 等效,因为\0转义后表示空字符,与ASCII表里的0代表的字符一样
    }

    /**
     * 解密字符串
     * @param string $data 字符串
     * @param string $key 加密key
     * @return string
     */
    public function decrypt($text)
    {
        $iv = substr($this->key, 0, 16);

        //php7已废弃
//        $ciphertext_dec = base64_decode($text);
//        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
//        mcrypt_generic_init($module, $this->key, $iv);
//
//        $decrypted = mdecrypt_generic($module, $ciphertext_dec);
//        mcrypt_generic_deinit($module);
//        mcrypt_module_close($module);

        //php7替代方案
        $decrypted = openssl_decrypt(base64_decode($text), "aes-256-cbc", $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

        $result = $this->_unpad($decrypted);

        //去除16位随机字符串,网络字节序和AppId
        if (strlen($result) < 16) return "";

        $raw_content = substr($result, 16, strlen($result));
        $len_list = unpack("N", substr($raw_content, 0, 4));

        $len = $len_list[1];
        $content = substr($raw_content, 4, $len);
//        $corpid = substr($raw_content, $len + 4);

        return $content;
    }

    /**
     * 加密字符串
     * @param string $data 字符串
     * @param string $key 加密key
     * @return string
     */
    public function encrypt($text)
    {
        //获得16位随机字符串,填充到明文之前
        $random = $this->getRandomStr();
        $text = $random . pack("N", strlen($text)) . $text . $this->corpid;

        $iv = substr($this->key, 0, 16);
        $text = $this->pkcs7_pad($text, self::$block_size);

        //php7已废弃
//        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
//        mcrypt_generic_init($module, $this->key, $iv);
//        $encrypted = mcrypt_generic($module, $text);
//        mcrypt_generic_deinit($module);
//        mcrypt_module_close($module);

        //php7替代方案
        $encrypted = openssl_encrypt($text, "aes-256-cbc", $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

        return base64_encode($encrypted);
    }

    public function getRandomStr()
    {
        $str = "";
        $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($str_pol) - 1;
        for ($i = 0; $i < 16; $i++) {
            $str .= $str_pol[mt_rand(0, $max)];
        }
        return $str;
    }

}

参考:
升级 PHP7.1 后 openssl 解密 mcrypt AES 数据不兼容问题如何处理 - V2EX
https://www.v2ex.com/t/370493

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.