userinfo.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // pages/userinfo/userinfo.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. buttonDisabled: false,
  9. modalHidden: true,
  10. userInfo: {},
  11. hasUserInfo: false,
  12. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  13. inputContent: '',
  14. inputContentxml: '',
  15. id: '',
  16. lock: false,
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. var that = this;
  23. console.log('userinfo');
  24. console.log("userinfo"+app.globalData.userInfo);
  25. if(app.globalData.userInfo){
  26. that.setData({
  27. hasUserInfo: true,
  28. userInfo: res.userInfo
  29. });
  30. wx.login({
  31. success: function (res) {
  32. // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
  33. // 根据自己的需求有其他操作再补充
  34. // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
  35. wx.request({
  36. url: 'https://applet.yull.top/index/index/login',//获取唯一标识 openId
  37. data: {
  38. code: res.code
  39. },
  40. header: {
  41. 'content-type': 'application/json' // 数据格式(默认值)
  42. },
  43. method: 'post', //上传方式
  44. success: function (res) {
  45. console.log("openid" + res.data.openid);
  46. // wx.setStorageSync('token', res.data.token)//消息模板会用到
  47. wx.setStorageSync('openid', res.data.openid);//存储openid
  48. }
  49. });
  50. }
  51. });
  52. }else{
  53. that.setData({
  54. hasUserInfo: false
  55. });
  56. }
  57. // wx.getSetting({
  58. // success: function (res) {
  59. // if (res.authSetting['scope.userInfo']) {
  60. // wx.getUserInfo({
  61. // success: function (res) {
  62. // // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
  63. // // 根据自己的需求有其他操作再补充
  64. // // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
  65. // console.log(res);
  66. // that.setData({
  67. // hasUserInfo: true,
  68. // userInfo:res.userInfo
  69. // });
  70. // wx.login({
  71. // success: function (res) {
  72. // // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
  73. // // 根据自己的需求有其他操作再补充
  74. // // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
  75. // wx.request({
  76. // url: 'https://applet.yull.top/index/index/login',//获取唯一标识 openId
  77. // data: {
  78. // code: res.code
  79. // },
  80. // header: {
  81. // 'content-type': 'application/json' // 数据格式(默认值)
  82. // },
  83. // method: 'post', //上传方式
  84. // success: function (res) {
  85. // console.log("openid" + res.data.openid);
  86. // // wx.setStorageSync('token', res.data.token)//消息模板会用到
  87. // wx.setStorageSync('openid', res.data.openid);//存储openid
  88. // }
  89. // });
  90. // }
  91. // });
  92. // }
  93. // });
  94. // } else {
  95. // // 用户没有授权
  96. // // 改变 isHide 的值,显示授权页面
  97. // that.setData({
  98. // hasUserInfo: false
  99. // });
  100. // }
  101. // }
  102. // });
  103. // // 如果获取到用户信息就存储
  104. // if (app.globalData.userInfo) {
  105. // console.log('用户信息');
  106. // this.setData({
  107. // userInfo: app.globalData.userInfo,
  108. // hasUserInfo: true
  109. // })
  110. // wx.setStorageSync('username', that.data.userInfo.nickName)
  111. // wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
  112. // console.log("在index页面全局app1中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
  113. // } else if (this.data.canIUse) {
  114. // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  115. // // 所以此处加入 callback 以防止这种情况
  116. // app.userInfoReadyCallback = res => {
  117. // // console.log("用户名2:" + res.userInfo.nickName + " " + res.userInfo.avatarUrl)
  118. // // wx.setStorageSync('username', res.userInfo.nickName)
  119. // // wx.setStorageSync('headpath', res.userInfo.avatarUrl)
  120. // this.setData({
  121. // userInfo: res.userInfo,
  122. // hasUserInfo: true
  123. // })
  124. // wx.setStorageSync('username', that.data.userInfo.nickName)
  125. // wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
  126. // console.log("在index页面全局app2中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
  127. // }
  128. // }
  129. },
  130. //点击按钮授权
  131. getUserInfo: function (e) {
  132. var that = this;
  133. var openid = wx.getStorageSync("openid");
  134. if (e.detail.userInfo) {
  135. if (!openid) {
  136. wx.login({
  137. success: function (res) {
  138. // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
  139. // 根据自己的需求有其他操作再补充
  140. // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
  141. wx.request({
  142. url: 'https://applet.yull.top/index/index/login',//获取唯一标识 openId
  143. data: {
  144. code: res.code
  145. },
  146. header: {
  147. 'content-type': 'application/json' // 数据格式(默认值)
  148. },
  149. method: 'post', //上传方式
  150. success: function (res) {
  151. console.log("openid" + res.data.openid);
  152. // wx.setStorageSync('token', res.data.token)//消息模板会用到
  153. wx.setStorageSync('openid', res.data.openid);//存储openid
  154. }
  155. });
  156. }
  157. });
  158. }
  159. app.globalData.userInfo = e.detail.userInfo
  160. console.log(app.globalData.userInfo);
  161. this.setData({
  162. userInfo: e.detail.userInfo,
  163. hasUserInfo: true
  164. })
  165. wx.setStorageSync('username', that.data.userInfo.nickName)
  166. wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
  167. console.log("在index页面临时授权中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
  168. } else {
  169. wx.showToast({
  170. title: '未授权',
  171. icon: 'none',
  172. })
  173. }
  174. },
  175. addgong: function (e) {
  176. if (this.data.hasUserInfo != true) {
  177. wx.showToast({
  178. title: '请点击获取头像',
  179. icon: 'none',
  180. })
  181. return;
  182. }
  183. wx.navigateTo({
  184. url: '../../pages/addgong/addgong'
  185. })
  186. },
  187. //弹出回复框
  188. showModal: function (event) {
  189. if (this.data.hasUserInfo != true){
  190. wx.showToast({
  191. title: '请点击获取头像',
  192. icon: 'none',
  193. })
  194. return;
  195. }
  196. this.setData({
  197. modalHidden: !this.data.modalHidden,
  198. })
  199. },
  200. // 获取弹出框密码
  201. getInputContent: function (e) {
  202. console.log(e.detail.value)
  203. this.setData({
  204. inputContent: e.detail.value
  205. })
  206. },
  207. //确定
  208. modalBindaconfirm: function () {
  209. var that = this;
  210. console.log(that.data.inputContent)
  211. if (that.data.inputContent == "") {
  212. wx.showToast({
  213. title: '秘钥不能为空!',
  214. icon: 'none',
  215. })
  216. } else {
  217. //通过秘钥换取公众号 id
  218. that.getIdentifyId();
  219. }
  220. },
  221. //取消
  222. modalBindcancel: function () {
  223. this.setData({
  224. modalHidden: !this.data.modalHidden,
  225. })
  226. },
  227. //通过后台秘钥获取公众号 id
  228. getIdentifyId: function () {
  229. var that = this;
  230. wx.request({
  231. url: app.globalData.apiUrl + '/index/index/identify', //获取公众号信息
  232. data: {
  233. password: that.data.inputContent, //登录后台密码
  234. openid: wx.getStorageSync('openid')
  235. },
  236. header: {
  237. 'content-type': 'application/json' // 数据格式(默认值)
  238. },
  239. method: 'post', //上传方式
  240. success: function (res) { //回调成功
  241. console.log(res.data)
  242. if (res.statusCode == 200) {
  243. if (res.data.result == '1') {
  244. //弹出框消失
  245. that.setData({
  246. modalHidden: !that.data.modalHidden,
  247. buttonDisabled: !that.data.buttonDisabled,
  248. id: res.data.content[0].id,
  249. })
  250. //上传openid
  251. // that.postOpenid();
  252. //跳转后台管理界面
  253. wx.navigateTo({
  254. url: '../../pages/select/select?id=' + that.data.id + "&avatarUrl=" + that.data.userInfo.avatarUrl + "&nickName=" + that.data.userInfo.nickName + "&headpath=" + res.data.content[0].headpath + "&name=" + res.data.content[0].name + "&describe=" + res.data.content[0].describes
  255. })
  256. } else {
  257. wx.showToast({
  258. title: '你不是管理员或者密码错误',
  259. icon: 'none',
  260. })
  261. }
  262. } else {
  263. wx.showModal({
  264. title: '服务器错误',
  265. content: 'none',
  266. })
  267. }
  268. },
  269. //回调失败
  270. fail: function (res) {
  271. console.log(res.errMsg)
  272. wx.showToast({
  273. title: '联网失败 ',
  274. icon: 'fail',
  275. })
  276. },
  277. })
  278. },
  279. /**
  280. * 生命周期函数--监听页面初次渲染完成
  281. */
  282. onReady: function () {
  283. },
  284. /**
  285. * 生命周期函数--监听页面显示
  286. */
  287. onShow: function () {
  288. },
  289. /**
  290. * 生命周期函数--监听页面隐藏
  291. */
  292. onHide: function () {
  293. },
  294. /**
  295. * 生命周期函数--监听页面卸载
  296. */
  297. onUnload: function () {
  298. },
  299. /**
  300. * 页面相关事件处理函数--监听用户下拉动作
  301. */
  302. onPullDownRefresh: function () {
  303. },
  304. /**
  305. * 页面上拉触底事件的处理函数
  306. */
  307. onReachBottom: function () {
  308. },
  309. /**
  310. * 用户点击右上角分享
  311. */
  312. onShareAppMessage: function () {
  313. }
  314. })