前端

错误1 发送没有反应,觉得是地址不对,老师上课有改,但是找不到。

查了以前的是mui.post处,但是是window.serv,本以为是系统自动检测,

但是也不可能是监测到9527吧,

从网上https://www.cnblogs.com/wen-kang/p/10778812.html博客里找到:

为了避免hbuilder中的js频繁的给后端发送AJAX,更换ip,可以将ip设置为全部变量,在引用文件的js文件添加代码如下:

window.serv="http://192.168.1.114:9527";
extras:{
      .....//自定义扩展参数,可以用来处理页面间传值
    },

前端的音乐播放的哪里的资源?

数据库里没有给传过去MP3!

<button type="button" class="mui-btn mui-btn-green mui-btn-block" id="resume">继续</button>
var Sdata = null;					#通过plus.webview可获取应用界面管理对象。
Sdata = plus.webview.currentWebview();	#获取当前窗口的WebviewObject对象
 
document.getElementById("title").innerText = "正在播放       " + Sdata.title;
ap = plus.audio.createPlayer(window.music_serv + Sdata.music);	# 括号里的‘xx.MP3’从哪里来	‘http://192.168.12.70:9527/get_music/’
ap.play();

document.getElementById('resume').addEventListener('tap', function() {
				ap.resume();	
			});

mui.js 里有配置参数

window.serv = "http://192.168.12.70:9527";
window.ws_serv = "ws://192.168.12.70:9527/app/";
window.image_serv = window.serv + "/get_cover/";
window.music_serv = window.serv + "/get_music/";	

播放的是 这里的 send_file

@content_bp.route('/get_music/<filename>',methods=['GET'])
def get_music(filename):

    music_path = os.path.join(MUSIC_PATH, filename)
    return send_file(music_path)	

