authorizedLoginDialog.js 11 KB

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