- //app.js
- var http = require('service/http.js')
- App({
- onLaunch: function() {
- //Call API to get data from local cache
- // var that = this;
- },
- getAuthKey: function () {
- var that = this;
- returnnew Promise(function (resolve, reject) {
- // call the login interface
- wx.login({
- success: function (res) {
- if (res.code) {
- that.globalData.code = res.code;
- //call the login interface
- wx.getUserInfo({
- withCredentials: true,
- success: function (res) {
- that.globalData.UserRes = res;
- that.globalData.userInfo = res.userInfo;
- that.func.postReq('/api/v1/image/oauth', {
- code: that.globalData.code,
- signature: that.globalData.UserRes.signature,
- encryptedData: that.globalData.UserRes.encryptedData,
- rawData: that.globalData.UserRes.rawData,
- iv: that.globalData.UserRes.iv
- }, function (res) {
- wx.setStorage({
- key: "auth_key",
- data: res.data.auth_key
- })
- var res = {
- status: 200,
- data: res.data.auth_key
- }
- resolve(res);
- })
- }
- })
- } else {
- console.log( 'Failed to get user login status!' + res.errMsg);
- var res = {
- status: 300,
- data: 'error'
- }
- reject('error');
- }
- }
- })
- });
- },
- })
-
- //index.js
- onLoad: function () {
- app.getAuthKey().then(function (res) {
- console.log(res);
- if (res.status == 200){
- var auth_key = res.data;
- app.func.req('/api/v1/image/theme-list', {
- page: 1,
- auth_key: auth_key
- }, function (res) {
- var page = that.data.pageValue + 1;
- that.setData({
- images: res.data,
- pageValue: page
- });
- });
- }else{
- console.log(res.data);
- }
- });
The solution of applet onLaunch and onload asynchronous request
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325979794&siteId=291194637
Ranking