readAppInfo.js 7.7 KB

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