审查视图

addons/litestore/wxapp/app.js 6.6 KB
李忠强 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
import Toast from '/res/vant/toast/toast';
App({
  //全局API地址
  Domain: 'http://newfa.217dan.com/',
  //Domain: 'https://faliteshop.217dan.com',
  api_url: '',
  /* 设置api地址 */
  setApiUrl: function () {
    this.api_url = this.Domain + '/addons/litestore/api.';
  },
  onLaunch: function () {
    this.setApiUrl();
    //如果需要一进入小程序就要求授权登录,可在这里发起调用
    this.check(function (ret) { 
      
    });
  },
  onShow: function (options) {
    let that = this;
    // 获取小程序基础信息
    that.getWxappBase(function (wxapp) {
      // 设置navbar标题、颜色
      that.wx_setcolor(wxapp);
    });
  },
  wx_setcolor: function (wxapp){
    wx.setNavigationBarColor({
      frontColor: wxapp.TopTextColor,
      backgroundColor: wxapp.BackgroundColor,
    })
  },
  /**  获取小程序基础信息 */
  getWxappBase: function (callback) {
    this._get('wxapp/base', {}, function (result) {
      // 记录小程序基础信息
      wx.setStorageSync('wxapp', result.data.wxapp);
      callback && callback(result.data.wxapp);
    }, false, false);
  },
  /**post请求*/
  _post: function (url, data, success, fail, complete) {
    wx.showNavigationBarLoading();
    let App = this;
    data.token = wx.getStorageSync('token');
    wx.request({
      url: App.api_url + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: data,
      success: function (res) {
        console.log(res);
        if (res.data.code === 401) {
          App.check(App._post(url, data, success, fail, complete));
          return false;
        } else if (res.data.code === 0) {
          App.showError(res.data.msg, function () {
            fail && fail(res);
          });
          return false;
        }
        success && success(res.data);
      },
      fail: function (res) {
        App.showError(res.errMsg, function () {
          fail && fail(res);
        });
      },
      complete: function (res) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        complete && complete(res);
      }
    });
  },


  /**get请求*/
  _get: function (url, data, success, fail, complete) {
    wx.showNavigationBarLoading();
    let App = this;
    // 构造请求参数
    data = data || {};

    // 构造get请求
    let request = function () {
      data.token = wx.getStorageSync('token');
      wx.request({
        url: App.api_url + url,
        header: {
          'content-type': 'application/json'
        },
        data: data,
        success: function (res) {
          console.log(res);
          if (res.data.code === 401) {
            App.check(App._get(url, data, success, fail, complete));
            return false;
          } else if (res.data.code === 0) {
            App.showError(res.data.msg);
            return false;
          } else {
            success && success(res.data);
          }
        },
        fail: function (res) {
          // console.log(res);
          App.showError(res.errMsg, function () {
            fail && fail(res);
          });
        },
        complete: function (res) {
          wx.hideNavigationBarLoading();
          complete && complete(res);
        },
      });
    };
    request();
  },

  showError: function (msg, callback) {
    wx.showModal({
      title: '温馨提示',
      content: msg,
      showCancel: false,
      success: function (res) {
        callback && callback();
      }
    });
  },
  showSuccess: function (msg, callback) {
    Toast.success(msg);
    callback && (setTimeout(function () {
      callback();
    }, 800));
    /*
    wx.showToast({
      title: msg,
      icon: 'success',
      success: function () {
        callback && (setTimeout(function () {
          callback();
        }, 1500));
      }
    });
    */
  },
  getStorageSyncwxapp: function (cb) {
    var that = this;
    if (wx.getStorageSync('wxapp')) {
      typeof cb == "function" && cb(wx.getStorageSync('wxapp'));
    } else {
      //这里循环等待出数据
      setTimeout(function () { that.getStorageSyncwxapp(cb); }, 500);
    }
  },

  /* 关于登录 */
  //判断是否登录
  check: function (cb) {
    var that = this;
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo);
    } else {
      this.login(cb);
    }
  },
  Log_after_fun: function (cb) {
    var that = this;
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo);
    } else {
      //这里循环等待出数据
      setTimeout(function () { that.Log_after_fun(cb); }, 500);
    }
  },
  //登录
  login: function (cb) {
    var that = this;
    var token = wx.getStorageSync('token') || '';
    //调用登录接口
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: that.api_url + 'user/login_hawk',
            data: {
              code: res.code,
              token: token
            },
            method: 'post',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
            success: function (lres) {
              console.log(lres);
              var response = lres.data
              if (response.code == 1) {
                that.globalData.userInfo = response.data.userInfo;
                wx.setStorageSync('token', response.data.userInfo.token);
                typeof cb == "function" && cb(that.globalData.userInfo);
              } else {
                wx.setStorageSync('token', '');
                console.log("用户登录失败");
                if (response.data.errcode == 40125 || response.data.errcode == 40013 ){
                  wx.showModal({
                    title: '用户登录失败',
                    content: '请检查您是否正确配置了后台的小程序ID、小程序密钥以及开发者工具的小程序ID。',
                    showCancel: false,
                    success: function (res) {
                      //that.login(cb);
                    }
                  });
                }else{
                  wx.showModal({
                    title: '用户登录失败',
                    content: '请检查您是否已经安装“第三方登录”插件,然后重试。',
                    showCancel: false,
                    success: function (res) {
                      //that.login(cb);
                    }
                  });
                }
              }
            }
          });
        } else {
          console.log("用户失败")
        }
      }
    });
  },
  globalData: {
    userInfo: null
  },

})