def goPartlogin(self): #登录 code = self.REQUEST.get('code', '') if code == '' or code == 'None' or code == 'undefined': return self.jsons({ 'code': 300, 'data': { 'msg': self.error_code[300].format('code') } }) mall = self.oMALL.get(self.subusr_id) if mall == {}: return self.jsons({'code': 404, 'msg': '请到店铺设置填写小程序设置'}) app_id = mall['appid'] secret = mall['secret'] api = wx_minapp_login(app_id, secret) session_info = api.get_session_info(code=code) if session_info.get('errcode'): return self.jsons({ 'code': 602, 'msg': '微信用户信息解密错误请检查appid和secret信息', 'data': session_info.get('errmsg') }) open_id = session_info['openid'] user = self.oUSER.get(self.subusr_id, open_id) #self.print_log('subusr_id:%s,open_id:%s'%(self.subusr_id,open_id),'%s'%self.oUSER.get(self.subusr_id,open_id)) if user == {}: sqll = """select id from wechat_mall_user where open_id=%s and usr_id=%s and COALESCE(del_flag,0)=0 """ lT, iN = self.db.select(sqll, [open_id, self.subusr_id]) if iN == 0: return self.jsons({ 'code': 10000, 'msg': self.error_code[10000] }) wechat_user_id = lT[0][0] else: wechat_user_id = user['id'] try: ip = self.objHandle.headers["X-Real-IP"] except: ip = self.objHandle.remote_addr sqli = "update wechat_mall_user set utime=now(),last_login_ip=%s where id =%s and usr_id=%s " self.db.query(sqli, [ip, wechat_user_id, self.subusr_id]) token = self.create_token(self.subusr_id, open_id, wechat_user_id) self.oUSER.update(self.subusr_id, wechat_user_id) return self.jsons({ 'code': 0, 'data': { 'token': token, 'uid': wechat_user_id } })
def goPartregister(self): #注册 code = self.REQUEST.get('code', '') encrypted_data = self.REQUEST.get('encryptedData', '') rawData = self.REQUEST.get('rawData', '') iv = self.REQUEST.get('iv', '') signature = self.REQUEST.get('signature', '') if not code or code == '' or code == 'None' or code == 'undefined': return self.jsons({ 'code': 300, 'msg': self.error_code[300].format('code') }) if not encrypted_data or encrypted_data == '' or encrypted_data == 'None' or encrypted_data == 'undefined': return self.jsons({ 'code': 300, 'msg': self.error_code[300].format('encryptedData') }) if not iv or iv == '' or iv == 'None' or iv == 'undefined': return self.jsons({ 'code': 300, 'msg': self.error_code[300].format('iv') }) if not rawData or rawData == '' or rawData == 'None' or rawData == 'undefined': return self.jsons({ 'code': 300, 'msg': self.error_code[300].format('rawData') }) if not signature or signature == '' or signature == 'None' or signature == 'undefined': return self.jsons({ 'code': 300, 'msg': self.error_code[300].format('signature') }) # sql = "select appid,secret from mall where usr_id=%s" # l, t = self.db.select(sql,self.subusr_id) # # if t == 0: # return self.jsons({'code': 404, 'msg': '请到后台填写‘微信设置’'}) # app_id = l[0][0] # secret = l[0][1] mall = self.oMALL.get(self.subusr_id) if mall == {}: return self.jsons({'code': 404, 'msg': '请到店铺设置填写小程序设置'}) app_id = mall['appid'] secret = mall['secret'] try: api = wx_minapp_login(app_id, secret) session_info = api.get_session_info(code=code) if session_info.get('errcode'): return self.jsons({ 'code': 602, 'msg': '微信用户信息解密错误请检查appid和secret信息', 'data': session_info.get('errmsg') }) session_key = session_info.get('session_key') crypt = WXBizDataCrypt(app_id, session_key) # 解密得到 用户信息 user_info = crypt.decrypt(encrypted_data, iv) except: return self.jsons({ 'code': 602, 'msg': '微信用户信息解密错误请检查appid和secret信息' }) try: register_ip = self.objHandle.headers["X-Real-IP"] except: register_ip = self.objHandle.remote_addr now = datetime.datetime.now() delta = datetime.timedelta(days=7) n_days = now + delta up_time = n_days.strftime('%Y-%m-%d %H:%M:%S') data = { 'cname': user_info['nickName'], 'open_id': user_info['openId'], 'gender': user_info['gender'], 'languages': user_info['language'], 'country': user_info['country'], 'province': user_info['province'], 'city': user_info['city'], 'avatar_url': user_info['avatarUrl'], 'register_ip': register_ip, 'usr_id': self.subusr_id, 'ctime': self.getToday(9), 'up_time': up_time, 'del_flag': 0 } openId = user_info['openId'] user = self.oUSER.get(self.subusr_id, user_info['openId']) if user == {}: sqll = """select id from wechat_mall_user where open_id=%s and usr_id=%s and coalesce(del_flag,0)=0 """ lT, iN = self.db.select(sqll, [openId, self.subusr_id]) if iN > 0: return self.jsons({'code': 0, 'msg': 'success'}) self.db.insert('wechat_mall_user', data) sqll = """select id from wechat_mall_user where open_id=%s and usr_id=%s and coalesce(del_flag,0)=0 """ lT, iN = self.db.select(sqll, [openId, self.subusr_id]) if iN == 0: return self.jsons({'code': 10000, 'msg': self.error_code[10000]}) wid = lT[0][0] self.oUSER.update(self.subusr_id, wid) sql = "select coalesce(new_score,0) from shop_set where usr_id=%s" l, t = self.db.select(sql, self.subusr_id) if t > 0: #注册送积分 new_score = l[0][0] if new_score > 0: sql = "update wechat_mall_user set score=coalesce(score,0)+%s where open_id=%s and usr_id=%s" self.db.query(sql, [new_score, openId, self.subusr_id]) self.oUSER.update(self.subusr_id, wid) self.oOPENID.update(wid) return self.jsons({'code': 0, 'msg': 'success'})
def goPartlogin(self):#登录 code=self.REQUEST.get('code','') if code=='' or code=='None': return self.jsons({'code': 300, 'data': {'msg': self.error_code[300].format('code')}}) sql="select appid,secret from mall where usr_id=%s"%self.subusr_id l,t=self.db.select(sql) if t==0: return self.jsons({'code': 404, 'msg': '请到后台填写‘微信设置’'}) app_id = l[0][0] secret = l[0][1] api = wx_minapp_login(app_id, secret) session_info = api.get_session_info(code=code) if session_info.get('errcode'): return self.jsons({'code': -1, 'msg': self.error_code[-1], 'data': session_info.get('errmsg')}) open_id = session_info['openid'] session_key=session_info['session_key'] sqll = "select id from wechat_mall_user where open_id='%s' and usr_id=%s and COALESCE(del_flag,0)=0" % (open_id,self.subusr_id) lT,iN=self.db.select(sqll) if iN==0: return self.jsons({'code': 10000, 'msg': self.error_code[10000]}) wechat_user_id=lT[0][0] try: ip=self.objHandle.headers["X-Real-IP"] except: ip = self.objHandle.remote_addr sqli=" update wechat_mall_user set last_login=now(),ip='%s' where id =%s and usr_id=%s "%(ip,lT[0][0],self.subusr_id) self.db.query(sqli) sqlt="select id,token from wechat_mall_access_token where open_id ='%s' and usr_id=%s"%(open_id,self.subusr_id) li,i=self.db.select(sqlt) if i==0: token=self.create_token(self.subusr_id, open_id, wechat_user_id) # token_max_hours = 2 # s = Serializer(self.SECRET_KEY, expires_in=token_max_hours * 3600) # timestamp = time.time() # temp = s.dumps({'openid': open_id,'iat': timestamp}) # token=temp.decode('ascii')#原来转为字符串 #print(token) #ken = s.loads(token)#{'openid': open_id} data={ 'open_id': open_id, 'session_key': session_key, 'token':token, 'usr_id':self.subusr_id, 'create_date': self.getToday(9) } self.db.insert('wechat_mall_access_token',data) sqlt = "select id,token from wechat_mall_access_token where open_id ='%s' and usr_id=%s" % (open_id, self.subusr_id) l,t = self.db.select(sqlt) else: # token_max_hours = 2 # s = Serializer(self.SECRET_KEY, expires_in=token_max_hours * 3600) # timestamp = time.time() # temp = s.dumps({'openid': open_id, 'iat': timestamp}) # token = temp.decode('ascii') # 原来转为字符串 token = self.create_token(self.subusr_id, open_id, wechat_user_id) data = { 'open_id': open_id, 'session_key': session_key, 'token': token, 'create_date': self.getToday(9) } self.db.update('wechat_mall_access_token', data, "open_id ='%s' and usr_id=%s"%(open_id,self.subusr_id)) sqlt = "select id,token from wechat_mall_access_token where open_id ='%s' and usr_id=%s" % ( open_id, self.subusr_id) l, t = self.db.select(sqlt) try: access_token=l[0][1] uid=wechat_user_id except: access_token='None' uid=0 return self.jsons({'code':0,'data':{'token': access_token,'uid':uid}})
def goPartregister(self): #注册 #print('login,llllllllllllll') code = self.REQUEST.get('code','') encrypted_data = self.REQUEST.get('encryptedData','') rawData = self.REQUEST.get('rawData','') iv = self.REQUEST.get('iv','') signature = self.REQUEST.get('signature','') if not code or code=='' or code=='None': return self.jsons({'code': 300, 'msg': self.error_code[300].format('code')}) if not encrypted_data or encrypted_data =='' or encrypted_data =='None': return self.jsons({'code': 300, 'msg': self.error_code[300].format('encryptedData')}) if not iv or iv=='' or iv=='None': return self.jsons({'code': 300, 'msg': self.error_code[300].format('iv')}) if not rawData or rawData=='' or rawData=='None': return self.jsons({'code': 300, 'msg': self.error_code[300].format('rawData')}) if not signature or signature=='' or signature=='None': return self.jsons({'code': 300, 'msg': self.error_code[300].format('signature')}) sql = "select appid,secret from mall where usr_id=%s" % self.subusr_id l, t = self.db.select(sql) if t == 0: return self.jsons({'code': 404, 'msg': '请到后台填写‘微信设置’'}) app_id = l[0][0] secret = l[0][1] api=wx_minapp_login(app_id,secret) session_info = api.get_session_info(code=code) session_key = session_info.get('session_key') crypt = WXBizDataCrypt(app_id, session_key) # 解密得到 用户信息 user_info = crypt.decrypt(encrypted_data, iv) try: register_ip=self.objHandle.headers["X-Real-IP"] except: register_ip = self.objHandle.remote_addr data={ 'name': user_info['nickName'], 'open_id': user_info['openId'], 'gender': user_info['gender'], 'language': user_info['language'], 'country': user_info['country'], 'province': user_info['province'], 'city': user_info['city'], 'avatar_url': user_info['avatarUrl'], 'register_ip': register_ip, 'usr_id':self.subusr_id, 'create_date':self.getToday(9), 'del_flag':0 } sqll = "select id from wechat_mall_user where open_id='%s' and usr_id=%s" % (user_info['openId'], self.subusr_id) #print(sqll,'6666666666') lT, iN = self.db.select(sqll) if iN == 0: self.db.insert('wechat_mall_user', data) sqll = "select id from wechat_mall_user where open_id='%s'and usr_id=%s and COALESCE(del_flag,0)=0" % (user_info['openId'], self.subusr_id) l,t = self.db.select(sqll) if t>0:#如果注册送积分增加积分 jf="select score from score_send where usr_id=%s and COALESCE(del_flag,0)=0 and code=1 "% self.subusr_id f,g=self.db.select(jf) if g>0: now_amount=int(f[0][0]) sqs="select now_amount from integral_log where usr_id=%s and wechat_user_id=%s order by id desc"%(self.subusr_id,l[0][0]) k,h=self.db.select(sqs) if h>0: now_amount+=int(k[0][0]) ql="""insert into integral_log(usr_id,wechat_user_id,type,typestr,in_out,inoutstr,amount,now_amount,cid,ctime) values(%s,%s,%s,'%s',%s,'%s',%s,%s,%s,now()) """%(self.subusr_id,l[0][0],0,'注册赠送',0,'收入',int(f[0][0]),now_amount,l[0][0]) self.db.query(ql) return self.jsons({'code': 0, 'msg': 'success'}) self.db.update('wechat_mall_user', data,'id=%s'%lT[0][0]) sqll = "select id from wechat_mall_user where open_id='%s'and usr_id=%s and COALESCE(del_flag,0)=0" % (user_info['openId'], self.subusr_id) #print(sqll) l, t = self.db.select(sqll) if t > 0:# 如果注册送积分增加积分 jf = "select score from score_send where usr_id=%s and COALESCE(del_flag,0)=0 and code=1 " % self.subusr_id f, g = self.db.select(jf) if g > 0: now_amount = int(f[0][0]) sqs = "select now_amount from integral_log where usr_id=%s and wechat_user_id=%s order by id desc" % (self.subusr_id, l[0][0]) k, h = self.db.select(sqs) if h > 0: now_amount += int(k[0][0]) ql = """insert into integral_log(usr_id,wechat_user_id,type,typestr,in_out,inoutstr,amount,now_amount,cid,ctime) values(%s,%s,%s,'%s',%s,'%s',%s,%s,%s,now()) """ % (self.subusr_id, l[0][0], 0, '注册赠送', 0, '收入', int(f[0][0]), now_amount, l[0][0]) self.db.query(ql) return self.jsons({'code': 0, 'msg': 'success'})
def __init__(self, request): self.objHandle = request # if self.objHandle.method == 'POST': # self.REQUEST = self.objHandle.form # else: # self.REQUEST = self.objHandle.args self.REQUEST = request.values self.ATTACH_ROOT = ATTACH_ROOTR self.db = db self.jsons = jsonify self.usr_id = 0 self.usr_id_p = 0 self.app_id = '' self.secret = '' self.classpath = 'sell' self.SECRET_KEY = '5bf030dbb13422031ea802a9ab75900a' self.error_code = { -1: u'服务器内部错误', 0: u'接口调用成功', 403: u'禁止访问', 405: u'错误的请求类型', 501: u'数据库错误', 502: u'并发异常,请重试', 600: u'缺少参数', 601: u'无权操作:缺少 token', 602: u'签名错误', 700: u'暂无数据', 701: u'该功能暂未开通', 702: u'资源余额不足', 901: u'登录超时', 300: u'缺少{}参数', 400: u'域名错误', 401: u'该域名已删除', 402: u'该域名已禁用', 404: u'暂无数据', 10000: u'微信用户未注册', 'ok': 'success' } # 获取网址请求过来的常用参数 self.viewid = self.GP('viewid', 'home') # viewid值 self.part = self.GP('part', 'begin') self.appid = self.GP('appid', '') self.malitoken = self.objHandle.headers.get('malitoken', '') if self.malitoken == '': self.malitoken = self.REQUEST.get('malitoken', '') # token值 if self.malitoken and self.part not in ['login', 'Gologin']: dR = self.checktoken(self.malitoken) if dR['code'] == 0: self.usr_id = int(dR['usr_id']) self.usr_id_p = self.get_usr_id_p(self.usr_id) # ***************************************************************** self.oSHOP = oSHOP self.oUSER = oUSER self.oMALL = oMALL self.oQINIU = oQINIU #self.oKUAIDI = oKUAIDI self.oGOODS = oGOODS self.oGOODS_D = oGOODS_D self.oORDER_SET = oORDER_SET self.oGOODS_N = oGOODS_N self.oGOODS_G = oGOODS_G self.oOPENID = oOPENID self.oSHOP_T = oSHOP_T self.oCATEGORY = oCATEGORY self.oUSERS = oUSERS self.oGOODS_SELL = oGOODS_SELL self.oTOLL = oTOLL self.oGOODS_PT = oGOODS_PT self.oGOODS_DPT = oGOODS_DPT self.oPT_GOODS = oPT_GOODS # ########################################### self.app_id = self.oMALL.get(0).get('appid', '') self.secret = self.oMALL.get(0).get('secret', '') self.api = wx_minapp_login(self.app_id, self.secret)