瀏覽代碼

优化设备信息生成二维码

hujingpei 4 年之前
父節點
當前提交
a9b5a3ce7a
共有 3 個文件被更改,包括 27 次插入1 次删除
  1. 5 0
      apps/WechatApplet/models.py
  2. 1 1
      apps/tenant/device/serializers.py
  3. 21 0
      utils/wx/wechat.py

+ 5 - 0
apps/WechatApplet/models.py

@@ -89,3 +89,8 @@ class WechatApplet(models.Model):
         page = 'pages/index/index'
         filename = WeChat.getWXAppCode(self.getAccessToken(),page, company_no)
         return filename
+
+    def getDeviceCode(self, device_id):
+        page = 'pages/index/index'
+        filename = WeChat.getDeviceCode(self.getAccessToken(),page, device_id)
+        return filename

+ 1 - 1
apps/tenant/device/serializers.py

@@ -19,7 +19,7 @@ class DeviceSerializer(serializers.ModelSerializer):
 
         id = instance.id
         applet = WechatApplet.objects.filter(authorizer_appid=settings.WEAPP['appid']).first()
-        filename = applet.getWXAppCode(id)
+        filename = applet.getDeviceCode(id)
         instance.wxapp_img = "{0}{1}".format(settings.MEDIA_URL,filename)
         instance.save()
 

+ 21 - 0
utils/wx/wechat.py

@@ -246,6 +246,27 @@ class WeChat(object):
             destination.write(result.content)
         return filename
 
+    @staticmethod
+    def getDeviceCode(access_token, page, device_id):
+        '''获取设备二维码'''
+        url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={}'.format(access_token)
+        data = {"scene": "device_id={}".format(device_id),
+                "width": 1280,
+                "line_color": {"r": 43, "g": 162, "b": 69},  # 自定义颜色
+                "is_hyaline": True}
+        result = requests.post(url, json=data)
+
+        upload_path = PathAndRename("device/")
+        filename = "%s%s.png" % (
+            upload_path.path,
+            device_id,
+        )
+        filename = filename.lower()
+        full_filename = "%s%s" % (settings.MEDIA_ROOT, filename)
+        with open(full_filename, 'wb') as destination:
+            destination.write(result.content)
+        return filename
+
     @staticmethod
     def releaseCode(access_token):
         '''已审核代码发布'''