微信公众号分享给微信好友和朋友圈

前端

引入js

 

后台

    /**
     * wechat
     * 获取jsapi config参数
     */
    public function jsapi_config_params() {
        $sign_data = [];
        $sign_data['jsapi_ticket'] = $this->get_jsapi_ticket($_SESSION['user']['tenant_id']);//获取jsapi_ticket
        $this->return['data']['noncestr'] = $sign_data['noncestr'] = md5(rand(100000,999999));//生成noncestr
        $this->return['data']['timestamp'] = $sign_data['timestamp'] = time();//生成timestamp
        $this->return['data']['url'] = $sign_data['url'] = $this->input->post('url');//生成url
        $this->return['data']['signature'] = $this->get_jsapi_sign($sign_data);//生成signature
        $this->return['data']['appid'] = $this->app_id;//这是appid
        $this->result();

    }

//验证签名

    private function get_jsapi_sign($data) {
        ksort($data);
        $tmpSign = $this->arrToTempStr($data);
        $sign = strtolower(sha1($tmpSign));
        return $sign;

    }

    /**
     * wechat
    * 获取当前jsapi_ticket
    */

    public function get_jsapi_ticket($tenant_id) {

    //将jsapi_ticket存入数据库$tenant_id为数据库的唯一标识数据库表结构如下

    微信公众号分享给微信好友和朋友圈_第1张图片

        $this->load->model('system_wechat_jsapi_tickets_model','system_wechat_jsapi_tickets');

//查询出jsapi_ticket是否存在

        $res = $this->system_wechat_jsapi_tickets->find(['tenant_id' => $tenant_id]);

//不存在就创建一条

        if (!$res || !is_array($res)) {

            $jsapi_ticket = $this->get_new_jsapi_ticket($tenant_id);//获取一个新的jsapi_ticket

        //在数据库增加一条新的数据

            $this->system_wechat_jsapi_tickets->create(['jsapi_ticket' => $jsapi_ticket,'time' => date('Y-m-d H:i:s'),'tenant_id' => $tenant_id]);
            return $jsapi_ticket;//返回jsapi_ticket
        } else if (time() - strtotime($res['time']) > 7150) {//如果生成的jsapi_ticket时间超过7200秒就更改jsapi_ticket
            $jsapi_ticket = $this->get_new_jsapi_ticket($tenant_id);
            $this->system_wechat_jsapi_tickets->update(['jsapi_ticket' => $jsapi_ticket,'time' => date('Y-m-d H:i:s')],['id' => $res['id']]);
            return $jsapi_ticket;//返回jsapi_ticket
        } else {
            return $res['jsapi_ticket'];//正常直接返回数据库存放的jsapi_ticket
        }

    }

 

    /**
     *重新获取jsapi_ticket
     */
    private function get_new_jsapi_ticket($tenant_id) {

        $result = file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $this->get_access_token($tenant_id) . '&type=jsapi');

//get_access_token带着唯一标识获取token

        $result = json_decode($result,true);
        return $result['ticket'];
    }

 

    /**
     * 获取当前access_token同获取jsapi_ticket一样数据库表结构为

     */

    微信公众号分享给微信好友和朋友圈_第2张图片

    public function get_access_token($tenant_id) {
        $this->load->model('system_wechat_access_token_model','system_wechat_access_token');
        $res = $this->system_wechat_access_token->find(['tenant_id' => $tenant_id]);
        if (!$res || !is_array($res)) {
            $access_token = $this->get_new_access_token();
            // 插入access_token
            $this->system_wechat_access_token->create(['access_token' => $access_token,'time' => date('Y-m-d H:i:s'),'tenant_id' => $tenant_id]);
            return $access_token;
        } else if (time() - strtotime($res['time']) > 7150) {
            $access_token = $this->get_new_access_token();
            // 更新access_token
            $this->system_wechat_access_token->update(['access_token' => $access_token,'time' => date('Y-m-d H:i:s')],['id' => $res['id']]);
            return $access_token;
        } else {
            return $res['access_token'];
        }

    }

    /**
     * 重新从微信获取access_token
     */
    private function get_new_access_token() {
        $result = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->app_id . '&secret=' . $this->app_secret);
        $result = json_decode($result,true);
        return $result['access_token'];

    }

    /**
     * 数组转签名字符串
     */
    private function arrToTempStr($arr) {
        $str = '';
        foreach ($arr as $key => $value) {
            $str .= $key . '=' . $value . '&';
        }
        return substr($str,0,-1);
    }

你可能感兴趣的:(wechat(公众号),ci)