index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <template>
  2. <div class="login-wrapper">
  3. <div class="shading">
  4. <image :src="logoUrl" v-if="logoUrl" />
  5. <image src="/static/images/logo2.png" v-else />
  6. </div>
  7. <div class="whiteBg" v-if="formItem === 1">
  8. <div class="list" v-if="current !== 1">
  9. <form @submit.prevent="submit">
  10. <div class="item">
  11. <div class="acea-row row-middle">
  12. <image src="/static/images/phone_1.png" style="width: 24rpx; height: 34rpx;"></image>
  13. <input type="text" placeholder="输入手机号码" v-model="account" maxlength="11" required />
  14. </div>
  15. </div>
  16. <div class="item">
  17. <div class="acea-row row-middle">
  18. <image src="/static/images/code_1.png" style="width: 28rpx; height: 32rpx;"></image>
  19. <input type="password" placeholder="填写登录密码" v-model="password" required />
  20. </div>
  21. </div>
  22. </form>
  23. <!-- <navigator class="forgetPwd" hover-class="none" url="/pages/users/retrievePassword/index">
  24. <span class="iconfont icon-wenti"></span>忘记密码
  25. </navigator> -->
  26. </div>
  27. <div class="list" v-if="current !== 0">
  28. <div class="item">
  29. <div class="acea-row row-middle">
  30. <image src="/static/images/phone_1.png" style="width: 24rpx; height: 34rpx;"></image>
  31. <input type="text" placeholder="输入手机号码" v-model="account" maxlength="11" />
  32. </div>
  33. </div>
  34. <div class="item">
  35. <div class="acea-row row-middle">
  36. <image src="/static/images/code_2.png" style="width: 28rpx; height: 32rpx;"></image>
  37. <input type="text" placeholder="填写验证码" class="codeIput" v-model="captcha" />
  38. <button class="code" :disabled="disabled" :class="disabled === true ? 'on' : ''" @click="code">
  39. {{ text }}
  40. </button>
  41. </div>
  42. </div>
  43. <div class="item" v-if="isShowCode">
  44. <div class="acea-row row-middle">
  45. <image src="/static/images/code_2.png" style="width: 28rpx; height: 32rpx;"></image>
  46. <input type="text" placeholder="填写验证码" class="codeIput" v-model="codeVal" />
  47. <div class="code" @click="again"><img :src="codeUrl" /></div>
  48. </div>
  49. </div>
  50. </div>
  51. <div class="logon" @click="loginMobile" v-if="current !== 0">登录</div>
  52. <div class="logon" @click="submit" v-if="current === 0">登录</div>
  53. <div class="tips">
  54. <div v-if="current==0" @click="current = 1">快速登录</div>
  55. <div v-if="current==1" @click="current = 0">账号登录</div>
  56. </div>
  57. </div>
  58. <div class="bottom"></div>
  59. </div>
  60. </template>
  61. <script>
  62. import dayjs from "@/plugin/dayjs/dayjs.min.js";
  63. import sendVerifyCode from "@/mixins/SendVerifyCode";
  64. import {
  65. loginH5,
  66. loginMobile,
  67. registerVerify,
  68. register,
  69. getCodeApi,
  70. getUserInfo
  71. } from "@/api/user";
  72. import attrs, {
  73. required,
  74. alpha_num,
  75. chs_phone
  76. } from "@/utils/validate";
  77. import {
  78. validatorDefaultCatch
  79. } from "@/utils/dialog";
  80. import {
  81. getLogo
  82. } from "@/api/public";
  83. // import cookie from "@/utils/store/cookie";
  84. import {
  85. VUE_APP_API_URL
  86. } from "@/utils";
  87. const BACK_URL = "login_back_url";
  88. export default {
  89. name: "Login",
  90. mixins: [sendVerifyCode],
  91. data: function() {
  92. return {
  93. navList: ["快速登录", "账号登录"],
  94. current: 0,
  95. account: "",
  96. password: "",
  97. captcha: "",
  98. formItem: 1,
  99. type: "login",
  100. logoUrl: "",
  101. keyCode: "",
  102. codeUrl: "",
  103. codeVal: "",
  104. isShowCode: false
  105. };
  106. },
  107. watch: {
  108. formItem: function(nval, oVal) {
  109. if (nval == 1) {
  110. this.type = 'login'
  111. } else {
  112. this.type = 'register'
  113. }
  114. }
  115. },
  116. mounted: function() {
  117. // this.getCode();
  118. this.getLogoImage();
  119. },
  120. methods: {
  121. again() {
  122. this.codeUrl =
  123. VUE_APP_API_URL +
  124. "/sms_captcha?" +
  125. "key=" +
  126. this.keyCode +
  127. Date.parse(new Date());
  128. },
  129. code() {
  130. let that = this
  131. getCodeApi()
  132. .then(res => {
  133. that.keyCode = res.data.key;
  134. that.getCode();
  135. })
  136. .catch(res => {
  137. that.$util.Tips({
  138. title: res
  139. });
  140. });
  141. },
  142. async getLogoImage() {
  143. let that = this;
  144. getLogo(2).then(res => {
  145. that.logoUrl = res.data.logo_url;
  146. });
  147. },
  148. async loginMobile() {
  149. let that = this;
  150. if (!that.account) return that.$util.Tips({
  151. title: '请填写手机号码'
  152. });
  153. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  154. title: '请输入正确的手机号码'
  155. });
  156. if (!that.captcha) return that.$util.Tips({
  157. title: '请填写验证码'
  158. });
  159. if (!/^[\w\d]+$/i.test(that.captcha)) return that.$util.Tips({
  160. title: '请输入正确的验证码'
  161. });
  162. loginMobile({
  163. phone: that.account,
  164. captcha: that.captcha,
  165. spread: that.$Cache.get("spread")
  166. })
  167. .then(res => {
  168. let data = res.data;
  169. that.$store.commit("LOGIN", {
  170. 'token': data.token,
  171. 'time': data.expires_time - this.$Cache.time()
  172. });
  173. const backUrl = that.$Cache.get(BACK_URL) || "/pages/index/index";
  174. that.$Cache.clear(BACK_URL);
  175. getUserInfo().then(res => {
  176. that.$store.commit("SETUID", res.data.uid);
  177. if(backUrl.indexOf('/pages/users/login/index') !== -1){
  178. backUrl = '/pages/index/index';
  179. }
  180. uni.reLaunch({
  181. url: backUrl
  182. });
  183. // if (backUrl === '/pages/index/index' || backUrl === '/pages/order_addcart/order_addcart' || backUrl ===
  184. // '/pages/user/index') {
  185. // uni.switchTab({
  186. // url: backUrl
  187. // });
  188. // } else {
  189. // uni.navigateBack()
  190. // }
  191. // switch (backUrl) {
  192. // case '/pages/index/index':
  193. // case '/pages/order_addcart/order_addcart':
  194. // case '/pages/user/index':
  195. // uni.switchTab({
  196. // url: backUrl
  197. // });
  198. // break;
  199. // case '/pages/offline_cashier/payment/index':
  200. // uni.navigateTo({
  201. // url: backUrl
  202. // });
  203. // break;
  204. // default:
  205. // uni.switchTab({
  206. // url: '/pages/index/index'
  207. // });
  208. // break;
  209. // }
  210. })
  211. })
  212. .catch(res => {
  213. that.$util.Tips({
  214. title: res
  215. });
  216. });
  217. },
  218. async register() {
  219. let that = this;
  220. if (!that.account) return that.$util.Tips({
  221. title: '请填写手机号码'
  222. });
  223. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  224. title: '请输入正确的手机号码'
  225. });
  226. if (!that.captcha) return that.$util.Tips({
  227. title: '请填写验证码'
  228. });
  229. if (!/^[\w\d]+$/i.test(that.captcha)) return that.$util.Tips({
  230. title: '请输入正确的验证码'
  231. });
  232. if (!that.password) return that.$util.Tips({
  233. title: '请填写密码'
  234. });
  235. if (/^([0-9]|[a-z]|[A-Z]){0,6}$/i.test(that.password)) return that.$util.Tips({
  236. title: '您输入的密码过于简单'
  237. });
  238. register({
  239. account: that.account,
  240. captcha: that.captcha,
  241. password: that.password,
  242. spread: that.$Cache.get("spread")
  243. })
  244. .then(res => {
  245. that.$util.Tips({
  246. title: res
  247. });
  248. that.formItem = 1;
  249. })
  250. .catch(res => {
  251. that.$util.Tips({
  252. title: res
  253. });
  254. });
  255. },
  256. async getCode() {
  257. let that = this;
  258. if (!that.account) return that.$util.Tips({
  259. title: '请填写手机号码'
  260. });
  261. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  262. title: '请输入正确的手机号码'
  263. });
  264. if (that.formItem == 2) that.type = "register";
  265. await registerVerify({
  266. phone: that.account,
  267. type: that.type,
  268. key: that.keyCode,
  269. code: that.codeVal
  270. })
  271. .then(res => {
  272. that.$util.Tips({
  273. title: res.msg
  274. });
  275. that.sendCode();
  276. })
  277. .catch(res => {
  278. console.log(res, 'res')
  279. // if (res.data.status === 402) {
  280. // that.codeUrl = `${VUE_APP_API_URL}/sms_captcha?key=${that.keyCode}`;
  281. // that.isShowCode = true;
  282. // }
  283. that.$util.Tips({
  284. title: res
  285. });
  286. });
  287. },
  288. navTap: function(index) {
  289. this.current = index;
  290. },
  291. async submit() {
  292. let that = this;
  293. if (!that.account) return that.$util.Tips({
  294. title: '请填写账号'
  295. });
  296. if (!/^[\w\d]{5,16}$/i.test(that.account)) return that.$util.Tips({
  297. title: '请输入正确的账号'
  298. });
  299. if (!that.password) return that.$util.Tips({
  300. title: '请填写密码'
  301. });
  302. loginH5({
  303. account: that.account,
  304. password: that.password,
  305. spread: that.$Cache.get("spread")
  306. })
  307. .then(({
  308. data
  309. }) => {
  310. console.log(this.$Cache.time());
  311. that.$store.commit("LOGIN", {
  312. 'token': data.token,
  313. 'time': data.expires_time - this.$Cache.time()
  314. });
  315. const backUrl = that.$Cache.get(BACK_URL) || "/pages/index/index";
  316. that.$Cache.clear(BACK_URL);
  317. getUserInfo().then(res => {
  318. that.$store.commit("SETUID", res.data.uid);
  319. uni.reLaunch({
  320. url: backUrl
  321. });
  322. })
  323. })
  324. .catch(e => {
  325. that.$util.Tips({
  326. title: e
  327. });
  328. });
  329. }
  330. }
  331. };
  332. </script>
  333. <style lang="scss">
  334. .code img {
  335. width: 100%;
  336. height: 100%;
  337. }
  338. .acea-row.row-middle {
  339. input {
  340. margin-left: 20rpx;
  341. display: block;
  342. }
  343. }
  344. .login-wrapper {
  345. padding: 30rpx;
  346. .shading {
  347. display: flex;
  348. align-items: center;
  349. justify-content: center;
  350. width: 100%;
  351. height: 200rpx;
  352. margin-top: 200rpx;
  353. image {
  354. width: 180rpx;
  355. height: 180rpx;
  356. }
  357. }
  358. .whiteBg {
  359. margin-top: 100rpx;
  360. .list {
  361. border-radius: 16rpx;
  362. overflow: hidden;
  363. .item {
  364. border-bottom: 1px solid #F0F0F0;
  365. background: #fff;
  366. .row-middle {
  367. position: relative;
  368. padding: 16rpx 45rpx;
  369. input {
  370. flex: 1;
  371. font-size: 28rpx;
  372. height: 80rpx;
  373. }
  374. .code {
  375. position: absolute;
  376. right: 30rpx;
  377. top: 50%;
  378. color: #E93323;
  379. font-size: 26rpx;
  380. transform: translateY(-50%);
  381. }
  382. }
  383. }
  384. }
  385. .logon {
  386. display: flex;
  387. align-items: center;
  388. justify-content: center;
  389. width: 100%;
  390. height: 86rpx;
  391. margin-top: 80rpx;
  392. background-color: $theme-color;
  393. border-radius: 120rpx;
  394. color: #FFFFFF;
  395. font-size: 30rpx;
  396. }
  397. .tips {
  398. margin: 30rpx;
  399. text-align: center;
  400. color: #999;
  401. }
  402. }
  403. }
  404. </style>