Example #1
0
def html_oper(request, oper_type):
    response = Response.ResponseObj()
    user_id = request.GET.get('user_id')  # 用户ID
    if oper_type == 'zhengnengliang':
        pass
        # posters = request.GET.get('posters')  # 海报ID
        # posters_objs = models.Posters.objects.get(id=posters)
        # img_path = posters_objs.posters_url
        #
        # user_obj = models.Userprofile.objects.get(id=user_id)
        #
        # img_url = img_path.split(host_url)[1]  # 切除域名
        # image = Image.open(img_url).convert('RGBA')
        # color = image_color_recognition(image)  # 识别图片颜色 给出对应文字颜色
        # print('b64decode(user_obj.name)----------> ', b64decode(user_obj.name))
        # # color = (0,0,0)
        # data = {
        #     'img_path': img_path,
        #     'name': b64decode(user_obj.name),
        #     'phone': user_obj.phone_number,
        #     'set_avator': user_obj.set_avator,
        #     'color': color,
        # }
        # print('data====----> ', data)
        # return render(request, 'zhengnengliang.html', locals())
    elif oper_type == 'tuiguang':
        data = get_ent_info(user_id)
        weichat_api_obj = WeChatApi(data)
        qc_code_url = weichat_api_obj.generate_qrcode(
            {'inviter_user_id': user_id})
        print(qc_code_url)

        expire_date = (datetime.datetime.now().date() +
                       datetime.timedelta(days=30)).strftime("%Y-%m-%d")
        data = {
            'qc_code_url': qc_code_url,
            'expire_date': expire_date,
            'user_name': data.get('user_name'),  # 用户名称
            'user_set_avator': data.get('user_set_avator'),  # 头像
        }

        return render(request, 'tuiguang.html', locals())

    return JsonResponse(response.__dict__)
