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