authorizedLoginDialog.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // componets/authorized-login-dialog/authorizedLoginDialog.js
  2. const defaultAvatarUrl = "../../static/images/no-avatar.png"
  3. const homeApi_empower = "https://aipush.aidsleep.cn";
  4. const token_empower = "b74fd5754c5ef24cf600c39194abdaeb";
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. showDialog: {
  11. type: Boolean,
  12. value: false,
  13. observer(newVal, oldVal, changePath) {
  14. console.log(newVal, oldVal, changePath)
  15. const that = this;
  16. if (newVal) {
  17. // console.log("开始登录");
  18. // wx.login({
  19. // success: res => {
  20. // if (res.code) {
  21. // // 发送 code 到服务器换取 session_key, openid
  22. // console.log('res.code==' + res.code);
  23. // that.setData({ showLoading: false, wxCode: res.code })
  24. // that.checkHasUserInfo()
  25. // }
  26. // }
  27. // });
  28. that.setData({
  29. avatarUrl: defaultAvatarUrl,
  30. nickName: "",
  31. phoneNumber: "",
  32. disableCommitBtn: true,
  33. showLoading: false,
  34. hasUserInfo: false,
  35. })
  36. } else {
  37. console.log('that.data.wxCode==' + that.data.wxCode);
  38. }
  39. }
  40. }
  41. },
  42. attached() {
  43. },
  44. /**
  45. * 组件的初始数据
  46. */
  47. data: {
  48. intervalId: null,
  49. canIUseGetUserProfile: wx.canIUse('getUserProfile'),
  50. canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  51. avatarUrl: defaultAvatarUrl,
  52. tempAvatarUrl: '',
  53. showLoading: false,
  54. encryptphone: "",
  55. phoneNumber: "",
  56. nickName: "",
  57. disableCommitBtn: true,
  58. hasUserInfo: false,
  59. wxCode: "",
  60. unionid: "",
  61. openid: "",
  62. session_key: "",
  63. },
  64. /**
  65. * 组件的方法列表
  66. */
  67. methods: {
  68. // 关闭
  69. onCloseDialog() {
  70. this.setData({ showDialog: false });
  71. },
  72. // 选择头像 后期需要上传
  73. onChooseAvatar(e) {
  74. const { avatarUrl } = e.detail
  75. // this.setData({
  76. // avatarUrl: avatarUrl,
  77. // })
  78. // wx.showToast({ title: "进来了" })
  79. const that = this;
  80. this.setData({ tempAvatarUrl: avatarUrl });
  81. if (avatarUrl.includes("wxfile://")) {
  82. that.uploadAvatar(avatarUrl);
  83. } else {
  84. // 下载头像图片
  85. wx.downloadFile({
  86. url: avatarUrl,
  87. success(res) {
  88. if (res.statusCode === 200) {
  89. console.log('download success');
  90. const tempFilePath = res.tempFilePath
  91. that.uploadAvatar(tempFilePath);
  92. } else {
  93. wx.showToast({ title: '上传失败' })
  94. // wx.showToast({ title: JSON.stringify(res) })
  95. // wx.showToast({ title: JSON.stringify(avatarUrl) })
  96. }
  97. }, fail: function (err) {
  98. wx.showToast({ title: '上传失败' })
  99. // wx.showToast({ title: JSON.stringify(err) })
  100. // wx.showToast({ title: JSON.stringify(avatarUrl) })
  101. }
  102. });
  103. }
  104. this.checkHasUserInfo();
  105. },
  106. uploadAvatar(tempFilePath) {
  107. wx.showLoading({ title: '上传中...' });
  108. const randomString = this.generateRandomString(16);
  109. const base64String = wx.arrayBufferToBase64(new Uint8Array([...randomString].map(c => c.charCodeAt(0))));
  110. wx.request({
  111. url: 'https://aipush.aidsleep.cn/wxuptokens',
  112. method: 'POST',
  113. data: {
  114. rk: base64String
  115. },
  116. success: (res) => {
  117. if (res.data.success) {
  118. this.doUpload(tempFilePath, res.data.token);
  119. } else {
  120. wx.hideLoading();
  121. wx.showToast({ title: '获取上传凭证失败', icon: 'none' });
  122. }
  123. },
  124. fail: () => {
  125. wx.hideLoading();
  126. wx.showToast({ title: '获取上传凭证失败', icon: 'none' });
  127. }
  128. });
  129. },
  130. doUpload(tempFilePath, token) {
  131. wx.uploadFile({
  132. url: 'https://aipush.aidsleep.cn/wxups',
  133. filePath: tempFilePath,
  134. name: 'file',
  135. formData: {
  136. 'token': token
  137. },
  138. success: (res) => {
  139. wx.hideLoading();
  140. const data = JSON.parse(res.data);
  141. if (data.success) {
  142. this.setData({ avatarUrl: data.url });
  143. wx.showToast({ title: '头像上传成功', icon: 'success' });
  144. this.checkHasUserInfo();
  145. } else {
  146. wx.showToast({ title: data.message || '头像上传失败', icon: 'none' });
  147. }
  148. },
  149. fail: () => {
  150. wx.hideLoading();
  151. wx.showToast({ title: '头像上传失败', icon: 'none' });
  152. }
  153. });
  154. },
  155. generateRandomString(length) {
  156. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  157. let result = '';
  158. for (let i = 0; i < length; i++) {
  159. result += characters.charAt(Math.floor(Math.random() * characters.length));
  160. }
  161. return result;
  162. },
  163. // 输入昵称
  164. onInputNameChange(e) {
  165. console.log('e.detail==' + JSON.stringify(e.detail));
  166. const nickName = e.detail.value
  167. this.setData({
  168. nickName: nickName,
  169. })
  170. wx.setStorageSync('nickName', nickName);
  171. this.checkHasUserInfo();
  172. },
  173. // 输入手机号
  174. onInputPhoneChange(e) {
  175. console.log('e.detail==' + JSON.stringify(e.detail));
  176. const phoneNumber = e.detail.value
  177. this.setData({
  178. phoneNumber: phoneNumber,
  179. })
  180. this.checkHasUserInfo();
  181. },
  182. // 获取手机号
  183. onGetPhoneNumber: function (e) {
  184. console.log('onGetPhoneNumber=' + JSON.stringify(e));
  185. var that = this;
  186. if (e.detail.errMsg === "getPhoneNumber:ok") {
  187. // 发送 encryptedData, iv, 和 sessionKey 到你的后台服务器
  188. wx.request({
  189. url: `${homeApi_empower}/wxtel`,
  190. method: 'POST',
  191. data: {
  192. encryptedData: e.detail.encryptedData,
  193. iv: e.detail.iv,
  194. token: wx.getStorageSync("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. wx.showToast({
  203. title: '无法获取用户手机号',
  204. icon: 'none'
  205. });
  206. return;
  207. }
  208. console.log('e==' + JSON.stringify(e));
  209. console.log('e.detail==' + JSON.stringify(e.detail));
  210. that.setData({
  211. encryptphone: res.data.encryptphone,
  212. phoneNumber: res.data.shieldphone,
  213. })
  214. that.checkHasUserInfo();
  215. wx.setStorageSync('phoneNumber', res.data.shieldphone);
  216. }
  217. })
  218. }
  219. },
  220. checkHasUserInfo() {
  221. var disableCommitBtn = this.data.avatarUrl == defaultAvatarUrl || !this.data.phoneNumber || !this.data.nickName;
  222. this.setData({
  223. hasUserInfo: this.data.avatarUrl != defaultAvatarUrl && this.data.phoneNumber && this.data.nickName,
  224. disableCommitBtn: disableCommitBtn
  225. })
  226. },
  227. // 提交
  228. handleUserInfo: function (e) {
  229. if (!this.data.hasUserInfo) {
  230. wx.showToast({
  231. title: '请设置头像、昵称和手机号',
  232. icon: 'none'
  233. });
  234. return;
  235. }
  236. var that = this;
  237. if (e.detail.userInfo) {
  238. // 用户点击允许,获取到用户信息
  239. this.setData({
  240. showLoading: true,
  241. disableCommitBtn: true,
  242. });
  243. wx.request({
  244. url: `${homeApi_empower}/wxlogin`,
  245. data: {
  246. token: wx.getStorageSync("token") || token_empower,
  247. unionid: wx.getStorageSync("unionid"),
  248. openid: wx.getStorageSync("openid"),
  249. avatarUrl: that.data.avatarUrl,
  250. nickname: that.data.nickName,
  251. encryptphone: that.data.encryptphone
  252. },
  253. method: 'POST',
  254. success: function (loginRes) {
  255. console.log('loginRes=' + JSON.stringify(loginRes));
  256. console.log('loginRes.data=' + JSON.stringify(loginRes.data));
  257. if (loginRes.data.codes && loginRes.data.codes == 'success') {
  258. that.setData({ showLoading: false });
  259. console.log('123123123');
  260. console.log('loginRes.data.back.unionid=' + loginRes.data.back.unionid);
  261. console.log('loginRes.data.back.openid=' + loginRes.data.back.openid);
  262. wx.setStorageSync('unionid', loginRes.data.back.unionid);
  263. wx.setStorageSync('openid', loginRes.data.back.openid);
  264. that.setData({
  265. disableCommitBtn: false,
  266. unionid: loginRes.data.back.unionid,
  267. openid: loginRes.data.back.openid,
  268. });
  269. wx.setStorageSync('userInfo', {
  270. unionid: that.data.unionid,
  271. openid: that.data.openid,
  272. session_key: that.data.session_key,
  273. nickName: that.data.nickName,
  274. avatarUrl: that.data.avatarUrl,
  275. encryptphone: that.data.encryptphone,
  276. phoneNumber: that.data.phoneNumber
  277. })
  278. wx.setStorageSync('hasAuth', true);
  279. that.triggerEvent('authorizationSuccessful', true);
  280. // 获取后台授权
  281. that.startInterval();
  282. } else {
  283. that.setData({ showLoading: false });
  284. wx.showToast({
  285. title: '后台授权失败',
  286. icon: 'none'
  287. });
  288. }
  289. },
  290. fail: function () {
  291. that.setData({ showLoading: false });
  292. wx.showToast({
  293. title: '后台授权失败',
  294. icon: 'none'
  295. });
  296. }
  297. });
  298. } else {
  299. // 用户拒绝授权
  300. wx.showToast({
  301. title: '您拒绝了授权',
  302. icon: 'none'
  303. });
  304. // 可以选择在这里处理用户拒绝授权后的逻辑,如跳转到其他页面或显示提示信息
  305. }
  306. },
  307. // 轮询后台信息 暂时废弃
  308. startInterval: function () {
  309. var that = this;
  310. this.data.intervalId = setInterval(() => {
  311. console.log('后台授权中...');
  312. wx.request({
  313. url: `${homeApi_empower}/wxstatus`,
  314. data: {
  315. unionid: wx.getStorageSync('unionid'),
  316. openid: wx.getStorageSync('openid'),
  317. token: wx.getStorageSync('token') || token_empower,
  318. },
  319. method: 'POST',
  320. success: function (intervalRes) {
  321. that.setData({ showLoading: false });
  322. that.clearInterval();
  323. console.log('intervalRes=' + JSON.stringify(intervalRes));
  324. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  325. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  326. wx.setStorageSync('hasHotelAuth', true);
  327. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  328. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  329. } else {
  330. wx.setStorageSync('hasHotelAuth', false);
  331. }
  332. },
  333. fail: function (error) {
  334. that.setData({ showLoading: false });
  335. that.clearInterval();
  336. }
  337. });
  338. }, 3000);
  339. },
  340. clearInterval: function () {
  341. var that = this;
  342. if (this.data.intervalId) {
  343. clearInterval(that.data.intervalId);
  344. that.data.intervalId = null;
  345. }
  346. },
  347. }
  348. })