Esempio n. 1
0
    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'})
Esempio n. 2
0
    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'})