login.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <template>
  2. <view class="page-container">
  3. <view class="welcome-title">
  4. 超吉炫,欢迎您!
  5. </view>
  6. <view class="main-card">
  7. <image class="logo" :src="imgurl+'/cjx/hexiao/lobin_lobin.png'" mode="aspectFit"></image>
  8. <view class="user-type-selector">
  9. <view
  10. class="type-button"
  11. :class="{ 'active': userType === 'distributor' }"
  12. @click="switchUserType('distributor')"
  13. >
  14. <uni-icons type="person" :color="userType === 'distributor' ? '#fff' : '#4a8af4'" size="20"></uni-icons>
  15. <text>经销商</text>
  16. </view>
  17. <view
  18. class="type-button"
  19. :class="{ 'active': userType === 'salesman' }"
  20. @click="switchUserType('salesman')"
  21. >
  22. <uni-icons type="staff" :color="userType === 'salesman' ? '#fff' : '#4a8af4'" size="20"></uni-icons>
  23. <text>业务员</text>
  24. </view>
  25. </view>
  26. <view class="form-group">
  27. <view class="input-wrapper">
  28. <uni-icons type="person" size="24" color="#cccccc"></uni-icons>
  29. <input class="input-field" type="number" v-model="phone" placeholder="请输入手机号" placeholder-class="placeholder" />
  30. </view>
  31. <view class="input-wrapper">
  32. <uni-icons type="locked" size="24" color="#cccccc"></uni-icons>
  33. <input class="input-field" type="number" v-model="code" placeholder="请输入验证码" placeholder-class="placeholder" />
  34. <text
  35. class="code-btn"
  36. :class="{ 'disabled': isCountingDown }"
  37. @click="getVerificationCode"
  38. >
  39. {{ codeBtnText }}
  40. </text>
  41. </view>
  42. </view>
  43. <button class="login-btn" @click="handleLogin">登 录</button>
  44. <!-- <view class="version-info">-->
  45. <!-- 版本号:V1.0.0-->
  46. <!-- </view>-->
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. // 1. 导入你的API方法
  52. import { getVerificationCode, login } from '@/api/hexiao.js';
  53. import {getAdminToken, setAdminInfo, setAdminToken} from "../../../utils/auth";
  54. export default {
  55. data() {
  56. return {
  57. userType: 'distributor',
  58. phone: '18569549881',
  59. code: '888888',
  60. codeBtnText: '获取手机验证码',
  61. isCountingDown: false,
  62. countdown: 60,
  63. timer: null,
  64. imgurl:"https://hyscancode.oss-cn-hangzhou.aliyuncs.com/xiaochengxu",
  65. }
  66. },
  67. onUnload() {
  68. if (this.timer) {
  69. clearInterval(this.timer);
  70. this.timer = null;
  71. }
  72. },
  73. onLoad(){
  74. let token = getAdminToken()
  75. if(token){
  76. this.doLoginSuccess();
  77. }
  78. },
  79. methods: {
  80. doLoginSuccess(){
  81. let userType = 0;
  82. let pageUrl = '/pages/cjx/hexiao/ywy/index';
  83. if(this.userType === 'distributor'){
  84. pageUrl = '/pages/cjx/hexiao/jxs/index';
  85. }
  86. if(this.userType === 'salesman'){
  87. pageUrl = '/pages/cjx/hexiao/ywy/index';
  88. }
  89. uni.reLaunch({
  90. url: pageUrl
  91. });
  92. },
  93. switchUserType(type) {
  94. this.userType = type;
  95. },
  96. // --- 更新:调用获取验证码的API ---
  97. getVerificationCode() {
  98. if (this.isCountingDown) {
  99. return;
  100. }
  101. if(!this.phone) {
  102. uni.showToast({ title: '请输入手机号', icon: 'none' });
  103. return;
  104. }
  105. if(this.timer){
  106. clearInterval(this.timer);
  107. this.timer = null;
  108. }
  109. let userType = 0;
  110. if(this.userType === 'distributor'){
  111. userType = 1;
  112. }
  113. if(this.userType === 'salesman'){
  114. userType = 2;
  115. }
  116. // 调用API
  117. getVerificationCode(this.phone,userType).then(res => {
  118. if(res.code !== 0){
  119. uni.showToast({ title: res.msg || '找不到用户信息,请确认', icon: 'none' });
  120. return;
  121. }
  122. console.log('验证码接口成功返回:', res);
  123. uni.showToast({ title: res.msg || '验证码已发送', icon: 'success' });
  124. // 开始倒计时
  125. this.isCountingDown = true;
  126. this.codeBtnText = `${this.countdown}s后重新获取`;
  127. this.timer = setInterval(() => {
  128. this.countdown--;
  129. if (this.countdown > 0) {
  130. this.codeBtnText = `${this.countdown}s后重新获取`;
  131. } else {
  132. clearInterval(this.timer);
  133. this.timer = null;
  134. this.countdown = 60;
  135. this.isCountingDown = false;
  136. this.codeBtnText = '获取手机验证码';
  137. }
  138. }, 1000);
  139. }).catch(err => {
  140. // if(res.code === -99){
  141. // uni.showToast({ title: err.msg || '找不到用户信息,请确认', icon: 'none' });
  142. // return;
  143. // }
  144. // console.error('验证码接口失败返回:', err);
  145. // uni.showToast({ title: err.msg || '发送失败,请重试', icon: 'none' });
  146. });
  147. },
  148. setUserInfo(res){
  149. setAdminInfo(res);
  150. },
  151. // --- 更新:调用登录的API ---
  152. handleLogin() {
  153. if(!this.phone || !this.code) {
  154. uni.showToast({ title: '请填写完整信息', icon: 'none' });
  155. return;
  156. }
  157. uni.showLoading({ title: '登录中...' });
  158. let userType = 0;
  159. if(this.userType === 'distributor'){
  160. userType = 1;
  161. }
  162. if(this.userType === 'salesman'){
  163. userType = 2;
  164. }
  165. uni.login({
  166. provider: 'weixin', //使用微信登录
  167. success: function (loginRes) {
  168. let jsCode = loginRes.code;
  169. login(this.phone, this.code,userType,jsCode).then(res => {
  170. // 登录成功 (res.code === 0)
  171. uni.hideLoading();
  172. if (res.code === 0) {
  173. if (res.data.isWxAuth == 1) {
  174. uni.setStorageSync('Authorization-status', true);
  175. }
  176. setAdminToken(res.data.sessionId)
  177. this.setUserInfo(res.data);
  178. this.doLoginSuccess();
  179. } else {
  180. console.log('登录失败!' + res.msg);
  181. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  182. }
  183. }).catch(err => {
  184. // 登录失败 (res.code !== 0 或网络错误)
  185. uni.hideLoading();
  186. console.error('登录失败:', err);
  187. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  188. });
  189. }
  190. });
  191. // 调用API
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss">
  197. .page-container {
  198. width: 100vw;
  199. height: 100vh;
  200. display: flex;
  201. flex-direction: column;
  202. align-items: center;
  203. overflow: hidden;
  204. min-height: 100vh;
  205. background-image: url("https://hyscancode.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/cjx/hexiao/login_bg.png");
  206. background-size: contain;
  207. background-position: center;
  208. background-repeat: no-repeat;
  209. position: relative;
  210. }
  211. .welcome-title {
  212. font-size: 48rpx;
  213. font-weight: bold;
  214. color: #488CFF;
  215. margin-top: 28%;
  216. margin-bottom: 40rpx;
  217. }
  218. .main-card {
  219. width: 88%;
  220. border-radius: 40rpx;
  221. padding: 40rpx;
  222. box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.05);
  223. display: flex;
  224. flex-direction: column;
  225. align-items: center;
  226. position: relative;
  227. top: 10%;
  228. height: 100%;
  229. }
  230. .logo {
  231. width: 150rpx;
  232. height: 150rpx;
  233. background-color: #f0f0f0;
  234. margin-bottom: 40rpx;
  235. border-radius: 20rpx;
  236. }
  237. .user-type-selector {
  238. display: flex;
  239. justify-content: center;
  240. width: 100%;
  241. margin-bottom: 50rpx;
  242. .type-button {
  243. display: flex;
  244. align-items: center;
  245. justify-content: center;
  246. font-size: 30rpx;
  247. padding: 15rpx 40rpx;
  248. border-radius: 50rpx;
  249. transition: all 0.3s ease;
  250. border: 1px solid #4a8af4;
  251. color: #4a8af4;
  252. background-color: #fff;
  253. margin: 0 15rpx;
  254. .uni-icons {
  255. margin-right: 10rpx;
  256. }
  257. &.active {
  258. background-color: #4a8af4;
  259. color: #ffffff;
  260. border-color: #4a8af4;
  261. }
  262. }
  263. }
  264. .form-group {
  265. width: 100%;
  266. }
  267. .input-wrapper {
  268. display: flex;
  269. align-items: center;
  270. width: 100%;
  271. padding: 20rpx 0;
  272. border-bottom: 1px solid #f0f0f0;
  273. margin-bottom: 30rpx;
  274. .input-field {
  275. flex: 1;
  276. font-size: 30rpx;
  277. margin-left: 20rpx;
  278. }
  279. .placeholder {
  280. color: #cccccc;
  281. }
  282. .code-btn {
  283. font-size: 28rpx;
  284. color: #4a8af4;
  285. white-space: nowrap;
  286. padding-left: 20rpx;
  287. transition: color 0.3s; // 添加颜色过渡效果
  288. // --- 新增:置灰样式 ---
  289. &.disabled {
  290. color: #cccccc;
  291. }
  292. }
  293. }
  294. .login-btn {
  295. width: 100%;
  296. background: #4a8af4;
  297. color: #ffffff;
  298. font-size: 32rpx;
  299. border-radius: 50rpx;
  300. margin-top: 40rpx;
  301. box-shadow: 0 10rpx 20rpx rgba(74, 138, 244, 0.3);
  302. }
  303. .version-info {
  304. margin-top: 40rpx;
  305. font-size: 24rpx;
  306. color: #b0b0b0;
  307. }
  308. </style>