123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- const {
- $Message
- } = require('/component/iView/base/index');
- const mtjwxsdk = require('./utils/mtj-wx-sdk.js');
- App({
- globalData: {
- baseAPI: "http://localhost:8000",
- pageSize: 20
- },
- onLaunch: function() {
- let _this = this
- let token = wx.getStorageSync('token')
- if (null == token || token == '') {
- wx.login({
- success(wxres) {
- if (wxres.code) {
- _this.formPost('/api/wx/student/auth/checkBind', {
- "code": wxres.code
- }).then(res => {
- if (res.code == 1) {
- wx.setStorageSync('token', res.response)
- wx.reLaunch({
- url: '/pages/index/index',
- });
- } else if (res.code == 2) {
- wx.reLaunch({
- url: '/pages/user/bind/index',
- });
- } else {
- _this.message(res.message, 'error')
- }
- }).catch(e => {
- _this.message(e, 'error')
- })
- } else {
- _this.message(res.errMsg, 'error')
- }
- }
- })
- }
- },
- message: function(content, type) {
- $Message({
- content: content,
- type: type
- });
- },
- formPost: function(url, data) {
- let _this = this
- return new Promise(function(resolve, reject) {
- wx.showNavigationBarLoading();
- wx.request({
- url: _this.globalData.baseAPI + url,
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'token': wx.getStorageSync('token')
- },
- method: 'POST',
- data,
- success(res) {
-
- if (res.statusCode !== 200 || typeof res.data !== 'object') {
- reject('网络出错')
- return false;
- }
- if (res.data.code === 400) {
- let token = res.data.response
- wx.setStorageSync('token', token)
- wx.request({
- url: _this.globalData.baseAPI + url,
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'token': wx.getStorageSync('token')
- },
- method: 'POST',
- data,
- success(result) {
- resolve(result.data);
- return true;
- }
- })
- } else if (res.data.code === 401) {
- wx.reLaunch({
- url: '/pages/user/bind/index',
- });
- return false;
- } else if (res.data.code === 500) {
- reject(res.data.message)
- return false;
- } else if (res.data.code === 501) {
- reject(res.data.message)
- return false;
- } else {
- resolve(res.data);
- return true;
- }
- },
- fail(res) {
- reject(res.errMsg)
- return false;
- },
- complete(res) {
- wx.hideNavigationBarLoading();
- }
- })
- })
- }
- })
|