app.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //代理全局变量
  2. var watchGlobalData = function (app) {
  3. let handler = {
  4. get(target, key) {
  5. return target[key];
  6. },
  7. set(target, key, value) {
  8. target[key] = value;
  9. app.watch.$emit(key, value);
  10. return true;
  11. }
  12. }
  13. app.globalData = new Proxy({
  14. userInfo: null,
  15. config: null,
  16. token: '',
  17. indexTabList: [],
  18. newsTabList: [],
  19. productTabList: [],
  20. bannerList: []
  21. }, handler)
  22. }
  23. App({
  24. //请注意小程序只支持https
  25. apiUrl: 'http://www.fa.com',
  26. si: 0,
  27. //小程序启动
  28. onLaunch: function () {
  29. var that = this;
  30. //加入代理全局变量
  31. watchGlobalData(that);
  32. that.request('/addons/cms/wxapp.common/init', {}, function (data, ret) {
  33. that.globalData.config = data.config;
  34. that.globalData.indexTabList = data.indexTabList;
  35. that.globalData.bannerList = data.bannerList;
  36. that.globalData.newsTabList = data.newsTabList;
  37. that.globalData.productTabList = data.productTabList;
  38. //如果需要一进入小程序就要求授权登录,可在这里发起调用
  39. if (wx.getStorageSync("token")) {
  40. that.check(function (ret) {});
  41. }
  42. }, function (data, ret) {
  43. that.error(ret.msg);
  44. });
  45. },
  46. //投票
  47. vote: function (event, cb) {
  48. var that = this;
  49. if (!that.globalData.userInfo) {
  50. that.error('请登录后再发表意见');
  51. return;
  52. }
  53. var id = event.currentTarget.dataset.id;
  54. var type = event.currentTarget.dataset.type;
  55. var vote = wx.getStorageSync("vote") || [];
  56. if (vote.indexOf(id) > -1) {
  57. that.info("你已经发表过意见了,请勿重复操作");
  58. return;
  59. }
  60. vote.push(id);
  61. wx.setStorageSync("vote", vote);
  62. this.request('/addons/cms/wxapp.archives/vote', {
  63. id: id,
  64. type: type
  65. }, function (data, ret) {
  66. typeof cb == "function" && cb(data);
  67. }, function (data, ret) {
  68. that.error(ret.msg);
  69. });
  70. },
  71. //判断是否登录
  72. check: function (cb) {
  73. var that = this;
  74. if (this.globalData.userInfo) {
  75. typeof cb == "function" && cb(this.globalData.userInfo);
  76. } else {
  77. that.login({}, function () {})
  78. }
  79. },
  80. //登录
  81. login: function (userInfo, cb) {
  82. var that = this;
  83. var token = wx.getStorageSync('token') || '';
  84. //调用登录接口
  85. wx.login({
  86. success: function (res) {
  87. if (res.code) {
  88. //发起网络请求
  89. wx.request({
  90. url: that.apiUrl + '/addons/cms/wxapp.user/login',
  91. data: {
  92. code: res.code,
  93. rawData: JSON.stringify(userInfo),
  94. token: token
  95. },
  96. method: 'post',
  97. header: {
  98. "Content-Type": "application/x-www-form-urlencoded",
  99. },
  100. success: function (lres) {
  101. var response = lres.data
  102. if (response.code == 1) {
  103. that.globalData.userInfo = response.data.userInfo;
  104. wx.setStorageSync('token', response.data.userInfo.token);
  105. typeof cb == "function" && cb(that.globalData.userInfo);
  106. } else {
  107. wx.setStorageSync('token', '');
  108. console.log("用户登录失败")
  109. that.showLoginModal(cb);
  110. }
  111. }
  112. });
  113. } else {
  114. that.showLoginModal(cb);
  115. }
  116. }
  117. });
  118. },
  119. //显示登录或授权提示
  120. showLoginModal: function (cb) {
  121. var that = this;
  122. if (!that.globalData.userInfo) {
  123. //获取用户信息
  124. wx.showModal({
  125. title: '温馨提示',
  126. content: '当前无法获取到你的个人信息,部分操作可能受到限制',
  127. confirmText: "重新登录",
  128. cancelText: "暂不登录",
  129. success: function (res) {
  130. if (res.confirm) {
  131. wx.getUserProfile({
  132. lang: 'zh',
  133. desc: '授权用户信息',
  134. success: function (res) {
  135. that.login(res.userInfo, function () {});
  136. },
  137. fail: function (e) {
  138. that.info(JSON.stringify(e));
  139. }
  140. })
  141. } else {
  142. console.log('用户暂不登录');
  143. }
  144. }
  145. });
  146. } else {
  147. typeof cb == "function" && cb(that.globalData.userInfo);
  148. }
  149. },
  150. //退出
  151. logout: function (cb) {
  152. var that = this;
  153. var token = wx.getStorageSync('token') || '';
  154. //发起网络请求
  155. wx.request({
  156. url: that.apiUrl + '/addons/cms/wxapp.user/logout',
  157. data: {
  158. token: token
  159. },
  160. method: 'post',
  161. header: {
  162. "Content-Type": "application/x-www-form-urlencoded",
  163. },
  164. success: function (lres) {
  165. var response = lres.data
  166. if (response.code == 1) {
  167. that.globalData.userInfo = null;
  168. wx.setStorageSync('token', "");
  169. typeof cb == "function" && cb(that.globalData.userInfo);
  170. } else {
  171. wx.setStorageSync('token', '');
  172. }
  173. }
  174. });
  175. },
  176. //发起网络请求
  177. request: function (url, data, success, error) {
  178. var that = this;
  179. if (typeof data == 'function') {
  180. success = data;
  181. error = success;
  182. data = {};
  183. }
  184. if (this.globalData.userInfo) {
  185. data['user_id'] = this.globalData.userInfo.id;
  186. data['token'] = this.globalData.userInfo.token;
  187. }
  188. //移除最前的/
  189. // while (url.charAt(0) === '/')
  190. // url = url.slice(1);
  191. this.loading(true);
  192. let cookie = wx.getStorageSync('cookieKey');
  193. let header = {
  194. "Content-Type": "application/x-www-form-urlencoded"
  195. };
  196. if (cookie) {
  197. header.Cookie = cookie;
  198. }
  199. if (this.globalData.__token__) {
  200. data.__token__ = this.globalData.__token__;
  201. }
  202. data._ajax = 1;
  203. wx.request({
  204. url: this.apiUrl + url,
  205. data: data,
  206. method: 'post',
  207. header: header,
  208. success: function (res) {
  209. that.loading(false);
  210. var code, msg, json;
  211. if (res && res.header) {
  212. if (res.header['Set-Cookie']) {
  213. wx.setStorageSync('cookieKey', res.header['Set-Cookie']); //保存Cookie到Storage
  214. }
  215. if (res.header['__token__']) {
  216. that.globalData.__token__ = res.header['__token__'];
  217. }
  218. }
  219. if (res.statusCode === 200) {
  220. json = res.data;
  221. if (json.code === 1) {
  222. typeof success === 'function' && success(json.data, json);
  223. } else {
  224. typeof error === 'function' && error(json.data, json);
  225. }
  226. } else {
  227. json = typeof res.data === 'object' ? res.data : {
  228. code: 0,
  229. msg: '发生一个未知错误',
  230. data: null
  231. };
  232. typeof error === 'function' && error(json.data, json);
  233. }
  234. },
  235. fail: function (res) {
  236. that.loading(false);
  237. console.log("fail:", res);
  238. typeof error === 'function' && error(null, {
  239. code: 0,
  240. msg: '',
  241. data: null
  242. });
  243. }
  244. });
  245. },
  246. //构造CDN地址
  247. cdnurl: function (url) {
  248. return url.toString().match(/^https?:\/\/(.*)/i) ? url : this.globalData.config.upload.cdnurl + url;
  249. },
  250. //文本提示
  251. info: function (msg, cb) {
  252. wx.showToast({
  253. title: msg,
  254. icon: 'none',
  255. duration: 2000,
  256. complete: function () {
  257. typeof cb == "function" && cb();
  258. }
  259. });
  260. },
  261. //成功提示
  262. success: function (msg, cb) {
  263. wx.showToast({
  264. title: msg,
  265. icon: 'success',
  266. image: '/assets/images/ok.png',
  267. duration: 2000,
  268. complete: function () {
  269. typeof cb == "function" && cb();
  270. }
  271. });
  272. },
  273. //错误提示
  274. error: function (msg, cb) {
  275. wx.showToast({
  276. title: msg,
  277. icon: 'none',
  278. // image: '/assets/images/error.png',
  279. duration: 2000,
  280. complete: function () {
  281. typeof cb == "function" && cb();
  282. }
  283. });
  284. },
  285. //警告提示
  286. warning: function (msg, cb) {
  287. wx.showToast({
  288. title: msg,
  289. image: '/assets/images/warning.png',
  290. duration: 2000,
  291. complete: function () {
  292. typeof cb == "function" && cb();
  293. }
  294. });
  295. },
  296. //Loading
  297. loading: function (msg) {
  298. if (typeof msg == 'boolean') {
  299. if (!msg) {
  300. if (!this.si) {
  301. return;
  302. }
  303. clearTimeout(this.si);
  304. wx.hideLoading({});
  305. return;
  306. }
  307. }
  308. msg = typeof msg == 'undefined' || typeof msg == 'boolean' ? '加载中' : msg;
  309. this.globalData.loading = true;
  310. if (this.si) {
  311. return;
  312. }
  313. this.si = setTimeout(function () {
  314. wx.showLoading({
  315. title: msg
  316. });
  317. }, 300);
  318. },
  319. watch: function (method = null) {
  320. },
  321. //全局信息
  322. globalData: {},
  323. //事件触发
  324. watch: (function () {
  325. let events = {};
  326. return {
  327. $once(name, callback) {
  328. console.log(name)
  329. events[name] = callback;
  330. },
  331. $emit(key, value) {
  332. let fn = events[key];
  333. if(fn == undefined){
  334. return;
  335. }
  336. if (typeof fn == 'function') {
  337. fn(value)
  338. }
  339. delete events[key];
  340. }
  341. }
  342. }()),
  343. })