码迷,mamicode.com
首页 > Web开发 > 详细

util.js(涉及到当前项目的一些公共方法)

时间:2020-02-25 12:41:44      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:ret   utf-8   for   nlog   conf   pre   message   OLE   with   

var requestSign = require(./requestData.js);
var api = require(./api.js).open_api;
var re = require(./request.js);
var header = getApp().header;
var app = getApp();
var time = require(../utils/time.js)

export function getTemData() {

}
//检测是否已授权登录状态,没有登录去登录
export function checklogin() {
  const value = wx.getStorageSync(user_token);
  if (value) {
    return true;
  } else {
    return false;
  }
}
//跳转到登录页,如果成功则返回上一页,没有上一页返回设置的页面。
export function jumpToLogin(furl) {
  var JTurl = /pages/logon/index;
  if (furl) {
    JTurl = /pages/logon/index?furl= + furl
  }
  wx.navigateTo({
    url: JTurl
  })
}
/**
 * url page地址
 * num 选用的跳转方式1-switchTab,2-reLaunch,3-redirectTo,4-navigateTo,5-navigateBack
 * param 参数
 */
export function jumpPage(data) {
  let url = data.url;
  let num = data.num;
  let param = data.param;
  for (let item of app.globalData.tab_list) {
    item = / + item;
    if (url == item) {
      num = 1
      break
    }
  }

  if (param != ‘‘) {
    url = url + param;
  }

  switchPage(url, num);

}

function switchPage(url, num) {
  switch (num) {
    case 1:
      switchTab(url);
      break;
    case 2:
      reLaunch(url);
      break;
    case 3:
      redirectTo(url);
      break;
    case 4:
      navigateTo(url);
      break;
    case 5:
      navigateBack(url);
      break;
  }
}

function switchTab(url) {
  wx.switchTab({
    url: url,
  })
}

function reLaunch(url) {
  wx.reLaunch({
    url: url
  })
}

function redirectTo(url) {
  wx.redirectTo({
    url: url
  })
}

function navigateTo(url) {
  wx.navigateTo({
    url: url
  })
}

function navigateBack(url) {
  wx.navigateBack({
    delta: url
  })
}



/**
 * 邀请码
 */
export function extend_code() {
  let postData = {}
  let datainfo = requestSign.requestSign(postData);
  header.sign = datainfo
  re.request(api.get_qrcode, postData, header).then((res) => {
    if (res.data.code == 0) {
      wx.setStorageSync("extend_code", res.data.data.extend_code);
    }
  })

}

/**
 * 检查手机号是否正确 
 * */
export function checkPhone(phone) {
  var telStr = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
  if (!(telStr.test(phone))) {
    console.log("手机号码有误,请重填");
    return false;
  }
}


export function checkIdCardNo(idCardNo) {
  // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X 
  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  if (reg.test(idCardNo) === false) {
    wx.showToast({
      title: 身份证号码不正确!,
      icon: none,
    })
    return false;
  }
}


/**
 * 检查手机号是否存在 
 */
export function hasPhone() {
  const that = this;
  const have_mobile = wx.getStorageSync(have_mobile);
  if (have_mobile != true) {
    return false
  }
}
/**
 * 分销字段设置
 */
export function distributionSet() {
  const that = this;
  let postData = {}
  let datainfo = requestSign.requestSign(postData);
  header.sign = datainfo;
  return new Promise((resolve, reject) => {
    wx.request({
      url: api.get_distributionSet,
      data: postData,
      header: header,
      method: POST,
      dataType: json,
      responseType: text,
      success: (res) => {
        if (res.data.code == 0) {
          let distributionData = res.data.data;
          getApp().globalData.distributionData = distributionData;
          resolve(res);
        } else {
          // wx.showToast({
          //   title: res.data.message,
          //   icon: ‘none‘,
          // })
          console.log(res.data.message)
        }
      },
      fail: (res) => {},
    })
  })
}

/**
 * 分红字段设置
 */
export function bonusSet() {
  const that = this;
  let postData = {}
  let datainfo = requestSign.requestSign(postData);
  header.sign = datainfo;
  return new Promise((resolve, reject) => {
    wx.request({
      url: api.get_bonusSet,
      data: postData,
      header: header,
      method: POST,
      dataType: json,
      responseType: text,
      success: (res) => {
        if (res.data.code == 0) {
          let bonusData = res.data.data;
          getApp().globalData.bonusData = bonusData;
          resolve(res);
        } else {
          // wx.showToast({
          //   title: res.data.message,
          //   icon: ‘none‘,
          // })
          console.log(res.data.message)
        }
      },
      fail: (res) => {},
    })
  })
}

