| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- // package-user/pages/register/index.js
- const api = require('../../../api/index.js');
-
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- items: [
- {value: 5, name: '买电'},
- {value: 6, name: '卖电'},
- ],
- fileurl:'',
- back:'',
- identitycard:'',
- greenelectricity:'',
- nickname:'',
- telephone:'',
- powerstationname:''
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
-
- },
- // 上传
- onchooseImage(e) {
- this.setData({
- identitycard:e.target.dataset.name
- })
- let _this = this
- // 先检查隐私授权状态
- wx.getPrivacySetting({
- success: (res) => {
- if (res.needAuthorization) {
- // 弹出隐私协议弹窗
- wx.requirePrivacyAuthorize({
- success: () => {
- console.log('用户已同意隐私协议');
- _this.doUploadImage(); // 继续执行上传
- },
- fail: () => {
- console.log('用户拒绝了隐私协议');
- wx.showToast({
- title: '需同意隐私协议才能使用',
- icon: 'none'
- });
- }
- });
- } else {
- _this.chooseImage(); // 已授权,直接上传
- }
- }
- });
- },
- // 上传图片
- chooseImage() {
- let _this = this
- wx.chooseMedia({
- count: 1,
- mediaType: ['image'],
- sourceType: ['album', 'camera'],
- success(res) {
- console.log(res);
- const tempFiles = res.tempFiles;
- _this.setData({
- imageList: tempFiles.map(file => file.tempFilePath),
- });
- _this.uploadImages(tempFiles);
- },
- fail(err) {
- console.error("选择失败:", err);
- }
- })
- },
- radioChange(e) {
- console.log('radio发生change事件,携带value值为:', e.detail.value)
-
- // const items = this.data.items
- // for (let i = 0, len = items.length; i < len; ++i) {
- // items[i].checked = items[i].value === e.detail.value
- // }
-
- this.setData({
- greenelectricity:e.detail.value
- })
- console.log(this.data.greenelectricity);
- },
- bindKeyInput(e){
- console.log(e.detail.value);
- this.setData({
- nickname:e.detail.value
- })
- },
- bindtelephone(e){
-
- console.log(e.detail.value);
-
- this.setData({
- telephone:e.detail.value
- })
- },
- bindpowerstationname(e){
- console.log(e.detail.value);
- this.setData({
- powerstationname:e.detail.value
- })
- },
- // 上传到服务器
- uploadImages(files) {
- let _this =this
- const token = wx.getStorageSync('token');
- files.forEach((file) => {
- wx.uploadFile({
- url: 'https://esos-iot.com:9443/system/config/upload/webPost',
- filePath: file.tempFilePath,
- name: 'file',
- formData: {
- type:_this.data.identitycard,
- front:file.tempFilePath
- },
- header: {
- // 'Authorization': 'Bearer ' + token // Also add to header if needed
- },
- success: (res) => {
- const obj = JSON.parse(res.data);
- console.log(obj.data.companyLicense);
- _this.setData({
- fileurl:obj.data.companyLicense
- })
- console.log(this.data.fileurl);
-
- },
- fail: (err) => {
- console.error('上传失败', err);
- },
- });
- });
- },
-
- register(){
- const phone = this.data.telephone.trim();
- const phoneReg = /^1[3-9]\d{9}$/;
- console.log(phone);
- // 1. 校验是否为空
- if (!phone) {
-
- wx.showToast({
- title:'请输入手机号',
- icon: 'none',
- });
- return false;
- }
-
- // 2. 校验格式是否正确
- if (!phoneReg.test(phone)) {
-
- wx.showToast({
- title:'请输入正确的手机号',
- icon: 'none',
- });
- return false;
- }
- if (this.data.nickname==''||this.data.greenelectricity==''||this.data.powerstationname=='') {
- wx.showToast({
- title:'请填写完整信息',
- icon: 'success',
- });
- return
- }
- let obj = {
- phonecode:this.data.telephone,
- operationName:this.data.nickname,
- operationRole:this.data.greenelectricity,
- companyName:this.data.powerstationname,
- companyLicense:this.data.fileurl,
- }
- api.request(`/sysoperation/registeredusers`, 'post',obj,{ isPublic: true })
- .then((data) => {
- console.log(data);
- if (data.code == 200) {
- if (data.msg=='当前用户已注册') {
- wx.showToast({
- title:data.msg,
- icon: 'success',
- });
- return
- }
- wx.showToast({
- title:data.msg,
- icon: 'success',
- });
- wx.navigateTo({
- url: '/package-user/pages/login/index', // 登录页面的路径,注意路径前面加上 '/' 表示从根目录开始
- });
- }else{
- wx.showToast({
- title: data.msg,
- icon: 'success',
- });
- }
-
- })
- .catch((err) => {
- wx.showToast({
- title:'注册失败',
- icon: 'none',
- });
- console.error('请求失败:', err);
- });
- },
- previewSingleImage(e) {
- // 获取大图路径
- const largePicUrl = e.currentTarget.dataset.url;
- // 调用API预览大图
- wx.previewImage({
- current: largePicUrl, // 当前展示的大图路径
- urls: [largePicUrl] // 需预览的图片数组,单图时数组中仅存该图路径
- });
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
-
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
-
- },
-
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
-
- },
-
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
-
- },
-
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
-
- },
-
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
-
- },
-
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
-
- }
- })
|