微信小程序生成帶參數的二維碼以及參數的獲取

POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

$params['page'] = 'pages/home/home';

$options['width'] = 300;

$options['scene'] = '0&zhaiali';

$data array_merge($params$options);

$result $this->http_post($api, self::json_encode($data));

//此處用了自定義的靜態方法json_encode

if (!is_object(json_decode($result))) { //判斷返回值是否爲json,是否生成小程序碼成功

            if (empty($filename)) {            // 如果沒有指定要保存到的文件

                $path = UPLOAD_PATH . 'WxaQrcode/';

                if (!is_dir($path)) {

                    @mkdir($path, 0777);

                }

                if ($data['is_hyaline'] === true) { //如果透明小程序碼生成png

                    $filename $path . time() . '.png';

                else {

                    $filename $path . time() . '.jpg';

                }

            }

            $res file_put_contents($filename$result);

            if (!$res) {

                return false;

            }

            return $filename;

        else {

            return json_decode($result);

 }

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

/**

 * 微信api不支持中文轉義的json結構

 * @param array $arr

 */

static function json_encode($arr) {

    if (count($arr) == 0) return "[]";

    $parts array ();

    $is_list = false;

    //Find out if the given array is a numerical array

    $keys array_keys $arr );

    $max_length count $arr ) - 1;

    if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1

        $is_list = true;

        for($i = 0; $i count $keys ); $i ++) { //See if each key correspondes to its position

            if ($i != $keys [$i]) { //A key fails at position check.

                $is_list = false; //It is an associative array.

                break;

            }

        }

    }

    foreach $arr as $key => $value ) {

        if (is_array $value )) { //Custom handling for arrays

            if ($is_list)

                $parts [] = self::json_encode ( $value ); /* :RECURSION: */

            else

                $parts [] = '"' $key '":' . self::json_encode ( $value ); /* :RECURSION: */

        else {

            $str '';

            if (! $is_list)

                $str '"' $key '":';

            //Custom handling for multiple data types

            if (!is_string $value ) && is_numeric $value ) && $value<2000000000)

                $str .= $value//Numbers

            elseif ($value === false)

                $str .= 'false'//The booleans

            elseif ($value === true)

                $str .= 'true';

            else

                $str .= '"' addslashes $value ) . '"'//All other things

            // :TODO: Is there any more datatype we should be in the lookout for? (Object?)

            $parts [] = $str;

        }

    }

    $json = implode ( ','$parts );

    if ($is_list)

        return '[' $json ']'//Return numerical JSON

    return '{' $json '}'//Return associative JSON

}

if (options.scene) {

    var scene = decodeURIComponent(options.scene);

    wx.setStorageSync('scene', scene);

    var navid = scene.split("&")[0];

    var source = scene.split('&')[1];

    console.log("scene:" + navid + "," + source);

}

 

 

 

 

 

 

 

 

 

 

 

發佈了45 篇原創文章 · 獲贊 23 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章