Browse Source

qrcode edit by ming

ming 2 years ago
parent
commit
b8a9961b8c
1 changed files with 15 additions and 71 deletions
  1. 15 71
      app/api/api_v1/endpoints/line.py

+ 15 - 71
app/api/api_v1/endpoints/line.py

@@ -66,6 +66,7 @@ def check_account(event):
 
     # 都存在db的話
     if result1:
+        print('already in db')
         db.close()
         settings.line_bot_api.reply_message(
             event.reply_token,
@@ -81,18 +82,22 @@ def check_account(event):
         }
         try:
             profile = settings.line_bot_api.get_profile(real_user_id)
-            print(profile['displayName'])
+            #print(profile['displayName'])
         except LineBotApiError as e:
             pass
         user_id = event.source.user_id
         data = 'uid=' + user_id
         r = requests.post(url=url, headers=headers, data=data)
-
-        # extract the account address
-        dict_str = json.loads(r.text)
-        user_account = dict_str['account']
-        user_address = user_account['address']
-
+        try:
+            dict_str = json.loads(r.text)
+            user_account = dict_str['account']
+            user_address = user_account['address']
+        except:
+            get_acc_url = 'https://nft-api-staging.joyso.io/api/v1/accounts/'+user_id
+            r = requests.get(url=get_acc_url,headers=headers)
+            dict_str = json.loads(r.text)
+            user_account = dict_str['account']
+            user_address = user_account['address']
         # generate qr code from user id
         qr = qrcode.QRCode(
             version=1,
@@ -106,7 +111,7 @@ def check_account(event):
         img_qr.save(filename)
 
         # add to db
-        data = dict(userid=real_user_id, useraddress=user_address)
+        data = dict(userid=real_user_id, useraddress=user_address,is_active=1,created_at=dt.datetime.now(),updated_at=dt.datetime.now(),hashed_password='',email='',account='',is_superuser=0)
         table.insert(data)
 
         db.close()
@@ -129,69 +134,7 @@ async def callback(request: fastapi.Request):
 @settings.handler.add(FollowEvent)
 def handle_follow(event):
     # get user id when follow
-    real_user_id = event.source.user_id
-
-    # db connect and search
-    db = dataset.connect(
-        'mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4'
-    )
-    table = db['users']
-    result1 = table.find_one(userid=real_user_id)
-
-    # 都存在db的話
-    if result1:
-        db.close()
-        settings.line_bot_api.reply_message(
-            event.reply_token,
-            TextSendMessage(text='很高興再見到您!'))
-
-    # 建立全新使用者
-    else:
-        # create user account api
-        url = 'https://nft-api-staging.joyso.io/api/v1/accounts'
-        headers = {
-            'Authorization':
-            'Basic bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'
-        }
-        try:
-            profile = settings.line_bot_api.get_profile(real_user_id)
-            print(profile['displayName'])
-        except LineBotApiError as e:
-            pass
-        # setup for temp use (unique id)
-        rand_num = str(randrange(99999))
-        # user_id = event.source.user_id + rand_num
-        user_id = event.source.user_id
-        data = 'uid=' + user_id
-        r = requests.post(url=url, headers=headers, data=data)
-
-        # extract the account address
-        dict_str = json.loads(r.text)
-        user_account = dict_str['account']
-        user_address = user_account['address']
-
-        # generate qr code from user id
-        qr = qrcode.QRCode(
-            version=1,
-            box_size=10,
-            border=5)
-        qr.add_data(user_address)
-        qr.make(fit=True)
-        img_qr = qr.make_image(fill='black', back_color='white')
-        filename = "/var/www/ArkCard-Linebot/ArkCard-web/qrcode/" + \
-                   real_user_id + '.png'
-        img_qr.save(filename)
-
-        # add to db
-        data = dict(userid=real_user_id, useraddress=user_address)
-        table.insert(data)
-
-        db.close()
-
-        settings.line_bot_api.reply_message(
-            event.reply_token,
-            TextSendMessage(text='歡迎加入好友'))
-
+    check_account(event)
 
 # message handler
 @settings.handler.add(MessageEvent, message=TextMessage)
@@ -212,6 +155,7 @@ def message(event):
                 template=button_template_message))
 
     elif '我要接收' in event.message.text:
+
         check_account(event)
         button_template_message = ButtonsTemplate(
             title=' ',