Esempio n. 1
0
def ordercmd():
    if not is_admin():return E("没有权限") 
    id = flask.request.json['id']
    cmd = flask.request.json['cmd']
    o = Order.objects.with_id(id)
    if cmd=='print':
        import route_print
        route_print.printOrder(o.id)
        return A("已经加入打印队列")
    if cmd=='noticeuser':
        weimsg = route_weixin.renderOrder(o)
        r,err = weixin.send_weixin(o.openid, weimsg)
        if r:
            return A("微信通知发送成功")
        elif err==45015:
            o.detail.append(tool.now()+" "+"微信通知发送不成功")
            return A("由于用户没有关注我们的微信号或者长时间没有操作,微信通知发送不成功.")
        else:
            log.error("服务器错误,请联系管理员"+o.to_json()+str(r))
            return E("微信通知发送不成功,错误编号:"+str(err)) 
    if cmd=='noticeshop':
        ok,msg = sendToShop(id)
        if ok: return A(msg)
        else :return E(msg)
    if cmd=='smsnoticeshop':
        tel = o.info.tel
        ok,msg = weixin.send_sms(tel,"你的店铺有新的订单,请登录微信查看。")
        if ok :return  A(msg)
        else: return E(msg)
    if cmd=="cancel":
        o.state="fail"
        o.save()
        return A("取消订单成功")
    return E("no such cmd")
Esempio n. 2
0
def send_weixin(openid,text,times=0,save=True):
    if times>3:
        log.error("weixin times to many")
        return False,"weixin times to many"
    w = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s"%token_cache.get_token()
    data = {
        "touser":openid,
        "msgtype":"text",
        "text":{
            "content":text
        }
    }
    r = requests.post(w,json.dumps(data,ensure_ascii=False).encode("utf8")).text
    r = json.loads(r)
    q = WeixinQueue()
    q.openid = openid
    q.text = text
    if r['errcode'] == 40001:
        token_cache.refresh_token()
        return send_weixin(openid, text,times+1)
    if r['errcode']!=0:
        q.result = "pending"
        q.errorId = r['errcode']
        if save: q.save()
        return False,r['errcode']
    q.result = "success"
    if save :q.save()
    return True,0
Esempio n. 3
0
def ordercmd():
    if not is_admin(): return E("没有权限")
    id = flask.request.json['id']
    cmd = flask.request.json['cmd']
    o = Order.objects.with_id(id)
    if cmd == 'print':
        import route_print
        route_print.printOrder(o.id)
        return A("已经加入打印队列")
    if cmd == 'noticeuser':
        weimsg = route_weixin.renderOrder(o)
        r, err = weixin.send_weixin(o.openid, weimsg)
        if r:
            return A("微信通知发送成功")
        elif err == 45015:
            o.detail.append(tool.now() + " " + "微信通知发送不成功")
            return A("由于用户没有关注我们的微信号或者长时间没有操作,微信通知发送不成功.")
        else:
            log.error("服务器错误,请联系管理员" + o.to_json() + str(r))
            return E("微信通知发送不成功,错误编号:" + str(err))
    if cmd == 'noticeshop':
        ok, msg = sendToShop(id)
        if ok: return A(msg)
        else: return E(msg)
    if cmd == 'smsnoticeshop':
        tel = o.info.tel
        ok, msg = weixin.send_sms(tel, "你的店铺有新的订单,请登录微信查看。")
        if ok: return A(msg)
        else: return E(msg)
    if cmd == "cancel":
        o.state = "fail"
        o.save()
        return A("取消订单成功")
    return E("no such cmd")
Esempio n. 4
0
def ordermodifystate():
    if not is_admin():return E("没有权限") 
    try:
        id = flask.request.json['id']
        state = flask.request.json['state']
        o = Order.objects.with_id(id)
        o.state = state
        o.save()
        return  A("修改成功")
    except Exception as e:
        log.error(e)
        return E("修改失败")
Esempio n. 5
0
def ordermodifystate():
    if not is_admin(): return E("没有权限")
    try:
        id = flask.request.json['id']
        state = flask.request.json['state']
        o = Order.objects.with_id(id)
        o.state = state
        o.save()
        return A("修改成功")
    except Exception as e:
        log.error(e)
        return E("修改失败")
Esempio n. 6
0
def send_weixin(openid, text, times=0, save=True):
    if times > 3:
        log.error("weixin times to many")
        return False, "weixin times to many"
    w = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s" % token_cache.get_token(
    )
    data = {"touser": openid, "msgtype": "text", "text": {"content": text}}
    r = requests.post(w,
                      json.dumps(data, ensure_ascii=False).encode("utf8")).text
    r = json.loads(r)
    q = WeixinQueue()
    q.openid = openid
    q.text = text
    if r['errcode'] == 40001:
        token_cache.refresh_token()
        return send_weixin(openid, text, times + 1)
    if r['errcode'] != 0:
        q.result = "pending"
        q.errorId = r['errcode']
        if save: q.save()
        return False, r['errcode']
    q.result = "success"
    if save: q.save()
    return True, 0
