app.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //app.js
  2. App({
  3. data: {
  4. deviceInfo: {}
  5. },
  6. onLaunch: function () {
  7. var that = this;
  8. that.data.deviceInfo = wx.getSystemInfoSync();
  9. //调用API从本地缓存中获取数据
  10. var logs = wx.getStorageSync('logs') || []
  11. logs.unshift(Date.now());
  12. wx.setStorageSync('logs', logs);
  13. wx.clearStorage();
  14. // 查看是否授权【只有处于授权状态,才可以利用 wx.getUserInfo 获取用户信息】
  15. wx.getSetting({
  16. success: function (res) {
  17. console.log(res);
  18. if (res.authSetting['scope.userInfo']) {
  19. //对于授权过小程序的用户,获取用户信息
  20. that.getUserRole();
  21. }
  22. }
  23. })
  24. },
  25. //获取用户角色
  26. getUserRole: function (id) {
  27. var that = this;
  28. var host = that.globalData.servsers;
  29. wx.login({
  30. success: function (res) {
  31. var code = res.code;
  32. wx.getUserInfo({
  33. withCredentials: true,
  34. success: function (res) {
  35. that.globalData.userInfo = res.userInfo;
  36. wx.request({
  37. url: host + "api/distributeSell/getUserRole",
  38. data: {
  39. code: code,
  40. iv: res.iv,
  41. encryptedData: res.encryptedData,
  42. },
  43. method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  44. header: {
  45. 'content-type': 'application/x-www-form-urlencoded'
  46. },
  47. success: function (res) {
  48. //成功
  49. if (res.data.code == '200') {
  50. that.globalData.un_id = res.data.data.user_id;
  51. that.globalData.openid = res.data.data.openid;
  52. that.globalData.isNewUser = false;//不是新用户
  53. that.globalData.userRole = res.data.data.userRole;
  54. that.globalData.userInfo.avatarUrl = res.data.data.icon;
  55. var name = res.data.data.name;
  56. name = name.replace(/\?/g, '');
  57. name = unescape(escape(name).replace(/\%uD.{3}/g, ''));
  58. that.globalData.userInfo.nickName = name;
  59. if (that.employIdCallback) {
  60. that.employIdCallback(res.data.data.user_id);
  61. }
  62. if (that.employIdCallback2) {
  63. that.employIdCallback2(res.data.data.openid);
  64. }
  65. if (that.employIdCallbackUser) {
  66. that.employIdCallbackUser(false);
  67. }
  68. if (that.employIdCallbackRole) {
  69. that.employIdCallbackRole(res.data.data.userRole);
  70. }
  71. }else{
  72. wx.navigateTo({
  73. url: '/pages/authorize/authorize?link=midAutumn',
  74. })
  75. }
  76. if (res.data.data == '') {
  77. that.globalData.isNewUser = true;//是新用户
  78. }
  79. if (res == null || res.data == null) {
  80. console.error('网络请求失败');
  81. return;
  82. }
  83. }
  84. });
  85. },
  86. fail: function (e) {
  87. console.log("需重新授权");
  88. }
  89. })
  90. }
  91. });
  92. },
  93. //插入用户信息
  94. insertUser:function(e){
  95. var that = this;
  96. var host = that.globalData.servsers;
  97. wx.login({
  98. success: function (res) {
  99. var code = res.code;
  100. wx.getUserInfo({
  101. withCredentials: true,
  102. success: function (res) {
  103. that.globalData.userInfo = res.userInfo;
  104. }
  105. })
  106. }
  107. })
  108. },
  109. //设置全局变量
  110. globalData: {
  111. userInfo: [],
  112. un_id:null,
  113. open_id: null,
  114. openid : null,
  115. userRole : '',
  116. isNewUser : '',
  117. fromUserId : '',//分享者的id
  118. fromUserRole : '',//分享者的role
  119. //servsers: "https://52yqf.cn/",
  120. servsers: "https://www.daliangzao.net/",//正式库
  121. //servsers: "https://tt.daliangzao.net/",//测试库
  122. appid: 'wxf9a0ddcb8c70b939',//appid
  123. secret: 'a009cb3e46c8ecef9c54a50522f11823',//secret
  124. }
  125. })