readAppInfo.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import {
  2. selectMerchant,
  3. queryStoreBranch,
  4. getImUser
  5. } from '@/commit/api.js'
  6. import {
  7. emConnect,
  8. } from '@/EaseIM/emApis';
  9. import loginStore from '@/store/modules/login.js';
  10. const {
  11. loginWithPassword
  12. } = emConnect();
  13. const setEMUserLoginInfosToStorage = (userId, token) => {
  14. const params = {
  15. key: `EM_LOGIN_INFOS`,
  16. data: {
  17. userId: userId,
  18. token: token
  19. },
  20. };
  21. uni.setStorage({
  22. ...params
  23. });
  24. }
  25. const loginWithUserId = async () => {
  26. let loginEaseIMId = uni.getStorageSync('im_userid')
  27. try {
  28. const res = await loginWithPassword(
  29. loginEaseIMId,
  30. uni.getStorageSync('im_pass')
  31. );
  32. loginStore.mutations['SET_LOGIN_USER_BASE_INFOS']({
  33. loginUserId: loginEaseIMId,
  34. });
  35. setEMUserLoginInfosToStorage(
  36. loginEaseIMId.toLowerCase(),
  37. res.accessToken
  38. //注意:res.accessToken
  39. );
  40. } catch (error) {
  41. console.log('>>>>>>', error);
  42. uni.showToast({
  43. title: '登录失败',
  44. icon: 'none',
  45. });
  46. } finally {
  47. loginEaseIMId = '';
  48. }
  49. }
  50. const llwFunc = async () => {
  51. //lww
  52. let storeStaff = uni.getStorageSync('storeStaff')
  53. if (storeStaff != null) {
  54. const res = await queryStoreBranch({
  55. id: storeStaff.department
  56. })
  57. if (res.data.status == 200) {
  58. uni.setStorageSync('jurisdiction', paramsData.jurisdiction);
  59. }
  60. } else {
  61. uni.setStorageSync('jurisdiction', [7, 9, 5, 3]);
  62. }
  63. // TODO 需要明确含义,目前逻辑是undifined
  64. let type;
  65. if (type == 1) {
  66. console.log('返回上一页')
  67. uni.navigateBack({
  68. delta: 1
  69. })
  70. } else {
  71. uni.switchTab({
  72. url: '../pages/cashier/index'
  73. });
  74. }
  75. }
  76. const readQuery = () => {
  77. const hash = window.location.hash; // 例如:#/?param1=value1&param2=value2
  78. // 检查 hash 是否为空
  79. if (hash) {
  80. // 创建一个 URLSearchParams 实例
  81. const queryString = hash.split('?')[1]; // 获取 '?' 后面的部分
  82. const params = new URLSearchParams(queryString);
  83. return JSON.parse(decodeURIComponent(params.get('data')))
  84. }
  85. }
  86. const func = async () => {
  87. try {
  88. const paramsData = readQuery()
  89. if(!paramsData) {
  90. return;
  91. }
  92. console.log('%c [ 1. 读取url登录信息url参数 ]-4', 'font-size:13px; background:pink; color:#bf2c9f;', paramsData)
  93. const paramsData1 = {
  94. "storeStaff": null,
  95. "x_token": "MTYzMzYxODYzMjY4MTA0NTIwMjUtMDEtMDYgYXQgMjI6MDA6MDkgQ1NU",
  96. "user": {
  97. "uid": 16336,
  98. "wechat_user_id": "0",
  99. "ali_user_id": "0",
  100. "account": "18632681045",
  101. "real_name": "",
  102. "label_id": null,
  103. "group_id": "11",
  104. "nickname": "用户1045",
  105. "avatar": "https://app.bjtdba.com/uploads/logo.jpg",
  106. "phone": "18632681045",
  107. "now_score": null,
  108. "score": 0,
  109. "now_money": 0,
  110. "brokerage_price": 0,
  111. "admin_agent_rate": null,
  112. "locktime": null,
  113. "user_number": null,
  114. "im": 0,
  115. "im_uuid": null,
  116. "last_ip": "20.100.5.11",
  117. "identity": 5,
  118. "spreadUid": null,
  119. "user_type": "MP",
  120. "pwd": "$2y$10$XARVMD82YRAl9NMwzLcrKeSozdzlnUj7IXp2dvPGYGobzYFlf0j5K",
  121. "mer_id": 0,
  122. "spread_count": 0,
  123. "first_sign": 0
  124. }
  125. }
  126. uni.setStorageSync('userinfo', paramsData.user);
  127. uni.setStorageSync('authorization', paramsData.x_token);
  128. uni.setStorageSync('storeStaff', paramsData.storeStaff);
  129. // 用户状态判断
  130. const userInfoData = await selectMerchant({
  131. uid: paramsData.user.uid
  132. });
  133. console.log('%c [ 2. 获取用户信息 ]-4', 'font-size:13px; background:pink; color:#bf2c9f;', userInfoData)
  134. if(userInfoData.data.status === 200) {
  135. // 新逻辑
  136. uni.setStorageSync('selectmerchant', userInfoData.data.data);
  137. let storeStaff = uni.getStorageSync('storeStaff')
  138. console.log('%c [ storeStaff ]-4', 'font-size:13px; background:pink; color:#bf2c9f;', storeStaff, storeStaff != null)
  139. console.log('%c [ 本地存储用户信息 userInfo ]-4', 'font-size:13px; background:pink; color:#bf2c9f;', uni.getStorageSync('userinfo'))
  140. localStorage.setItem('aaa', '33333')
  141. console.log('读原生的', localStorage.getItem('aaa'))
  142. if (storeStaff) {
  143. queryStoreBranch({
  144. id: storeStaff.department
  145. }).then(res => {
  146. if (res.data.status == 200) {
  147. uni.setStorageSync('jurisdiction', res.data.data.jurisdiction);
  148. }
  149. })
  150. } else {
  151. uni.setStorageSync('jurisdiction', [7, 9, 5, 3]);
  152. }
  153. // uni.showToast({
  154. // title: res.data.message,
  155. // icon: "none"
  156. // })
  157. setTimeout(function() {
  158. console.log('%c [ 3. 获取用户信息成功,跳转页面 ]-4', 'font-size:13px; background:pink; color:#bf2c9f;')
  159. uni.switchTab({
  160. url: '../../cashier/index'
  161. });
  162. }, 700);
  163. }
  164. return;
  165. // 之前的逻辑
  166. uni.setStorageSync('selectmerchant', data.data.data);
  167. const {
  168. merchantIntention = {},
  169. } = userInfoData.data.data || {};
  170. uni.setStorage({
  171. key: 'myUsername',
  172. data: JSON.stringify(uni.getStorageSync('userinfo').uid),
  173. });
  174. uni.setStorage({
  175. key: 'mer_id',
  176. data: merchantIntention.mer_id,
  177. });
  178. if (userInfoData.data.status == 200) {
  179. uni.setStorageSync('selectmerchant', userInfoData.data.data);
  180. const imUserData = await getImUser({
  181. mer_id: uni.getStorageSync('mer_id'),
  182. });
  183. // actionEMReconnect();
  184. const {
  185. loginWithPassword
  186. } = emConnect();
  187. uni.setStorage({
  188. key: 'im_userid',
  189. data: imUserData.data.data.im_userid
  190. })
  191. uni.setStorage({
  192. key: 'im_pass',
  193. data: imUserData.data.data.im_pass
  194. })
  195. uni.setStorage({
  196. key: 'im_uuid',
  197. data: imUserData.data.data.im_uuid
  198. })
  199. //环信注册成功 登录
  200. if (imUserData.data.status == 200) {
  201. console.log('>>>>>>>>环信注册成功 登录')
  202. await loginWithUserId()
  203. }
  204. await llwFunc();
  205. }
  206. } catch (error) {
  207. console.error(error);
  208. //TODO handle the exception
  209. }
  210. }
  211. export default func;