├── README.md
└── 小程序
├── picker
└── index.vue
├── 图片压缩
├── common.js
└── index.vue
└── 地图定位
├── common.js
└── index.vue
/README.md:
--------------------------------------------------------------------------------
1 | # code-diary
--------------------------------------------------------------------------------
/小程序/picker/index.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
11 |
12 |
13 | 多列选择器
14 |
15 |
16 | 当前选择:{{mulArr[0][mulIndex[0]]}},{{mulArr[1][mulIndex[1]]}},{{mulArr[2][mulIndex[2]]}}
17 |
18 |
19 |
20 |
21 |
22 |
23 |
80 |
81 |
83 |
--------------------------------------------------------------------------------
/小程序/图片压缩/common.js:
--------------------------------------------------------------------------------
1 | //*************** 图片压缩 ***********
2 | // 判断图片大小是否满足需求
3 | function imageSizeIsLessLimitSize(imagePath, limitSize, lessCallBack, moreCallBack) {
4 | wx.getFileInfo({
5 | filePath: imagePath,
6 | success(res) {
7 | console.log("压缩前图片大小:", res.size / 1024, 'kb');
8 | if (res.size > 1024 * limitSize) {
9 | moreCallBack();
10 | } else {
11 | lessCallBack();
12 | }
13 | }
14 | })
15 | };
16 | /**
17 | * 获取画布图片
18 | */
19 | // 利用cavas进行压缩 每次压缩都需要ctx.draw() wx.canvasToTempFilePath()连用
20 | function getCanvasImage(canvasId, imagePath, imageW, imageH, getImgsuccess) {
21 | const ctx = wx.createCanvasContext(canvasId);
22 | ctx.drawImage(imagePath, 0, 0, imageW, imageH);
23 | ctx.draw(false, setTimeout(function() { // 一定要加定时器,因为ctx.draw()应用到canvas是有个时间的
24 | wx.canvasToTempFilePath({
25 | canvasId: canvasId,
26 | x: 0,
27 | y: 0,
28 | width: imageW,
29 | height: imageH,
30 | quality: 1,
31 | success: function(res) {
32 | getImgsuccess(res.tempFilePath);
33 | },
34 | });
35 | }, 200));
36 | };
37 |
38 | // 主调用方法
39 |
40 | /**
41 | * 获取小于限制大小的Image, limitSize默认为100KB,递归调用。
42 | */
43 | function getLessLimitSizeImage(canvasId, imagePath, limitSize = 100, drawWidth, callBack) {
44 | imageSizeIsLessLimitSize(imagePath, limitSize,
45 | (lessRes) => {
46 | callBack(imagePath);
47 | },
48 | (moreRes) => {
49 | wx.getImageInfo({
50 | src: imagePath,
51 | success: function(imageInfo) {
52 | var maxSide = Math.max(imageInfo.width, imageInfo.height);
53 | //画板的宽高默认是windowWidth
54 | var windowW = drawWidth;
55 | var scale = 1;
56 | if (maxSide > windowW) {
57 | scale = windowW / maxSide;
58 | }
59 | var imageW = Math.trunc(imageInfo.width * scale);
60 | var imageH = Math.trunc(imageInfo.height * scale);
61 | console.log('调用压缩', imageW, imageH);
62 | getCanvasImage(canvasId, imagePath, imageW, imageH,
63 | (pressImgPath) => {
64 | getLessLimitSizeImage(canvasId, pressImgPath, limitSize, drawWidth * 0.95, callBack);
65 | }
66 | );
67 | }
68 | })
69 | }
70 | )
71 | };
72 | // 图片转basee64 io操作 使用异步方式
73 | function getBase64(img) {
74 | return new Promise(function(resolve, reject) {
75 | const FSM = wx.getFileSystemManager();
76 | FSM.readFile({
77 | filePath: img,
78 | encoding: 'base64',
79 | success(data) {
80 | resolve(data)
81 | }
82 | })
83 | })
84 | }
85 |
86 | export { getLessLimitSizeImage, imageSizeIsLessLimitSize, getCanvasImage, getBase64 }
87 |
--------------------------------------------------------------------------------
/小程序/图片压缩/index.vue:
--------------------------------------------------------------------------------
1 |
2 |
21 |
22 |
23 |
101 |
102 |
105 |
--------------------------------------------------------------------------------
/小程序/地图定位/common.js:
--------------------------------------------------------------------------------
1 | // *********** 地图 ************
2 | import QQmapWX from './qqmap.min'
3 |
4 | //通过经纬度得到地址相关信息 该操作是异步操作,所以最好加个callback
5 | // function getAddress(long, lat, callback) {
6 | // // long 经度 lat 纬度
7 | // var address = '';
8 | // var locationString = lat + "," + long;
9 | // console.log(locationString);
10 | // wx.request({
11 | // url: 'http://apis.map.qq.com/ws/geocoder/v1/',
12 | // data: {
13 | // "key": "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxx",
14 | // "location": locationString
15 | // },
16 | // method: 'GET',
17 | // success: function(r) {
18 | // //输出一下位置信息
19 | // console.log('用户位置信息', r.data.result.address);
20 | // //r.data.result.address获得的就是用户的位置信息,将它保存到一个全局变量上
21 | // address = r.data.result.address;
22 | // callback(r.data.result);
23 | // },
24 | // fail(err) {
25 | // console.log(err)
26 | // }
27 | // });
28 | // return address
29 | // }
30 |
31 | function sdkAddress(long, lat, callback) {
32 | var address = '';
33 | var qqmapsdk = new QQmapWX({
34 | key: 'xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxx'
35 | });
36 | qqmapsdk.reverseGeocoder({
37 | location: {
38 | longitude: long,
39 | latitude: lat
40 | },
41 | success(res) {
42 | // 输出位置信息
43 | address = res.result.address;
44 | callback(res.result.address);
45 | }
46 |
47 | });
48 | return address
49 | }
50 | export { getAddress, sdkAddress }
51 |
52 |
--------------------------------------------------------------------------------
/小程序/地图定位/index.vue:
--------------------------------------------------------------------------------
1 |
2 |
20 |
21 |
22 |
76 |
77 |
80 |
--------------------------------------------------------------------------------