123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- //app.js
- App({
- //全局变量
- globalData: {
- //服务器地址
- serverUrl: 'https://mdyg.sdmingde.cn/api/v1/',
- uploadServerUrl: 'https://mdyg.sdmingde.cn/api/v1/',
- visitorServerUrl: 'https://mdyg.sdmingde.cn/api/h5/',
- appVersion: '1.0.0',
- platform: 'wxapp',
- userinfo:{}
- },
- onLaunch: function (options) {
- // let orgId = 3
- // wx.setStorageSync("orgId", orgId);
- // wx.setStorageSync("token", 3914262743650);
- // wx.setStorageSync("userId", 3);
- this.getScene(options);
- },
-
- getScene(options){
- const scene = decodeURIComponent(options.scene);
- let ss = scene.split('_');
- if(ss.length == 2){
- var orgId = wx.getStorageSync("orgId");
- if(!orgId || (orgId && Number(orgId) != Number(ss[1]))){
- wx.setStorageSync("orgId", ss[1]);
- this.logout();
- }
- }
- },
- logout(){
- wx.removeStorageSync("token");
- wx.removeStorageSync("userId");
- wx.removeStorageSync("visitorToken");
- wx.removeStorageSync("visitorUserId");
- },
- checkCode:function(code){
- if(code == -100){
- wx.showToast({
- title: '登录超时,请重新登录',
- icon: 'none',
- duration: 2000
- })
- this.logout();
- setTimeout(function(){
- wx.reLaunch({ //关闭所有页面,打开到应用内的某个页面
- url: '/pages/splash/splash'
- })
- },2000);
- return false;
- }
- return true;
- },
- ajax:function(params){
- if (!params.type) {
- var type = 'POST';
- }else{
- var type = params.type;
- }
- if (!params.apiname) {
- var apiname = '1';
- } else {
- var apiname = params.apiname;
- }
- if (!params.data) {
- var data = [];
- }else{
- var data = params.data;
- }
- var app = getApp();
- var userId = app.globalData.userinfo.userId;
- var token = app.globalData.userinfo.token;
- var orgId = wx.getStorageSync("orgId");
- if (!userId || userId == undefined) { //未登录
- wx.showToast({
- title: '未登录',
- icon: 'none',
- duration: 2000
- });
- this.logout();
- setTimeout(function () {
- wx.reLaunch({ //关闭所有页面,跳转到闪屏页
- url: '/pages/splash/splash'
- })
- }, 2000);
- return false;
- }
- data['userId'] = userId;
- data['token'] = token;
- data['orgId'] = orgId;
-
- wx.showLoading({
- title: '加载中',
- mask: true
- });
-
- wx.request({
- url: params.url,
- data: data,
- method: type,
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- success: function (res) {
- wx.hideLoading();
- wx.stopPullDownRefresh();
- var ret = app.checkCode(res.data.code); //检查token是否失效
- if(ret == false){
- return false;
- }
- if (res.data.code == 0) {
- if (app.ajaxReadyCallback) { //在onShow中调用数据的,添加在onShow方法内,防止不同页面覆盖
- res.data['apiname'] = apiname;
- app.ajaxReadyCallback(res)
- }
- } else {
- let showMsg = true;
- if(params.showMsg != undefined){
- showMsg = params.showMsg;
- }
- if(showMsg){
- wx.showToast({
- title: res.data.message,
- icon: 'none',
- duration: 2000
- })
- }
- }
- },
- fail: function (err) {
- wx.hideLoading();
- wx.showToast({
- title: '请求失败',
- icon: 'none',
- duration: 2000
- })
- }
- })
- },
-
- loginVisitor:function(type){
- wx.login({
- success: function (loginRes) {
- var app = getApp();
- var serverUrl = app.globalData.visitorServerUrl;
- if (loginRes) {
- wx.request({
- url: serverUrl + 'oauth/checkCode',
- data: {
- type: 2,
- code: loginRes.code//临时登录凭证
- },
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- success: function (res) {
- if (res.data.code == -101) {
- wx.removeStorageSync("visitorToken");
- if(type == 'visitorAdd'){
- wx.redirectTo({
- url: '/pages/splash/splash?type=visitorAdd'
- })
- }else{
- wx.redirectTo({
- url: '/pages/splash/splash?type=visitor'
- })
- }
- }
- if (res.data.code == 0) {
- try {
- wx.setStorageSync("visitorToken", res.data.data.token);
- wx.setStorageSync("visitorUserId", res.data.data.userId);
- } catch (e) {
- this.logout();
- wx.showModal({
- title: '提示',
- content: '登录异常!',
- showCancel: false
- })
- return false;
- };
-
- if(type == 'visitorAdd'){
- wx.redirectTo({
- url: '/pages/visitor/add/index'
- });
- }else{
- wx.redirectTo({
- url: '/pages/visitor/index'
- });
- }
- }
- },
- fail: function (err) {
- wx.showModal({
- title: '提示',
- content: '登录异常!',
- showCancel: false
- })
- }
- })
- } else {
- wx.showModal({
- title: '提示',
- content: '登录异常!',
- showCancel: false
- })
- }
- }
- })
- },
- checkCode2:function(code){
- if(code == -100){
- wx.showToast({
- title: '登录超时,请重新登录',
- icon: 'none',
- duration: 2000
- })
- wx.removeStorageSync('visitorToken');
- wx.removeStorageSync('visitorUserId');
- setTimeout(function(){
- var vtype = wx.getStorageSync('visitorType');
- if(vtype == 'visitorAdd'){
- wx.redirectTo({ //关闭所有页面,打开到应用内的某个页面
- url: '/pages/splash/splash?type=visitorAdd'
- })
- }else{
- wx.redirectTo({ //关闭所有页面,打开到应用内的某个页面
- url: '/pages/splash/splash?type=visitor'
- })
- }
- },2000);
- return false;
- }
- return true;
- },
- ajax2:function(params){
- if (!params.type) {
- var type = 'POST';
- }else{
- var type = params.type;
- }
- if (!params.apiname) {
- var apiname = '1';
- } else {
- var apiname = params.apiname;
- }
- if (!params.data) {
- var data = [];
- }else{
- var data = params.data;
- }
- var app = getApp();
- var token = wx.getStorageSync('visitorToken');
- var userId = wx.getStorageSync('visitorUserId');
- var vtype = wx.getStorageSync('visitorType');
- if (!token || token == undefined) { //未登录
- wx.showToast({
- title: '未登录',
- icon: 'none',
- duration: 2000
- });
- setTimeout(function () {
- if(vtype == 'visitorAdd'){
- wx.redirectTo({ //关闭所有页面,跳转到闪屏页
- url: '/pages/splash/splash?type=visitorAdd'
- })
- }else{
- wx.redirectTo({ //关闭所有页面,跳转到闪屏页
- url: '/pages/splash/splash?type=visitor'
- })
- }
- }, 2000);
- return false;
- }
- data['token'] = token;
- data['userId'] = userId;
-
- wx.showLoading({
- title: '加载中',
- mask: true
- });
-
- wx.request({
- url: params.url,
- data: data,
- method: type,
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- success: function (res) {
- wx.hideLoading();
- wx.stopPullDownRefresh();
- var ret = app.checkCode2(res.data.code); //检查token是否失效
- if(ret == false){
- return false;
- }
- if (res.data.code == 0) {
- if (app.ajaxReadyCallback) { //在onShow中调用数据的,添加在onShow方法内,防止不同页面覆盖
- res.data['apiname'] = apiname;
- app.ajaxReadyCallback(res)
- }
- } else {
- let showMsg = true;
- if(params.showMsg != undefined){
- showMsg = params.showMsg;
- }
- if(showMsg){
- wx.showToast({
- title: res.data.message,
- icon: 'none',
- duration: 2000
- })
- }
- }
- },
- fail: function (err) {
- wx.hideLoading();
- wx.showToast({
- title: '请求失败',
- icon: 'none',
- duration: 2000
- })
- }
- })
- },
- gotopage: function (url, data) { //传递数据到下一页面的跳转,在新页面onload中获取
- wx.setStorage({
- key: "gotopage",
- data: data,
- success: function () {
- wx.navigateTo({
- url: url,
- })
- },
- fail: function () {
- wx.showToast({
- title: '跳转失败',
- icon: 'none',
- duration: 2000
- })
- return false;
- }
- });
- },
- getgotodata: function (url, data) { //传递数据到下一页面的跳转,在新页面onload中获取
- wx.getStorage({
- key: 'gotopage',
- success(res) {
- return res.data;
- },
- fail(res) {
- wx.navigateBack({ delta: 1 });
- return false;
- }
- });
- }
- })
|