小程序開發注意事項

 

前言

公司須要開發一個小程序,小程序也算是一個新興技術,就本身研究了一下,總結了一些開發過程須要注意的事項,供你們參考。html

 

經驗總結

  • js、json是小程序必不可少的文件;

     

  • 除了app.js外,其它的js文件中必須有Page({}),否則會報錯;

     

  • 服務器域名必須是https;

     

  • 以前小程序新建項目時,選擇無APPId,能夠在程序中調用其它域名接口,可是如今也許版本更新了,本人測試,是沒法調用的,出現了403錯誤:

     

  • js中給顏色賦值時,須要用十六進制,不能用「red」、"white"等字符;

     

  •  「無 AppID」,沒法在真機上調試代碼,但不影響開發。html5

    •  

  • 開發時用Sublime Text軟件,展現用微信開發者工具比較好些;

     

  • 小程序上傳的代碼包大小上線是2048kb,所以程序中的圖片要放在服務器上,不要放在本地,否則會報以下錯誤:[html51]Error: 代碼包大小爲 2098 kb,超出限制 2048 kb;

     

  •  開發時是爲了方便調試,須要調用本地接口,能夠參考另外一篇博客java

    •  

  • 開發小程序時用到圖標,能夠從iconFont網站上下載;

     

  •  在小程序中使用<map>須要獲取位置經緯度,能夠在騰訊座標拾取器中獲取;web

    •  

  • 有時在一些方法中直接用「this」會報錯的,須要用「var that = this」轉換一下才行,如:
 1     var that = this;
 2     var timer = setInterval(function(){
 3       progressNum++;
 4       if(progressNum >= 100){
 5           clearInterval(timer)
 6       };
 7       that.setData({
 8         progress:progressNum
 9       });
10     },30)

 

  •  存儲數據

存儲輸入值spring

1 wx.setStorageSync('storage', this.data.storage)

 

從存儲中獲得數據json

 1 var that;  
 2 Page( {  
 3   data: {    
 4     storage:''
 5   },  
 6   onLoad: function(options) {  
 7     that = this; 
 8     //獲取存儲信息
 9     wx.getStorage({
10       key: 'storage',
11       success: function(res){
12         // success
13         that.setData({
14           storage:res.data
15         })
16       }
17     })
18   }
19 
20 })

 

  • JSON.stringify(res.data)能夠愉快的查看後臺的數據
  • 微信上傳圖像

    微信小程序端小程序

  chooseImage(){
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'http://127.0.0.1:8888/pesss/weChat/uploadImage.do', 
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: function (res) {
            var data = res.data
            //do something
          },fail:function(err){
            console.log(err)
          }
        })
      }
    })
  }

 

    java端微信小程序

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@Controller
public class ImageTestWebchatController {

    @RequestMapping(value = "/weChat/uploadImage", method = { RequestMethod.POST,RequestMethod.GET})
    public ModelAndView uploadImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("進入get方法!");

        MultipartHttpServletRequest req =(MultipartHttpServletRequest)request;
        MultipartFile multipartFile =  req.getFile("file");

        String realPath = "F:/image";
        try {
            File dir = new File(realPath);
            if (!dir.exists()) {
                dir.mkdir();
            }
            File file  =  new File(realPath,new Date().getTime() + ".jpg");
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        return null;
    }

}
相關文章
相關標籤/搜索