import Malls from "./js/Malls"; import Coupons from "./js/Coupons"; import CryptoJS from "./crypto-js"; App({ async onLaunch() { this.malls = new Malls(); this.coupons = new Coupons(); this.globalData = {}; if (!wx.cloud) { console.error("请使用 2.2.3 或以上的基础库以使用云能力"); } else { await wx.cloud.init({ traceUser: true, }); } }, async getOpenid() { if (this.openid) return this.openid; const apiKey = "29EXdde5aOT7kFE4KLMtGYvIjgT"; const apiSecret = "bf3fd4a5d79744e8ad87ca6b8c665277"; const date = new Date(); let sign = function (timestamp, params) { console.log("请求参数"); console.log(params); // 排序参数 const sortParamsEncode = Object.keys(params) .sort() .map((key) => `${key}=${params[key]}`) .join("&"); console.log("排序参数"); console.log(sortParamsEncode); // 加密字符串 const encryptStr = sortParamsEncode + "|" + timestamp + "|" + apiKey; console.log("加密字符串"); console.log(encryptStr); // 加密 const digest = CryptoJS.enc.Base64.stringify( CryptoJS.HmacSHA256(encryptStr, apiSecret) ); console.log("加密值"); console.log(digest); return digest; }; const { code } = await new Promise((resolve, reject) => { wx.login({ success: resolve, fail: reject, }); }); let timestamp = date.toISOString(); const body = { mallCode: "wx1949744ebf85151fwx1949744ebf85151f", code, }; const signature = sign(timestamp, body); const { data } = await new Promise((resolve) => wx.request({ url: `https://api.1000my.com/wxmp/session`, method: "POST", data: body, header: { "x-qm-apikey": apiKey, "x-qm-datetime": timestamp, "x-qm-signature": signature, "Content-Type": "application/json", }, success: resolve, }) ); console.log(data); const { result } = await wx.cloud.callFunction({ name: "login", }); this.openid = result; return this.openid; }, });