authorizedLoginDialog.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. } else {
  137. wx.showToast({ title: data.message || '头像上传失败', icon: 'none' });
  138. }
  139. },
  140. fail: () => {
  141. wx.hideLoading();
  142. wx.showToast({ title: '头像上传失败', icon: 'none' });
  143. }
  144. });
  145. },
  146. generateRandomString(length) {
  147. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  148. let result = '';
  149. for (let i = 0; i < length; i++) {
  150. result += characters.charAt(Math.floor(Math.random() * characters.length));
  151. }
  152. return result;
  153. },
  154. // 输入昵称
  155. onInputNameChange(e) {
  156. console.log('e.detail==' + JSON.stringify(e.detail));
  157. const nickName = e.detail.value
  158. this.setData({
  159. nickName: nickName,
  160. })
  161. wx.setStorageSync('nickName', nickName);
  162. this.checkHasUserInfo();
  163. },
  164. // 输入手机号
  165. onInputPhoneChange(e) {
  166. console.log('e.detail==' + JSON.stringify(e.detail));
  167. const phoneNumber = e.detail.value
  168. this.setData({
  169. phoneNumber: phoneNumber,
  170. })
  171. this.checkHasUserInfo();
  172. },
  173. // 获取手机号
  174. onGetPhoneNumber: function (e) {
  175. console.log('onGetPhoneNumber=' + JSON.stringify(e));
  176. var that = this;
  177. if (e.detail.errMsg === "getPhoneNumber:ok") {
  178. // 发送 encryptedData, iv, 和 sessionKey 到你的后台服务器
  179. wx.request({
  180. url: `${homeApi_empower}/wxtel`,
  181. method: 'POST',
  182. data: {
  183. encryptedData: e.detail.encryptedData,
  184. iv: e.detail.iv,
  185. token: wx.getStorageSync("token") || token_empower,
  186. },
  187. success: function (res) {
  188. // 处理服务器返回的结果
  189. console.log('手机号获取结果:', res.data);
  190. console.log('手机号获取结果:', res.data.encryptphone);
  191. console.log('手机号获取结果:', res.data.shieldphone);
  192. if (!res.data || !res.data.encryptphone || !res.data.shieldphone) {
  193. wx.showToast({
  194. title: '无法获取用户手机号',
  195. icon: 'none'
  196. });
  197. return;
  198. }
  199. console.log('e==' + JSON.stringify(e));
  200. console.log('e.detail==' + JSON.stringify(e.detail));
  201. that.setData({
  202. encryptphone: res.data.encryptphone,
  203. phoneNumber: res.data.shieldphone,
  204. })
  205. that.checkHasUserInfo();
  206. wx.setStorageSync('phoneNumber', res.data.shieldphone);
  207. }
  208. })
  209. }
  210. },
  211. checkHasUserInfo() {
  212. var disableCommitBtn = this.data.avatarUrl == defaultAvatarUrl || !this.data.phoneNumber || !this.data.nickName;
  213. this.setData({
  214. hasUserInfo: this.data.avatarUrl != defaultAvatarUrl && this.data.phoneNumber && this.data.nickName,
  215. disableCommitBtn: disableCommitBtn
  216. })
  217. },
  218. // 提交
  219. handleUserInfo: function (e) {
  220. if (!this.data.hasUserInfo) {
  221. wx.showToast({
  222. title: '请设置头像、昵称和手机号',
  223. icon: 'none'
  224. });
  225. return;
  226. }
  227. var that = this;
  228. if (e.detail.userInfo) {
  229. // 用户点击允许,获取到用户信息
  230. this.setData({
  231. showLoading: true,
  232. disableCommitBtn: true,
  233. });
  234. wx.request({
  235. url: `${homeApi_empower}/wxlogin`,
  236. data: {
  237. token: wx.getStorageSync("token") || token_empower,
  238. unionid: wx.getStorageSync("unionid"),
  239. openid: wx.getStorageSync("openid"),
  240. avatarUrl: that.data.avatarUrl,
  241. nickname: that.data.nickName,
  242. encryptphone: that.data.encryptphone
  243. },
  244. method: 'POST',
  245. success: function (loginRes) {
  246. console.log('loginRes=' + JSON.stringify(loginRes));
  247. console.log('loginRes.data=' + JSON.stringify(loginRes.data));
  248. if (loginRes.data.codes && loginRes.data.codes == 'success') {
  249. console.log('123123123');
  250. console.log('loginRes.data.back.unionid=' + loginRes.data.back.unionid);
  251. console.log('loginRes.data.back.openid=' + loginRes.data.back.openid);
  252. wx.setStorageSync('unionid', loginRes.data.back.unionid);
  253. wx.setStorageSync('openid', loginRes.data.back.openid);
  254. // 获取后台授权
  255. that.startInterval();
  256. // 可以在这里提示用户进行手机号授权
  257. that.setData({
  258. unionid: loginRes.data.back.unionid,
  259. openid: loginRes.data.back.openid,
  260. });
  261. } else {
  262. that.setData({ showLoading: false });
  263. wx.showToast({
  264. title: '后台授权失败',
  265. icon: 'none'
  266. });
  267. }
  268. },
  269. fail: function () {
  270. that.setData({ showLoading: false });
  271. wx.showToast({
  272. title: '后台授权失败',
  273. icon: 'none'
  274. });
  275. }
  276. });
  277. } else {
  278. // 用户拒绝授权
  279. wx.showToast({
  280. title: '您拒绝了授权',
  281. icon: 'none'
  282. });
  283. // 可以选择在这里处理用户拒绝授权后的逻辑,如跳转到其他页面或显示提示信息
  284. }
  285. },
  286. // 轮询后台信息
  287. startInterval: function () {
  288. var that = this;
  289. this.data.intervalId = setInterval(() => {
  290. console.log('后台授权中...');
  291. wx.request({
  292. url: `${homeApi_empower}/wxstatus`,
  293. data: {
  294. unionid: wx.getStorageSync('unionid'),
  295. openid: wx.getStorageSync('openid'),
  296. token: wx.getStorageSync('token') || token_empower,
  297. },
  298. method: 'POST',
  299. success: function (intervalRes) {
  300. that.setData({ showLoading: false });
  301. console.log('intervalRes=' + JSON.stringify(intervalRes));
  302. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  303. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  304. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  305. wx.setStorageSync('hasAuth', true);
  306. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  307. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  308. wx.setStorageSync('userInfo', {
  309. hotel: intervalRes.data.hotel,
  310. room: intervalRes.data.room,
  311. unionid: that.data.unionid,
  312. openid: that.data.openid,
  313. session_key: that.data.session_key,
  314. nickName: that.data.nickName,
  315. avatarUrl: that.data.avatarUrl,
  316. encryptphone: that.data.encryptphone,
  317. phoneNumber: that.data.phoneNumber
  318. })
  319. that.setData({
  320. disableCommitBtn: false,
  321. });
  322. wx.showToast({
  323. title: '后台授权成功',
  324. icon: 'none'
  325. });
  326. that.clearInterval();
  327. that.triggerEvent('authorizationSuccessful', true);
  328. } else {
  329. that.clearInterval();
  330. wx.showToast({
  331. title: '授权失败,请先办理入住',
  332. icon: 'none'
  333. });
  334. }
  335. },
  336. fail: function (error) {
  337. that.setData({ showLoading: false });
  338. that.clearInterval();
  339. wx.showToast({
  340. title: '后台授权失败',
  341. icon: 'none'
  342. });
  343. }
  344. });
  345. }, 3000);
  346. },
  347. clearInterval: function () {
  348. var that = this;
  349. if (this.data.intervalId) {
  350. clearInterval(that.data.intervalId);
  351. that.data.intervalId = null;
  352. }
  353. },
  354. }
  355. })