|
- //app.js
- App({
- onLaunch: function () {
- // 展示本地存储能力
- var openid = wx.getStorageSync('openid') || "";
- if (!openid) {
- // 登录
- this.wxLogin();
- } else {
- this.globalData.openid = openid;
- }
- var userMobile = wx.getStorageSync('userMobile');
- if (userMobile){
- this.globalData.userMobile = userMobile;
- }
- var friendOpenid = wx.getStorageSync('friendOpenid');
- if (friendOpenid) {
- this.globalData.friendOpenid = friendOpenid;
- }
- var userInfoData = wx.getStorageSync("userInfoData");
- if (userInfoData){
- this.globalData.userInfoData = {};
- this.globalData.userInfoData.avatarUrl = userInfoData.avatarUrl;
- this.globalData.userInfoData.nickName = userInfoData.nickName;
- }
- var userPhoneType = wx.getStorageSync('userPhoneType');
- if (userPhoneType) {
- this.globalData.userPhoneType = userPhoneType;
- } else {
- this.getPhoneType();
- }
- },
- getPhoneType() {//获取手机类型
- wx.getSystemInfo({
- success: res => {
- wx.setStorageSync("userPhoneType", res.platform);
- this.globalData.userPhoneType = res.platform;
- }
- })
- },
- wxLogin() {
- wx.login({
- success: res => {
- if (res.code) {
- this.getOpenid(res.code);
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- },
- globalData: {
- urlRoot: "https://dongfengqichen.jiyou-tech.com/",//测试接口根目录
- // urlRoot: "",//接口根目录
- urlStatic:"https://www.jiyou-tech.com/2020/496_qichen/static",//静态资源根目录
- openid: "",//OPENID
- friendOpenid:"",//推荐人的openid
- session_key: "",//session_key
- openidSuccessFuc: null,//方法回调
- nowPage:'1',//当前tabBar
- isRegister:false,//是否已注册
- userMobile:null,//用户手机号
- isFirstLogin:true,//是否为第一次登录
- indexData:{},//首页数据
- myCenterData:null,//个人中心数据
- userInfoData:null,
- certificationState:0,//1车主,2合伙人,3同事
- mobileData:null,
- userPhoneType:null,
- },
- // 获取openId
- getOpenid: function (code) {
- wx.showLoading({
- title: '加载中',
- mask: true,
- })
- this.wxRequest(this.globalData.urlRoot + "wxInfo/getOpenid", { code: code }, res => {
- wx.hideLoading();
- console.log(res);
- if (res.code == 200) {
- wx.setStorageSync("openid", res.data.openid);
- wx.setStorageSync("session_key", res.data.session_key);
- this.globalData.openid = res.data.openid;
- this.globalData.session_key = res.data.session_key;
- if (this.globalData.openidSuccessFuc) {
- this.globalData.openidSuccessFuc();
- }
- if(this.globalData.mobileData){
- this.getMobile2(this.globalData.mobileData.encryptedData, this.globalData.mobileData.iv, this.globalData.mobileData.callback, this.globalData.mobileData.thisArg);
- }
- var userInfoData = wx.getStorageSync("userInfoData");
- if (!userInfoData) {
- this.getUserInfo();
- }
- } else {
- wx.showToast({
- title: res.msg,
- icon: "none"
- })
- }
- }, this);
- },
- /**
- * wx.request
- */
- wxRequest: function (url, params, callback, thisArg, methods, openid) {
- let that = this;
-
- var httpUrl = url;
- var str = "";
- var count = 0;
- for (let key in params) {
- if (count) {
- str += "&" + key + "=" + params[key];
- } else {
- str += key + "=" + params[key];
- }
- count++;
- }
- if (str) {
- httpUrl += "?" + str;
- }
- if (!methods) {
- methods = "GET";
- }
- if (methods == "POST") {
- wx.request({
- url: url,
- data: params,
- method: methods,
- header: {
- "content-type": "application/x-www-form-urlencoded",
- "OPENID": this.globalData.openid
- },
- success: function (re) {
- // if (re.data.code == -1002) {
- // wx.removeStorageSync('openid');
- // that.wxLogin();
- // // that.wxRequest(url, params, callback, thisArg, methods, openid);
- // }
- if (callback && thisArg) {
- callback.call(thisArg, re.data);
- }
- },
- fail: function (re) {
- wx.hideLoading();
- }
- })
- } else {
- wx.request({
- url: httpUrl,
- method: methods,
- header: {
- "OPENID": this.globalData.openid,
- },
- success: function (re) {
- if (re.data.code == -1002) {
- wx.removeStorageSync('openid');
- that.wxLogin();
- // that.wxRequest(url, params, callback, thisArg, methods, openid);
- }
- if (callback && thisArg) {
- callback.call(thisArg, re.data);
- }
- },
- fail: function (res) {
- wx.hideLoading();
- }
- })
- }
- },
- submitUserMsg: function (avatarUrl, nickName) {//提交用户信息
- this.wxRequest(this.globalData.urlRoot + "userInfo/updateUserInfo", { avatarUrl: avatarUrl, nickName: nickName }, res => {
- if (res.code == 200) {
- this.globalData.userInfoData = {};
- this.globalData.userInfoData.avatarUrl = avatarUrl;
- this.globalData.userInfoData.nickName = nickName;
- wx.setStorageSync('userInfoData', {
- avatarUrl: avatarUrl,
- nickName: nickName
- })
- }
- }, this, "POST")
- },
- codeVerify: function (card) {//身份证号码验证
- let rule = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
- return rule.test(card);
- },
- mobileVerify: function (mobile) {//手机号验证
- let rule = /^1\d{10}$/;
- return rule.test(mobile);
- },
- callMobile: function (e) {//拨打电话
- wx.makePhoneCall({
- phoneNumber: e //仅为示例,并非真实的电话号码
- })
- },
- sharePack: function () {//分享
- return {
- title: '',
- imageUrl: "",
- path: "/pages/index/index"
- }
- },
- addformId: function (e) {//添加formid
- this.wxRequest(this.globalData.urlRoot + "/msg/addFormId", { formid: e }, res => {
- console.log(res.msg);
- }, this, "POST");
- },
- getMobile: function (encryptedData, iv, callback, thisArg) {//检查登录态是否过期
- console.log(encryptedData);
- console.log(iv);
- wx.checkSession({
- success: res => {
- this.getMobile2(encryptedData,iv,callback,thisArg);
- },
- fail: res => {
- this.globalData.mobileData = {};
- this.globalData.mobileData.encryptedData = encryptedData;
- this.globalData.mobileData.iv = iv;
- this.globalData.mobileData.callback = callback;
- this.globalData.mobileData.thisArg = thisArg;
- this.wxLogin();
- }
- })
- },
- getMobile2: function (encryptedData, iv, callback, thisArg) {//和后台置换手机号
- this.globalData.mobileData = null;
- this.wxRequest(this.globalData.urlRoot + "userInfo/getUserPhoneNumber", { encryptedData: encryptedData, iv: iv }, res => {
- if (res.code == 200) {
- if (res.data && res.data.decodeData) {
- wx.setStorageSync('userMobile', res.data.decodeData.phoneNumber);
- this.globalData.userMobile = res.data.decodeData.phoneNumber;
- }
- }
- if (callback && thisArg) {
- callback.call(thisArg, res);
- }
- }, this, "POST");
- },
- getUserInfo: function () {//获取个人信息
- this.wxRequest(this.globalData.urlRoot +"userInfo/getUserInfo",{},res=>{
- if(res.code==200){
- if (res.data && res.data.avatarUrl) {
- this.globalData.userInfoData = {};
- this.globalData.userInfoData.avatarUrl = res.data.avatarUrl;
- this.globalData.userInfoData.nickName = res.data.nickName;
- wx.setStorageSync('userInfoData', {
- avatarUrl: res.data.avatarUrl,
- nickName: res.data.nickName
- })
- }
- }
- },this)
- }
- })
|