index.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { Component } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import { connect } from 'react-redux'
  4. import { View, Input, Image } from '@tarojs/components'
  5. import { login, setOpenId, getInfo } from '../../utils/servers/servers';
  6. import { add, minus, asyncAdd } from '../../actions/counter'
  7. import { AtButton, AtIcon, AtModal } from 'taro-ui'
  8. import NavBar from '../../components/common/NavBar';
  9. import BG from '../../image/bg.png';
  10. import LOGO from '../../image/kdjt.png';
  11. import 'taro-ui/dist/style/components/button.scss';
  12. import 'taro-ui/dist/style/components/loading.scss';
  13. import 'taro-ui/dist/style/components/icon.scss';
  14. import 'taro-ui/dist/style/components/modal.scss';
  15. import selectIcon from '../../image/select.png';
  16. import './index.less'
  17. @connect(({ counter }) => ({
  18. counter
  19. }), (dispatch) => ({
  20. add() {
  21. dispatch(add())
  22. },
  23. dec() {
  24. dispatch(minus())
  25. },
  26. asyncAdd() {
  27. dispatch(asyncAdd())
  28. }
  29. }))
  30. class Login extends Component {
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. username: '',
  35. password: '',
  36. isOpened: false,
  37. openedType: 0,
  38. usernameList: Taro.getStorageSync('usernameList') || [],
  39. rememberPassword: true
  40. }
  41. this.login = this.login.bind(this);
  42. this.authorization = this.authorization.bind(this);
  43. this.setUsernameList = this.setUsernameList.bind(this);
  44. this.getUsernameList = this.getUsernameList.bind(this);
  45. this.getInfo = this.getInfo.bind(this);
  46. }
  47. componentDidShow() {
  48. }
  49. componentDidMount() {
  50. }
  51. authorization() {
  52. this.setState({
  53. loading: true,
  54. })
  55. let _this = this;
  56. Taro.getSetting({
  57. withSubscriptions: true,
  58. success: (res) => {
  59. if (res.subscriptionsSetting.mainSwitch) {
  60. if (res.subscriptionsSetting['2L9AJWl2yNfgd83Fw_f_E6HiaEvPf109iw_xFtI8PXI'] === "reject") {
  61. this.setState({
  62. isOpened: true,
  63. loading: false,
  64. openedType: 1
  65. })
  66. return;
  67. }
  68. Taro.requestSubscribeMessage({ //获取下发权限
  69. tmplIds: [
  70. '2L9AJWl2yNfgd83Fw_f_E6HiaEvPf109iw_xFtI8PXI',
  71. ], //此处写在后台获取的模板ID,可以写多个模板ID,看自己的需求
  72. success: function (s) {
  73. _this.setState({
  74. loading: false,
  75. })
  76. _this.login();
  77. },
  78. fail: function (err) {
  79. _this.setState({
  80. loading: false,
  81. })
  82. //console.log(err)
  83. }
  84. })
  85. } else {
  86. this.setState({
  87. isOpened: true,
  88. loading: false,
  89. })
  90. }
  91. },
  92. fail: () => {
  93. this.setState({
  94. loading: false,
  95. })
  96. }
  97. })
  98. }
  99. login() {
  100. if (!this.state.username) {
  101. Taro.showToast({
  102. title: '请输入用户名',
  103. icon: 'none',
  104. duration: 2000
  105. })
  106. return;
  107. }
  108. if (!this.state.password) {
  109. Taro.showToast({
  110. title: '请输入密码',
  111. icon: 'none',
  112. duration: 2000
  113. })
  114. return;
  115. }
  116. this.setState({
  117. loading: true,
  118. })
  119. login({
  120. username: this.state.username,
  121. password: this.state.password
  122. }).then((msg) => {
  123. this.setState({
  124. loading: false
  125. });
  126. this.getInfo()
  127. }).catch((err) => {
  128. this.setState({
  129. loading: false
  130. })
  131. })
  132. }
  133. getInfo() {
  134. getInfo({}).then((msg) => {
  135. if (msg.code === 200) {
  136. if (!msg.user.openId) {
  137. Taro.login({
  138. success: (res) => {
  139. setOpenId({
  140. code: res.code,
  141. }).then(v => {
  142. if (v.error.length === 0) {
  143. this.setUsernameList(this.state.username, this.state.password);
  144. Taro.eventCenter.trigger('getStorageSync');
  145. // this.getWorkingHours(msg.departmentId);
  146. } else {
  147. Taro.showToast({
  148. title: v.error[0].message,
  149. icon: 'none',
  150. })
  151. }
  152. })
  153. },
  154. fail: () => {
  155. Taro.showToast({ title: '系统错误,请稍后重试' })
  156. }
  157. });
  158. } else {
  159. this.setUsernameList(this.state.username, this.state.password);
  160. Taro.eventCenter.trigger('getStorageSync');
  161. // this.getWorkingHours(msg.departmentId);
  162. }
  163. // let obj = Taro.getStorageSync('userInfor');
  164. // obj.workTimeInfor = msg.user;
  165. Taro.setStorageSync('userInfor', msg.user);
  166. Taro.setStorageSync('roles', msg.roles);
  167. Taro.switchTab({
  168. // url: '/pages/project/index',
  169. url: '/pages/punchClock/index',
  170. })
  171. } else {
  172. Taro.showToast({ title: msg.error[0].message, icon: 'none' })
  173. }
  174. }).catch((err) => {
  175. // console.log(err)
  176. })
  177. }
  178. setUsernameList(username, password) {
  179. if (!this.state.rememberPassword) {
  180. let usernameList = Taro.getStorageSync('usernameList') || [];
  181. let index = usernameList.findIndex(v => v.username === username);
  182. if (index !== -1) {
  183. usernameList.splice(index, 1)
  184. }
  185. Taro.setStorageSync('usernameList', usernameList);
  186. } else {
  187. let usernameList = Taro.getStorageSync('usernameList') || [];
  188. let index = usernameList.findIndex(v => v.username === username);
  189. if (index === -1) {
  190. usernameList.push({
  191. username,
  192. password
  193. })
  194. } else {
  195. usernameList[index] = {
  196. username,
  197. password
  198. }
  199. }
  200. Taro.setStorageSync('usernameList', usernameList);
  201. }
  202. }
  203. getUsernameList(value = this.state.username) {
  204. let usernameList = Taro.getStorageSync('usernameList') || [];
  205. let arr = [];
  206. for (let i of usernameList) {
  207. if (i.username.indexOf(value) !== -1) {
  208. arr.push(i)
  209. }
  210. }
  211. this.setState({
  212. usernameList: arr,
  213. isopenUsernameList: true
  214. })
  215. }
  216. render() {
  217. return (
  218. <View className='login' onClick={() => {
  219. this.setState({
  220. isopenUsernameList: false
  221. })
  222. }}>
  223. <NavBar
  224. extClass='NavBar'
  225. title={<Image src={LOGO} mode='heightFix' />}
  226. background='#0000'
  227. color='#FFF'
  228. onHome={() => { }}
  229. />
  230. {/* <Image src={LOGO} className="logo" mode='heightFix' /> */}
  231. <View className='welcome'>
  232. <Image src={BG} className='bg' mode='heightFix' />
  233. <View className='welcomeTitle'>研发工时管理服务系统</View>
  234. <View className='welcomeTitle'>欢迎您!</View>
  235. </View>
  236. <privacy />
  237. <View className='loginContent'>
  238. <View className='loginItem'>
  239. <AtIcon value='user' size='20' color='#999999' />
  240. <View className='usernameContent'>
  241. <Input
  242. style={{ marginLeft: '15px', width: '100%' }}
  243. type='text'
  244. placeholder='请输入您的系统账户'
  245. value={this.state.username}
  246. onInput={(v) => {
  247. this.setState({
  248. username: v.detail.value
  249. })
  250. this.getUsernameList(v.detail.value);
  251. }}
  252. onFocus={() => {
  253. this.getUsernameList();
  254. }}
  255. onBlur={() => {
  256. // this.setState({
  257. // isopenUsernameList:false
  258. // })
  259. }}
  260. />
  261. </View>
  262. </View>
  263. <View className='usernameListContent'>
  264. {this.state.isopenUsernameList && this.state.usernameList.length > 0 ? <View className='usernameList'>
  265. {
  266. this.state.usernameList.map((v, k) => (
  267. <View key={k} className='usernameItem' onClick={() => {
  268. this.setState({
  269. username: v.username,
  270. password: v.password,
  271. isopenUsernameList: false
  272. })
  273. }}>
  274. <AtIcon value='user' size='20' color='#c6c4c4' />
  275. <View className='value'>
  276. <View className='name'>{v.username}</View>
  277. <View className='password'>*******</View>
  278. </View>
  279. </View>
  280. ))
  281. }
  282. </View> : null}
  283. </View>
  284. <View className='loginItem'>
  285. <AtIcon value='lock' size='20' color='#999999' />
  286. <Input
  287. style={{ marginLeft: '15px', width: '100%' }}
  288. type='password'
  289. placeholder='请输入密码'
  290. value={this.state.password}
  291. onInput={(v) => {
  292. this.setState({
  293. password: v.detail.value
  294. })
  295. }}
  296. />
  297. </View>
  298. <View className='rememberPassword'>
  299. <View className='selectContent' onClick={() => {
  300. this.setState({
  301. rememberPassword: !this.state.rememberPassword
  302. })
  303. }}>
  304. <Image src={selectIcon} className='selectIcon' style={{ visibility: this.state.rememberPassword ? "visible" : "hidden" }} />
  305. </View>
  306. 记住密码
  307. </View>
  308. <AtButton type='primary' circle loading={this.state.loading} onClick={this.authorization}>登录</AtButton>
  309. <View className='tips'>
  310. 暂无账号?请联系管理员进行添加
  311. </View>
  312. <View className='company'>
  313. {/* 湖南科德信息咨询集团有限公司 */}
  314. </View>
  315. </View>
  316. <AtModal
  317. isOpened={this.state.isOpened}
  318. title='提醒'
  319. cancelText='取消'
  320. confirmText='确认'
  321. onClose={() => {
  322. this.setState({
  323. isOpened: false,
  324. openedType: 0,
  325. })
  326. }}
  327. onCancel={() => {
  328. this.setState({
  329. isOpened: false,
  330. openedType: 0
  331. })
  332. }}
  333. onConfirm={() => {
  334. this.setState({
  335. isOpened: false,
  336. openedType: 0
  337. }, () => {
  338. Taro.openSetting({
  339. success: (value) => {
  340. }
  341. })
  342. })
  343. }}
  344. content={this.state.openedType === 1 ? '您存在拒绝接收的消息类型,请点击确认按钮,前往设置页->通知管理,点击不接受的消息通知类型,调整为允许' : '您关闭了接收通知权限,将不能收到订阅通知,请点击确认按钮,前往设置页->通知管理->接收通知,打开接收通知权限'}
  345. />
  346. </View>
  347. )
  348. }
  349. }
  350. export default Login