app.js 7.4 KB

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