authorizedLoginDialog.js 12 KB

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