Преглед на файлове

Wesmiler 2021-0115第4期分销功能新增

wesmiler преди 5 години
родител
ревизия
b05255e7c9

+ 68 - 0
app/api/controller/MarketController.php

@@ -232,6 +232,74 @@ class MarketController extends MarketBaseController
     }
 
     /**
+     * 保存资料
+     */
+    public function saveInfo()
+    {
+        $params = input();
+        $validate = new MemberValidate();
+        if (!$validate->scene('marketInfo')->check($params)) {
+            showJson(1004, $validate->getError());
+        }
+
+        $newMobile = isset($params['mobile']) ? trim($params['mobile']) : '';
+        $mobile = isset($this->userInfo['mobile']) ? $this->userInfo['mobile'] : '';
+        if ($newMobile && $newMobile != $mobile) {
+            $code = isset($params['code']) ? trim($params['code']) : '';
+            $result = Sms::checkCode($newMobile, $code);
+            if ($result !== true) {
+                showJson(1004, $result);
+            }
+
+            // 验证手机号码是否被使用
+            $id = Member::where(['mobile' => $newMobile])->value('id');
+            if ($id && $id != $this->userId) {
+                showJson(1004, 2001);
+            }
+        }
+
+        $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
+        $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
+        $birthday = isset($params['birthday']) ? strtotime($params['birthday']) : 0;
+        $info = [
+            'userid' => $this->userId,
+            'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
+            'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
+            'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
+            'wechat_code' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
+            'qq' => isset($params['qq']) ? trim($params['qq']) : '',
+        ];
+        Db::startTrans();
+        if (UserProfile::checkProfile($this->userId)) {
+            $profileData['updated_at'] = date('Y-m-d H:i:s');
+            $res = UserProfile::saveData(['userid' => $this->userId], $info);
+        } else {
+            $res = UserProfile::insertGetId($info);
+        }
+
+        if (!$res) {
+            Db::rollback();
+            showJson(1004, 2104);
+        }
+
+
+        $memberData = [
+            'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
+            'birthday' => $birthday,
+            'sex' => isset($params['sex']) ? intval($params['sex']) : 0,
+        ];
+        if ($newMobile && $mobile != $newMobile) {
+            $memberData['mobile'] = $newMobile;
+        }
+        if (!Member::saveData(['id' => $this->userId], $memberData)) {
+            Db::rollback();
+            showJson(1004, 2104);
+        }
+        Db::commit();
+        showJson(1005, 2029);
+    }
+
+    /**
      * 余额收益提现
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\ModelNotFoundException

+ 1 - 0
app/weixin/validate/MemberValidate.php

@@ -78,6 +78,7 @@ class MemberValidate extends Validate
         'reg'  => ['nickname','mobile','code','sex','brithday','height','weight','now_address','home_address','married','wechat_code'],
         'marketReg'  => ['nickname','mobile','code','now_address','wechat_code'],
         'info'  => ['nickname','sex','brithday','height','weight','company','now_address','home_address','married','wechat_code'],
+        'marketInfo'  => ['nickname','sex','wechat_code'],
         'login'  => ['mobile','password'],
         'forget'  => ['mobile','code','password'],
         'code'  => ['mobile'],

+ 2 - 2
public/themes/default/weixin/market/profile.html

@@ -1,5 +1,5 @@
 <include file="weixin@block:header"/>
-<link rel="stylesheet" href="__TMPL__/weixin/public/assets/css/profile/market-profile.css?v={$version}">
+<link rel="stylesheet" href="__TMPL__/weixin/public/assets/css/market-profile.css?v={$version}">
 <link rel="stylesheet" type="text/css" href="__TMPL__/weixin/public/assets/css/lcalendar.css?v={$version}" />
 <div id="app" v-cloak>
     <div class="fanhui2">
@@ -58,5 +58,5 @@
 
 <script src="__TMPL__/weixin/public/assets/lib/city-picker.min.js?v={$version}"></script>
 <script src="__TMPL__/weixin/public/assets/lib/lcalendar.js?v={$version}" type="text/javascript"></script>
-<script src="__TMPL__/weixin/public/assets/js/profile/market-profile.js?v={$version}"></script>
+<script src="__TMPL__/weixin/public/assets/js/market-profile.js?v={$version}"></script>
 <include file="weixin@block:footer"/>

+ 1 - 4
public/themes/default/weixin/public/assets/js/market-profile.js

@@ -339,7 +339,7 @@ var app = new Vue({
             _this.submitting.submit = true;
             $.showLoading('数据保存中...');
             $.ajax({
-                url: '/api/member/saveInfo',
+                url: '/api/market/saveInfo',
                 data: params,
                 type: "post",
                 dataType: 'json',
@@ -348,9 +348,6 @@ var app = new Vue({
                     _this.submitting.submit = false;
                     if(res.code == 'success'){
                         $.toast(res.message,'text');
-                        setTimeout(function(){
-                            location.href = '/weixin/member/profile';
-                        }, 500);
                     }else if (res.code == 'exception'){
                         var url = res.data.url;
                         $.toast(res.message,'text');

+ 404 - 0
public/themes/default/weixin/public/assets/js/market.js

@@ -5,11 +5,32 @@ var app = new Vue({
         curNav: 'member',
         // 用户信息
         memberInfo: {},
+        // 裁剪参数
+        options: {},
+        cropParams: {},
+        // 文件
+        file: null,
+        // 上传弹窗
+        uploadBox: false,
+        submitting: {'sendcode': null, 'submit': null},
+        rebackurl: '',
+        cropData: {},
+        id: 0,
     },
     created: function () {
         this.getInfo();
     },
     methods: {
+        // 显示头像弹窗
+        showPop: function () {
+            if (this.uploadBox) {
+                this.uploadBox = false;
+                $("body").css('overflowY', 'auto');
+            } else {
+                this.uploadBox = true;
+                $("body").css('overflowY', 'hidden');
+            }
+        },
         // 获取用户信息
         getInfo: function () {
             var _this = this;
@@ -39,5 +60,388 @@ var app = new Vue({
                 }
             }, "json");
         },
+        // 选择图片
+        selectImg: function (ele, previewId) {
+            var _this = this;
+            var selectFile = ele.target.files[0];
+            if (selectFile) {
+                var reader = new FileReader();
+                reader.readAsDataURL(selectFile);
+                reader.onloadend = function (even) {
+                    _this.file = selectFile;
+                    $('#preview_' + previewId).attr('src', even.currentTarget.result);
+                    $('#preview_' + previewId).attr('old-src', even.currentTarget.result);
+                    _this.picEdit(previewId);
+                }
+            }
+        },
+        picEdit: function (id) {
+            var _this = this;
+            $.closePopup();
+            var src = $('#preview_' + id).attr('src');
+            if (src == '') {
+                $.toast('请重新选择图片');
+                return false;
+            }
+
+            _this.showPop();
+            $("#cropPreview").hide();
+            $("#preview_box").show();
+            $(".fanhui2 .del").show();
+            $("body").css('overflowY', 'hidden');
+            $image = $('#preview_' + id);
+            $image.attr('src', src);
+            $image.attr('data-src', src);
+            var width = $("body").outerWidth() * 0.85;
+            var height = (425 / 343) * width;
+
+            var _image = new Image();
+            _image.src = src;
+            var iWidth = _image.width;
+            var iHeight = _image.height;
+            var rate = iWidth/iHeight;
+            $(".preview-box").css('width', width);
+            $(".preview-box").css('height', height);
+            $(".crop-preview .desc").css('width', width);
+            $("#cropBox").popup();
+            $.showLoading('图片加载中')
+            _this.options = {
+                aspectRatio: 0,
+                // center: true,
+                movable: true,
+                zoomable: true,
+                zoomOnTouch: true,
+                zoomOnWheel: false,
+                scalable: true,
+                wheelZoomRatio: 0.1,
+                background: false,
+                responsive: true,
+                guides: false,
+                strict: false,
+                width: width,
+                dragMode: 'move',
+                height: height,
+                autoCropArea: 1,
+                cropBoxMovable: false,
+                cropBoxResizable: false,
+                minContainerWidth: width,
+                minContainerHeight: height,
+                minCropBoxWidth: width,
+                maxCropBoxWidth: width,
+                minCropBoxHeight: height,
+                maxCropBoxHeight: height,
+                minCanvasWeight: width,
+                maxCanvasWeight: width,
+                minCanvasHeight: height,
+                maxCanvasHeight: height,
+                viewMode: 1,
+                ready: function () {
+                    $.hideLoading();
+
+                },
+                crop: function (data) {
+                    $.hideLoading();
+                    _this.cropData = data
+
+                    $(".cropper-container img").attr('id', 'cropimg');
+                    $("#cropPreview").html('<div class="cropper-container" style="width: ' + width + 'px; height: ' + height + 'px;">' + $(".cropper-container").html() + '</div>');
+                    $("#cropPreview .cropper-dashed").remove();
+                    $("#cropPreview .cropper-dashed").remove();
+                    $("#cropPreview .cropper-face").remove();
+                    $("#cropPreview .cropper-line").remove();
+                    $("#cropPreview .cropper-point").remove();
+                    _this.cropParams = {
+                        width: data.height,
+                        height: data.width,
+                        rotate: data.rotate,
+                        imageSmoothingQuality: 'high',
+                        fillColor: '#fff',
+                    };
+                }
+            }
+
+            if (_this.jc != null) {
+                $image.cropper(_this.options);
+                $image.cropper('replace', src);
+            } else {
+                $image.cropper(_this.options);
+                _this.jc = $image.cropper('replace', src);
+            }
+
+            _image.onload = function(){
+                rate = 0;
+                iWidth = _image.width;
+                iHeight = _image.height;
+
+                if((iWidth<iHeight*0.65 || iHeight<=iWidth)){
+                    if(iWidth/iHeight < width/height){
+                        rate = parseFloat((width/height)/(iWidth/iHeight)).toFixed(2);
+                        console.log('ss:'+rate)
+                    }else{
+                        rate = parseFloat((height/width)/(iHeight/iWidth)).toFixed(2);
+                    }
+                    rate = iWidth/iHeight>0.9 && iWidth/iHeight<1.1? 0.2 : rate;
+                    if(rate<1 && iHeight<height){
+                        rate = height/iHeight * 0.2;
+                    }
+                    if(rate<1 && iWidth<width){
+                        rate = width/iWidth* 0.2;
+                    }
+                }
+
+                setTimeout(function(){
+                    if(rate>0){
+                        console.log('放大:'+rate)
+                        $image.cropper('zoom', rate);
+                    }
+                    var cropHeight = $(".cropper-view-box img").height();
+                    var cropWidth = $(".cropper-view-box img").width();
+                    var marginLeft = cropWidth>width? -(cropWidth-width)/2 : (width-cropWidth)/2;
+                    var marginTop = cropHeight>height? -(cropHeight-height)/2 : (height-cropHeight)/2;
+                    var mleft = parseFloat($(".cropper-view-box img").css('marginLeft').replace('px',''));
+                    var mtop = parseFloat($(".cropper-view-box img").css('marginTop').replace('px',''));
+                    //console.log(iWidth+'++'+iHeight+'++'+width+'++'+height+'++++'+cropWidth+'++'+cropHeight+'++'+rate+'++'+marginLeft+'++'+marginTop+'++'+mleft+'++'+mtop+'++'+(parseFloat(marginLeft)-parseFloat(mleft))+"++"+(parseFloat(marginTop)-parseFloat(mtop)))
+
+                    $image.cropper('move', parseFloat(marginLeft)-parseFloat(mleft), parseFloat(marginTop)-parseFloat(mtop));
+
+                }, 50)
+            }
+
+
+        },
+        zoom: function(id, size){
+            size = typeof(size) != 'undefined' && size >0? size : 1.75;
+            $image = $("#preview_" + id);
+            this.jc = $image.cropper('zoom', size);
+        },
+        // 取消
+        cropCancel: function () {
+            this.showPop();
+            $("#upload").val("");
+            $(".fanhui2 .del").hide();
+            $("body").css('overflowY', 'scroll');
+            $.closePopup();
+        },
+        // 重置
+        cropReset: function (id) {
+            $image = $("#preview_" + id);
+            var src = $image.attr('old-src');
+            $image.cropper(this.options);
+            this.jc = $image.cropper('replace', src);
+
+            var _image = new Image();
+            _image.src = src;
+            var iWidth = _image.width;
+            var iHeight = _image.height;
+            var width = $("body").outerWidth() * 0.9;
+            var height = (425 / 343) * width;
+            _image.onload = function(){
+                var rate = 0;
+                iWidth = _image.width;
+                iHeight = _image.height;
+                if((iWidth<iHeight*0.65 || iHeight<=iWidth)){
+                    if(iWidth/iHeight < width/height){
+                        rate = parseFloat((width/height)/(iWidth/iHeight)).toFixed(2);
+                        console.log('ss:'+rate)
+                    }else{
+                        rate = parseFloat((height/width)/(iHeight/iWidth)).toFixed(2);
+                    }
+                    rate = iWidth/iHeight>0.9 && iWidth/iHeight<1.1? 0.2 : rate;
+                    if(rate<1 && iHeight<height){
+                        rate = height/iHeight * 0.2;
+                    }
+                    if(rate<1 && iWidth<width){
+                        rate = width/iWidth* 0.2;
+                    }
+                }
+
+                setTimeout(function(){
+                    if(rate){
+                        $image.cropper('zoom', rate);
+                    }
+                    var cropHeight = $(".cropper-view-box img").height();
+                    var cropWidth = $(".cropper-view-box img").width();
+                    var marginLeft = cropWidth>width? -(cropWidth-width)/2 : 0;
+                    var marginTop = cropHeight>height? -(cropHeight-height)/2 : 0;
+                    var mleft = parseFloat($(".cropper-view-box img").css('marginLeft').replace('px',''));
+                    var mtop = parseFloat($(".cropper-view-box img").css('marginTop').replace('px',''));
+                    $image.cropper('move', parseFloat(marginLeft)-parseFloat(mleft), parseFloat(marginTop)-parseFloat(mtop));
+                }, 50)
+            }
+
+        },
+        // 完成
+        cropConfirm: function (id) {
+            var _this = this;
+            if (this.jc != null) {
+                if (!_this.cropParams) {
+                    $.toast('请先处理图片', 'text');
+                    return false;
+                }
+                $image = $("#preview_" + id);
+                $.showLoading('图片处理中...');
+                $("#cropPreview").show();
+                $("#preview_box").hide();
+                console.log(_this.cropData)
+                //alert(JSON.stringify(_this.cropData))
+                var cropSuccess = false;
+                var canvas = $image.cropper('getCroppedCanvas', _this.cropParams);
+                var url = canvas.toBlob(function (e) {
+                    cropSuccess = true;
+                    if (e != null) {
+                        console.log("裁剪方式");
+                        var timestamp = Date.parse(new Date());
+                        e.name = timestamp + ".jpeg";
+                        url = URL.createObjectURL(e);
+                        _this.file = url;
+                        _this.file = _this.dataURLtoFile(e, e.name);
+                        $image.attr('data-src', url);
+                        $("body").css('overflowY', 'scroll');
+                        setTimeout(function () {
+                            $.hideLoading();
+                            $.closePopup();
+                            $(".fanhui2 .del").hide();
+                            $(id).cropper('destroy');
+                            $('#' + id).attr('src', $image.attr('data-src'));
+                            $("#upload").val("");
+                            _this.postSubmit();
+                            _this.jc = null;
+                        }, 200);
+
+                    } else {
+                        console.log("合成方式");
+                        var width = $("body").outerWidth() * 0.9;
+                        var height = (425 / 343) * width;
+                        var opts = {
+                            width: width,
+                            height: height,
+                            scale: 5,
+                            background: '#fff',
+                            useCROS: true,
+                        };
+                        //console.log(opts);
+
+                        var _image = document.getElementById('cropPreview');
+                        //console.log($(".cropper-container")[0]);
+                        html2canvas(_image, opts).then(function (canvas) {
+                            const context = canvas.getContext('2d');
+                            context.translate(0.5, 0.5);
+                            context.mozImageSmoothingEnabled = false;
+                            context.webkitImageSmoothingEnabled = false;
+                            context.msImageSmoothingEnabled = false;
+                            context.imageSmoothingEnabled = false;
+
+                            var src = canvas.toDataURL('image/jpeg');
+                            var timestamp = Date.parse(new Date());
+                            var name = timestamp + ".jpeg";
+                            _this.file = _this.dataURLtoFile1(src, name ? name : 'avatar.jpeg');
+                            if (_this.file == null) {
+                                $.showLoading('图片处理失败请重试');
+                                setTimeout(function () {
+                                    $.closePopup();
+                                    $(".fanhui2 .del").hide();
+                                    $("#cropPreview").hide();
+                                    $("#preview_box").show();
+                                    $("#upload").val("");
+                                }, 800);
+                                return false;
+                            }
+
+                            $image.attr('src', src);
+                            $("body").css('overflowY', 'scroll');
+                            setTimeout(function () {
+                                $.hideLoading();
+                                $.closePopup();
+                                $(".fanhui2 .del").hide();
+                                $("#cropPreview").hide();
+                                $("#preview_box").show();
+                                $(id).cropper('destroy');
+                                $('#' + id).attr('src', src);
+                                $("#upload").val("");
+                                _this.postSubmit();
+                                _this.jc = null;
+                            }, 200);
+                        });
+
+                    }
+                }, "image/jpeg");
+
+            } else {
+                $.showLoading('处理失败请刷新重试');
+                setTimeout(function(){
+                    $.hideLoading();
+                }, 1200);
+                $.closePopup();
+            }
+        },
+        // base转文件
+        dataURLtoFile: function (dataurl, filename) {//将base64转换为文件
+            return new File([dataurl], filename, {type: 'image/jpeg'});
+        },
+        //
+        dataURLtoFile1: function (dataurl, filename) {//将base64转换为文件
+            var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
+                bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
+            while (n--) {
+                u8arr[n] = bstr.charCodeAt(n);
+            }
+            return new File([u8arr], filename, {type: mime});
+        },
+        // 提交数据
+        postSubmit: function () {
+            var _this = this;
+            if (_this.submitting.submit) {
+                return false;
+            }
+
+            var formData = new FormData();
+            if (_this.file == null) {
+                $.showLoading('请选择图片');
+                setTimeout(function(){
+                    $.hideLoading();
+                }, 1200);
+                return false;
+            }
+            formData.append('image', _this.file);
+            if (!confirm('确定修改头像?')) {
+                _this.getInfo();
+                return false;
+            }
+            console.log(formData)
+            _this.submitting.submit = true;
+            $.showLoading('数据保存中...');
+            $.ajax({
+                url: '/api/member/setAvatar',
+                data: formData,
+                type: "post",
+                dataType: 'json',
+                cache: false,
+                contentType: false,
+                processData: false,
+                success: function (res) {
+                    $.hideLoading();
+                    _this.submitting.submit = false;
+                    if (res.code == 'success') {
+                        $.toast(res.message, 'text');
+                        //_this.showPop();
+                    } else if (res.code == 'login') {
+                        var url = res.data.url;
+                        $.showLoading(res.message, 'text');
+                        if (url) {
+                            setTimeout(function () {
+                                $.hideLoading();
+                                location.href = url;
+                            }, 500)
+                        }
+                    } else {
+                        $.toast(res.message, 'text');
+                    }
+                },
+                error: function (res) {
+                    _this.submitting.submit = false;
+                    $.toast(res.message, 'text');
+                }
+            });
+        }
     }
 })