123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 'use strict';
- const Controller = require('egg').Controller;
- const path = require("path");
- const uuid = require('uuid');
- const fs = require("fs");
- const awaitWriteStream = require("await-stream-ready").write;
- const sendToWormhole = require("stream-wormhole");
- const dayjs = require("dayjs");
- class CommonController extends Controller {
-
- async upload() {
- let {ctx } = this;
- // const parts = ctx.multipart({ autoFields: true });
- // const files = [];
- // let stream;
- // while ((stream = await parts()) != null) {
- // const filename = stream.filename.toLowerCase();
- // // const target = path.join(this.config.baseDir, 'app/public', filename);
- // // const writeStream = fs.createWriteStream(target);
- // // await pump(stream, writeStream);
- // console.log("xxx, ok11", filename);
- // files.push(filename);
- // }
- const stream = await this.ctx.getFileStream();
- // 基础的目录
- const uplaodBasePath = 'app/public/uploads';
- // 生成文件名
- const filename = `${Date.now()}${Number.parseInt( Math.random() * 1000)}${path.extname(stream.filename).toLocaleLowerCase()}`;
- // 生成文件夹
- const dirname = dayjs(Date.now()).format('YYYY-MM-DD');
- function mkdirsSync(dirname) {
- if (fs.existsSync(dirname)) {
- return true;
- } else {
- if (mkdirsSync(path.dirname(dirname))) {
- fs.mkdirSync(dirname);
- return true;
- }
- }
- }
- mkdirsSync(path.join(uplaodBasePath, dirname));
- // 生成写入路径
- const target = path.join(uplaodBasePath, dirname, filename);
- // 写入流
- const writeStream = fs.createWriteStream(target);
- try {
- //异步把文件流 写入
- await awaitWriteStream(stream.pipe(writeStream));
- } catch (err) {
- //如果出现错误,关闭管道
- await sendToWormhole(stream);
-
- throw err;
- }
- let url = `uploads/${dirname}/${filename}`;
- ctx.adminOK(true, url);
- }
- async sendSms() {
- let { ctx } = this;
- let {phone, use} = ctx.request.body;
- if( !use || !phone ) {
- ctx.resultFail("error");
- return;
- }
- console.log("sendSms", use);
-
- let code = await ctx.service.sms.send(phone);
- await ctx.service.cache.setString(`sms#${phone}#${use}`, code, 180);//3分钟后失效
- ctx.resultOK("发送成功");
- }
- async bindThirdparty() {
- let { ctx , app} = this;
- let {mobile, wechatCode, alipayCode, code} = ctx.request.body;
- if( !alipayCode && !wechatCode ) {
- ctx.resultFail("授权码不能为空");
- return;
- }
- if( !mobile ) {
- ctx.resultFail("手机号码不能为空");
- return;
- }
- if( !code ) {
- ctx.resultFail("验证码不能为空");
- return;
- }
- let cacheKey = `sms#${mobile}#bind`;
- let cacheCode = await ctx.service.cache.getString(cacheKey);//3分钟后失效
- if( cacheCode != code) {
- ctx.resultFail("验证码错误");
- return;
- }
- await ctx.service.cache.removeCache(cacheKey);
- let thirdUser = {userId:"", nickName: "", avatar:""};
- if( alipayCode ) {
- thirdUser = await ctx.service.alipay.getUserInfo( alipayCode );
- }
- //更新用户
- let user = await app.mysql.get("app_user", {tel: mobile}, {columns:["id","name","avatar"]});
- if( user && user.id ) {
- let v = {};
- if( wechatCode ) v.wechat = thirdUser.userId;
- else if( alipayCode ) v.alipay = thirdUser.userId;
- if( !user.name ) v.name = thirdUser.nickName;
- if( !user.avatar ) v.avatar = thirdUser.avatar;
-
- await app.mysql.update("app_user", v, {where:{id: user.id} });
- let token = await ctx.jwtSign({name:user.name, tel:mobile, id:user.id}, app.config.loginUser.secret, { expiresIn: app.config.loginUser.expiresIn });
- ctx.resultOK({id: user.id, username: user.name, avatar:user.avatar,tel:mobile, token}, "绑定成功");
- return;
- }
- //创建用户
- let res = await app.mysql.query(`insert into app_user (name, avatar, tel,${alipayCode?"alipay":"wechat"}, add_time, last_login, last_login_ip) values (?,?,?,?,now(),now(), ?)`, [thirdUser.nickName, thirdUser.avatar, mobile, thirdUser.userId, ctx.ip]);
- let id = res.insertId;
- if( id ) {
- let token = await ctx.jwtSign({name:thirdUser.nickName, id, tel:mobile}, app.config.loginUser.secret, { expiresIn: 24 * 60 * 60 });
- ctx.resultOK({id: id, tel:mobile, username: thirdUser.nickName, avatar: thirdUser.avatar, token}, "绑定成功");
- return;
- }
- ctx.resultFail("绑定失败");
- }
- }
- module.exports = CommonController;
|