小程序開發注意事項


 

前言

公司需要開發一個小程序,小程序也算是一個新興技術,就自己研究了一下,總結了一些開發過程需要注意的事項,供大家參考。

 

經驗總結

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

     

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

     

  • 服務器域名必須是https;

     

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

     

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

     

  •  “無 AppID”,無法在真機上調試代碼,但不影響開發。

    •  

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

     

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

     

  •  開發時是為了方便調試,需要調用本地接口,可以參考另一篇博客

    •  

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

     

  •  在小程序中使用<map>需要獲取位置經緯度,可以在騰訊坐標拾取器中獲取;

    •  

  • 有時在一些方法中直接用“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)

 

  •  存儲數據

存儲輸入值

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

 

從存儲中得到數據

 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;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM