国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序 開發(fā)中的問題(simba_wx)

這篇具有很好參考價值的文章主要介紹了微信小程序 開發(fā)中的問題(simba_wx)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

一、將 proto 文件轉(zhuǎn)成 json 文件

需要先下載 protobufjs-cli 依賴

npm i -D protobufjs-cli

然后再當(dāng)前目錄下的終端,執(zhí)行以下命令

npx pbjs -t json proto/test.proto > proto/test.json

proto/test.proto > proto/test.json :意思是找到當(dāng)前目錄的 proto 文件夾下的 test.proto 文件,將該文件轉(zhuǎn)為 test.json 并將該文件放到 proto 文件夾下

二、使用 test.json 文件

npm i -S protobufjs

由于收到數(shù)據(jù)是二進(jìn)制的,雖然拿到的 proto 文件的 json 結(jié)構(gòu),但這還是不能直接使用,還需要下轉(zhuǎn) protobufjs 依賴,將收到的二進(jìn)制數(shù)據(jù)根據(jù) test.json 文件轉(zhuǎn)成對應(yīng)的 json 結(jié)構(gòu)

import protobuf from "protobufjs";
import protoJson from "./proto/test.json";
?
// 使用 protobufjs 拿到 test.json 的 json 結(jié)構(gòu)
const root = protobuf.Root.fromJSON(protoJson);
// 找到要翻譯的包和字段
const protoData = root.lookupType("interface_message.GeneralDetectionMapData");
?
// data是收到二進(jìn)制數(shù)據(jù),使用 decode 方法,將二進(jìn)制數(shù)據(jù)根據(jù)【包和字段】轉(zhuǎn)成 json 結(jié)構(gòu)的數(shù)據(jù),該過程簡稱:解密
const data = protoData.decode(data);
?
// 當(dāng)需要發(fā)送修改過后的 proto 數(shù)據(jù)發(fā)送給后端時,使用 encode 可將數(shù)據(jù)轉(zhuǎn)成 proto 結(jié)構(gòu)的數(shù)據(jù),該過程簡稱:加密
const protoData = protoData.encode(data).finish()

例如:

const serverUrl = 'http://192.168.200.200:7081';

var protobuf = require('@/weichatPb/protobuf.js');
const awesomeConfig = require('@/proto/order.js');
var AwesomeRoot = protobuf.Root.fromJSON(awesomeConfig);

// 錯誤結(jié)果
var ErrorRequest = AwesomeRoot.lookupType("ErrorMessage");
// 結(jié)果sucess
var OperationGeneralResp = AwesomeRoot.lookupType("OperationGeneralResp");

// 獲取驗(yàn)證碼
var VerificationCodeResp = AwesomeRoot.lookupType("VerifyCodeMessage"); // 獲取驗(yàn)證碼結(jié)果

// 登錄
var SignInRes = AwesomeRoot.lookupType("UserLogInRequest"); // 登錄傳值
var SignInResp = AwesomeRoot.lookupType("LoginResp"); // 登錄結(jié)果

// 獲取初始信息 --> 1:銷售  2:商務(wù)
var AuthorInitRes = AwesomeRoot.lookupType("AuthorizationFileTicketInitReq"); // 傳值
var AuthorInitResp = AwesomeRoot.lookupType("AuthorizationFileTicketInitResp"); // 結(jié)果

// 查詢授權(quán)工單
var SearchAuthorRes = AwesomeRoot.lookupType("GetAuthorizationFileTicketReq"); // 傳值
var SearchAuthorResp = AwesomeRoot.lookupType("SalesGetAuthorizationFileTicketResp"); // 結(jié)果

// 新建授權(quán)工單
var AddAuthorRes = AwesomeRoot.lookupType("AddAuthorizationFileTicketReq"); // 傳值

// 關(guān)閉工單
var DelRes = AwesomeRoot.lookupType("CloseAuthorizationFileTicketReq"); // 傳值

// 修改工單
var UpdateRes = AwesomeRoot.lookupType("EditAuthorizationFileTicketReq"); // 傳值

// 審核工單
var ProcessRes = AwesomeRoot.lookupType("AuditAuthorizationFileTicketReq"); // 傳值

// 下載附件
var AnnexRes = AwesomeRoot.lookupType("DeleteAuthFileAttachmentReq"); // 傳值

// 上傳附件
var UpAnnexRes = AwesomeRoot.lookupType("AddAuthFileAttachmentReq"); // 傳值

const timeout = 15000;
export class Api {
	// 公共發(fā)送請求體
	static _request(url, requestBuffer, resMode) {
		var promise = new Promise((resolve, reject) => {
			var that = this;
			uni.request({
				url: serverUrl + url,
				method: "POST",
				dataType: 'protobuf',
				data: requestBuffer,
				responseType: "arraybuffer",
				timeout: timeout,
				header: {
					"X-Requested-With": "XMLHttpRequest",
					"Content-Type": "application/octet-stream",
				},
				success(res) {
					if (res.statusCode == 200) {
						var deMessage = resMode.decode(res.data);
					} else {
						var deMessage = ErrorRequest.decode(res.data);
					}
					resolve(deMessage);
				},
				error: function (e) {
					reject("網(wǎng)絡(luò)出錯");
					console.log("網(wǎng)絡(luò)出錯")
				}
			});
		});
		return promise;
	}
	// 序列化數(shù)據(jù)
	static getUnit8Data(Message, payload) {
		var message = Message.create(payload);
		var buffer = Message.encode(message).finish();
		var requestBuffer = new Uint8Array([...buffer]).buffer;
		return requestBuffer
	}

	// 獲取初始信息
	static async getAuthInit(payload) {
		var url = "/AuthorizationFileTicketInit";
		var requestBuffer = Api.getUnit8Data(AuthorInitRes, payload)
		const res = await Api._request(url, requestBuffer, AuthorInitResp);
		console.log(res)
		return res;
	}

	// 新建授權(quán)工單
	static async setAuthData(payload) {
		var url = "/AddAuthorizationFileTicket";
		var requestBuffer = Api.getUnit8Data(AddAuthorRes, payload)
		const res = await Api._request(url, requestBuffer, OperationGeneralResp);
		console.log(res)
		return res;
	}

	// 查詢授權(quán)工單
	static async getAuthData(payload) {
		var url = "/GetAuthorizationFileTicket";
		var requestBuffer = Api.getUnit8Data(SearchAuthorRes, payload)
		const res = await Api._request(url, requestBuffer, SearchAuthorResp);
		console.log(res)
		return res;
	}

	// 關(guān)閉工單
	static async getDel(payload) {
		var url = "/CloseAuthorizationFileTicket";
		var requestBuffer = Api.getUnit8Data(DelRes, payload)
		const res = await Api._request(url, requestBuffer, OperationGeneralResp);
		console.log(res)
		return res;
	}

	// 修改工單
	static async getUpdate(payload) {
		var url = "/EditAuthorizationFileTicket";
		var requestBuffer = Api.getUnit8Data(UpdateRes, payload)
		const res = await Api._request(url, requestBuffer, OperationGeneralResp);
		console.log(res)
		return res;
	}

	// 審核工單
	static async getProcess(payload) {
		var url = "/AuditAuthorizationFileTicket";
		var requestBuffer = Api.getUnit8Data(ProcessRes, payload)
		const res = await Api._request(url, requestBuffer, OperationGeneralResp);
		console.log(res)
		return res;
	}

	// 刪除附件
	static async getAnnex(payload) {
		var url = "/DeleteAuthFileAttachment";
		var requestBuffer = Api.getUnit8Data(AnnexRes, payload)
		const res = await Api._request(url, requestBuffer, OperationGeneralResp);
		console.log(res)
		return res;
	}

	// 上傳附件
	static async getUpAnnex(payload) {
		var url = "/AddAuthFileAttachment";
		var requestBuffer = Api.getUnit8Data(UpAnnexRes, payload)
		const res = await Api._request(url, requestBuffer, OperationGeneralResp);
		console.log(res)
		return res;
	}

	// 獲取驗(yàn)證碼
	static async getVerificationCode() {
		let payload = {}
		var url = "/GenerateVerificationCode";
		var requestBuffer = Api.getUnit8Data(AuthorInitRes, payload)
		const res = await Api._request(url, requestBuffer, VerificationCodeResp);
		return res;
	}

	// 登錄
	static async getSignIn(payload) {
		var url = "/UserLogin";
		var requestBuffer = Api.getUnit8Data(SignInRes, payload)
		const res = await Api._request(url, requestBuffer, SignInResp);
		return res;
	}
}

三、微信小程序插件網(wǎng)址

四、vant-weapp網(wǎng)址

五、文本讀取文件

微信小程序 開發(fā)中的問題(simba_wx)

// this.fileList3 格式如上圖所示即可
if (this.fileList3 != 0) {
  for (let i = 0; i < this.fileList3.length; i++) {
    const fm = wx.getFileSystemManager(); // 獲取文件管理器
    var arrayBuffer = new ArrayBuffer(`${this.fileList3[i].size}`);
    try {
      var fd = fm.openSync({
        filePath: this.fileList3[i].path,
      });
      fm.readSync({
        fd: fd,
        arrayBuffer: arrayBuffer, //數(shù)據(jù)寫入的緩沖區(qū)
        length: this.fileList3[i].size, //讀取的字節(jié)數(shù),默認(rèn)0
      }); // 讀取文件內(nèi)容
      // var enc = new TextDecoder("utf-8");
      // var fileContent = enc.decode(arrayBuffer); //將ArrayBuffer轉(zhuǎn)為字符串

      filesAll.push({
        name: this.fileList3[i].name,
        file: new Uint8Array(arrayBuffer),
      });

      fm.closeSync({ fd: fd }); // 關(guān)閉文件
    } catch (exception) {
      console.log("onShow >> 讀文件失敗, 異常消息: ", exception);
    }
  }
}

六、字符串內(nèi)容轉(zhuǎn)arraybuffer

微信小程序 JS 字符串string與utf8編碼的arraybuffer的相互轉(zhuǎn)換

&#8194最近在做一個微信小程序,和后端用websocket連接,后端要求傳輸過去的信息是UTF8編碼的二進(jìn)制信息。JS并沒有可以直接進(jìn)行轉(zhuǎn)換的庫函數(shù),因此必須自己寫一個編碼以及解析的函數(shù)。
????最開始采用了一個字符一個字符的charCodeAt,但是通過這種方式可以傳輸非中文的內(nèi)容,有中文時則無法正確的轉(zhuǎn)換。
????后來參考了網(wǎng)上的一些資料,通過先將字符串編碼并轉(zhuǎn)換為byte[],再轉(zhuǎn)換為對應(yīng)的arraybuffer(解析同理,先將arraybuffer轉(zhuǎn)換為byte[],再進(jìn)行解碼)
????編碼及解碼的過程較復(fù)雜,不過這兩個函數(shù)是可以直接使用的,同時支持中文和英文。
????
字符串內(nèi)容轉(zhuǎn)arraybuffer

stringToArrayBuffer(str) {
   var bytes = new Array();
   var len, c;
   len = str.length;
   for (var i = 0; i < len; i++) {
     c = str.charCodeAt(i);
     if (c >= 0x010000 && c <= 0x10ffff) {
       bytes.push(((c >> 18) & 0x07) | 0xf0);
       bytes.push(((c >> 12) & 0x3f) | 0x80);
       bytes.push(((c >> 6) & 0x3f) | 0x80);
       bytes.push((c & 0x3f) | 0x80);
     } else if (c >= 0x000800 && c <= 0x00ffff) {
       bytes.push(((c >> 12) & 0x0f) | 0xe0);
       bytes.push(((c >> 6) & 0x3f) | 0x80);
       bytes.push((c & 0x3f) | 0x80);
     } else if (c >= 0x000080 && c <= 0x0007ff) {
       bytes.push(((c >> 6) & 0x1f) | 0xc0);
       bytes.push((c & 0x3f) | 0x80);
     } else {
       bytes.push(c & 0xff);
     }
   }
   var array = new Int8Array(bytes.length);
   for (var i in bytes) {
     array[i] = bytes[i];
   }
   return array.buffer;
 },

arraybuffer轉(zhuǎn)字符串內(nèi)容

function arrayBufferToString(arr){
    if(typeof arr === 'string') {  
        return arr;  
    }  
    var dataview=new DataView(arr.data);
    var ints=new Uint8Array(arr.data.byteLength);
    for(var i=0;i<ints.length;i++){
      ints[i]=dataview.getUint8(i);
    }
    arr=ints;
    var str = '',  
        _arr = arr;  
    for(var i = 0; i < _arr.length; i++) {  
        var one = _arr[i].toString(2),  
            v = one.match(/^1+?(?=0)/);  
        if(v && one.length == 8) {  
            var bytesLength = v[0].length;  
            var store = _arr[i].toString(2).slice(7 - bytesLength);  
            for(var st = 1; st < bytesLength; st++) {  
                store += _arr[st + i].toString(2).slice(2);  
            }  
            str += String.fromCharCode(parseInt(store, 2));  
            i += bytesLength - 1;  
        } else {  
            str += String.fromCharCode(_arr[i]);  
        }  
    }  
    return str; 
}

