empower.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // index.js
  2. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  3. const homeApi_empower = "https://aipush.aidsleep.cn";
  4. const token_empower = "b74fd5754c5ef24cf600c39194abdaeb";
  5. Page({
  6. data: {
  7. motto: 'Hello World',
  8. defaultAvatarUrlD: defaultAvatarUrl,
  9. userInfo: {
  10. avatarUrl: defaultAvatarUrl,
  11. nickName: '',
  12. },
  13. phoneNumber: '',//手机号
  14. hasUserInfo: false,
  15. canIUseGetUserProfile: wx.canIUse('getUserProfile'),
  16. canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  17. intervalId: null,
  18. loading: false ,// 标记是否正在加载
  19. isLoading: false,
  20. disableButtom: false,
  21. unionid: '',
  22. openid: '',
  23. session_key: '',
  24. encryptphone: '',//加密的手机号
  25. },
  26. bindViewTap() {
  27. // wx.navigateTo({
  28. // url: '../logs/logs'
  29. // })
  30. },
  31. onChooseAvatar(e) {
  32. const { avatarUrl } = e.detail
  33. const { nickName } = this.data.userInfo
  34. var that = this
  35. this.setData({
  36. "userInfo.avatarUrl": avatarUrl,
  37. hasUserInfo: nickName && this.data.phoneNumber && avatarUrl && avatarUrl !== defaultAvatarUrl,
  38. })
  39. wx.setStorageSync('userInfo_em', this.data.userInfo);
  40. wx.setStorageSync('phoneNumber', this.data.phoneNumber);
  41. },
  42. onInputChange(e) {
  43. console.log('e=='+JSON.stringify(e));
  44. console.log('e.detail=='+JSON.stringify(e.detail));
  45. const nickName = e.detail.value
  46. const { avatarUrl } = this.data.userInfo
  47. this.setData({
  48. "userInfo.nickName": nickName,
  49. hasUserInfo: nickName && this.data.phoneNumber && avatarUrl && avatarUrl !== defaultAvatarUrl,
  50. })
  51. wx.setStorageSync('userInfo_em', this.data.userInfo);
  52. wx.setStorageSync('phoneNumber', this.data.phoneNumber);
  53. },
  54. getUserProfile(e) {
  55. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  56. wx.getUserProfile({
  57. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  58. success: (res) => {
  59. console.log(res)
  60. this.setData({
  61. userInfo: res.userInfo,
  62. hasUserInfo: true
  63. })
  64. }
  65. })
  66. },
  67. onLoad: function(options) {
  68. const userInfo_em = wx.getStorageSync('userInfo_em');
  69. const phoneNumber = wx.getStorageSync('phoneNumber');
  70. console.log('userInfo_em====='+JSON.stringify(userInfo_em));
  71. if(userInfo_em && phoneNumber){
  72. const { avatarUrl,nickName } = userInfo_em;
  73. this.setData({
  74. userInfo: userInfo_em,
  75. phoneNumber: phoneNumber,
  76. hasUserInfo: nickName && phoneNumber && avatarUrl && avatarUrl !== defaultAvatarUrl,
  77. disableButtom: wx.getStorageSync('userInfo_em') && wx.getStorageSync('unionid') && wx.getStorageSync('phoneNumber') && wx.getStorageSync('hasAuth')
  78. })
  79. }
  80. const unionid = wx.getStorageSync('unionid');
  81. const hasAuth = wx.getStorageSync('hasAuth');
  82. if(userInfo_em && unionid && hasAuth){
  83. this.setData({
  84. disableButtom: true,
  85. });
  86. }else{
  87. this.setData({
  88. disableButtom: false,
  89. });
  90. // setTimeout(function() {
  91. // wx.showModal({
  92. // title: '提示',
  93. // content: '请进行授权',
  94. // showCancel: false
  95. // });
  96. // }, 2000) // 设置延时时间,单位为毫秒
  97. // wx.showToast({
  98. // title: '请进行授权',
  99. // icon: 'info',
  100. // duration: 3000
  101. // });
  102. // wx.showModal({
  103. // title: '提示',
  104. // content: '请进行授权',
  105. // showCancel: false,
  106. // duration: 3000
  107. // });
  108. }
  109. },
  110. handleUserInfo: function(e) {
  111. if(!this.data.hasUserInfo){
  112. // setTimeout(function() {
  113. wx.showToast({
  114. title: '请设置头像、昵称和手机号',
  115. icon: 'none'
  116. });
  117. // }, 2000) // 设置延时时间,单位为毫秒
  118. return;
  119. }
  120. var that = this;
  121. if (e.detail.userInfo) {
  122. // 用户点击允许,获取到用户信息
  123. this.setData({
  124. // userInfo: e.detail.userInfo,
  125. // loading: true,
  126. disableButtom: true,
  127. isLoading: true,
  128. });
  129. wx.login({
  130. success: res => {
  131. if (res.code) {
  132. // 发送 code 到服务器换取 session_key, openid
  133. console.log('res.code=='+res.code);
  134. wx.request({
  135. url: `${homeApi_empower}/wxlogin`,
  136. data: {
  137. code: res.code,
  138. token: token_empower,
  139. nickname: that.data.userInfo.nickName,
  140. encryptphone: that.data.encryptphone
  141. },
  142. method: 'POST',
  143. success: function(loginRes) {
  144. console.log('loginRes='+JSON.stringify(loginRes));
  145. console.log('loginRes.data='+JSON.stringify(loginRes.data));
  146. if (loginRes.data.codes && loginRes.data.codes == 'success') {
  147. console.log('123123123');
  148. console.log('loginRes.data.back.unionid='+loginRes.data.back.unionid);
  149. console.log('loginRes.data.back.openid='+loginRes.data.back.openid);
  150. wx.setStorageSync('unionid', loginRes.data.back.unionid);
  151. wx.setStorageSync('openid', loginRes.data.back.openid);
  152. // 获取后台授权
  153. that.startInterval();
  154. // 可以在这里提示用户进行手机号授权
  155. that.setData({
  156. canGetPhoneNumber: true,
  157. isLoading: true,
  158. unionid: loginRes.data.back.unionid,
  159. openid: loginRes.data.back.openid,
  160. session_key: loginRes.data.back.session_key,
  161. });
  162. }
  163. }
  164. });
  165. }
  166. }
  167. });
  168. // 假设有异步操作(如发送数据到服务器),则放在这里
  169. } else {
  170. // 用户拒绝授权
  171. wx.showToast({
  172. title: '您拒绝了授权',
  173. icon: 'none'
  174. });
  175. // 可以选择在这里处理用户拒绝授权后的逻辑,如跳转到其他页面或显示提示信息
  176. }
  177. },
  178. onGetPhoneNumber: function(e) {
  179. console.log('onGetPhoneNumber='+JSON.stringify(e));
  180. var that = this;
  181. if (e.detail.errMsg === "getPhoneNumber:ok") {
  182. // 发送 encryptedData, iv, 和 sessionKey 到你的后台服务器
  183. wx.login({
  184. success: function (res) {
  185. if (res.code) {
  186. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  187. wx.request({
  188. url: `${homeApi_empower}/wxtel`,
  189. method: 'POST',
  190. data: {
  191. encryptedData: e.detail.encryptedData,
  192. iv: e.detail.iv,
  193. code: res.code,
  194. token: token_empower,
  195. },
  196. success: function(res) {
  197. // 处理服务器返回的结果
  198. console.log('手机号获取结果:', res.data);
  199. console.log('手机号获取结果:', res.data.encryptphone);
  200. console.log('手机号获取结果:', res.data.shieldphone);
  201. if(!res.data || !res.data.encryptphone || !res.data.shieldphone){
  202. // setTimeout(function() {
  203. wx.showToast({
  204. title: '无法获取用户手机号',
  205. icon: 'none'
  206. });
  207. // }, 2000) // 设置延时时间,单位为毫秒
  208. return;
  209. }
  210. console.log('e=='+JSON.stringify(e));
  211. console.log('e.detail=='+JSON.stringify(e.detail));
  212. const { avatarUrl,nickName } = that.data.userInfo
  213. that.setData({
  214. encryptphone: res.data.encryptphone,
  215. phoneNumber: res.data.shieldphone,
  216. hasUserInfo: nickName && res.data.shieldphone && avatarUrl && avatarUrl !== defaultAvatarUrl,
  217. })
  218. wx.setStorageSync('userInfo_em', that.data.userInfo);
  219. wx.setStorageSync('phoneNumber',res.data.shieldphone);
  220. }
  221. })
  222. }
  223. }
  224. })
  225. }
  226. },
  227. startInterval: function () {
  228. var that = this;
  229. this.data.intervalId = setInterval(() => {
  230. console.log('后台授权中...');
  231. console.log('startInterval-unionid='+that.data.unionid);
  232. console.log('startInterval-openid='+that.data.openid);
  233. wx.request({
  234. url: `${homeApi_empower}/wxstatus`,
  235. data: {
  236. //nickName: that.data.userInfo.nickName,
  237. unionid: wx.getStorageSync('unionid'),
  238. openid: wx.getStorageSync('openid'),
  239. //phoneNumber: that.data.phoneNumber,
  240. token: token_empower,
  241. },
  242. method: 'POST',
  243. success: function(intervalRes) {
  244. console.log('intervalRes='+JSON.stringify(intervalRes));
  245. console.log('intervalRes.data='+JSON.stringify(intervalRes.data));
  246. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  247. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1' ) {
  248. wx.setStorageSync('hasAuth', 'hasAuth');
  249. //这里后边改成接口返回值
  250. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  251. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  252. // 可以在这里提示用户进行手机号授权
  253. that.setData({
  254. canGetPhoneNumber: true,
  255. isLoading: false,
  256. disableButtom: false,
  257. });
  258. wx.showToast({
  259. title: '后台授权成功',
  260. icon: 'none'
  261. });
  262. that.clearInterval();
  263. setTimeout(function() {
  264. // if(!wx.getStorageSync('res') || !wx.getStorageSync('res').result){
  265. // 跳转到scan页面
  266. wx.reLaunch({
  267. url: '/pages/scan/scan',
  268. })
  269. // }else{
  270. // // 跳转到上一页
  271. // wx.navigateBack({
  272. // delta: 1 // 返回上级页面
  273. // });
  274. // }
  275. }, 2000) // 设置延时时间,单位为毫秒
  276. }else{
  277. that.clearInterval();
  278. wx.showToast({
  279. title: '授权失败,请先办理入住',
  280. icon: 'none'
  281. });
  282. }
  283. },
  284. fail: function (error) {
  285. that.clearInterval();
  286. wx.showToast({
  287. title: '后台授权失败',
  288. icon: 'none'
  289. });
  290. }
  291. });
  292. }, 3000);
  293. },
  294. clearInterval: function () {
  295. var that = this;
  296. if (this.data.intervalId) {
  297. clearInterval(that.data.intervalId);
  298. that.data.intervalId = null;
  299. }
  300. },
  301. onHide() {
  302. // this.clearInterval();
  303. },
  304. onUnload() {
  305. this.clearInterval();
  306. },
  307. })