authorize.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // pages/authorize/authorize.js
  2. var app = getApp();
  3. Page({
  4. data: {
  5. link :'' ,//授权成功后的跳转地址
  6. code : '',
  7. fromUserId : '',//分享者的id
  8. fromUserRole : '',//分享者的角色
  9. hiddenmodal: true, //提示弹窗 - 初始隐藏
  10. },
  11. onLoad: function (options) {
  12. var that = this;
  13. var link = options.link;
  14. that.setData({
  15. link: link
  16. })
  17. },
  18. onShow:function(){
  19. var that = this;
  20. wx.login({
  21. success: function (res) {
  22. if (res.code) {
  23. that.setData({
  24. code: res.code
  25. })
  26. }
  27. }
  28. })
  29. },
  30. //确认授权
  31. onGotUserInfo:function(e){
  32. var that = this;
  33. var userInfo = e.detail.userInfo;
  34. var host = getApp().globalData.servsers;
  35. var un_id = e.detail.userInfo;
  36. var link = that.data.link;
  37. console.log(e.detail);
  38. if (userInfo != undefined){
  39. var pages = getCurrentPages();
  40. var currPage = pages[pages.length - 1]; //当前页面
  41. var prevPage = pages[pages.length - 2]; //上一个页面
  42. var prevPageUrl = prevPage.route;
  43. var name = e.detail.userInfo.nickName;
  44. name = name.replace(/\?/g, '');
  45. name = unescape(escape(name).replace(/\%uD.{3}/g, ''));
  46. var icon = e.detail.userInfo.avatarUrl;
  47. var iv = e.detail.iv;
  48. var encryptedData = e.detail.encryptedData;
  49. getApp().globalData.userInfo = userInfo;
  50. // wx.login({
  51. // success: function (res) {
  52. var code = that.data.code;
  53. console.log("code:" + code);
  54. wx.request({
  55. url: host + "userapi/insertadd",
  56. data: {
  57. user_id: code,
  58. name: name,
  59. icon: icon,
  60. iv: iv,
  61. encryptedData: encryptedData,
  62. },
  63. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  64. header: {
  65. 'Accept': 'application/json'
  66. },
  67. success: function (res) {
  68. console.log("auu:");
  69. console.log(res);
  70. if(res.data.code == 500){
  71. that.setData({
  72. hiddenmodal: false,
  73. modalCont: '授权超时,请重新授权!'
  74. })
  75. setTimeout(function () {
  76. that.setData({
  77. hiddenmodal: true
  78. })
  79. wx.login({
  80. success: function (res) {
  81. if (res.code) {
  82. that.setData({
  83. code: res.code
  84. })
  85. }
  86. }
  87. })
  88. }, 1000)
  89. return false;
  90. }else{
  91. //确认授权
  92. getApp().globalData.un_id = res.data.unionId;
  93. getApp().globalData.openid = res.data.openid;
  94. getApp().globalData.isNewUser = res.data.isNewUser;
  95. getApp().globalData.userRole = res.data.userRole;
  96. console.log("authorize-unionId:" + res.data.unionId);
  97. console.log("authorize-openid:" + res.data.openid);
  98. console.log("authorize-isNewUser:" + res.data.isNewUser);
  99. console.log("authorize-userRole:" + res.data.userRole);
  100. console.log("link:" + link);
  101. //授权成功后,跳转
  102. if (link != 'awaitState' && link != 'buy' && link != 'mine' && link != 'customized') {//来自收礼物页面的授权请求 来自产品详情页
  103. wx.redirectTo({
  104. url: "/pages/" + link + "/" + link,
  105. })
  106. } else if (link == 'buy') {
  107. prevPage.setData({
  108. authorize: 1
  109. })
  110. wx.navigateBack({
  111. delta: 1
  112. })
  113. } else if (link == 'mine') {
  114. wx.switchTab({
  115. url: "/pages/" + link + "/" + link,
  116. })
  117. } else if (link == 'customized') { // 设计
  118. wx.redirectTo({
  119. url: "/customized/pages/made/made",
  120. })
  121. }else if (link == 'midAutumn') {
  122. wx.navigateBack({
  123. delta: 1
  124. })
  125. } else {
  126. prevPage.setData({
  127. authorize: 1
  128. })
  129. wx.navigateBack({
  130. delta: 2
  131. })
  132. }
  133. if (res == null || res.data == null) {
  134. console.error('网络请求失败');
  135. return;
  136. }
  137. }
  138. }
  139. })
  140. // }
  141. // })
  142. }else{
  143. //拒绝
  144. return false;
  145. }
  146. },
  147. //页面卸载[完全关闭]
  148. onUnload: function () {
  149. var that = this;
  150. that.setData({
  151. wrapShowFlag: '',
  152. loadingFinish: false,
  153. })
  154. wx.hideLoading();
  155. },
  156. })
  157. var Util = require('../../utils/util.js');