Example #2
0
def tuiguang(user_id):
    print(
        '-0-------------------------------------------------------------生成图片二维码'
    )

    # 获取用户数据
    data = get_ent_info(user_id)
    weichat_api_obj = WeChatApi(data)
    print('-----------------开始生成二维码=-==========================',
          datetime.datetime.today())
    qc_code_url = weichat_api_obj.generate_qrcode({'inviter_user_id':
                                                   user_id})  # 生成二维码
    print('-----------------结束生成二维码=-==========================',
          datetime.datetime.today())
    expire_date = (datetime.datetime.now().date() +
                   datetime.timedelta(days=30)).strftime("%Y-%m-%d")
    name = data.get('user_name')  # 用户名
    user_set_avator = data.get('user_set_avator')  # 用户头像
    expire_date_title = '有效期至: {}'.format(expire_date)
    img_path = os.path.join('statics', 'img',
                            randon_str() + '.png')  # 推广二维码图片 保存位置

    # 下载二维码
    linshi_qc_code_url_path = os.path.join('statics', 'img',
                                           randon_str() + '.png')
    print('---------------------------下载二维码=--------------------',
          datetime.datetime.today(), qc_code_url)
    ret = requests.get(qc_code_url)
    print('---------------------------下载二维码=--------------------',
          datetime.datetime.today())
    with open(linshi_qc_code_url_path, 'wb') as f:
        f.write(ret.content)

    # 下载头像
    linshi_user_set_avator_path = os.path.join('statics', 'img',
                                               randon_str() + '.png')
    print('---------------------------下载头像=--------------------',
          datetime.datetime.today(), user_set_avator)
    ret = requests.get(user_set_avator)
    print('---------------------------下载头像=--------------------',
          datetime.datetime.today())
    with open(linshi_user_set_avator_path, 'wb') as f:
        f.write(ret.content)

    huabu_x = 375  # 画布宽度
    huabu_y = 550  # 画布高度
    title1 = '我正在使用 微商天眼 进行'
    title2 = '客户追踪、分 享 事 业 机 会'
    title3 = '伙伴们一起来吧 !'
    title4 = '客 户 追 踪 神 器'
    title5 = '关注微商天眼公众号'

    # 新建画布纯白色           宽度↓   ↓高度    ↓ 颜色
    p = Image.new('RGBA', (huabu_x, huabu_y), (26, 172, 25))
    image_draw = ImageDraw.Draw(p)
    if 'linux' in sys.platform:  # 获取平台
        font = ImageFont.truetype('/usr/share/fonts/chinese/MSYHL.TTC', 18)
        font1 = ImageFont.truetype('/usr/share/fonts/chinese/MSYHBD.TTC', 18)
        font2 = ImageFont.truetype('/usr/share/fonts/chinese/MSYHL.TTC', 12)
        font3 = ImageFont.truetype('/usr/share/fonts/chinese/MSYHL.TTC', 15)
    else:
        font = ImageFont.truetype('/usr/share/fonts/chinese/msyhl.ttc', 18)
        font1 = ImageFont.truetype('/usr/share/fonts/chinese/msyhbd.ttc', 18)
        font2 = ImageFont.truetype('/usr/share/fonts/chinese/msyhl.ttc', 12)
        font3 = ImageFont.truetype('/usr/share/fonts/chinese/msyhl.ttc', 15)

    # circle(old_linshi_user_set_avator_path, new_linshi_user_set_avator_path) # 生成圆头像
    touxiang_img = Image.open(linshi_user_set_avator_path)
    touxiang_img = touxiang_img.resize((70, 70))
    p.paste(touxiang_img, (int((huabu_x - 50) / 2), 30))
    name_x, name_y = image_draw.textsize(name, font=font)
    image_draw.text((int((huabu_x - name_x) / 2) + 10, 100),
                    name,
                    font=font3,
                    fill=(248, 248, 242))

    # 二维码
    erweima_img = Image.open(linshi_qc_code_url_path)
    erweima_img = erweima_img.resize((100, 100))
    p.paste(erweima_img, (huabu_x - 130, huabu_y - 130))

    # logo
    logo_path = os.path.join('statics', 'img', 'promote_eye.png')
    logo_img = Image.open(logo_path)
    logo_img = logo_img.resize((100, 100))
    p.paste(logo_img, (int((huabu_x - 100) / 2), int((huabu_y - 100) / 2)))

    # 左1线(粗线)
    image_draw.line(((5, 5), (5, huabu_y - 5)), fill=(248, 248, 242), width=2)
    # 左2线(细线)
    image_draw.line(((13, 12), (13, huabu_y - 13)),
                    fill=(248, 248, 242),
                    width=1)
    # 上1线
    image_draw.line(((5, 5), (huabu_x - 5, 5)), fill=(248, 248, 242), width=2)
    # 上2线
    image_draw.line(((13, 12), (huabu_x - 13, 12)),
                    fill=(248, 248, 242),
                    width=1)
    # 右1线
    image_draw.line(((huabu_x - 5, 5), (huabu_x - 5, huabu_y - 5)),
                    fill=(248, 248, 242),
                    width=2)
    # 右2线
    image_draw.line(((huabu_x - 13, 12), (huabu_x - 13, huabu_y - 12)),
                    fill=(248, 248, 242),
                    width=1)
    # 下1线
    image_draw.line(((5, huabu_y - 5), (huabu_x - 5, huabu_y - 5)),
                    fill=(248, 248, 242),
                    width=2)
    # 下2线
    image_draw.line(((13, huabu_y - 12), (huabu_x - 13, huabu_y - 12)),
                    fill=(248, 248, 242),
                    width=1)

    title1_x, title1_y = image_draw.textsize(title1, font=font)
    title2_x, title2_y = image_draw.textsize(title2, font=font)
    title4_x, title4_y = image_draw.textsize(title4, font=font)

    touxiangweizhi = 130  # 头像预留大小
    ziti_x = (huabu_x - title1_x) / 2  # 字体左侧位置

    image_draw.text((ziti_x, touxiangweizhi),
                    title1,
                    font=font,
                    fill=(248, 248, 242))
    image_draw.text((ziti_x, touxiangweizhi + title1_y + 5),
                    title2,
                    font=font1,
                    fill=(248, 248, 242))
    image_draw.text((ziti_x, touxiangweizhi + title1_y + title2_y + 15),
                    title3,
                    font=font,
                    fill=(248, 248, 242))
    image_draw.text((25, huabu_y - 130),
                    title4,
                    font=font1,
                    fill=(248, 248, 242))
    image_draw.text((25, (huabu_y - 130) + title4_y),
                    title5,
                    font=font2,
                    fill=(248, 248, 242))

    # 有效期
    expire_date_x, expire_date_y = image_draw.textsize(expire_date_title,
                                                       font=font)
    image_draw.text((huabu_x - 130, huabu_y - (expire_date_y / 2 + 17)),
                    expire_date_title,
                    font=font2,
                    fill=(248, 248, 242))

    # 画箭头
    image_draw.line(((50 + title4_x, huabu_y - 130 + title4_y),
                     (70 + title4_x, huabu_y - 130 + title4_y)),
                    fill=(248, 248, 242),
                    width=10)
    image_draw.line(((65 + title4_x, huabu_y - 140 + title4_y),
                     (65 + title4_x, huabu_y - 120 + title4_y)),
                    fill=(248, 248, 242),
                    width=1)  # 竖线

    image_draw.line(((65 + title4_x, huabu_y - 140 + title4_y),
                     (70 + title4_x + 5, huabu_y - 130 + title4_y)),
                    fill=(248, 248, 242),
                    width=1)
    image_draw.line(((65 + title4_x, huabu_y - 120 + title4_y),
                     (70 + title4_x + 5, huabu_y - 130 + title4_y)),
                    fill=(248, 248, 242),
                    width=1)

    num_x = 65
    num_y1 = 140
    num_y2 = 120
    for i in range(10):
        image_draw.line(((num_x + title4_x, huabu_y - num_y1 + title4_y),
                         (num_x + title4_x, huabu_y - num_y2 + title4_y)),
                        fill=(248, 248, 242),
                        width=1)
        num_x += 1
        num_y1 -= 1
        num_y2 += 1

    p.save(img_path)
    os.remove(linshi_user_set_avator_path)
    os.remove(linshi_qc_code_url_path)  # 删除下载的二维码
    print(
        '-0-------------------------------------------------------------结束生成图片二维码'
    )
    return img_path, expire_date