app.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //app.js
  2. App({
  3. //全局变量
  4. globalData: {
  5. //服务器地址
  6. serverUrl: 'https://mdyg.sdmingde.cn/api/v1/',
  7. uploadServerUrl: 'https://mdyg.sdmingde.cn/api/v1/',
  8. visitorServerUrl: 'https://mdyg.sdmingde.cn/api/h5/',
  9. appVersion: '1.0.0',
  10. platform: 'wxapp',
  11. userinfo:{}
  12. },
  13. onLaunch: function (options) {
  14. // let orgId = 3
  15. // wx.setStorageSync("orgId", orgId);
  16. // wx.setStorageSync("token", 3914262743650);
  17. // wx.setStorageSync("userId", 3);
  18. this.getScene(options);
  19. },
  20. getScene(options){
  21. const scene = decodeURIComponent(options.scene);
  22. let ss = scene.split('_');
  23. if(ss.length == 2){
  24. var orgId = wx.getStorageSync("orgId");
  25. if(!orgId || (orgId && Number(orgId) != Number(ss[1]))){
  26. wx.setStorageSync("orgId", ss[1]);
  27. this.logout();
  28. }
  29. }
  30. },
  31. logout(){
  32. wx.removeStorageSync("token");
  33. wx.removeStorageSync("userId");
  34. wx.removeStorageSync("visitorToken");
  35. wx.removeStorageSync("visitorUserId");
  36. },
  37. checkCode:function(code){
  38. if(code == -100){
  39. wx.showToast({
  40. title: '登录超时,请重新登录',
  41. icon: 'none',
  42. duration: 2000
  43. })
  44. this.logout();
  45. setTimeout(function(){
  46. wx.reLaunch({ //关闭所有页面,打开到应用内的某个页面
  47. url: '/pages/splash/splash'
  48. })
  49. },2000);
  50. return false;
  51. }
  52. return true;
  53. },
  54. ajax:function(params){
  55. if (!params.type) {
  56. var type = 'POST';
  57. }else{
  58. var type = params.type;
  59. }
  60. if (!params.apiname) {
  61. var apiname = '1';
  62. } else {
  63. var apiname = params.apiname;
  64. }
  65. if (!params.data) {
  66. var data = [];
  67. }else{
  68. var data = params.data;
  69. }
  70. var app = getApp();
  71. var userId = app.globalData.userinfo.userId;
  72. var token = app.globalData.userinfo.token;
  73. var orgId = wx.getStorageSync("orgId");
  74. if (!userId || userId == undefined) { //未登录
  75. wx.showToast({
  76. title: '未登录',
  77. icon: 'none',
  78. duration: 2000
  79. });
  80. this.logout();
  81. setTimeout(function () {
  82. wx.reLaunch({ //关闭所有页面,跳转到闪屏页
  83. url: '/pages/splash/splash'
  84. })
  85. }, 2000);
  86. return false;
  87. }
  88. data['userId'] = userId;
  89. data['token'] = token;
  90. data['orgId'] = orgId;
  91. wx.showLoading({
  92. title: '加载中',
  93. mask: true
  94. });
  95. wx.request({
  96. url: params.url,
  97. data: data,
  98. method: type,
  99. header: {
  100. 'content-type': 'application/x-www-form-urlencoded'
  101. },
  102. success: function (res) {
  103. wx.hideLoading();
  104. wx.stopPullDownRefresh();
  105. var ret = app.checkCode(res.data.code); //检查token是否失效
  106. if(ret == false){
  107. return false;
  108. }
  109. if (res.data.code == 0) {
  110. if (app.ajaxReadyCallback) { //在onShow中调用数据的,添加在onShow方法内,防止不同页面覆盖
  111. res.data['apiname'] = apiname;
  112. app.ajaxReadyCallback(res)
  113. }
  114. } else {
  115. let showMsg = true;
  116. if(params.showMsg != undefined){
  117. showMsg = params.showMsg;
  118. }
  119. if(showMsg){
  120. wx.showToast({
  121. title: res.data.message,
  122. icon: 'none',
  123. duration: 2000
  124. })
  125. }
  126. }
  127. },
  128. fail: function (err) {
  129. wx.hideLoading();
  130. wx.showToast({
  131. title: '请求失败',
  132. icon: 'none',
  133. duration: 2000
  134. })
  135. }
  136. })
  137. },
  138. loginVisitor:function(type){
  139. wx.login({
  140. success: function (loginRes) {
  141. var app = getApp();
  142. var serverUrl = app.globalData.visitorServerUrl;
  143. if (loginRes) {
  144. wx.request({
  145. url: serverUrl + 'oauth/checkCode',
  146. data: {
  147. type: 2,
  148. code: loginRes.code//临时登录凭证
  149. },
  150. method: 'POST',
  151. header: {
  152. 'content-type': 'application/x-www-form-urlencoded'
  153. },
  154. success: function (res) {
  155. if (res.data.code == -101) {
  156. wx.removeStorageSync("visitorToken");
  157. if(type == 'visitorAdd'){
  158. wx.redirectTo({
  159. url: '/pages/splash/splash?type=visitorAdd'
  160. })
  161. }else{
  162. wx.redirectTo({
  163. url: '/pages/splash/splash?type=visitor'
  164. })
  165. }
  166. }
  167. if (res.data.code == 0) {
  168. try {
  169. wx.setStorageSync("visitorToken", res.data.data.token);
  170. wx.setStorageSync("visitorUserId", res.data.data.userId);
  171. } catch (e) {
  172. this.logout();
  173. wx.showModal({
  174. title: '提示',
  175. content: '登录异常!',
  176. showCancel: false
  177. })
  178. return false;
  179. };
  180. if(type == 'visitorAdd'){
  181. wx.redirectTo({
  182. url: '/pages/visitor/add/index'
  183. });
  184. }else{
  185. wx.redirectTo({
  186. url: '/pages/visitor/index'
  187. });
  188. }
  189. }
  190. },
  191. fail: function (err) {
  192. wx.showModal({
  193. title: '提示',
  194. content: '登录异常!',
  195. showCancel: false
  196. })
  197. }
  198. })
  199. } else {
  200. wx.showModal({
  201. title: '提示',
  202. content: '登录异常!',
  203. showCancel: false
  204. })
  205. }
  206. }
  207. })
  208. },
  209. checkCode2:function(code){
  210. if(code == -100){
  211. wx.showToast({
  212. title: '登录超时,请重新登录',
  213. icon: 'none',
  214. duration: 2000
  215. })
  216. wx.removeStorageSync('visitorToken');
  217. wx.removeStorageSync('visitorUserId');
  218. setTimeout(function(){
  219. var vtype = wx.getStorageSync('visitorType');
  220. if(vtype == 'visitorAdd'){
  221. wx.redirectTo({ //关闭所有页面,打开到应用内的某个页面
  222. url: '/pages/splash/splash?type=visitorAdd'
  223. })
  224. }else{
  225. wx.redirectTo({ //关闭所有页面,打开到应用内的某个页面
  226. url: '/pages/splash/splash?type=visitor'
  227. })
  228. }
  229. },2000);
  230. return false;
  231. }
  232. return true;
  233. },
  234. ajax2:function(params){
  235. if (!params.type) {
  236. var type = 'POST';
  237. }else{
  238. var type = params.type;
  239. }
  240. if (!params.apiname) {
  241. var apiname = '1';
  242. } else {
  243. var apiname = params.apiname;
  244. }
  245. if (!params.data) {
  246. var data = [];
  247. }else{
  248. var data = params.data;
  249. }
  250. var app = getApp();
  251. var token = wx.getStorageSync('visitorToken');
  252. var userId = wx.getStorageSync('visitorUserId');
  253. var vtype = wx.getStorageSync('visitorType');
  254. if (!token || token == undefined) { //未登录
  255. wx.showToast({
  256. title: '未登录',
  257. icon: 'none',
  258. duration: 2000
  259. });
  260. setTimeout(function () {
  261. if(vtype == 'visitorAdd'){
  262. wx.redirectTo({ //关闭所有页面,跳转到闪屏页
  263. url: '/pages/splash/splash?type=visitorAdd'
  264. })
  265. }else{
  266. wx.redirectTo({ //关闭所有页面,跳转到闪屏页
  267. url: '/pages/splash/splash?type=visitor'
  268. })
  269. }
  270. }, 2000);
  271. return false;
  272. }
  273. data['token'] = token;
  274. data['userId'] = userId;
  275. wx.showLoading({
  276. title: '加载中',
  277. mask: true
  278. });
  279. wx.request({
  280. url: params.url,
  281. data: data,
  282. method: type,
  283. header: {
  284. 'content-type': 'application/x-www-form-urlencoded'
  285. },
  286. success: function (res) {
  287. wx.hideLoading();
  288. wx.stopPullDownRefresh();
  289. var ret = app.checkCode2(res.data.code); //检查token是否失效
  290. if(ret == false){
  291. return false;
  292. }
  293. if (res.data.code == 0) {
  294. if (app.ajaxReadyCallback) { //在onShow中调用数据的,添加在onShow方法内,防止不同页面覆盖
  295. res.data['apiname'] = apiname;
  296. app.ajaxReadyCallback(res)
  297. }
  298. } else {
  299. let showMsg = true;
  300. if(params.showMsg != undefined){
  301. showMsg = params.showMsg;
  302. }
  303. if(showMsg){
  304. wx.showToast({
  305. title: res.data.message,
  306. icon: 'none',
  307. duration: 2000
  308. })
  309. }
  310. }
  311. },
  312. fail: function (err) {
  313. wx.hideLoading();
  314. wx.showToast({
  315. title: '请求失败',
  316. icon: 'none',
  317. duration: 2000
  318. })
  319. }
  320. })
  321. },
  322. gotopage: function (url, data) { //传递数据到下一页面的跳转,在新页面onload中获取
  323. wx.setStorage({
  324. key: "gotopage",
  325. data: data,
  326. success: function () {
  327. wx.navigateTo({
  328. url: url,
  329. })
  330. },
  331. fail: function () {
  332. wx.showToast({
  333. title: '跳转失败',
  334. icon: 'none',
  335. duration: 2000
  336. })
  337. return false;
  338. }
  339. });
  340. },
  341. getgotodata: function (url, data) { //传递数据到下一页面的跳转,在新页面onload中获取
  342. wx.getStorage({
  343. key: 'gotopage',
  344. success(res) {
  345. return res.data;
  346. },
  347. fail(res) {
  348. wx.navigateBack({ delta: 1 });
  349. return false;
  350. }
  351. });
  352. }
  353. })