login.vue 8.5 KB

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