index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <view class="page">
  3. <view class="system-height" :style="{ height: statusBarHeight }"></view>
  4. <!-- #ifdef MP -->
  5. <view class="title-bar" style="height: 43px">
  6. <view class="icon" @click="back" v-if="!isHome">
  7. <image src="/static/left.png"></image>
  8. </view>
  9. <view class="icon" @click="home" v-else>
  10. <image src="/static/home.png"></image>
  11. </view>
  12. 账户登录
  13. </view>
  14. <!-- #endif -->
  15. <view class="wechat_login">
  16. <view class="img">
  17. <image src="/static/wechat_login.png" mode="widthFix"></image>
  18. </view>
  19. <view class="btn-wrapper">
  20. <!-- #ifdef H5 -->
  21. <button hover-class="none" @click="wechatLogin" class="bg-green btn1">
  22. 微信登录
  23. </button>
  24. <!-- #endif -->
  25. <!-- #ifdef MP -->
  26. <button hover-class="none" @tap="getUserProfile" class="bg-green btn1">
  27. 微信登录
  28. </button>
  29. <!-- #endif -->
  30. <!-- <button hover-class="none" @click="isUp = true" class="btn2">手机号登录</button> -->
  31. </view>
  32. </view>
  33. <block v-if="isUp">
  34. <mobileLogin
  35. :isUp="isUp"
  36. @close="maskClose"
  37. :authKey="authKey"
  38. @wechatPhone="wechatPhone"
  39. ></mobileLogin>
  40. </block>
  41. <block v-if="isPhoneBox">
  42. <routinePhone
  43. :logoUrl="logoUrl"
  44. :isPhoneBox="isPhoneBox"
  45. @close="bindPhoneClose"
  46. :authKey="authKey"
  47. >
  48. </routinePhone>
  49. </block>
  50. </view>
  51. </template>
  52. <script setup>
  53. import { ref, getCurrentInstance } from "vue";
  54. import { onLoad } from "@dcloudio/uni-app";
  55. import { useAppStore } from "@/stores/app";
  56. import mobileLogin from "@/components/login_mobile/index.vue";
  57. import routinePhone from "@/components/login_mobile/routine_phone.vue";
  58. import { getLogo, getUserPhone } from "@/api/public";
  59. import { getUserInfo } from "@/api/user.js";
  60. import Routine from "@/libs/routine";
  61. import wechat from "@/libs/wechat";
  62. import Cache from "@/utils/cache";
  63. import { EXPIRES_TIME } from "@/config/cache";
  64. const appStore = useAppStore();
  65. const { proxy } = getCurrentInstance();
  66. const isUp = ref(false);
  67. const phone = ref("");
  68. const statusBarHeight = ref(uni.getSystemInfoSync().statusBarHeight + "px");
  69. const isHome = ref(false);
  70. const isPhoneBox = ref(false);
  71. const logoUrl = ref("");
  72. const code = ref("");
  73. const authKey = ref("");
  74. const options = ref({});
  75. const userInfo = ref({});
  76. const codeNum = ref(0);
  77. function back() {
  78. uni.navigateBack();
  79. }
  80. function home() {
  81. uni.switchTab({ url: "/pages/index/index" });
  82. }
  83. function maskClose() {
  84. isUp.value = false;
  85. }
  86. function bindPhoneClose(data) {
  87. if (data.isStatus) {
  88. isPhoneBox.value = false;
  89. proxy.$util.Tips(
  90. {
  91. title: "登录成功",
  92. icon: "success",
  93. },
  94. { tab: 3 }
  95. );
  96. } else {
  97. isPhoneBox.value = false;
  98. }
  99. }
  100. // #ifdef MP
  101. function getphonenumber(e) {
  102. uni.showLoading({ title: "正在登录中" });
  103. Routine.getCode()
  104. .then((code) => {
  105. getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
  106. })
  107. .catch(() => {
  108. uni.$emit("closePage", false);
  109. uni.hideLoading();
  110. });
  111. }
  112. function getUserPhoneNumber(encryptedData, iv, codeVal) {
  113. getUserPhone({
  114. encryptedData,
  115. iv,
  116. code: codeVal,
  117. type: "routine",
  118. key: authKey.value,
  119. })
  120. .then((res) => {
  121. appStore.LOGIN({ token: res.data.token });
  122. appStore.SETUID(res.data.uid);
  123. // 保存过期时间(7天后过期)
  124. const expiresTime = Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
  125. Cache.set(EXPIRES_TIME, expiresTime, 0);
  126. getUserInfoFn();
  127. proxy.$util.Tips(
  128. {
  129. title: "登录成功",
  130. icon: "success",
  131. },
  132. { tab: 3 }
  133. );
  134. })
  135. .catch((res) => {
  136. uni.hideLoading();
  137. proxy.$util.Tips({ title: res });
  138. });
  139. }
  140. function getUserInfoFn() {
  141. getUserInfo().then((res) => {
  142. uni.hideLoading();
  143. userInfo.value = res.data;
  144. appStore.UPDATE_USERINFO(res.data);
  145. proxy.$util.Tips(
  146. {
  147. title: "登录成功",
  148. icon: "success",
  149. },
  150. { tab: 3 }
  151. );
  152. });
  153. }
  154. function getUserProfile() {
  155. uni.showLoading({ title: "正在登录中" });
  156. Routine.getUserProfile()
  157. .then((res) => {
  158. Routine.getCode()
  159. .then((codeVal) => {
  160. getWxUser(codeVal, res);
  161. })
  162. .catch(() => {
  163. uni.hideLoading();
  164. });
  165. })
  166. .catch(() => {
  167. uni.hideLoading();
  168. });
  169. }
  170. function getWxUser(codeVal, res) {
  171. let user = res.userInfo;
  172. user.code = codeVal;
  173. user.spread_spid = getApp().globalData.spid;
  174. user.spread_code = getApp().globalData.code;
  175. user.avatar = user.avatarUrl;
  176. user.city = user.city;
  177. user.country = user.country;
  178. user.nickName = user.nickName;
  179. user.province = user.province;
  180. user.sex = user.gender;
  181. user.type = "routine";
  182. Routine.authUserInfo(user.code, user)
  183. .then((res2) => {
  184. authKey.value = res2.data.key;
  185. if (res2.data.type === "register") {
  186. uni.hideLoading();
  187. isPhoneBox.value = true;
  188. }
  189. if (res2.data.type === "login") {
  190. uni.hideLoading();
  191. appStore.LOGIN({ token: res2.data.token });
  192. appStore.SETUID(res2.data.uid);
  193. getUserInfoFn();
  194. proxy.$util.Tips(
  195. {
  196. title: res2,
  197. icon: "success",
  198. },
  199. { tab: 3 }
  200. );
  201. }
  202. })
  203. .catch((res2) => {
  204. uni.hideLoading();
  205. uni.showToast({
  206. title: res2,
  207. icon: "none",
  208. duration: 2000,
  209. });
  210. });
  211. }
  212. // #endif
  213. // #ifdef H5
  214. function getQueryString(name) {
  215. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  216. var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
  217. var r = window.location.search.substr(1).match(reg);
  218. var q = window.location.pathname.substr(1).match(reg_rewrite);
  219. if (r != null) {
  220. return unescape(r[2]);
  221. } else if (q != null) {
  222. return unescape(q[2]);
  223. } else {
  224. return null;
  225. }
  226. }
  227. function wechatLogin() {
  228. if (!code.value && options.value.scope !== "snsapi_base") {
  229. proxy.$wechat.oAuth("snsapi_userinfo", "/pages/users/wechat_login/index");
  230. } else {
  231. isUp.value = true;
  232. }
  233. }
  234. function wechatPhone() {
  235. proxy.$Cache.clear("snsapiKey");
  236. if (options.value.back_url) {
  237. let url = uni.getStorageSync("snRouter");
  238. url = url.indexOf("/pages/index/index") != -1 ? "/" : url;
  239. if (url.indexOf("/pages/users/wechat_login/index") !== -1) {
  240. url = "/";
  241. }
  242. if (!url) {
  243. url = "/pages/index/index";
  244. }
  245. isUp.value = false;
  246. uni.showToast({
  247. title: "登录成功",
  248. icon: "none",
  249. });
  250. setTimeout(() => {
  251. location.href = url;
  252. }, 800);
  253. } else {
  254. uni.navigateBack();
  255. }
  256. }
  257. // #endif
  258. onLoad((opts) => {
  259. getLogo().then((res) => {
  260. logoUrl.value = res.data.logoUrl;
  261. });
  262. // #ifdef H5
  263. document.body.addEventListener("focusout", () => {
  264. setTimeout(() => {
  265. const scrollHeight =
  266. document.documentElement.scrollTop || document.body.scrollTop || 0;
  267. window.scrollTo(0, Math.max(scrollHeight - 1, 0));
  268. }, 100);
  269. });
  270. const { code: c, state, scope } = opts;
  271. options.value = opts;
  272. code.value = c || "";
  273. if (c && options.value.scope !== "snsapi_base") {
  274. let spread = getApp().globalData.spid ? getApp().globalData.spid : 0;
  275. wechat
  276. .auth(c, spread)
  277. .then((res) => {
  278. if (res.type === "register") {
  279. authKey.value = res.key;
  280. isUp.value = true;
  281. }
  282. if (res.type === "login") {
  283. appStore.LOGIN({ token: res.data.token });
  284. appStore.SETUID(res.data.uid);
  285. // 保存过期时间(7天后过期)
  286. const expiresTime = Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
  287. Cache.set(EXPIRES_TIME, expiresTime, 0);
  288. getUserInfoFn();
  289. wechatPhone();
  290. }
  291. })
  292. .catch(() => {});
  293. }
  294. // #endif
  295. let pages = getCurrentPages();
  296. // let prePage = pages[pages.length - 2]
  297. // if (prePage.route == 'pages/order_addcart/order_addcart') {
  298. // isHome.value = true
  299. // } else {
  300. // isHome.value = false
  301. // }
  302. });
  303. </script>
  304. <style lang="scss">
  305. page {
  306. background: #fff;
  307. height: 100%;
  308. }
  309. .page {
  310. background: #fff;
  311. height: 100%;
  312. }
  313. .wechat_login {
  314. padding: 72rpx 34rpx;
  315. .img image {
  316. width: 100%;
  317. }
  318. .btn-wrapper {
  319. margin-top: 86rpx;
  320. padding: 0 66rpx;
  321. button {
  322. width: 100%;
  323. height: 86rpx;
  324. line-height: 86rpx;
  325. margin-bottom: 40rpx;
  326. border-radius: 120rpx;
  327. font-size: 30rpx;
  328. &.btn1 {
  329. color: #fff;
  330. }
  331. &.btn2 {
  332. color: #666666;
  333. border: 1px solid #666666;
  334. }
  335. }
  336. }
  337. }
  338. .title-bar {
  339. position: relative;
  340. display: flex;
  341. align-items: center;
  342. justify-content: center;
  343. font-size: 36rpx;
  344. }
  345. .icon {
  346. position: absolute;
  347. left: 30rpx;
  348. top: 0;
  349. display: flex;
  350. align-items: center;
  351. justify-content: center;
  352. width: 86rpx;
  353. height: 86rpx;
  354. image {
  355. width: 50rpx;
  356. height: 50rpx;
  357. }
  358. }
  359. </style>