readAppInfo.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. const paramsData1 = {
  101. storeStaff: null,
  102. x_token: "MTYzMzYxODYzMjY4MTA0NTIwMjUtMDEtMDYgYXQgMjI6MDA6MDkgQ1NU",
  103. user: {
  104. uid: 16336,
  105. wechat_user_id: "0",
  106. ali_user_id: "0",
  107. account: "18632681045",
  108. real_name: "",
  109. label_id: null,
  110. group_id: "11",
  111. nickname: "用户1045",
  112. avatar: "https://app.bjtdba.com/uploads/logo.jpg",
  113. phone: "18632681045",
  114. now_score: null,
  115. score: 0,
  116. now_money: 0,
  117. brokerage_price: 0,
  118. admin_agent_rate: null,
  119. locktime: null,
  120. user_number: null,
  121. im: 0,
  122. im_uuid: null,
  123. last_ip: "20.100.5.11",
  124. identity: 5,
  125. spreadUid: null,
  126. user_type: "MP",
  127. pwd: "$2y$10$XARVMD82YRAl9NMwzLcrKeSozdzlnUj7IXp2dvPGYGobzYFlf0j5K",
  128. mer_id: 0,
  129. spread_count: 0,
  130. first_sign: 0
  131. }
  132. };
  133. uni.setStorageSync("userinfo", paramsData.user);
  134. uni.setStorageSync("authorization", paramsData.x_token);
  135. uni.setStorageSync("storeStaff", paramsData.storeStaff);
  136. // 用户状态判断
  137. const userInfoData = await selectMerchant({
  138. uid: paramsData.user.uid
  139. });
  140. console.log(
  141. "%c [ 2. 获取用户信息 ]-4",
  142. "font-size:13px; background:pink; color:#bf2c9f;",
  143. userInfoData
  144. );
  145. if (userInfoData.data.status === 200) {
  146. // 新逻辑
  147. uni.setStorageSync("selectmerchant", userInfoData.data.data);
  148. let storeStaff = uni.getStorageSync("storeStaff");
  149. console.log(
  150. "%c [ storeStaff ]-4",
  151. "font-size:13px; background:pink; color:#bf2c9f;",
  152. storeStaff,
  153. storeStaff != null
  154. );
  155. console.log(
  156. "%c [ 本地存储用户信息 userInfo ]-4",
  157. "font-size:13px; background:pink; color:#bf2c9f;",
  158. uni.getStorageSync("userinfo")
  159. );
  160. localStorage.setItem("aaa", "33333");
  161. console.log("读原生的", localStorage.getItem("aaa"));
  162. getPhpToken()
  163. if (storeStaff) {
  164. queryStoreBranch({
  165. id: storeStaff.department
  166. }).then((res) => {
  167. if (res.data.status == 200) {
  168. uni.setStorageSync("jurisdiction", res.data.data.jurisdiction);
  169. }
  170. });
  171. } else {
  172. uni.setStorageSync("jurisdiction", [7, 9, 5, 3]);
  173. }
  174. // uni.showToast({
  175. // title: res.data.message,
  176. // icon: "none"
  177. // })
  178. setTimeout(function () {
  179. console.log(
  180. "%c [ 3. 获取用户信息成功,跳转页面 ]-4",
  181. "font-size:13px; background:pink; color:#bf2c9f;"
  182. );
  183. uni.switchTab({
  184. url: "../../cashier/index"
  185. });
  186. }, 700);
  187. }
  188. return;
  189. // 之前的逻辑
  190. uni.setStorageSync("selectmerchant", data.data.data);
  191. const {
  192. merchantIntention = {}
  193. } = userInfoData.data.data || {};
  194. uni.setStorage({
  195. key: "myUsername",
  196. data: JSON.stringify(uni.getStorageSync("userinfo").uid)
  197. });
  198. uni.setStorage({
  199. key: "mer_id",
  200. data: merchantIntention.mer_id
  201. });
  202. if (userInfoData.data.status == 200) {
  203. uni.setStorageSync("selectmerchant", userInfoData.data.data);
  204. const imUserData = await getImUser({
  205. mer_id: uni.getStorageSync("mer_id")
  206. });
  207. // actionEMReconnect();
  208. const {
  209. loginWithPassword
  210. } = emConnect();
  211. uni.setStorage({
  212. key: "im_userid",
  213. data: imUserData.data.data.im_userid
  214. });
  215. uni.setStorage({
  216. key: "im_pass",
  217. data: imUserData.data.data.im_pass
  218. });
  219. uni.setStorage({
  220. key: "im_uuid",
  221. data: imUserData.data.data.im_uuid
  222. });
  223. //环信注册成功 登录
  224. if (imUserData.data.status == 200) {
  225. console.log(">>>>>>>>环信注册成功 登录");
  226. await loginWithUserId();
  227. }
  228. await llwFunc();
  229. }
  230. } catch (error) {
  231. console.error(error);
  232. //TODO handle the exception
  233. }
  234. };
  235. export default func;
  236. export async function getPhpToken(merId) {
  237. let phpToken = uni.getStorageSync("phpToken");
  238. let selectmerchant = uni.getStorageSync("selectmerchant");
  239. console.log("phpToken", phpToken);
  240. console.log("selectmerchant", selectmerchant);
  241. if (!phpToken && (selectmerchant && selectmerchant.merId > 0) || merId > 0) {
  242. uni.showLoading({
  243. title: "数据加载中",
  244. mask: true
  245. })
  246. const merIdS = merId || selectmerchant.merId || ""
  247. try {
  248. const res = await getPhpGlobalToken({
  249. merId: merIdS
  250. }, merIdS)
  251. if (res.data.status == 200) {
  252. const token = res.data.data.token || "";
  253. uni.setStorageSync("phpToken", token);
  254. console.log(this, "uni.navigateBack({})")
  255. return token
  256. // window.location.reload()
  257. } else {
  258. uni.setStorageSync("phpToken", "");
  259. }
  260. } catch (error) {}
  261. }
  262. }