Python添加用户判断

    def saveRegistData(self):
        """用户注册"""
        print("保存数据")
        saveDataPath=os.path.join(os.getcwd(),"config","accout.json")
        accountType=self.accoutTypeComboBox.currentText() # 账户类型
        account=self.account_le.text().strip() # 账户名称
        accountName=self.accountName_le.text() # 账户姓名
        accountPhone=self.accountPhone_le.text() # 账户联系方式
        accountPwd=self.password_le.text() # 账户密码
        accountConfirePwd=self.confirm_pwd_le.text() # 账户确认密码
        # 获取注册用户参数限制条件
        configPath = os.path.join(os.getcwd(), "config", "config.json")
        # 是否包含中文
        isContainChinese=self.isContainChinese(accountName)
        # 获取配置文件配置限制参数
        try:
            dboperate=DBOperate.DBOperate()
            querySql="SELECT * FROM config_paras"
            paras=dboperate.QueryAsDict(querySql)[0]
        except Exception as ex:
            print(f"加载配置文件参数出错\n{ex}")
        else:
            accountMinChar = int(paras.get("accountMinChar", 5))
            accountNameContainChineseMinChar = int(paras.get("accountNameContainChineseMinChar", 6))
            accountNameNotContainChineseMinChar = int(paras.get("accountNameNotContainChineseMinChar", 5))
            accountPwdMinChar = int(paras.get("accountPwdMinChar", 6))
            dboperate.CloseDB()

        if len(account.encode())<accountMinChar:
            QMessageBox().warning(None, "警告", "账户名称小于5个字符,请重新输入!", QMessageBox.Ok)
            return
        elif len(accountName.encode())<accountNameContainChineseMinChar and isContainChinese: # 中文
            QMessageBox().warning(None, "警告", "姓名不能少于2个汉字!", QMessageBox.Ok)
            return
        elif len(accountName.encode())<accountNameNotContainChineseMinChar and not isContainChinese: # 英文
            QMessageBox().warning(None, "警告", "姓名不能少于5个英文字母!", QMessageBox.Ok)
            return
        elif len(accountPwd)<accountPwdMinChar:
            QMessageBox().warning(None, "警告", "密码必须大于6位!", QMessageBox.Ok)
            return
        elif accountPwd != accountConfirePwd:
            QMessageBox().warning(None, "警告", "两次输入密码不一致,请重新输入!",QMessageBox.Ok)
            self.password_le.setText("")
            self.confirm_pwd_le.setText("")
            return

        registerTime=datetime.datetime.now().strftime("%Y%m%d%H%M%S")



        accoutData={
            "accountType":accountType,
            "account":account,
            "accountName":accountName,
            "accountPhone":accountPhone,
            "accountPwd":self.generateMD5(accountPwd),
            "registerTime":registerTime,
            "lastLoginTime":"",
            # 0:管理人员 1:普通人员
            "isAdmin":"0" if accountType == "管理人员" else "1"
        }


        data=[]
        # 所有注册用户列表
        allAccoutList=[]
        try:
            if not os.path.exists(saveDataPath):
               data.append(accoutData)
               with open(saveDataPath ,"w+" , encoding="utf8") as fw:
                   json.dump(data,fw,ensure_ascii=False,indent=2)
               time.sleep(1)
            else:
                with open(saveDataPath,"r",encoding="utf8") as fo:
                    loadData=json.load(fo)
                    for item in loadData:
                       allAccoutList.append(item.get("account"))
                    if account in allAccoutList:
                       QMessageBox.warning(None, "警告", "您输入的账户名称已经被占用,请重新输入", QMessageBox.Ok)
                       return
                    else:
                       loadData.append(accoutData)
                with open(saveDataPath,"w+",encoding="utf8") as fw:
                    json.dump(loadData,fw,ensure_ascii=False,indent=2)
        except:
            QMessageBox.warning(None, "错误", "注册失败",QMessageBox.Ok)
        else:
            result=QMessageBox().information(None , "信息", "注册成功", QMessageBox.Ok)
            if result== QMessageBox.Ok:
                self.close()
posted @ 2020-04-26 22:01  Surpassme  阅读(260)  评论(0编辑  收藏  举报