七、微信小程序頁面跳轉(zhuǎn) 的幾種方式

wx.navigateTo(OBJECT)

保留當(dāng)前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個頁面, 小程序中左上角有一個返回箭頭,可返回上一個頁面,也可以通過方法 wx.navigateBack 返回原頁面.

wx.redirectTo(OBJECT)

關(guān)閉當(dāng)前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個頁面, 左上角沒有返回箭頭,不能返回上一個頁面

wx.redirectTo(OBJECT)

關(guān)閉當(dāng)前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個頁面, 左上角沒有返回箭頭,不能返回上一個頁面

wx.switchTab(OBJECT)

跳轉(zhuǎn)到 tabBar 頁面,并關(guān)閉其他所有非 tabBar 頁面, wx.navigateTo 和 wx.redirectTo 不允許跳轉(zhuǎn)到 tabbar 頁面,只能用 wx.switchTab 跳轉(zhuǎn)到 tabbar 頁面

wx.reLaunch(OBJECT)

關(guān)閉所有頁面,打開到應(yīng)用內(nèi)的某個頁面, 跟wx.redirectTo 一樣左上角不會出現(xiàn)返回箭頭,但兩者卻不完全相同

這里要提到小程序中的 getCurrentPages() 方法,在wx.navigateTo中,每跳轉(zhuǎn)一個新的頁面,其原始頁面就會被加入堆棧,通過調(diào)用wx.navigateBack(OBJECT)可通過獲取堆棧中保存的頁面 返回上一級或多級頁面

wx.redirectTo,方法則不會被加入堆棧,但仍可通過wx.navigateBack(OBJECT)方法返回之前堆棧中的頁面

wx.reLaunch 方法則會清空當(dāng)前的堆棧

例如: A跳轉(zhuǎn)到B,B跳轉(zhuǎn)到C,C navigateBack,將返回b頁面文章來源地址http://www.zghlxwxcb.cn/news/detail-425821.html

// 此處是A頁面
wx.navigateTo({
  url: 'B?id=1'
})
// 此處是B頁面
wx.navigateTo({
  url: 'C?id=1'
})
// 在C頁面內(nèi) navigateBack,將返回b頁面
wx.navigateBack({
  delta: 1
})
// 此處是B頁面
wx.redirectTo({
 url: 'C?id=1'
})
// 在C頁面內(nèi) navigateBack,則會返回a頁面 
wx.navigateBack({
 delta: 1
 })
// 此處是B頁面
wx.reLaunch({
 url: 'C?id=1'
})
// 在C頁面內(nèi) navigateBack,則無效