//检测是登录状态 是否开通渠道 和 开通服务
export function checkloginAndChannelAndServer() {
  const value = wx.getStorageSync(user_token);
  if (value) {
    checkApplay(); //检测是否是代理商 。
  } else {
    // jumpToLogin(‘/packageThirst/pages/cacher/index/index‘);
    jumpToLogin(/pages/logon/index);
  }
}



//检测是否是供应商的判断去申请页面
export function checkApplay() {
  const that=this;
  applayChannelForm().then((res) => {
    console.log(res.statusCode);
    if (res.statusCode == 500) {
      wx.showModal({
        title: 提示,
        content: 接口数据错误,
      })
    }
    if (res.data.code == 1) {
      wx.setStorageSync(ischannel, res.data.data.is_channel);
      if (res.data.data.is_channel == false) {
        let onPageData = {
          url: /package/pages/channel/apply/index,
          num: 4,
          param: ‘‘,
        }
        jumpPage(onPageData);
        // wx.showModal({
        //   title: ‘提示‘,
        //   content: ‘您还不是渠道商,请先申请‘,
        //   success(res) {
        //     if (res.confirm) {
        //       let onPageData = {
        //         url: ‘/package/pages/channel/apply/index‘,
        //         num: 4,
        //         param: ‘‘,
        //       }
        //       jumpPage(onPageData);
        //     } else if (res.cancel) {
        //       let onPageData = {
        //         url: ‘/packageThirst/pages/cacher/mine/index‘,
        //         num: 4,
        //         param: ‘‘,
        //       }
        //       jumpPage(onPageData);
        //     }
        //   },
        // })
      } else {

        // that.setData({
        //   pageShow: true,
        //   channelData: res.data.data,
        // })
        getOrderInfoByUid(); //渠道商通过之后判断是否开通服务。
      }
    }
  })
}

//获取用户的渠道商数据,是否是渠道商。
function applayChannelForm() {
  let postData = {}
  let datainfo = requestSign.requestSign(postData);
  header.sign = datainfo
  return new Promise((resolve, reject) => {
    wx.request({
      url: api.get_channelIndex,
      data: postData,
      header: header,
      method: POST,
      dataType: json,
      responseType: text,
      success: (res) => {
        resolve(res);
      },
      fail: (res) => {
        reject(res);
      },
    })
  })
}

//请求开通服务  
function getOrderInfoByUid() {
  let postData = {}
  let datainfo = requestSign.requestSign(postData);
  header.sign = datainfo
  wx.request({
    url: api.getOrderInfoByUid,
    data: postData,
    header: header,
    method: POST,
    dataType: json,
    responseType: text,
    success: (res) => {
      let code = res.data.code;
      if (res.data.code > 0) {
        console.log(time.js_date_time(res.data.data.expire_time))
        // that.setData({
        //   expTime: time.js_date_time(res.data.data.expire_time),
        //   expStatus: res.data.data.order_status
        // })
        checkGotoService(res.data.data.order_status);
      } else {
        // that.setData({
        //   expStatus: 0
        // })
        checkGotoService(0);
      }
    },
    fail: (res) => {},
  })
}

function checkGotoService(orderstatus) {
  if (orderstatus == 0) {
    wx.showModal({
      title: 马上买【商家版】未开通,
      content: 是否开通服务?,
      confirmText: ,
      confirmColor: #57bf6a,
      cancelText: ,
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: /packageThirst/pages/cacher/services/index
          })
        } else if (res.cancel) {
          wx.redirectTo({
            url: /packageThirst/pages/cacher/mine/index
          })
        }
      }
    })
  }
  if (orderstatus == 3) {
    wx.showModal({
      title: 马上买【商家版】已到期,
      content: 是否开通服务?,
      confirmText: ,
      confirmColor: #57bf6a,
      cancelText: ,
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: /packageThirst/pages/cacher/services/index
          })
        } else if (res.cancel) {
          wx.redirectTo({
            url: /packageThirst/pages/cacher/mine/index
          })
        }
      }
    })
  }
}

/**
 * 登录
 */
