app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. //var bmap = require('/mapjs/bmap-wx.min.js');
  2. var QQMapWX = require("./utils/qqmap-wx-jssdk.js");
  3. var map;
  4. App({
  5. /**
  6. * 全局变量
  7. */
  8. // apiRoot: "https://qingyunhui.songlanyun.com/qyh/api/", // 正式环境api地址
  9. apiRoot: 'http://192.168.0.101:8687/qyh/api/',
  10. // apiRoot: 'http://192.168.0.70:8687/qyh/api/',
  11. // apiRoot: 'http://192.168.0.193:8687/qyh/api/',
  12. // apiRoot: 'http://192.168.0.232:8687/wjxy/api/', //黄理志
  13. // apiRoot: 'http://192.168.0.186:8687/wjxy/api/',
  14. webViewUrl: "http://192.168.0.193:5173/#/", // 青创赛h5端地址
  15. siteInfo: require("siteinfo.js"),
  16. token: "",
  17. // appAssetsUrl: 'https://pm.lanzhongrenli.com/qmjz/app', //静态资源服务器
  18. // appAssetsUrl: "https://wj.songlanyun.com/wjxy/statics/images/app", //静态资源服务器
  19. // appAssetsUrl2: 'http://192.168.0.101:8687/wjxy/statics/images/app/images/', //静态资源服务器
  20. // appAssetsUrl2: "https://wj.songlanyun.com/wjxy/statics/images/app/images/", //静态资源服务器
  21. appAssetsUrl: "https://qingyunhui.songlanyun.com/statics/images/app", //静态资源服务器
  22. appAssetsUrl2: "https://qingyunhui.songlanyun.com/statics/images/app/images/", //静态资源服务器
  23. appAssetsUrl3: "https://qingyunhui.songlanyun.com/statics/images/app/", //静态资源服务器
  24. //自定义参数
  25. params: {
  26. pePageToIndex: "",
  27. },
  28. bottomLeft: 0,
  29. sexList: [],
  30. /**
  31. * 生命周期函数--监听小程序初始化
  32. */
  33. onLaunch: function () {
  34. let that = this;
  35. map = new QQMapWX({
  36. key: "RA3BZ-PSGW4-GZUUX-DDAU7-6B54E-KJFQ7", // 必填
  37. });
  38. console.log("首次启动");
  39. const updateManager = wx.getUpdateManager();
  40. updateManager.onCheckForUpdate(function (res) {
  41. // 请求完新版本信息的回调
  42. console.log(res.hasUpdate);
  43. });
  44. updateManager.onUpdateReady(function () {
  45. wx.showModal({
  46. title: "更新提示",
  47. content: "新版本已经准备好,是否重启应用?",
  48. success (res) {
  49. if (res.confirm) {
  50. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  51. updateManager.applyUpdate();
  52. }
  53. },
  54. });
  55. });
  56. updateManager.onUpdateFailed(function () {
  57. // 新版本下载失败
  58. wx.showToast({
  59. title: "版本更新失败,请检查网络!",
  60. icon: "none",
  61. duration: 1500,
  62. });
  63. });
  64. /* setInterval(function(){
  65. wx.removeStorageSync('Weather');
  66. that.getWeaTher();
  67. console.log("天气已更新");
  68. },7200000);*/
  69. //that.getWeaTher();
  70. // http://zqgj.vaiwan.com
  71. // this.setApiRoot("http://zqgj.vaiwan.com/zqgj");
  72. /* wx.getNetworkType({
  73. success(res) {
  74. const networkType = res.networkType
  75. wx.showToast({
  76. title: '当前网络为' + networkType + '',
  77. icon: 'none'
  78. })
  79. }
  80. })*/
  81. //this.getLocal();
  82. //ios安全区域
  83. wx.getSystemInfo({
  84. success: (res) => {
  85. this.bottomLeft = res.screenHeight - res.safeArea.bottom;
  86. },
  87. });
  88. wx.request({
  89. url: this.apiRoot + "bizlibrary/infoList/gender",
  90. method: "get",
  91. success: function (res) {
  92. // console.log(res);
  93. if (res.data.code == 0) {
  94. that.sexList = res.data.dictList;
  95. }
  96. },
  97. });
  98. // that.font();
  99. },
  100. font () {
  101. wx.loadFontFace({
  102. family: "FZZhunYuan-M02S",
  103. global: true,
  104. source:
  105. 'url("https://wujiexiaoyuan-wechat.oss-cn-hangzhou.aliyuncs.com/font/FZZhunYuan-M02S.woff2")',
  106. // source: 'url("https://wujiexiaoyuan-wechat.oss-cn-hangzhou.aliyuncs.com/font/FZZhunYuan-M02S.ttf")',
  107. success: (msg) => {
  108. console.log(msg);
  109. },
  110. fail: (err) => {
  111. console.log(err);
  112. },
  113. complete: (res) => {
  114. // console.log(res);
  115. },
  116. });
  117. wx.loadFontFace({
  118. family: "FZCuYuan-M03",
  119. global: true,
  120. source:
  121. 'url("https://wujiexiaoyuan-wechat.oss-cn-hangzhou.aliyuncs.com/font/yuanGBK.woff2")',
  122. // source: 'url("https://wujiexiaoyuan-wechat.oss-cn-hangzhou.aliyuncs.com/font/yuanGBK.ttf")',
  123. success: (msg) => {
  124. console.log(msg);
  125. },
  126. fail: (err) => {
  127. console.log(err);
  128. },
  129. complete: (res) => {
  130. // console.log(res);
  131. },
  132. });
  133. },
  134. getLocal () {
  135. const _this = this;
  136. wx.getFuzzyLocation({
  137. type: "gcj02",
  138. success (res) {
  139. const latitude = res.latitude;
  140. const longitude = res.longitude;
  141. //通过【小程序定位】获取经纬度,在通过第三方sdk获取的地址信息
  142. _this.pointToAddress(latitude, longitude, function (address) {
  143. if (address) {
  144. wx.setStorageSync("CHOOSECITY", {
  145. cityName: address.city,
  146. areaName: address.district,
  147. });
  148. let pages = getCurrentPages();
  149. let prevpage = pages[pages.length - 1];
  150. if (prevpage.hasOwnProperty("loadCity")) {
  151. prevpage.loadCity();
  152. }
  153. }
  154. });
  155. },
  156. fail () {
  157. let pages = getCurrentPages();
  158. let prevpage = pages[pages.length - 1];
  159. if (prevpage.hasOwnProperty("loadCity")) {
  160. prevpage.loadCity();
  161. }
  162. },
  163. });
  164. },
  165. // 定义 pointToAddress 方法
  166. pointToAddress: function (latitude, longitude, callback) {
  167. var _this = this;
  168. // 调用接口
  169. map.reverseGeocoder({
  170. location: {
  171. latitude: latitude,
  172. longitude: longitude,
  173. },
  174. success: function (res) {
  175. // 解析成功返回地址
  176. callback(res.result.ad_info);
  177. },
  178. fail: function (res) {
  179. // console.log(res);
  180. },
  181. complete: function (res) { },
  182. });
  183. },
  184. /**
  185. * 当小程序启动,或从后台进入前台显示,会触发 onShow
  186. */
  187. /*
  188. onShow: function (options) {
  189. // 获取小程序基础信息
  190. this.getWxappBase(function (wxapp) {
  191. // 设置navbar标题、颜色
  192. wx.setNavigationBarColor({
  193. frontColor: wxapp.navbar.top_text_color.text,
  194. backgroundColor: wxapp.navbar.top_background_color
  195. })
  196. });
  197. },*/
  198. onShow: function (ops) {
  199. var that = this;
  200. wx.checkSession({
  201. success: function () {
  202. console.log("当前登录状态:有效");
  203. },
  204. fail: function () { },
  205. });
  206. wx.onNetworkStatusChange(function (res) {
  207. if (res.isConnected) {
  208. if (res.networkType !== "wifi") {
  209. wx.showToast({
  210. title: "注意!您正在使用" + res.networkType + "网络",
  211. icon: "none",
  212. });
  213. }
  214. }
  215. });
  216. },
  217. getWeaTher: function () {
  218. if (!wx.getStorageSync("Weather")) {
  219. // 新建百度地图对象
  220. var BMap = new bmap.BMapWX({
  221. ak: "pSzxYKshUcuo1Y9pPrbFs6r7LGvx8sxg",
  222. });
  223. var fail = function (data) {
  224. // console.log(data)
  225. };
  226. var success = function (data) {
  227. var weatherData = data.currentWeather[0];
  228. /* weatherData = '城市:' + weatherData.currentCity + '\n' + 'PM2.5:' + weatherData.pm25 + '\n' + '日期:' + weatherData.date + '\n' + '温度:' + weatherData.temperature + '\n' + '天气:' + weatherData.weatherDesc + '\n' + '风力:' + weatherData.wind + '\n';*/
  229. //Weather(weatherData.weatherDesc, _this, weatherData);
  230. wx.setStorageSync("Weather", weatherData);
  231. };
  232. // 发起weather请求
  233. BMap.weather({
  234. fail: fail,
  235. success: success,
  236. });
  237. }
  238. },
  239. /**
  240. * 设置api地址
  241. */
  242. setApiRoot: function () {
  243. //this.apiRoot = this.siteInfo.siteroot + '/api/';
  244. },
  245. /**
  246. * 获取小程序基础信息
  247. */
  248. /*
  249. getWxappBase: function (callback) {
  250. let App = this;
  251. App._get('wxapp/base', {}, function (result) {
  252. // 记录小程序基础信息
  253. wx.setStorageSync('wxapp', result.data.wxapp);
  254. callback && callback(result.data.wxapp);
  255. }, false, false);
  256. },*/
  257. /**
  258. * 显示成功提示框
  259. */
  260. showSuccess: function (msg, callback) {
  261. wx.showToast({
  262. title: msg,
  263. icon: "success",
  264. success: function () {
  265. callback &&
  266. setTimeout(function () {
  267. callback();
  268. }, 1500);
  269. },
  270. });
  271. },
  272. /**
  273. * 显示失败提示框
  274. */
  275. showError: function (msg, callback) {
  276. wx.showToast({
  277. title: msg,
  278. icon: "none",
  279. duration: 2000,
  280. success: function (res) {
  281. callback && callback();
  282. },
  283. });
  284. },
  285. /**
  286. * get请求
  287. */
  288. _get: function (url, data, success, fail, complete) {
  289. wx.showNavigationBarLoading();
  290. let App = this;
  291. // 构造请求参数
  292. data = data || {};
  293. wx.request({
  294. url: App.apiRoot + url,
  295. data: data,
  296. method: "get",
  297. success: function (res) {
  298. wx.hideNavigationBarLoading();
  299. wx.hideLoading();
  300. if (res.statusCode !== 200 || typeof res.data !== "object") {
  301. return false;
  302. }
  303. if (res.data.code === -1) {
  304. // 登录态失效, 重新登录
  305. return false;
  306. } else {
  307. success && success(res.data);
  308. }
  309. },
  310. fail: function (res) {
  311. wx.hideNavigationBarLoading();
  312. // console.log(res);
  313. App.showError(res.errMsg, function () {
  314. fail && fail(res);
  315. });
  316. },
  317. complete: function (res) {
  318. wx.hideNavigationBarLoading();
  319. complete && complete(res);
  320. },
  321. });
  322. },
  323. /**
  324. * post提交
  325. */
  326. _post_form: function (url, header, data, success, fail, complete) {
  327. wx.showNavigationBarLoading();
  328. let App = this;
  329. wx.request({
  330. url: App.apiRoot + url,
  331. header: {
  332. "content-type": header,
  333. },
  334. method: "POST",
  335. data: data,
  336. success: function (res) {
  337. wx.showNavigationBarLoading();
  338. wx.hideLoading();
  339. if (res.statusCode !== 200 && typeof res.data.code !== 1) {
  340. App.showError("网络请求出错");
  341. return false;
  342. }
  343. if (res.data.code === -1) {
  344. // 登录态失效, 重新登录
  345. App.doLogin(function () {
  346. App._post_form(url, data, success, fail);
  347. });
  348. return false;
  349. } else if (res.data.code === 0) {
  350. success && success(res.data);
  351. } else if (res.data.retCode === "000000") {
  352. success && success(res.data);
  353. return;
  354. } else {
  355. var msg = "";
  356. if (res.data.message) {
  357. msg = res.data.message;
  358. } else if (res.data.msg) {
  359. msg = res.data.msg;
  360. }
  361. App.showError(msg);
  362. success && success(res.data);
  363. return false;
  364. }
  365. },
  366. fail: function (res) {
  367. wx.showNavigationBarLoading();
  368. App.showError(res.errMsg, function () {
  369. fail && fail(res);
  370. });
  371. },
  372. complete: function (res) {
  373. complete && complete(res);
  374. wx.hideNavigationBarLoading();
  375. },
  376. });
  377. },
  378. // put 请求
  379. _put_form: function (url, header, data, success, fail, complete) {
  380. wx.showNavigationBarLoading();
  381. let App = this;
  382. wx.request({
  383. url: App.apiRoot + url,
  384. header: {
  385. "content-type": header,
  386. },
  387. method: "PUT",
  388. data: data,
  389. success: function (res) {
  390. wx.showNavigationBarLoading();
  391. wx.hideLoading();
  392. if (res.statusCode !== 200 && typeof res.data.code !== 1) {
  393. App.showError("网络请求出错");
  394. return false;
  395. }
  396. if (res.data.code === -1) {
  397. // 登录态失效, 重新登录
  398. App.doLogin(function () {
  399. App._post_form(url, data, success, fail);
  400. });
  401. return false;
  402. } else if (res.data.code === 0) {
  403. success && success(res.data);
  404. } else if (res.data.retCode === "000000") {
  405. success && success(res.data);
  406. return;
  407. } else {
  408. var msg = "";
  409. if (res.data.message) {
  410. msg = res.data.message;
  411. } else if (res.data.msg) {
  412. msg = res.data.msg;
  413. }
  414. // App.showError(msg);
  415. console.log(msg, 11111111);
  416. success && success(res.data);
  417. wx.showToast({
  418. title: msg,
  419. icon: "none",
  420. duration: 2000,
  421. });
  422. return false;
  423. }
  424. },
  425. fail: function (res) {
  426. wx.showNavigationBarLoading();
  427. App.showError(res.errMsg, function () {
  428. fail && fail(res);
  429. });
  430. },
  431. complete: function (res) {
  432. complete && complete(res);
  433. wx.hideNavigationBarLoading();
  434. },
  435. });
  436. },
  437. /**
  438. * 对象转URL
  439. */
  440. urlEncode: function urlencode (data) {
  441. var _result = [];
  442. for (var key in data) {
  443. var value = data[key];
  444. if (value.constructor == Array) {
  445. value.forEach(function (_value) {
  446. _result.push(key + "=" + _value);
  447. });
  448. } else {
  449. _result.push(key + "=" + value);
  450. }
  451. }
  452. return _result.join("&");
  453. },
  454. city: function () {
  455. return cityCode;
  456. },
  457. /**
  458. * 设置当前页面标题
  459. */
  460. /*
  461. setTitle: function() {
  462. let App = this,
  463. wxapp;
  464. if (wxapp = wx.getStorageSync('wxapp')) {
  465. wx.setNavigationBarTitle({
  466. title: wxapp.navbar.wxapp_title
  467. });
  468. } else {
  469. App.getWxappBase(function() {
  470. App.setTitle();
  471. });
  472. }
  473. },
  474. */
  475. });