index.jsx 11 KB

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