123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- App({
- onLaunch: function () {
- // 展示本地存储能力
- wx.login({
- success: function (res) {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- console.log(res.code)
- var data = {
- 'code': res.code
- };
- wx.request({
- url: 'https://yutang.web.ximengnaikang.com/api/login',
- method: 'POST',
- data:data,
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- success: function (res) {
- console.log(res)
- wx.setStorageSync('token', res.data.data.authorize)
- wx.setStorageSync('userInfo', res.data.data.user)
- wx.setStorageSync('session_key', res.data.data.session_key)
- }
- })
- }
- })
- },
- globalData: {
- userInfo: null
- },
- request(api, params, method, req1) {
- var that = this;
- // console.log(req1)
- if (req1 == false) return;
- if (req1) {
- that.globalData.req = false;
- }
- if (wx.getStorageSync('token') != '' || wx.getStorageSync('token') != undefined) {
- if (req1 == true || req1 == undefined) {
- return new Promise((resolve, reject) => {
- wx.request({
- url: this.globalData.url + api,
- data: params,
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'Authorization': wx.getStorageSync('token'),
- 'merchant-id': this.globalData.merchant_id
- },
- method: method,
- success: (res) => {
- resolve(res)
- if (res.statusCode == 450) {
- return;
- }
- if (res.data.statusCode != 200) {
- if (res.message != undefined) {
- wx.showToast({
- title: res.message,
- icon: 'none',
- duration: 3000,
- mask: true
- })
- } else if (res.data.message != undefined) {
- wx.showToast({
- title: res.data.message,
- icon: 'none',
- duration: 3000,
- mask: true
- })
- }
- } else if (res.statusCode != 200) {
- if (res.message != undefined) {
- wx.showToast({
- title: res.message,
- icon: 'none',
- duration: 3000,
- mask: true
- })
- } else if (res.data.message != undefined) {
- wx.showToast({
- title: res.data.message,
- icon: 'none',
- duration: 3000,
- mask: true
- })
- }
- }
- },
- fail: (err) => {
- wx.showToast({
- title: err,
- icon: 'none',
- mask: true
- });
- reject("请求失败")
- },
- complete: () => {
- that.globalData.req = true
- }
- })
- })
- }
- }
- },
- })
|