mpweixinsso.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * 微信公众号单点登录
  3. * 目前这个页面有两个功能 一个是绑定账号 一个是单点登录
  4. */
  5. layout.addReady(function(){
  6. (function(layout){
  7. layout.mobile = true;
  8. var href = locate.href;
  9. if (href.indexOf("debugger") != -1) layout.debugger = true;
  10. var _bindWeixin2User = function () {
  11. var uri = href.toURI();
  12. var code = uri.getData("code"); //微信code
  13. if (code) {
  14. // layout.showLoading();
  15. o2.Actions.load("x_organization_assemble_authentication").MPweixinAction.bindWithCode(code, function (json) {
  16. // layout.hideLoading();
  17. //绑定成功
  18. var box = new Element("div", { "style": "text-align: center;" }).inject($("appContent"));
  19. new Element("h2", { "text": "绑定成功!" }).inject(box);
  20. }, function (err) {
  21. // layout.hideLoading();
  22. console.log(err);
  23. // layout.notice('绑定账号失败', 'error');
  24. })
  25. } else {
  26. // layout.hideLoading();
  27. console.log('没有传入微信code无法绑定');
  28. // layout.notice('没有传入微信code无法绑定', 'error');
  29. }
  30. };
  31. var uri = href.toURI();
  32. var redirect = uri.getData("redirect"); //登录成功后跳转地址
  33. var code = uri.getData("code"); //微信code
  34. console.log("code:" + code)
  35. var type = uri.getData("type"); // bind 是绑定
  36. console.log("type:" + type)
  37. if (type && type === "bind") { // 绑定要登录
  38. if (layout.session && layout.session.user){
  39. _bindWeixin2User();
  40. }else{
  41. if (layout.sessionPromise){
  42. layout.sessionPromise.then(function(){
  43. _bindWeixin2User();
  44. },function(){});
  45. }
  46. }
  47. } else { //code 单点登录
  48. // layout.showLoading();
  49. o2.Actions.load("x_organization_assemble_authentication").MPweixinAction.loginWithCode(code, function (json) {
  50. // layout.hideLoading();
  51. layout.session.user = json.data;
  52. if (redirect) {
  53. history.replaceState(null, "page", redirect);
  54. redirect.toURI().go();
  55. } else {
  56. history.replaceState(null, "page", "../x_desktop/appMobile.html?app=process.TaskCenter");
  57. "appMobile.html?app=process.TaskCenter".toURI().go();
  58. }
  59. }, function (err) {
  60. // layout.hideLoading();
  61. console.log(err)
  62. // layout.notice('单点登录失败,请先绑定用户', 'error');
  63. });
  64. }
  65. })(layout);
  66. });