11package cn .binarywang .wx .miniapp .api .impl ;
22
3- import org .apache .commons .codec .digest .DigestUtils ;
4-
53import cn .binarywang .wx .miniapp .api .WxMaService ;
64import cn .binarywang .wx .miniapp .api .WxMaUserService ;
75import cn .binarywang .wx .miniapp .bean .WxMaJscode2SessionResult ;
86import cn .binarywang .wx .miniapp .bean .WxMaPhoneNumberInfo ;
97import cn .binarywang .wx .miniapp .bean .WxMaUserInfo ;
8+ import cn .binarywang .wx .miniapp .config .WxMaConfig ;
109import cn .binarywang .wx .miniapp .util .crypt .WxMaCryptUtils ;
10+ import com .google .gson .JsonArray ;
11+ import com .google .gson .JsonObject ;
12+ import me .chanjar .weixin .common .error .WxError ;
1113import me .chanjar .weixin .common .error .WxErrorException ;
14+ import me .chanjar .weixin .common .util .SignUtils ;
15+ import org .apache .commons .codec .binary .Hex ;
16+ import org .apache .commons .codec .digest .DigestUtils ;
17+
18+ import javax .crypto .Mac ;
19+ import javax .crypto .spec .SecretKeySpec ;
20+ import java .security .InvalidKeyException ;
21+ import java .security .NoSuchAlgorithmException ;
22+ import java .util .Map ;
1223
1324/**
1425 * @author <a href="https://github.com/binarywang">Binary Wang</a>
@@ -30,6 +41,30 @@ public WxMaUserInfo getUserInfo(String sessionKey, String encryptedData, String
3041 return WxMaUserInfo .fromJson (WxMaCryptUtils .decrypt (sessionKey , encryptedData , ivStr ));
3142 }
3243
44+ @ Override
45+ public void setUserStorage (Map <String , String > kvMap , String sessionKey , String openid ) throws WxErrorException {
46+ final WxMaConfig config = this .service .getWxMaConfig ();
47+ JsonObject param = new JsonObject ();
48+ JsonArray array = new JsonArray ();
49+ for (Map .Entry <String , String > e : kvMap .entrySet ()) {
50+ JsonObject jsonObject = new JsonObject ();
51+ jsonObject .addProperty ("key" , e .getKey ());
52+ jsonObject .addProperty ("value" , e .getValue ());
53+ array .add (jsonObject );
54+ }
55+ param .add ("kv_list" , array );
56+ String params = param .toString ();
57+ String signature = SignUtils .createHmacSha256Sign (params , sessionKey );
58+ String url = String .format ("https://api.weixin.qq.com/wxa/set_user_storage" +
59+ "?appid=%s&signature=%s&openid=%s&sig_method=%s" ,
60+ config .getAppid (), signature , openid , "hmac_sha256" );
61+ String result = this .service .post (url , params );
62+ WxError error = WxError .fromJson (result );
63+ if (error .getErrorCode () != 0 ) {
64+ throw new WxErrorException (error );
65+ }
66+ }
67+
3368 @ Override
3469 public WxMaPhoneNumberInfo getPhoneNoInfo (String sessionKey , String encryptedData , String ivStr ) {
3570 return WxMaPhoneNumberInfo .fromJson (WxMaCryptUtils .decrypt (sessionKey , encryptedData , ivStr ));
0 commit comments