export function onlogin() {
  let _this = this;
  return new Promise((resolve, reject) => {
    let header = {
      Content-Type: application/json; charset=utf-8,
      X-Requested-With: XMLHttpRequest,
      Program: miniProgram,
      "website-id": wx.getExtConfigSync().website_id,
    }
    wx.login({
      success(res) {
        var code = res.code;
        if (res.code) {
          wx.request({
            url: api.get_oauthLogin,
            header: header,
            data: {
              code: code,
              type: MP,
            },
            method: POST,
            success: function (res) {
              console.log("onlogin:res")
              console.log(res)
              if (res.data.code == 1) {
                if (getApp().userTokenEvent == ‘‘) {
                  wx.setStorageSync("user_token", res.data.data.user_token)
                  getApp().userToken = res.data.data.user_token;
                } else {
                  getApp().userToken = getApp().userTokenEvent;
                }
                let token = user-token;
                getApp().header[token] = getApp().userToken

                wx.setStorageSync("have_mobile", res.data.data.have_mobile);
                wx.setStorageSync("openid", res.data.data.openid);
                var setCookie = res.header[Set-Cookie];
                wx.setStorageSync("setCookie", setCookie.split(";")[0])
                getApp().header.Cookie = setCookie.split(";")[0];
                getApp().loginStatus = true;
              } else if (res.data.code == 2) {
                wx.showModal({
                  title: 提示-2,
                  content: res.data.message,
                  success(res) {
                    if (res.confirm) {
                      wx.switchTab({
                        url: /pages/member/index,
                      })
                    }
                  }
                })
              } else if (res.data.code == 3) {
                wx.showModal({
                  title: 提示-3,
                  content: res.data.message,
                  showCancel: true
                })

              } else if (res.data.code == 5) {
                var setCookie = res.header[Set-Cookie];
                wx.setStorageSync("setCookie", setCookie.split(";")[0])
                getApp().header.Cookie = setCookie.split(";")[0];
                // loginAgain(res.data.data);
              } else {
                wx.showModal({
                  title: 提示,
                  content: 授权登录失败!,
                  showCancel: false,
                })
              }
              resolve(res);
            },
          })
        } else {
          console.log(登录失败! + res.errMsg);
        }
      }
    })
  })
}

function loginAgain(rData) {
  const that = this;
  let postData = {
    type: MP,
    "extend_code": wx.getStorageSync(higherExtendCode),
    "encrypted_data": encodeURIComponent(wx.getStorageSync(encrypted_data)),
    "iv": encodeURIComponent(wx.getStorageSync(iv)),
  }
  let header = {
    Content-Type: application/json; charset=utf-8,
    X-Requested-With: XMLHttpRequest,
    Program: miniProgram,
    Cookie: wx.getStorageSync(setCookie),
    "website-id": wx.getExtConfigSync().website_id,
  }
  let datainfo = requestSign.requestSign(postData);
  header.sign = datainfo;
  wx.request({
    url: api.get_oauthLogin,
    data: postData,
    header: header,
    method: POST,
    dataType: json,
    responseType: text,
    success: (res) => {
      if (res.data.code == 1) {
        if (getApp().userTokenEvent == ‘‘) {
          wx.setStorageSync("user_token", res.data.data.user_token)
          getApp().userToken = res.data.data.user_token;
        } else {
          getApp().userToken = getApp().userTokenEvent;
        }
        let token = user-token;
        getApp().header[token] = getApp().userToken;

        let configData = getApp().globalData.config;
        if (getApp().globalData.no_check_phone === 0) {
          wx.setStorageSync("have_mobile", true);
        } else {
          wx.setStorageSync("have_mobile", res.data.data.have_mobile);
        }

        wx.setStorageSync("openid", res.data.data.openid);
        getApp().loginStatus = true;
      } else if (res.data.code == 2) {
        wx.showModal({
          title: 提示-2,
          content: res.data.message,
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: /pages/member/index,
              })
            }
          }
        })
      } else if (res.data.code == 3) {
        wx.showModal({
          title: 提示-3,
          content: res.data.message,
          showCancel: true,
        })
      } else {
        wx.showModal({
          title: 提示,
          content: 授权登录错误,请重新授权,
          showCancel: false,
          complete: function () {
            wx.redirectTo({
              url: /pages/logon/index,
            })
          }
        })
      }
    },
    fail: (res) => {},
  })
}

 

util.js(涉及到当前项目的一些公共方法)

标签:ret   utf-8   for   nlog   conf   pre   message   OLE   with   

原文地址:https://www.cnblogs.com/xiaoxiaoxun/p/12360829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!