暂无描述

app.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. //app.js
  2. import wxValidate from 'utils/wxValidate';
  3. App({
  4. onLaunch: function () {},
  5. globalData: {
  6. userInfo: null,
  7. httpsUrl: 'https://cgdl.yutingcn.com/',
  8. // httpsUrlServer: 'http://39.164.159.226:9090', //个人接口地址
  9. httpsUrlServer:'http://zhyh.hnyulin.com/api/',
  10. zdzUrlServer:'http://58.246.189.90:30000', //市指导站推送测试接口地址
  11. openId: '',
  12. token: '',
  13. mq: 'Lh20Sys20K',
  14. mh: 'pre',
  15. fUserid: '',
  16. fUsercode: '',
  17. fPassword: '',
  18. latitude: '', //经度
  19. longitude: '', //维度
  20. wxPhoneNumber:'',
  21. personnel: true, // 工作人员模块 判断 对象
  22. msg: {
  23. // 饼图数据
  24. ykhd: 1,
  25. wkhd: 1,
  26. zkhd: 1,
  27. sqgy: 1,
  28. zhgy: 1,
  29. dwgy: 1,
  30. },
  31. orderType: '',
  32. orderState: '',
  33. currentTab: '',
  34. treeObject: {}, // 行道树修改
  35. pulicObject: {}, // 公共设施修改
  36. networkAnomalyFlag: null, //网络异常
  37. planModify: '', // 计划修改(公园)
  38. planModifyFlag: '', // 计划修改(公园)
  39. planID: '', // 查询详情用到的ID
  40. },
  41. //封装请求方法,用于请求时携带统一的参数
  42. request: function (data) {
  43. let that = this;
  44. if (typeof data.header === 'undefined') {
  45. data.header = {};
  46. }
  47. (data.header.Authorization = 'openId ' + wx.getStorageSync('openId')), //将请求必须的参数放在请求头
  48. wx.request(data);
  49. },
  50. /**
  51. * 封装wx.request请求
  52. * method: 请求方式
  53. * url: 请求地址
  54. * data: 要传递的参数
  55. * callback: 请求成功回调函数
  56. * errFun: 请求失败回调函数
  57. **/
  58. // token查看
  59. tabbarHide() {
  60. var token = wx.getStorageSync('token');
  61. if (token) {
  62. wx.showTabBar();
  63. } else {
  64. wx.hideTabBar();
  65. }
  66. },
  67. wxRequest(method, url, data, callback, errFun) {
  68. wx.request({
  69. url: url,
  70. method: method,
  71. data: data,
  72. header: {
  73. 'content-type':method == 'GET'?'application/json':'application/json; charset=utf-8',
  74. Accept: 'application/json',
  75. token: wx.getStorageSync('token'),
  76. fSource: 1,
  77. },
  78. dataType: 'json',
  79. success: function (res) {
  80. if(res.status == 600){
  81. wx.showToast({
  82. title: res.message,
  83. icon: 'none',
  84. duration: 2000,
  85. mask: true,
  86. });
  87. setTimeout(() => {
  88. console.log(12312);
  89. wx.reLaunch({
  90. url: '/pages/login/login',
  91. });
  92. }, 2000);
  93. }else{
  94. callback(res.data);
  95. }
  96. },
  97. fail: function (err) {
  98. errFun(err);
  99. },
  100. });
  101. },
  102. WxValidate: (rules, messages) => new wxValidate(rules, messages),
  103. // 转换时间戳
  104. getTimeStamp(val) {
  105. return new Date(val).getTime();
  106. },
  107. //时间戳转化为日期格式
  108. transTime(unixtime) {
  109. var dateTime = new Date(parseInt(unixtime));
  110. var year = dateTime.getFullYear();
  111. if (dateTime.getMonth() + 1 < 10) {
  112. var month = '0' + (dateTime.getMonth() + 1);
  113. } else {
  114. var month = dateTime.getMonth() + 1;
  115. }
  116. if (dateTime.getDate() < 10) {
  117. var day = '0' + dateTime.getDate();
  118. } else {
  119. var day = dateTime.getDate();
  120. }
  121. var now = new Date();
  122. var now_new = Date.parse(now.toDateString());
  123. var milliseconds = now_new - dateTime;
  124. var timeSpanStr = year + '-' + month + '-' + day;
  125. return timeSpanStr;
  126. },
  127. getReq(url, cb) {
  128. wx.request({
  129. url: url,
  130. method: 'get',
  131. header: {
  132. Accept: 'application/json',
  133. 'content-type': 'application/json;charset=UTF-8',
  134. token: wx.getStorageSync('token'),
  135. fSource: 1,
  136. },
  137. success: function (res) {
  138. console.log('resss-', res)
  139. if (res.data.status == 200) {
  140. return typeof cb == 'function' && cb(res.data);
  141. } else if (res.data.status == 600) {
  142. wx.showToast({
  143. title: res.data.message,
  144. icon: 'none',
  145. duration: 2000,
  146. mask: true,
  147. });
  148. setTimeout(() => {
  149. console.log(12312);
  150. wx.reLaunch({
  151. url: '/pages/login/login',
  152. });
  153. }, 2000);
  154. }
  155. },
  156. fail: (res) => {
  157. wx.hideLoading();
  158. this.networkAnomaly();
  159. return typeof cb == 'function' && cb(false);
  160. },
  161. });
  162. },
  163. getReqNew(url, data, cb) {
  164. wx.request({
  165. url: url,
  166. method: 'get',
  167. header: {
  168. Accept: 'application/json',
  169. 'content-type': 'application/json;charset=UTF-8',
  170. token: wx.getStorageSync('token'),
  171. fSource: 1,
  172. },
  173. data:data || {},
  174. success: function (res) {
  175. console.log(res)
  176. if (res.data.code == 0) {
  177. return typeof cb == 'function' && cb(res.data);
  178. } else if (res.data.code == 500) {
  179. wx.showToast({
  180. title: res.data.msg,
  181. icon: 'none',
  182. duration: 2000,
  183. mask: true,
  184. });
  185. }else if(res.statusCode == 404){
  186. wx.hideLoading();
  187. }
  188. },
  189. fail: (res) => {
  190. console.log(res)
  191. wx.hideLoading();
  192. this.networkAnomaly();
  193. return typeof cb == 'function' && cb(false);
  194. },
  195. });
  196. },
  197. postReq(url, data, cb) {
  198. wx.request({
  199. url: url,
  200. header: {
  201. Accept: 'application/json',
  202. 'content-type': 'application/json;charset=UTF-8',
  203. token: wx.getStorageSync('token'),
  204. fSource: 1,
  205. },
  206. data: {
  207. data,
  208. },
  209. method: 'post',
  210. success: function (res) {
  211. if (res.data.status == 200) {
  212. return typeof cb == 'function' && cb(res.data);
  213. } else if (res.status == 600) {
  214. wx.showToast({
  215. title: res.data.message || '网络错误',
  216. icon: 'none',
  217. duration: 2000,
  218. mask: true,
  219. });
  220. setTimeout(() => {
  221. wx.reLaunch({
  222. url: '/pages/login/login',
  223. });
  224. }, 2000);
  225. } else {
  226. wx.showToast({
  227. title: res.data.message || '网络错误',
  228. icon: 'none',
  229. duration: 2000,
  230. mask: true,
  231. });
  232. }
  233. },
  234. fail: () => {
  235. wx.hideLoading();
  236. this.networkAnomaly();
  237. return typeof cb == 'function' && cb(false);
  238. },
  239. });
  240. },
  241. postReqNew(url, data, cb) {
  242. wx.request({
  243. url: url,
  244. header: {
  245. Accept: 'application/json',
  246. 'content-type': 'application/json;charset=UTF-8',
  247. token: wx.getStorageSync('token'),
  248. fSource: 1,
  249. },
  250. data: data,
  251. method: 'post',
  252. success: function (res) {
  253. if (res.data.code == 0) {
  254. return typeof cb == 'function' && cb(res.data);
  255. } else if (res.status == 500) {
  256. wx.showToast({
  257. title: res.data.msg || '网络错误',
  258. icon: 'none',
  259. duration: 2000,
  260. mask: true,
  261. });
  262. // setTimeout(() => {
  263. // wx.reLaunch({
  264. // url: '/pages/login/login',
  265. // });
  266. // }, 2000);
  267. } else {
  268. wx.showToast({
  269. title: res.data.msg || '网络错误',
  270. icon: 'none',
  271. duration: 2000,
  272. mask: true,
  273. });
  274. }
  275. },
  276. fail: () => {
  277. wx.hideLoading();
  278. this.networkAnomaly();
  279. return typeof cb == 'function' && cb(false);
  280. },
  281. });
  282. },
  283. postReqNew2(url, data, cb) {
  284. wx.request({
  285. url: url,
  286. header: {
  287. Accept: 'application/json',
  288. 'content-type': 'application/json;charset=UTF-8',
  289. token: wx.getStorageSync('token'),
  290. fSource: 1,
  291. },
  292. data: data,
  293. method: 'post',
  294. success: function (res) {
  295. return typeof cb == 'function' && cb(res.data);
  296. },
  297. fail: () => {
  298. wx.hideLoading();
  299. this.networkAnomaly();
  300. return typeof cb == 'function' && cb(false);
  301. },
  302. });
  303. },
  304. // 获取字典封装方法
  305. getDictionaries(data, cb) {
  306. wx.request({
  307. url: this.globalData.httpsUrlServer + '/sysdictionaryvalue/selectDictionaryByFlag',
  308. header: {
  309. Accept: 'application/json',
  310. 'content-type': 'application/json;charset=UTF-8',
  311. token: wx.getStorageSync('token'),
  312. fSource: 1,
  313. },
  314. data: {
  315. data,
  316. },
  317. method: 'post',
  318. success: function (res) {
  319. // wx.hideLoading();
  320. return typeof cb == 'function' && cb(res.data);
  321. },
  322. fail: () => {
  323. // wx.hideLoading();
  324. // wx.showModal({
  325. // title: "网络错误",
  326. // content: "网络出错,请刷新重试",
  327. // showCancel: false,
  328. // })
  329. this.networkAnomaly();
  330. return typeof cb == 'function' && cb(false);
  331. },
  332. });
  333. },
  334. // 提示
  335. toast(title) {
  336. wx.showToast({
  337. title: title,
  338. icon: 'none',
  339. duration: 2000,
  340. });
  341. },
  342. // 等待
  343. loading(title = 'loading~') {
  344. wx.showLoading({
  345. title: title,
  346. mask: true,
  347. });
  348. },
  349. // 结束等待
  350. hideloading() {
  351. wx.hideLoading();
  352. },
  353. // 封装 验证手机号格式 方法
  354. checkPhone(phone) {
  355. if (!/^1\d{10}$/.test(phone)) {
  356. return false; //验证 失败
  357. } else {
  358. return true; //验证 成功
  359. }
  360. },
  361. // 封装 验证电话号 方法
  362. checkTel(tel) {
  363. if (!/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(tel)) {
  364. return false;
  365. } else {
  366. return true;
  367. }
  368. },
  369. // 封装 获取个人信息的 方法, 中心 或者 公园
  370. getUserTrue() {
  371. let url =
  372. this.globalData.httpsUrlServer + '/sysroleinfo/selectRoleInfoByUserId';
  373. const p = new Promise((resolve, reject) => {
  374. wx.request({
  375. url: url,
  376. method: 'get',
  377. header: {
  378. Accept: 'application/json',
  379. 'content-type': 'application/json;charset=UTF-8',
  380. token: wx.getStorageSync('token'),
  381. fSource: 1,
  382. },
  383. success: function (res) {
  384. resolve(res.data);
  385. },
  386. fail: function (err) {
  387. reject(err);
  388. },
  389. });
  390. });
  391. return p;
  392. },
  393. sendRequest(method, url, data) {
  394. url = this.globalData.httpsUrlServer + url;
  395. return new Promise((resolve, reject) => {
  396. wx.request({
  397. url,
  398. method,
  399. data,
  400. header: {
  401. Accept: 'application/json',
  402. 'content-type': 'application/json;charset=UTF-8',
  403. token: wx.getStorageSync('token'),
  404. fSource: 1,
  405. },
  406. success: function (res) {
  407. resolve(res.data);
  408. },
  409. fail: function (err) {
  410. reject(err);
  411. },
  412. });
  413. });
  414. },
  415. networkAnomaly() {
  416. if (!this.globalData.networkAnomalyFlag) {
  417. this.globalData.networkAnomalyFlag = setTimeout(() => {
  418. this.globalData.networkAnomalyFlag = null;
  419. }, 6000);
  420. wx.showModal({
  421. title: '网络错误',
  422. content: '网络出错,请刷新重试',
  423. showCancel: false,
  424. });
  425. }
  426. },
  427. });