到了這里,關(guān)于微信小程序 開發(fā)中的問題(simba_wx)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 微信小程序詳細(xì)講解頁面?zhèn)髦?,globalData,wx,web前端開發(fā)基礎(chǔ)

    微信小程序詳細(xì)講解頁面?zhèn)髦?,globalData,wx,web前端開發(fā)基礎(chǔ)

    在其他非app.js文件中修改: var app = getApp() ? app.globalData.hasLogin = true 二、 使用 wx.navigateTo 與 wx.redirectTo 的時候,可以將部分?jǐn)?shù)據(jù)放在 url 里面,并在新頁面 onLoad 的時候初始化 // Navigate wx.navigateTo({ url: ‘…/pageD/pageD?name=raymondgender=male’, }) ? // Redirect wx.redirectTo({ url: ‘…/p

    2024年04月25日
    瀏覽(23)
  • 微信小程序使用webview內(nèi)嵌h5頁面 wx.miniProgram.getEnv失效問題

    背景 最近接到一個h5需求,和普通的h5不一樣,這個h5頁面是嵌入到小程序中使用的,需求簡單來說就是展示一個跳轉(zhuǎn)按鈕,判斷如果是小程序環(huán)境下就進(jìn)行跳轉(zhuǎn)到其他小程序頁面。 實(shí)現(xiàn)思路 核心邏輯其實(shí)就是判斷小程序環(huán)境這一塊,我們可以直接使用wxsdk來進(jìn)行判斷小程序

    2024年02月09日
    瀏覽(168)
  • 【微信小程序內(nèi)嵌H5調(diào)用wx.miniProgram.navigateTo跳轉(zhuǎn)無效問題】

    之前項(xiàng)目遇到的,各種判斷邏輯都走通了,代碼走到wx.miniProgram.navigateTo面前了就是跳轉(zhuǎn)不了,試了很多種方法,都懷疑是這個api不適用了,結(jié)果一次嘗試,發(fā)現(xiàn)還是地址的問題。 客戶給的跳轉(zhuǎn)地址: “pages/check/index/index.html?type=1” 試過但沒成功的地址: “/pages/check/index/i

    2024年02月16日
    瀏覽(21)
  • 解決微信小程序bindgetphonenumber和wx.login獲取的code不同步問題

    解決微信小程序bindgetphonenumber和wx.login獲取的code不同步問題

    微信小程序使用 手機(jī)號快速驗(yàn)證組件 在獲取用戶手機(jī)號的時候,經(jīng)常會因?yàn)樘峤粎?shù)的code和iv、encryptedData參數(shù)匹配不一致而報錯。其根本原因在官方有相應(yīng)的解釋: 注意使用舊版本組件時 ,需先調(diào)用wx.login接口。所以在用戶點(diǎn)了拒絕之后授權(quán)之后,需要重新獲取調(diào)用wx.lo

    2024年02月11日
    瀏覽(38)
  • 微信小程序toast組件(解決wx.showToast文本最多顯示兩行問題)

    微信小程序toast組件(解決wx.showToast文本最多顯示兩行問題)

    創(chuàng)建toast組件 ?index.wxmi index.less index.json index.ts toast.js 使用 index.json index.wxml ?index.ts ?效果

    2024年02月12日
    瀏覽(37)
  • 微信小程序中的條件渲染和列表渲染,wx:if ,wx:elif,wx:else,wx:for,wx:key的使用,以及block標(biāo)記和hidden屬性的說明

    微信小程序中的條件渲染和列表渲染,wx:if ,wx:elif,wx:else,wx:for,wx:key的使用,以及block標(biāo)記和hidden屬性的說明

    1.1. 語法格式 (wx:if, wx:elif ,wx:else) 當(dāng)condition條件為true時,代碼塊渲染顯示,為false時,代碼塊不進(jìn)行渲染. 可以結(jié)合 wx:elif=“{{condition}}” 和 wx:else來進(jìn)行判斷 1.2. block標(biāo)記 可以使用block標(biāo)記,一次性的控制多個組件的顯示與隱藏,block標(biāo)記本身并不進(jìn)行渲染。 block并不是一個組

    2024年02月16日
    瀏覽(27)
  • 微信小程序 wx.showModal

    微信小程序--wx.showModal_海轟Pro的博客-CSDN博客

    2024年02月15日
    瀏覽(19)
  • 微信小程序-支付(wx.requestPayment)

    微信小程序-支付(wx.requestPayment)

    (學(xué)習(xí)中,持續(xù)更新) 直接調(diào)用的接口為wx.requestPayment(小程序前端調(diào)用)。 官方文檔的請求例子為: 其中paySign官方文檔給出了一個例子: MD5(appId=wxd678efh567hg6787nonceStr=5K8264ILTKCH16CQ2502SI8ZNMTM67VSpackage=prepay_id=wx2017033010242291fcfe0db70013231072signType=MD5timeStamp=1490840662key=qazwsxedcrfvtg

    2024年02月12日
    瀏覽(20)
  • 微信小程序 wx:if使用

    在微信小程序中,可以使用 wx:if 指令來控制某個元素是否需要被渲染到頁面上。根據(jù)條件表達(dá)式的結(jié)果, wx:if 指令決定元素是否顯示。 下面是使用 wx:if 的基本示例: 在上述代碼中, wx:if 指令的值為一個條件表達(dá)式 {{condition}} ,根據(jù)該條件表達(dá)式的結(jié)果決定是否渲染 view 元

    2024年02月09日
    瀏覽(25)
  • 微信小程序-登錄(wx.login)

    微信小程序-登錄(wx.login)

    用戶微信登錄小程序有兩種情況,分別為彈出登錄提示和不彈出登錄提示兩種。彈出登錄提示的情況下,用戶確定后會向后臺傳入更多參數(shù),例如用戶昵稱等。不彈出登錄提示只能獲取到用戶的臨時登錄憑證code。主要根據(jù)第二種情況進(jìn)行描述。 這個code具有時效性,能用于區(qū)

    2024年02月15日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包