微信公众号H5开发笔记

一、 首先授权配置

<dependency>
      <groupId>com.github.binarywang</groupId>
      <artifactId>weixin-java-mp</artifactId>
      <version>2.5.0</version>
    </dependency>
<dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.7</version>
    </dependency>
    <dependency>
        <groupId>xpp3</groupId>
        <artifactId>xpp3</artifactId>
        <version>1.1.4c</version>
    </dependency>
    <dependency>
        <groupId>xmlpull</groupId>
        <artifactId>xmlpull</artifactId>
        <version>1.1.3.1</version>
    </dependency>
@Configuration
@PropertySource(
        value={
         
  "classpath:wxProperties.properties"},
        ignoreResourceNotFound = true)
//@DependsOn("propertyPlaceholderConfigurer")
public class WeixinConfig {
          
   
    //直接获取资源文件中的配置的值
    @Value("${wxProperties.appid}")
    private String appid;//appId

    @Value("${wxProperties.appsecret}")
    private String appsecret;//Appsecret

    @Value("${wxProperties.token}")
    private String token;//Token

    @Value("${wxProperties.aeskey}")
    private String aesKey;//aeskey,有就填,没有就不填

    @Value("${wxProperties.partener_id}")
    private String partenerId;//商户号

    @Value("${wxProperties.partener_key}")
    private String partenerKey;//商户秘钥

    @Value("${wxProperties.notify_url}")
    private String notifyUrl;//支付后台通知接口地址

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
       PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
       ppc.setIgnoreUnresolvablePlaceholders(true);
       return ppc;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
        configStorage.setAppId(this.appid);
        configStorage.setSecret(this.appsecret);
        configStorage.setToken(this.token);
        configStorage.setAesKey(this.aesKey);
        configStorage.setPartnerId(this.partenerId);
        configStorage.setPartnerKey(this.partenerKey);
        configStorage.setNotifyURL(this.notifyUrl);
        return configStorage;
    }

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

}
@Autowired
    protected WxMpService wxMpService;
public StatusResult<Map<String, Object>> createJsapiSignature(String url) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            WxJsapiSignature  wxJsapiSignature = wxMpService.createJsapiSignature(url);
            String getJsapiTicket = wxMpService.getJsapiTicket();
            result.put("wxJsapiSignature", wxJsapiSignature);
            return StatusResult.success(result, "");
        } catch (WxErrorException e) {
            return StatusResult.failed("未知错误出现", result);
        }
    }

这里的url是H5用js代码获取的:

var url = location.href.split(#)[0];
jquery(function() {
          
   
    var expiresDate= new Date();
    expiresDate.setTime(expiresDate.getTime() + (30 * 60 * 1000));//半小时
    jquery.cookie("openId",${openId}, {
          path : /,//cookie的作用域为根目录,任何页面都是有效的
          expires : expiresDate
         });//cookie里面保存openId
});

然后在js中写一个方法每个页面都调用判断。

经验分享 程序员 微信小程序 职场和发展