const system=require("../../system");
const settings=require("../../../config/settings");
const ServiceBase=require("../sve.base");
const wxsvr=require("../wxsvr");
const fs=require("fs");
var WXPay = require('wx-pay');
var sha1 = require('sha1');
const uuidv4 = require('uuid/v4');
const path=require("path");
const servicepath=path.join(settings.basepath,"app/base/service");
class WxminiBossService{
  constructor(){
    this.cacheManager=system.getObject("db.cacheManager");
    this.restClient=system.getObject("util.restClient");
    this.wxpay = WXPay({
     //appid: 'wx6f3ebe44defe336a',
     //mch_id: '1232813602',
     //partner_key: 'sinotone2014sinotone2014sinotone',
      appid: 'wx5ab0a6c0311e6b1e',
      mch_id: '1514728461',
      nonceStr: 'Wm3WZYTPz0wzccnW',
      partner_key: 'gongsibao1234567890zhangfeilong1',
      pfx: fs.readFileSync(servicepath+'/apiclient_cert.p12'), //微信商户平台证书
    });
  }

  //创建一个js预付款单
  async createJsUnifiedOrder(desc,amount,notify_url,product_id,openid,attach,out_trade_no){
    var self=this;
    if(settings.env=="dev"){
      amount=0.01;
    }else{
      amount=Number(amount)*100;
      //amount=0.01;
    }
		var nc={}
    if(!out_trade_no){
      var u=uuidv4();
      out_trade_no=u.replace(/\-/g,"");
    }
		var order={
				body: desc,
				out_trade_no: out_trade_no,
				total_fee: amount,
				notify_url: notify_url,
				openid:openid,
        attach:attach,
        trade_type:"JSAPI",
		};
　　 nc.out_trade_no=out_trade_no;//把oid传到客户端，付款成功后，传递到服务端，确认付款成功
    var promise=new Promise(function(resv,rej){
      self.wxpay.getBrandWCPayRequestParams(order,function(err,paramWxJsPay){
  			console.log(paramWxJsPay);
        if(err){
          return rej(err);
        }else{
          nc.wxpayParam=paramWxJsPay;
          return resv(nc);
        }
  		});
    });
    return promise;
  }
}
module.exports=WxminiBossService;
// // console.log("xxxx");
// var wxs=new WxService();
// wxs.deleteWxMenu().then(d=>{
//   console.log("dele ok");
//   wxs.createWxMenu().then(x=>{
//     console.log(x);
//   });
//
// });
// //
// wxs.getJsApiConfig(null,"http://boss.gongsibao.com/").then(d=>{
//   console.log(d);
// });
