//签收消息
receiveMsg:
function
(e){
var
template_id =
'pjn7TuJghPCNW7PEmag_VflM4Ps5fNYrteaaGuhtCQ0'
;
var
to_page =
'/pages/CarIndex/CarIndex'
;
var
formId = e.detail.formId;
var
data = {
"keyword1"
: {
"value"
:
"顺丰快递"
,
"color"
:
"#4a4a4a"
},
"keyword2"
: {
"value"
:
"123456"
,
"color"
:
"#9b9b9b"
},
"keyword3"
: {
"value"
:
new
Date().toLocaleString(),
"color"
:
"#9b9b9b"
},
"keyword4"
: {
"value"
:
"李四"
,
"color"
:
"#9b9b9b"
},
}
common.getToken(formId,template_id,to_page,data);
},
var
app = getApp();
var
MD5Encode =
require
(
"MD5Encode.js"
);
var
host = app.globalData.host;
var
session_id = wx.getStorageSync(
'PHPSESSID'
);
var
token = wx.getStorageSync(
'token'
);
var
that;
var
header={
'Content-Type'
:
'application/x-www-form-urlencoded'
,
'Cookie'
:
'PHPSESSID='
+session_id,
'XX-Token'
: token,
'XX-Device-Type'
:
'wxapp'
};
/**
* 对字符串判空
*/
function
isStringEmpty(data) {
if
(
null
== data ||
""
== data) {
return
true
;
}
return
false
;
}
/**
* 封装网络请求
*/
function
sentHttpRequestToServer(uri, data, method, successCallback, failCallback, completeCallback) {
wx.request({
url: app.d.hostUrl + uri,
data: data,
method: method,
header: {
'Content-Type'
:
'application/x-www-form-urlencoded'
},
success: successCallback,
fail: failCallback,
complete: completeCallback
})
}
/**
* 将map对象转换为json字符串
*/
function
mapToJson(map) {
if
(
null
== map) {
return
null
;
}
var
jsonString =
"{"
;
for
(
var
key
in
map) {
jsonString = jsonString + key +
":"
+ map[key] +
","
;
}
if
(
","
== jsonString.charAt(jsonString.length -
1
)) {
jsonString = jsonString.substring(
0
, jsonString.length -
1
);
}
jsonString +=
"}"
;
return
jsonString;
}
/**
* 弹窗提示成功
*/
function
toastSuccess() {
wx.showToast({
title:
'成功'
,
icon:
'success'
,
duration:
2000
})
}
/**
* 调用微信支付
*/
function
doWechatPay(prepayId, successCallback, failCallback, completeCallback) {
var
nonceString = getRandomString();
var
currentTimeStamp = getCurrentTimeStamp();
var
packageName =
"prepay_id="
+ prepayId;
var
dataMap = {
timeStamp: currentTimeStamp,
nonceStr: nonceString,
package
: packageName,
signType:
"MD5"
,
paySign: getWechatPaySign(nonceString, packageName, currentTimeStamp),
success: successCallback,
fail: failCallback,
complete: completeCallback
}
console.log(dataMap);
wx.requestPayment(dataMap);
}
/**
* 获取微信支付签名字符串
*/
function
getWechatPaySign(nonceStr, packageName, timeStamp) {
var
beforMD5 =
"appid="
+ app.d.appId +
"&nonceStr="
+ nonceStr +
"&package="
+ packageName +
"&signType=MD5"
+
"&timeStamp="
+ timeStamp +
"&key="
+ app.d.appKey;
return
doMD5Encode(beforMD5).toUpperCase();
}
/**
* 获取当前时间戳
*/
function
getCurrentTimeStamp() {
var
timestamp = Date.parse(
new
Date());
return
timestamp +
""
;
}
/**
* 获取随机字符串,32位以下
*/
function
getRandomString() {
return
Math.random().toString(
36
).substring(
3
,
8
);
}
/**
* MD5加密
*/
function
doMD5Encode(toEncode) {
return
MD5Encode.hexMD5(toEncode);
}
//获得wxtoken
function
getToken(formId,template_id,to_page,data){
wx.request({
url: host+
"api/order/order/getToken"
,
method:
'post'
,
data: {
type
:
2
,
},
header: header,
success:
function
(res) {
if
(res.data.code==
1
){
var
open_id = res.data.data.open_id;
var
wx_token = res.data.data.wx_token;
msg(wx_token,open_id,template_id,to_page,formId,data);
}
},
fail:
function
() {
wx.showToast({
title:
'网络异常!'
,
duration:
2000
});
}
});
}
//通知消息
function
msg(wx_token,open_id,template_id,to_page,formId,data){
var
url =
'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='
+wx_token;
var
data = {
touser: open_id,
template_id: template_id,
page: to_page,
form_id: formId,
data: data,
color:
'#ccc'
,
}
wx.request({
url: url,
data: data,
method:
'POST'
,
success:
function
(res){
console.log(
"push msg"
);
console.log(res);
},
fail:
function
(err) {
console.log(
"push err"
)
console.log(err);
}
});
}
module
.exports = {
isStringEmpty: isStringEmpty,
sentHttpRequestToServer: sentHttpRequestToServer,
mapToJson: mapToJson,
toastSuccess: toastSuccess,
doWechatPay: doWechatPay,
msg: msg,
getToken: getToken
}