数据如下:应该是data.DATA data是 auto.login 传过来的数据 fuc(data)

 [{"_id":"5d32b0839c057a77211fad55","cover":"c6b68f04-9a4c-46b6-9560-0bb31c38c7b7.jpg","music":"c6b68f04-9a4c-46b6-9560-0bb31c38c7b7.mp3","title":"新年恰恰","zhuanji":"【一千零一夜】经典儿歌"},..........{"_id":"5d32b0839c057a77211fad68","cover":"17e969d0-6e37-4438-a877-e2240e191984.jpg","music":"17e969d0-6e37-4438-a877-e2240e191984.

stada.music : "music":"c6b68f04-9a4c-46b6-9560-0bb31c38c7b7.mp3"

sdata的值 plus.webview.currentWebview();?不是等于创建一个窗口吗?

{"__view_array__":[],"__IDENTITY__":"NWindow","__uuid__":"NWindow91563607904411","__callbacks__":{"back":[null],"menu":[null]},"__callback_id__":"plus41563607905521","id":"player.html","music":"c6b68f04-9a4c-46b6-9560-0bb31c38c7b7.mp3","title":"新年恰恰","cover":"c6b68f04-9a4c-46b6-9560-0bb31c38c7b7.jpg","zhuanji":"【一千零一夜】经典儿歌","_id":"5d32b0839c057a77211fad55"} at player.html:58

访问这个网址,就可以播放音乐,后端开启的状态下。这里面存了send_file的MP3

http://192.168.12.70:9527/get_music/e87e08f5-28e9-493f-8f5a-15ef02d3abc6.mp3

所以

ap = plus.audio.createPlayer(window.music_serv + Sdata.music); 'h://..xx.mp3'

就可以播放了(没有点击事件,只不过是后端传到网络上的资源

Sdata 
console.log('sdata:', Sdata)--->  sdata: at player.html:49  是这个? 

注册

@user_bp.route("/reg", methods=["POST"])
def reg():
    user_info = request.form.to_dict()
    print('注册')
    print(user_info)
    user_info["avatar"] = "baba.jpg" if user_info.get("gender") == "2" else "mama.jpg"
    user_info["bind_toys"] = []
    user_info["friend_list"] = []

    MDB.Users.insert_one(user_info)
    return jsonify({"CODE":0,"MSG":"注册成功","DATA":{}})

登录

@user_bp.route("/login", methods=["POST"])
def login():
    print('登录')
    print(request.form, type(request.form))
    user_info = request.form.to_dict()
    print(user_info.get('_id'))
    print(user_info, type(user_info))
    user_info_dict = MDB.Users.find_one(user_info)

    user_info_dict["_id"] = str(user_info_dict.get("_id"))
    print('登录')

    RET["CODE"] = 0
    RET["MSG"] = f"欢迎{user_info_dict.get('nickname')}登录"
    RET["DATA"] = user_info_dict

    return jsonify(RET)

自动登录

@user_bp.route("/auto_login",methods=["POST"])
def auto_login():
    print('自动登录')
    user_info = request.form.to_dict()
    print('原来', user_info.get('_id'), type(user_info.get('_id')))  # 原来 5d32b21dbd7107c6ea54c890 <class 'str'>
    user_info["_id"] = ObjectId(user_info.get("_id"))
    print('之后', user_info['_id'], type(user_info['_id']))   # 之后 5d32b21dbd7107c6ea54c890 <class 'bson.objectid.ObjectId'>
    user_info_dict = MDB.Users.find_one(user_info)  # 字符串怎么不行呢?  因为是_id ,之前的是自动生成的,这一次是给他值 
                                            # 默认格式是 '_id' : ObjectId('5d32b21dbd7107c6ea54c890') 而不是 5d32b21dbd7107c6ea54c890 的格式
    print('自动登录')

    user_info_dict["_id"] = str(user_info_dict.get("_id"))

    RET["CODE"] = 0
    RET["MSG"] = f"欢迎{user_info_dict.get('nickname')}登录"
    RET["DATA"] = user_info_dict
    print(RET)
    return jsonify(RET)

Barcode : 如果存在多个相同标识的扫码识别控件对象,则返回第一个查找到的扫码识别控件对象。 如果不存在指定标识的扫码识别控件对象,则返回null。

content的内容

import os

from flask import Blueprint, jsonify ,send_file
from setting import MDB,COVER_PATH,MUSIC_PATH

content_bp = Blueprint('content_bp', __name__)

@content_bp.route('/content_list', methods= ['POST', 'GET'])
def content_list():
    content = list(MDB.Content.find({}))

    # 代码簇  一个逻辑一个簇  方便阅读  代码簇放的越合理 越专业
    for index, item in enumerate(content):
        content[index]['_id'] = str(item.get('_id'))

    return jsonify(content)

@content_bp.route('/get_cover/<filename>',methods=['GET'])
def get_cover(filename):

    cover_path = os.path.join(COVER_PATH,filename)
    return send_file(cover_path)

@content_bp.route('/get_music/<filename>',methods=['GET'])
def get_music(filename):

    music_path = os.path.join(MUSIC_PATH, filename)
    return send_file(music_path)

createqr

import os
import requests

from setting import LT_URL, MDB, QRCODE_PATH
# res = requests.get(LT_URL%('xiao'))
#
# with open('aaa.jpg', 'wb') as f :
#     f.write(res.content)


# 创建二维码的条码
from uuid import uuid4
import time, hashlib
device_list = []
for i in range(5):
    qr_str = hashlib.md5(f'{uuid4()}{time.time()}{uuid4()}'.encode('utf-8')).hexdigest()    # haslib之后短
    print(qr_str)

    device_info = {'device_key':qr_str}
    device_list.append(device_info)

    res = requests.get(LT_URL%(qr_str))

    qrfile_path = os.path.join(QRCODE_PATH, f'{qr_str}.jpg')

    with open(qrfile_path, 'wb') as f:
        f.write(res.content)

MDB.Devices.insert_many(device_list)

pahcogn

# import requests
# res = requests.get('http://www.baidu.com')
# # res1 = requests.get('http://192.168.16.90:9527/content_list') #如果开着
# print(res.content)
# print(res.text)
# print(res.json()) # 请求的数据必须是json格式的

# 流的话可以 open
# requests 方法 get post

# data =  "{"ret":200,"msg":"成功","data":{"isSelfAlbum":false,"currentUid":0,"albumId":424529,"mainInfo":{"albumStatus":1,"showApplyFinishBtn":false,"showEditBtn":false,"showTrackManagerBtn":false,"showInformB
# 恰","playCount":1818160,"showLikeBtn":true,"isLike":false,"showShareBtn":true,"showCommentBtn":true,"showForwardBtn":true,"createDateFormat":"4年前","url":"/ertong/424529/7713678","duration":92},{"index":107,"trackId":7713564,"isPaid":false,"tag":0,"title":"我的快乐"
# 数据请求那个爬虫的
import json
import os
import time
import requests
from setting import COVER_PATH, MUSIC_PATH, MDB
from uuid import uuid4
music_list = []
 print(json.loads(data))
my_data = json.loads(data)

data_list = my_data.get('data').get('tracksAudioPlay')
for audio in data_list:
    # print(audio)    # {'index': 30, 'trackId': 7713678, 'trackName': '新年恰恰', 'trackUrl': '/ertong/424529/7713678', 'trackCoverPath'
    title = audio.get('trackName')
    zhuanji = audio.get('albumName')
    # print(title, zhuanji)    # 新年恰恰 【一千零一夜】经典儿歌
    cover_url = f"http:{audio.get('trackCoverPath')}"  # 封面 #  'trackCoverPath': '//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg'
    music_url = audio.get('src')    #  'src': 'https://fdfs.xmcdn.com/group11/M07/3C/12/wKgDa1WcxH-yUx0yAA4jOCnkzoo604.m4a',
    # print(music_url)    # https://fdfs.xmcdn.com/group12/M00/3B/B2/wKgDXFWcw12y8TanAAtkIsI9320251.m4a
    cover = requests.get(cover_url)
    # print(cover)    # <Response [200]>
    # print(cover.content)    # 流  可以写成文件 # b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00

    all_name = uuid4() # 音乐文件和封面文件同名
    # 获取封面
    cover_name = f'{all_name}.jpg'
    cover_path = os.path.join(COVER_PATH, cover_name)
    # print('嗯', cover_name)  # 344457a1-0562-4be3-9c0d-a5304f02f7cd.jpg
    # print('怕', cover_path)  # Cover\344457a1-0562-4be3-9c0d-a5304f02f7cd.jpg
    with open(cover_path, 'wb') as fcover:
        fcover.write(cover.content) # 写入流
    # print(cover.text)

    # 获取音乐文件
    music_name = f'{all_name}.mp3'
    music_path = os.path.join(MUSIC_PATH, music_name)
    music = requests.get(music_url)
    with open(music_path, 'wb') as fmusic:
        fmusic.write(music.content)

    # print(cover.json())
    time.sleep(0.5)  # 防止qps(每秒访问)限制反爬

    music_info = {
        'music': music_name,
        'cover': cover_name,
        'title': title,
        'zhuanji': zhuanji
    }
    music_list.append(music_info) # 加入数据库的是名字,而不是真正的文件
    # 所有的都加完之后,在加入数据库,而不是每一次都要加进去,浪费性能
    # MDB.Content.insert_one(music_info)


MDB.Content.insert_many(music_list)
# 小写的 many  我的Tina  TypeError: 'Collection' object is not callable. If you meant to call the 'insert_Many' method on a 'Collection' object it is failing because no such method exists.

posted @ 2019-07-20 21:07  learnacode  阅读(294)  评论(0编辑  收藏  举报