微信小程式與Java後台的通訊

2021-09-24 13:04:02 字數 2538 閱讀 3276

基本思路

string code = reqmap.get("code");

string url = maputils.getstring(configproperties, "url");//請求的位址

string grant_type = maputils.getstring(configproperties, "grant_type");

mapmap = new hashmap<>();

map.put("js_code",code);

map.put("grant_type",grant_type);

string wxreturnvalue = urlutil.sendpost(url, map);

將其轉化map,進行入庫操作:

maptempmap = jsonutils.convertjson2object(post, map.class);

if(tempmap.containskey("errcode"))

else if(tempmap.containskey("session_key"))

else

}//判斷快取資訊,如果存在去除掉舊的快取資訊,快取新的對應的資料。

//生成對應的uuid 將其建立起關係,返回即可。

補全上面提到的工具類:

/*

* 解密使用者敏感資料獲取使用者資訊

** @param sessionkey 資料進行加密簽名的金鑰

* @param encrypteddata 包括敏感資料在內的完整使用者資訊的加密資料

* @param iv 加密演算法的初始向量

* @return

*/public static mapgetuserinfo(string encrypteddata, string sessionkey, string iv)

// 初始化

security.addprovider(new bouncycastleprovider());

cipher cipher = cipher.getinstance("aes/cbc/pkcs7padding", "bc");

secretkeyspec spec = new secretkeyspec(keybyte, "aes");

algorithmparameters parameters = algorithmparameters.getinstance("aes");

parameters.init(new ivparameterspec(ivbyte));

cipher.init(cipher.decrypt_mode, spec, parameters);// 初始化

byte resultbyte = cipher.dofinal(databyte);

if (null != resultbyte && resultbyte.length > 0)

} catch (nosuchalgorithmexception e) catch (nosuchpaddingexception e) catch (invalidparameterspecexception e) catch (illegalblocksizeexception e) catch (badpaddingexception e) catch (unsupportedencodingexception e) catch (invalidkeyexception e) catch (invalidalgorithmparameterexception e) catch (nosuchproviderexception e) catch (ioexception e)

return null;

}

urlutils工具類:

/*

* 向指定 url 傳送post方法的請求

* @param url 傳送請求的 url

* @param parammap 請求引數

* @return 所代表遠端資源的響應結果

*/public static string sendpost(string url, mapparammap)

try

} catch (exception e)

//使用finally塊來關閉輸出流、輸入流

finally

if (in != null)

} catch (ioexception ex)

}return result;

}

jsonutils工具類:

/*

* json轉物件

* @param json

* @param type

* @param * @return

*/public static t convertjson2object(string json, classtype) throws ioexception

以上便可實現登入

微信小程式的元件通訊

實現方式類似於vue 引入子元件 在子元件上新增自定義屬性 例如 title,content 在子元件上的properties上新增要接收的屬性 title,content 在子元件上通過 就可以直接使用啦。ps 只是接收的時候vue是用props 小程式是properties 裡邊的方法都是一樣的...

前台微信小程式,後台解析openId

public static string decode string code 授權 必填 string grant type authorization code 預設引數 authorization code string sr sendget.sendget param system.out....

PHP後台實現微信小程式登入

1 呼叫wx.login獲取 code 2 呼叫wx.getuserinfo獲取簽名所需的 rawdata signatrue encryptdata 3 發起請求將獲取的資料傳送的後台。login function e method get success function info 服務端 登入...