readAppInfo.js 7.0 KB

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