Esempio n. 7
0
def submitorder():

    if not flask.session.has_key('openid'):
        return E("找不到你的微信用户信息,请点击右上角关注我们的服务号“爱上天使街”")
    # flask.session['openid']='oqxGSuCUGMmLWehQxCbg9O-XqPzk'
    # flask.session['weixinname']='weixinname'
    c = flask.request.json
    o = Order()
    o.allcount = c['allcount']
    o.allprice = c['allprice']
    o.addr = c['addr']
    o.msg = c['msg']
    o.name = c['name']
    o.shopId = c['shopId']
    o.tel = c['tel']
    o.openid = flask.session['openid']
    o.weixinname = flask.session['weixinname']
    w = Shop.objects.with_id(c['shopId'])
    # 限制下单间隔时间
    oo = Order.objects(openid=o.openid).order_by("-_id").first()
    if oo:
        if (datetime.datetime.now() - oo.time).total_seconds() < 10:
            return E("下单太频繁,请稍后再试")
    for i in w.goods:
        if str(i.id) in c['goods']:
            b = Buy()
            b.category = i.category
            b.name = i.name
            b.price = i.price
            b.point = i.point 
            b.id = i.id
            b.count = int(c['goods'][str(i.id)])
            o.goods.append(b)

    o.info = w.info
    o.state = "verify"

    allcount = 0
    allprice = 0
    for i in o.goods:
        allcount += i.count
        allprice += i.price * i.count
    if allcount!=o.allcount or allprice!=o.allprice:
        return E("订单验证错误,可能在选购商品的时候店家修改了商品信息,建议重新从微信菜单进入购买")
    if allprice <=0:
        return E("订单金额太少")
    import json
    import random
    o.pid = len(Order.objects)*10+random.randrange(10)
    try:
        o.validate()
    except ValidationError as e:
        log.error(str(e))
        return E("你填写的信息不合法")
    o.detail.append(tool.now() + "成功下单,系统正在处理中")
    o.save()
    w.info.salesVolume += 1
    w.save()
    route_print.printOrder(o.id)
    weimsg = route_weixin.renderOrder(o)
    r,err = weixin.send_weixin(o.openid, weimsg)
    if r:
        o.state = 'pending'
        thread.start_new_thread(sendToShop, (o.id,) )
        o.save()
        return A("你的订单已经成功提交,订单详情已经发送的你的微信上,请查收")
    elif err==45015:
        o.state = "waiting"
        o.detail.append(tool.now()+" "+"微信通知发送不成功")
        o.save()
        return A("你的订单已经成功提交,由于你没有关注我们的微信号或者长时间没有操作,请关注我们的微信号并点击任意菜单。")
    else:
        log.error("服务器错误,请联系管理员"+o.to_json()+"\nweixinre:"+str(err))
        return E("服务器错误,请联系管理员") 
Esempio n. 8
0
def submitorder():

    if not flask.session.has_key('openid'):
        return E("找不到你的微信用户信息,请点击右上角关注我们的服务号“爱上天使街”")
    # flask.session['openid']='oqxGSuCUGMmLWehQxCbg9O-XqPzk'
    # flask.session['weixinname']='weixinname'
    c = flask.request.json
    o = Order()
    o.allcount = c['allcount']
    o.allprice = c['allprice']
    o.addr = c['addr']
    o.msg = c['msg']
    o.name = c['name']
    o.shopId = c['shopId']
    o.tel = c['tel']
    o.openid = flask.session['openid']
    o.weixinname = flask.session['weixinname']
    w = Shop.objects.with_id(c['shopId'])
    # 限制下单间隔时间
    oo = Order.objects(openid=o.openid).order_by("-_id").first()
    if oo:
        if (datetime.datetime.now() - oo.time).total_seconds() < 10:
            return E("下单太频繁,请稍后再试")
    for i in w.goods:
        if str(i.id) in c['goods']:
            b = Buy()
            b.category = i.category
            b.name = i.name
            b.price = i.price
            b.point = i.point
            b.id = i.id
            b.count = int(c['goods'][str(i.id)])
            o.goods.append(b)

    o.info = w.info
    o.state = "verify"

    allcount = 0
    allprice = 0
    for i in o.goods:
        allcount += i.count
        allprice += i.price * i.count
    if allcount != o.allcount or allprice != o.allprice:
        return E("订单验证错误,可能在选购商品的时候店家修改了商品信息,建议重新从微信菜单进入购买")
    if allprice <= 0:
        return E("订单金额太少")
    import json
    import random
    o.pid = len(Order.objects) * 10 + random.randrange(10)
    try:
        o.validate()
    except ValidationError as e:
        log.error(str(e))
        return E("你填写的信息不合法")
    o.detail.append(tool.now() + "成功下单,系统正在处理中")
    o.save()
    w.info.salesVolume += 1
    w.save()
    route_print.printOrder(o.id)
    weimsg = route_weixin.renderOrder(o)
    r, err = weixin.send_weixin(o.openid, weimsg)
    if r:
        o.state = 'pending'
        thread.start_new_thread(sendToShop, (o.id, ))
        o.save()
        return A("你的订单已经成功提交,订单详情已经发送的你的微信上,请查收")
    elif err == 45015:
        o.state = "waiting"
        o.detail.append(tool.now() + " " + "微信通知发送不成功")
        o.save()
        return A("你的订单已经成功提交,由于你没有关注我们的微信号或者长时间没有操作,请关注我们的微信号并点击任意菜单。")
    else:
        log.error("服务器错误,请联系管理员" + o.to_json() + "\nweixinre:" + str(err))
        return E("服务器错误,请联系管理员")