readAppInfo.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. if (storeStaff) {
  163. queryStoreBranch({
  164. id: storeStaff.department
  165. }).then((res) => {
  166. if (res.data.status == 200) {
  167. uni.setStorageSync("jurisdiction", res.data.data.jurisdiction);
  168. }
  169. });
  170. } else {
  171. uni.setStorageSync("jurisdiction", [7, 9, 5, 3]);
  172. }
  173. // uni.showToast({
  174. // title: res.data.message,
  175. // icon: "none"
  176. // })
  177. setTimeout(function () {
  178. console.log(
  179. "%c [ 3. 获取用户信息成功,跳转页面 ]-4",
  180. "font-size:13px; background:pink; color:#bf2c9f;"
  181. );
  182. uni.switchTab({
  183. url: "../../cashier/index"
  184. });
  185. }, 700);
  186. }
  187. return;
  188. // 之前的逻辑
  189. uni.setStorageSync("selectmerchant", data.data.data);
  190. const {
  191. merchantIntention = {}
  192. } = userInfoData.data.data || {};
  193. uni.setStorage({
  194. key: "myUsername",
  195. data: JSON.stringify(uni.getStorageSync("userinfo").uid)
  196. });
  197. uni.setStorage({
  198. key: "mer_id",
  199. data: merchantIntention.mer_id
  200. });
  201. if (userInfoData.data.status == 200) {
  202. uni.setStorageSync("selectmerchant", userInfoData.data.data);
  203. const imUserData = await getImUser({
  204. mer_id: uni.getStorageSync("mer_id")
  205. });
  206. // actionEMReconnect();
  207. const {
  208. loginWithPassword
  209. } = emConnect();
  210. uni.setStorage({
  211. key: "im_userid",
  212. data: imUserData.data.data.im_userid
  213. });
  214. uni.setStorage({
  215. key: "im_pass",
  216. data: imUserData.data.data.im_pass
  217. });
  218. uni.setStorage({
  219. key: "im_uuid",
  220. data: imUserData.data.data.im_uuid
  221. });
  222. //环信注册成功 登录
  223. if (imUserData.data.status == 200) {
  224. console.log(">>>>>>>>环信注册成功 登录");
  225. await loginWithUserId();
  226. }
  227. await llwFunc();
  228. }
  229. } catch (error) {
  230. console.error(error);
  231. //TODO handle the exception
  232. }
  233. };
  234. export default func;
  235. export async function getPhpToken(merId) {
  236. let phpToken = uni.getStorageSync("phpToken");
  237. let selectmerchant = uni.getStorageSync("selectmerchant");
  238. // console.log("phpToken", phpToken);
  239. // console.log("selectmerchant", selectmerchant);
  240. if (!phpToken && (selectmerchant && selectmerchant.merId > 0) || merId > 0) {
  241. uni.showLoading({
  242. title:"数据加载中",
  243. mask:true
  244. })
  245. const merIdS = merId || selectmerchant.merId || ""
  246. try {
  247. const res = await getPhpGlobalToken({
  248. merId: merIdS
  249. }, merIdS)
  250. if (res.data.status == 200) {
  251. const token = res.data.data.token || "";
  252. uni.setStorageSync("phpToken", token);
  253. console.log(this,"uni.navigateBack({})")
  254. if(this.$router){
  255. this.$router.go(0)
  256. }else{
  257. window.location.reload()
  258. }
  259. } else {
  260. uni.setStorageSync("phpToken", "");
  261. }
  262. } catch (error) {}
  263. }
  264. }